Vehicle Detection Using OpenCV and Python Within 5 Minutes!

Vehicle Detection using OpenCV

In this article, you’ll learn about an advanced Discover easy Vehicle detection using OpenCV and Python in just 5 minutes! Simplify your coding journey and unlock quick, efficient solutions! The article will guide you in using the YOLOv3 model in conjunction with OpenCV-python. OpenCV is a Python real-time computer vision library.

Let’s start with the basics here first;

Detecting Moving Objects in Videos

Object detection is a fascinating area of computer vision. On top of that, when you’re dealing with video data, it takes it to a whole new level. The intricacy increases, and so do the rewards!

Using object detection techniques, you can perform extremely high-value jobs such as surveillance, traffic control, criminal fighting, and more. 

Counting the number of objects and determining the relative size of the items and the relative distance between the objects are sub-tasks in object detection. These sub-tasks are crucial since they help solve some of the most difficult real-world challenges.

Let’s have a look at intriguing object detection use cases in real-world applications.

Nowadays, video object detection is being used in a variety of sectors. Surveillance, sports broadcasting, and robot navigation are among the applications.

The good news is that the options are limitless regarding future use cases for video object detection and tracking. Here are some of the most fascinating applications you can count on:

  • Counting the crowd
  • Detection and recognition of Vehicle license plates (as discussed in the article)
  • Sports ball tracking
  • Robotics
  • Traffic management

Vehicle Detection using OpenCV

What Is YOLO?

YOLO is an acronym that stands for You Only Look Once. It is an object recognition algorithm that operates in real time. It is capable of classifying and localizing several objects in a single frame. 

Due to its smaller network topology, YOLO is an extremely quick and accurate algorithm. It mostly employs the following strategies:

Residual Blocks

It essentially splits an image into NxN grids.

Regression Using Bounding Boxes

The model receives one grid cell at a time. YOLO calculates the likelihood that the cell has a specific class, and the class with the highest probability is chosen.

IoU or Intersection Over Union

IOU is a statistic that examines the intersection of the predicted and actual bounding boxes. A non-max suppression (NMS) technique eliminates the close bounding boxes by executing the IoU with the box with the highest-class probability.

Architecture of YOLO

The YOLO network comprises 24 convolutional layers that are followed by two fully connected layers. The convolutional layers are trained on the ImageNet classification algorithm at half the resolution (224 x 224 input picture) before being trained for detection.

The layers minimize the feature set from previous layers, alternate 11 reduction layers, and 33 convolutional layers. The final four layers are added to train the network to detect objects.

The last layer forecasts the object class and bounding box probabilities. To interact with YOLO directly, we’ll use OpenCV’s DNN module. DNN is an abbreviation for Deep Neural Network, and OpenCV includes a function for running DNN algorithms.

Vehicle Detection System and Classification Project Using OpenCV

In this project, we will detect and classify Vehicle, HMV (Heavy Motor Vehicle), and LMV (Light Motor Vehicle), on the road, as well as count the number of Vehicles on the road. The data will be saved to examine various automobiles on the road.

To complete this project, we will develop two programs. The first will be a Vehicle detection tracker that uses OpenCV to keep track of every identified Vehicle on the road, and the second will be the primary detection of Vehicles.

Prerequisites for the OpenCV vehicle detection system and classification project.

Python - version 3.x (We used python 3.8.8 in this project)

OpenCV - version 4.4.0

DNN models should be executed on GPU whenever possible.

OpenCV can be installed with "pip install opencv-python opencv contrib-python".

Creating Python OpenCV Code: Vehicle Detection System and Classification In 5 Minutes

You should download the OpenCV Vehicle detection and classification source code if you haven’t already. Now, let’s begin!


The tracker uses the Euclidean distance to maintain track of an item. It computes the distance between two center points of an object in the current frame and the previous frame, and if the distance is smaller than the threshold distance, it certifies that the object in the previous frame is the same as the current frame.

Import math

# Get center point of new object

for rect in objects_rect:

x, y, w, h, index = rect

cx = (x + x + w) // 2

cy = (y + y + h) // 2

# Find out if that object was detected already

same_object_detected = False

for id, pt in self.center_points.items():

dist = math.hypot(cx - pt[0], cy - pt[1])

if dist < 25:

self.center_points[id] = (cx, cy)

# print(self.center_points)

objects_bbs_ids.append([x, y, w, h, id, index])

same_object_detected = True


The Euclidean distance is returned via the math.hypot() method.

If the distance is less than 25, the item is the same as in the previous frame.

Steps for Detection and Classification of Vehicle Detection Using OpenCV and Python 

  1. Import the relevant packages and start the network.
  2. Retrieve frames from a video file.
  3. Run the detection after pre-processing the frame.
  4. Perform post-processing on the output data.
  5. Count and track all Vehicles on the route.
  6. Save the completed data as a CSV file.

Vehicle Detection using OpenCV

Step #1 – Import Relevant Packages and Initialize the Network

import cv2

import csv

import collections

import numpy as np

from tracker import *

Initialize Tracker

tracker = EuclideanDistTracker()

Detection Confidence Threshold

confThreshold =0.1

nmsThreshold= 0.2

First, you import all of the project’s required packages. 

Then, from the tracker program, you initialize the EuclideanDistTracker() object and set the object to “tracker.”

confThreshold and nmsThreshold are the detection and suppression minimal confidence score thresholds, respectively.

# Middle cross line position

middle_line_position = 225

up_line_position = middle_line_position - 15

down_line_position = middle_line_position + 15

You need to modify the middle_line_position according to your need.

Store Coco Names in a list

classesFile = "coco.names"

