Commit bd628d1a authored by Methsarani's avatar Methsarani

Real Time Vehicle Type Detection code Add

parent 9377d9b1
import cv2
import numpy as np
import time
#Load YOLO
net = cv2.dnn.readNet("yolov3.weights","yolov3.cfg") # Original yolov3
#net = cv2.dnn.readNet("yolov3-tiny.weights","yolov3-tiny.cfg") #Tiny Yolo
classes = []
with open("coco.names","r") as f:
classes = [line.strip() for line in f.readlines()]
print(classes)
['person', 'bicycle', 'car', 'motorbike', 'aeroplane', 'bus', 'train', 'truck', 'boat', 'traffic light', 'fire hydrant', 'stop sign', 'parking meter', 'bench', 'bird', 'cat', 'dog', 'horse', 'sheep', 'cow', 'elephant', 'bear', 'zebra', 'giraffe', 'backpack', 'umbrella', 'handbag', 'tie', 'suitcase', 'frisbee', 'skis', 'snowboard', 'sports ball', 'kite', 'baseball bat', 'baseball glove', 'skateboard', 'surfboard', 'tennis racket', 'bottle', 'wine glass', 'cup', 'fork', 'knife', 'spoon', 'bowl', 'banana', 'apple', 'sandwich', 'orange', 'broccoli', 'carrot', 'hot dog', 'pizza', 'donut', 'cake', 'chair', 'sofa', 'pottedplant', 'bed', 'diningtable', 'toilet', 'tvmonitor', 'laptop', 'mouse', 'remote', 'keyboard', 'cell phone', 'microwave', 'oven', 'toaster', 'sink', 'refrigerator', 'book', 'clock', 'vase', 'scissors', 'teddy bear', 'hair drier', 'toothbrush']
layer_names = net.getLayerNames()
outputlayers = [layer_names[i[0] - 1] for i in net.getUnconnectedOutLayers()]
outputlayers
colors= np.random.uniform(0,255,size=(len(classes),3))
# loading image
cap=cv2.VideoCapture("highway.mp4") #0 for 1st webcam
#cap = cv2.VideoCapture(0)
font = cv2.FONT_HERSHEY_PLAIN
starting_time = time.time()
frame_id = 0
while True:
_, frame = cap.read() #
frame_id += 1
height, width, channels = frame.shape
# detecting objects
blob = cv2.dnn.blobFromImage(frame, 0.00392, (320, 320), (0, 0, 0), True, crop=False) # reduce 416 to 320
net.setInput(blob)
outs = net.forward(outputlayers)
# print(outs[1])
# Showing info on screen/ get confidence score of algorithm in detecting an object in blob
class_ids = []
confidences = []
boxes = []
for out in outs:
for detection in out:
scores = detection[5:]
class_id = np.argmax(scores)
confidence = scores[class_id]
if confidence > 0.3:
# onject detected
center_x = int(detection[0] * width)
center_y = int(detection[1] * height)
w = int(detection[2] * width)
h = int(detection[3] * height)
# cv2.circle(img,(center_x,center_y),10,(0,255,0),2)
# rectangle co-ordinaters
x = int(center_x - w / 2)
y = int(center_y - h / 2)
# cv2.rectangle(img,(x,y),(x+w,y+h),(0,255,0),2)
boxes.append([x, y, w, h]) # put all rectangle areas
confidences.append(
float(confidence)) # how confidence was that object detected and show that percentage
class_ids.append(class_id) # name of the object tha was detected
indexes = cv2.dnn.NMSBoxes(boxes, confidences, 0.4, 0.6)
for i in range(len(boxes)):
if i in indexes:
x, y, w, h = boxes[i]
label = str(classes[class_ids[i]])
confidence = confidences[i]
color = colors[class_ids[i]]
cv2.rectangle(frame, (x, y), (x + w, y + h), color, 2)
cv2.putText(frame, label + " " + str(round(confidence, 2)), (x, y + 30), font, 1, (255, 255, 255), 2)
elapsed_time = time.time() - starting_time
fps = frame_id / elapsed_time
cv2.putText(frame, "FPS:" + str(round(fps, 2)), (10, 50), font, 2, (0, 0, 0), 1)
cv2.imshow("Image", frame)
key = cv2.waitKey(2) # wait 1ms the loop will start again and we will process the next frame
if key == 27: # esc key stops the process
break;
cap.release()
cv2.destroyAllWindows()
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment