Open In App

Vehicle detection using OpenCV Python

Last Updated : 21 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Object Detection means identifying the objects in a video or image. In this article, we will learn how to detect vehicles using the Haar Cascade classifier and OpenCV. We will implement the vehicle detection on an image and as a result, we will get a video in which vehicles will be detected and it will be represented by a rectangular frame around it.

Why Vehicle Detection?

  • The startling losses both in human lives and finance caused by vehicle accidents.
  • Detecting vehicles in images acquired from a moving platform is a challenging problem.

Installing the requirements:

  • Install Python 3.x version, numpy, and OpenCV 2.4.x version. Check if your Windows either 32-bit or 64-bit is compatible and install accordingly.
  • Make sure to install numpy first and then install opencv.
  • Put the haar cascade file in the same folder. Download it from here.

Importing the Required Libraries

We are going to use OpenCV, so let’s import it.

Python3




# import libraries of python OpenCV 
import cv2


Now, to capture a video, we need to create a VideoCapture() object. So let’s create it. Also let’s load our trained XML classifier, i.e. Haar Cascade classifier that contains the features to identify the objects.

Python3




haar_cascade = 'cars.xml'
video = 'video.avi'
      
cap = cv2.VideoCapture(video)
car_cascade = cv2.CascadeClassifier(haar_cascade)


Now, as we are working with the video, and a video has many frames, hence we will run a while loop. In the loop, we will first read the frames from the video, and convert those frames to a grayscale. Then we will use the haar cascade classifier to detect the vehicle.

Python3




# reads frames from a video
ret, frames = cap.read()
        
# convert frames to gray scale 
gray = cv2.cvtColor(frames, cv2.COLOR_BGR2GRAY)
        
# Detects cars of different sizes in the input image
cars = car_cascade.detectMultiScale(gray, 1.1, 1)


After this step, we will use the OpenCV to draw rectangles around the vehicles. For this, we will use the coordinates that we got while using the haar cascade. And in the end, we will display the frames using cv2.imshow().

Python3




# To draw a rectangle in each cars
for (x,y,w,h) in cars:
  cv2.rectangle(frames,(x,y),(x+w,y+h),(0,0,255),2)
    
# Display frames in a window 
cv2.imshow('video', frames)


Let’s combine all the above steps:

Python3




# loop runs if capturing has been initialized.
while True:
    # reads frames from a video
    ret, frames = cap.read()
        
    # convert to gray scale of each frames
    gray = cv2.cvtColor(frames, cv2.COLOR_BGR2GRAY)
        
    
    # Detects cars of different sizes in the input image
    cars = car_cascade.detectMultiScale(gray, 1.1, 1)
        
    # To draw a rectangle in each cars
    for (x,y,w,h) in cars:
        cv2.rectangle(frames,(x,y),(x+w,y+h),(0,0,255),2)
    
    # Display frames in a window 
    cv2.imshow('video', frames)
        
    # Wait for Esc key to stop
    if cv2.waitKey(33) == 27:
        break
    
# De-allocate any associated memory usage
cv2.destroyAllWindows()


This is how the output will look

car-detection

Vehicle Detection

More articles on detections using Haar Cascade – Face Detection Basics



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads