Hello People!! In my previous blog, we learnt about detecting and counting persons and today we will learn how to use the YOLO Object Detector  to detect vehicles in video streams using Deep Learning, OpenCV and Python.

You can click here to read my previous blog.

Import Necessary Libraries

I’ll start by importing necessary libraries

import cv2
import numpy as np
import time

Loading YOLO weights and cfg

After importing libraries, I’ll load YOLO weights and cfg files to make classes using COCO files using DNN.

# Load Yolo
net = cv2.dnn.readNet("yolov3.weights", "yolov3.cfg")
classes = []
with open("coco.names", "r") as f:
    classes = [line.strip() for line in f.readlines()]
layer_names = net.getLayerNames()
output_layers = [layer_names[i[0] - 1] for i in net.getUnconnectedOutLayers()]
colors = np.random.uniform(0, 255, size=(len(classes), 3))

Detecting and Counting vehicles in video stream

I’ll load a video, and try to detect and count vehicles in that video. I’ll loop over the frames of video and construct a blob from the input frame and then perform a forward pass of the YOLO object detector, which will give us bounding boxes and associated probabilities. Next, I’ll filter out weak predictions, i.e., predictions having probability greater than 0.7 will be considered. Then, I’ll draw a bounding box rectangle over the vehicles.

# Loading image
cap = cv2.VideoCapture("India - 8698.mp4")
#instantiate a variable 'p' to keep count of vehicles
p = 0 
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, (416, 416), (0, 0, 0), True, crop=False)

    outs = net.forward(output_layers)

    # initialize our lists of detected bounding boxes, confidences, and class IDs, respectively
    class_ids = []
    confidences = []
    boxes = []
    # loop over each of the layer outputs
    for out in outs:
        # loop over each of the detections
        for detection in out:
            # extract the class ID and confidence 
            scores = detection[5:]
            class_id = np.argmax(scores)
            confidence = scores[class_id]
            # filter out weak predictions
            if confidence > 0.7:
                # Object detected
                center_x = int(detection[0] * width)
                center_y = int(detection[1] * height)
                w = int(detection[2] * width)
                h = int(detection[3] * height)

                # Rectangle coordinates
                x = int(center_x - w / 2)
                y = int(center_y - h / 2)

                # update our list of bounding box coordinates, confidences, and class IDs
                boxes.append([x, y, w, h])
    # apply non-maxima suppression to suppress weak, overlapping bounding boxes
    indexes = cv2.dnn.NMSBoxes(boxes, confidences, 0.8, 0.3)
    #detecting Vehicles
    for i in range(len(boxes)):
        if i in indexes:
            x, y, w, h = boxes[i]
            label = str(classes[class_ids[i]])
            if label=="car":
            elif label=="truck":
            elif label=="bus":
            elif label == "motor bike":
            confidence = confidences[i]
            color = colors[class_ids[i]]
            cv2.rectangle(frame, (x, y), (x + w, y + h), color, 2)
            cv2.putText(frame, label + ':' + str(p) , (x, y + 30), font, 0.5, color, 1)

    elapsed_time = time.time() - starting_time
    fps = frame_id / elapsed_time
    cv2.putText(frame, "FPS: " + str(round(fps, 2)), (10, 50), font, 0.5, (0, 0, 0), 1)
    cv2.imshow("Image", frame)
    key = cv2.waitKey(1)
    if key == 27:

The program is now ready to run. Each frame is run through the YOLO object detector and identified items are highlighted. The program can be stopped by pressing the key ‘ESC’ at any time.



Congratulations !! We are finally able to implement vehicle detection using OpenCV and YOLO.

Do share your views in the comments section.

Shruti Sharma

1 Comment

Denny · June 21, 2020 at 9:33 am

Wow cuz this is extremely excellent job! Congrats and keep it up.

Leave a Reply

Your email address will not be published. Required fields are marked *

Insert math as
Additional settings
Formula color
Text color
Type math using LaTeX
Nothing to preview