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.
- Download the input video, i.e. video.avi from here.
Importing the Required Libraries
We are going to use OpenCV, so let’s import it.
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
ret, frames = cap.read()
gray = cv2.cvtColor(frames, cv2.COLOR_BGR2GRAY)
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
for (x,y,w,h) in cars:
cv2.rectangle(frames,(x,y),(x + w,y + h),( 0 , 0 , 255 ), 2 )
cv2.imshow( 'video' , frames)
|
Let’s combine all the above steps:
Python3
while True :
ret, frames = cap.read()
gray = cv2.cvtColor(frames, cv2.COLOR_BGR2GRAY)
cars = car_cascade.detectMultiScale(gray, 1.1 , 1 )
for (x,y,w,h) in cars:
cv2.rectangle(frames,(x,y),(x + w,y + h),( 0 , 0 , 255 ), 2 )
cv2.imshow( 'video' , frames)
if cv2.waitKey( 33 ) = = 27 :
break
cv2.destroyAllWindows()
|
This is how the output will look

Vehicle Detection
More articles on detections using Haar Cascade – Face Detection Basics
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
05 Jul, 2023
Like Article
Save Article