classNames = open(classesFile).read().strip().split('\n')



The Output

Model Files

modelConfiguration = 'yolov3-320.cfg'

modelWeights = 'yolov3-320.weights'

Configure the Network Model

net = cv2.dnn.readNetFromDarknet(modelConfiguration, modelWeights)

Configure the Network Backend



Define Random Colors for Each Class


colors = np.random.randint(0, 255, size=(len(classNames), 3), dtype='uint8')

Step #2 – Read the Frames from the Video Files

  • reads each frame from the capture object after reading the video file using the video capture object.
  • You cut your frame in half by using cv2.reshape().
  • The crossing lines are drawn in the frame using the cv2.line() function.
  • Finally, you display the generated image using the cv2.imshow() function.

Initialize the VideoCapture Object

 cap = cv2.VideoCapture('video.mp4')

  def realTime():

while True:

success, img =

img = cv2.resize(img,(0,0),None,0.5,0.5)

ih, iw, channels = img.shape

# Draw the crossing lines

cv2.line(img, (0, middle_line_position), (iw, middle_line_position),

(255, 0, 255), 1)

cv2.line(img, (0, up_line_position), (iw, up_line_position), (0, 0, 255), 1)

cv2.line(img, (0, down_line_position), (iw, down_line_position), (0, 0, 255), 1)

# Show the frames

cv2.imshow('Output', img)

if name == 'main':


Step #3 – Pre-Processing Frames and Running Detection

  • This YOLO version accepts 320×320 image objects as input. The network’s input is a blob object. The function dnn.blobFromImage() accepts an image as input and returns a blob object that has been shrunk and normalized.
  • The image is fed onto the network using net.forward(), and it produces a result.
  • Finally, you invoke the custom postProcess() function to post-process the output.
input_size = 320

blob = cv2.dnn.blobFromImage(img, 1 / 255, (input_size, input_size), [0, 0, 0], 1, crop=False)

# Set the input of the network


layersNames = net.getLayerNames()

outputNames = [(layersNames[i[0] - 1]) for i in net.getUnconnectedOutLayers()]

# Feed data to the network

outputs = net.forward(outputNames)

# Find the objects from the network output


Vehicle Detection using OpenCV

Step #4 – Post-Processing Output

The forward network output has three outputs. Each output object is an 85-length vector.

  • Four times the bounding box (centerX, centerY, width, height)
  • One confidence box
  • 80x class assurance

Let’s start by defining the post-processing function.

detected_classNames = []

def postProcess(outputs,img):

global detected_classNames

height, width = img.shape[:2]

boxes = []

classIds = []

confidence_scores = []

detection = []

for output in outputs:

for det in output:

scores = det[5:]

classId = np.argmax(scores)

confidence = scores[classId]

if classId in required_class_index:

if confidence > confThreshold:

# print(classId)

w,h = int(det[2]width) , int(det[3]height)

x,y = int((det[0]width)-w/2) , int((det[1]height)-h/2)




Step #5 – Counting All the Tracked Vehicles on the Road

  • After receiving all detections, you use the tracker object to keep track of those things. The tracker. update() function maintains track of all identified objects and updates their positions.
  • The custom function count_vehicle counts the number of vehicles that have passed through the road.

The count_vehicle Feature

#Update the tracker for each object

boxes_ids = tracker.update(detection)

for box_id in boxes_ids:


Empty Lists

  • Create two empty lists (temporary ones) for storing the Vehicle IDs entering the entry crossing line.

List for store vehicle count information

temp_up_list = []

temp_down_list = []
  • To count four Vehicle classes in the up and down routes are Up_list and down_list.
up_list = [0, 0, 0, 0]

down_list = [0, 0, 0, 0]

Function for Counting Vehicle

def count_vehicle(box_id):

x, y, w, h, id, index = box_id

# Find the center of the rectangle for detection

center = find_center(x, y, w, h)

ix, iy = center

The find_center Function

  • The center point of a rectangle box is returned through the find_center function.
#Find the current position of the vehicle

if (iy > up_line_position) and (iy < middle_line_position):

    if id not in temp_up_list:


elif iy < down_line_position and iy > middle_line_position:

    if id not in temp_down_list:


elif iy < up_line_position:

    if id in temp_down_list:


        up_list[index] = up_list[index]+1

elif iy > down_line_position:

    if id in temp_up_list:


        down_list[index] = down_list[index] + 1

Step #6 – Saving the Final Data

  • You open a new file, data.csv, with write permission only using the open function.
  • Then, you write three rows: the first with class names and directions, the second with up and down route counts, and the third with both.
  • The writerow() function saves a row of data to a file.
#Write the Vehicle counting information in a file and save it

with open("data.csv", 'w') as f1:

cwriter = csv.writer(f1)

cwriter.writerow(['Direction', 'Vehicle', 'motorbike', 'bus', 'truck'])

up_list.insert(0, "Up")

down_list.insert(0, "Down")





Congratulations! Using OpenCV, you have created a sophisticated Vehicle detection system and classification system for this project. 

You have used the YOLOv3 algorithm with OpenCV to recognize and classify objects. In addition, you learned about deep neural networks, file systems, and advanced computer vision algorithms.

Vehicle Detection using OpenCV


A Glimpse into Excellence: Watch Folio3 AI Demo Video



Leave a Reply
Previous Post
Generative Chatbots in Healthcare: Revolutionizing Patient Interaction

Generative AI Powered Chatbots in Healthcare: Revolutionizing Patient Interaction

Next Post
Top 11 Generative AI Solutions Companies

Exploring the Top 11 Generative AI Solutions Companies: A Comprehensive Comparison

Related Posts