Vehicle detection using OpenCV Python
Vehicle detection is an important application of computer vision that helps in monitoring traffic, automating parking systems and surveillance systems. In this article, we’ll implement a simple vehicle detection system using Python and OpenCV using a pre-trained Haar Cascade classifier and we will get a video in which vehicles will be detected and it will be represented by a rectangular frame around it.
Step 1: Installing OpenCV Library
We need to install the OpenCV library or its implementation. To install it in our system we can use the below pip command:
pip install opencv-python
Step 2: Importing Required Libraries
We will using OpenCV for vehicle detection and handling video streams. Also we need time module to add delays after detecting a vehicle.
import cv2
import time
Step 3: Loading Haar Cascade Classifier and Video
We need to downlaod the Haar Cascade files (download it from here) and load the Haar Cascade classifier that’s trained to detect vehicles. Also we have to load the video file (download it from here) from which vehicles will be detected. Now to capture a video we need to create a VideoCapture() object.
haar_cascade = 'cars.xml'
car_cascade = cv2.CascadeClassifier(haar_cascade)
if car_cascade.empty():
raise Exception("Error loading Haar cascade file.")
video = 'car_video.avi'
cap = cv2.VideoCapture(video)
if not cap.isOpened():
raise Exception("Error opening video file.")
Step 4: Detecting Vehicles in Video Frames
We read each frame of the video, convert it to grayscale and use the Haar Cascade classifier to detect vehicles. Once a vehicle is detected we draw rectangles around it.
- cars = car_cascade.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=3): Detects vehicles in the grayscale image using the Haar Cascade classifier.
car_detected = False
detection_time = None
while cap.isOpened():
ret, frames = cap.read()
if not ret:
break
gray = cv2.cvtColor(frames, cv2.COLOR_BGR2GRAY)
cars = car_cascade.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=3)
if len(cars) > 0 and not car_detected:
car_detected = True
detection_time = time.time()
print("Car detected!")
if car_detected and time.time() - detection_time > 5:
break
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
Step 5: Release Video and Close Windows
After processing the frames we release the video capture object and close any OpenCV windows.
cap.release()
cv2.destroyAllWindows()
Output:

Vehicle detection systems can be further enhanced by integrating more advanced techniques such as deep learning-based models which offer better accuracy and robustness in complex environments. By using frameworks like TensorFlow or PyTorch vehicle detection systems can achieve real-time performance and handle various challenges.
Note : Above code will not run on online IDE. It will work on local system only.
You can download source code from here.
For more articles on detections using Haar Cascade - Face Detection Basics