![]() |
VOOZH | about |
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.
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
We will using OpenCV for vehicle detection and handling video streams. Also we need time module to add delays after detecting a vehicle.
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.
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.
After processing the frames we release the video capture object and close any OpenCV windows.
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