In this article, I will guide you on how to do real-time vehicle detection in python using the OpenCV library and trained cascade classifier in just a few lines of code.
a brief about vehicle detection
Real-time vehicle detection is one of the many application of object detection, whereby focuses on detecting cars within an image together with the location coordinates.
where is being used?
vehicle detection together with road detection is heavily applied on ** self-driving cars*, for a car to navigate safely along the road, it has to know where other cars are positioned so as it can avoid a collision.
Also vehicle detection is being used in traffic surveillance systems in such a way it can detect the traffic based on number vehicle and use that data to manage and control it
what will you build?
In this tutorial, we will learn how to perform Real-time vehicle detection in a video or from camera streams using OpenCV and a pre-trained cascade model.
To able to follow through with this tutorial you’re supposed to have the following on your machine
$ pip install opencv-python
Pre-trained Cascade classifier
As I have explained earlier, we are not going to be training our model to spot cars in video frames from scratch instead we gonna use a pre-trained one.
These trained cascade classifiers are usually being stored in the XML format, therefore you should download the cascade that was trained to detect cars and have it in the project directory.
To download the trained cascade mode click-here
Demo video with cars in it
You can actually use any video you want as long it has cars in it, the cascade model will be able to detect them.
If you would like to use the same video I used for this article Download here;
Your project directory should look like this
. ├── app.py ├── cars.mp4 └── haarcascade_car.xml
Let's get hands dirty
Now let's begin by building what we have just talked about, using you have the XML Model and demo video on your project directory.
Loading our Model
use cv2.CascadeClassifier() to load the trained haarcascade model as shown in the code below.
import cv2 cars_cascade = cv2.CascadeClassifier('haarcascade_car.xml')
Detecting cars in a video
we will use the detectMultiScale () method to detect and to get the coordinates of vehicles in the video frames.
The detectMultiScale () method receives 3 parameters to actually give your coordinates as shown below
- Grayscale image
Grayscale image specify the image to be processed, in our case a grayscale image is going to be image fetched from the video streams.
ScaleFactor specify how much the image size is reduced at each image scale, you can learn more about it here, a good value is mostly chosen as 1.05
minNeighbors specify how many neighbors each candidate rectangle should have to retain it, this parameter will affect the quality of the detected faces.
A higher value results in fewer detections but with higher quality usually, 3-6 is a good value for it
Syntax to detect cars + their positional coordinates
cars = cars_cascade.detectMultiScale(frame, scaleFactor, minNeighbors)
When you run the above line of code it will perform cars detection in the frame image and then return to us all coordinates of cars found (diagonal coordinates point).
Drawing rectangle around detected cars
After detecting all the coordinates of all the cars in a frame, we to draw a rectangle around it for us to able to see the detection process visually.
We will use the cv2.rectangle() method to draw a rectangle around every detected car using diagonal coordinate points returned by our cascade classifier.
Syntax to use the cv2.rectangle () method
cv2.rectangle(frame , point1, point2, color = (), thickness=value)
building a function to do all the detection process
We need to condense what we just learned and put into a single function that receives image frames and then draws rectangles around it using the detected coordinates just as shown below.
def detect_cars(frame): cars = cars_cascade.detectMultiScale(frame, 1.15, 4) for (x, y, w, h) in cars: cv2.rectangle(frame, (x, y), (x+w,y+h), color=(0, 255, 0), thickness=2) return frame
Building a function to simulate the detection process
finally let's add a single function to simulate the whole process from loading the video, to perform vehicle detection by calling the detect_cars function, and then render a frame with detected vehicles on the screen.
def Simulator(): CarVideo = cv2.VideoCapture('cars.mp4') while CarVideo.isOpened(): ret, frame = CarVideo.read() controlkey = cv2.waitKey(1) if ret: cars_frame = detect_cars(frame) cv2.imshow('frame', cars_frame) else: break if controlkey == ord('q'): break CarVideo.release() cv2.destroyAllWindows()
Add these two lines so as we make sure that we are running our python code as a script.
if __name__ == '__main__': Simulator()
Let's bundle everything together
Now we know how to do each independent piece of our detection script, it's time to put them together so as we can run it.
Once you put all the concept we learned above into one app.py, your code is going to look just as shown below
import cv2 cars_cascade = cv2.CascadeClassifier('haarcascade_car.xml') def detect_cars(frame): cars = cars_cascade.detectMultiScale(frame, 1.15, 4) for (x, y, w, h) in cars: cv2.rectangle(frame, (x, y), (x+w,y+h), color=(0, 255, 0), thickness=2) return frame def Simulator(): CarVideo = cv2.VideoCapture('cars.mp4') while CarVideo.isOpened(): ret, frame = CarVideo.read() controlkey = cv2.waitKey(1) if ret: cars_frame = detect_cars(frame) cv2.imshow('frame', cars_frame) else: break if controlkey == ord('q'): break CarVideo.release() cv2.destroyAllWindows() if __name__ == '__main__': Simulator()
We have reached the end of our article, hope you learned something, now share it with your fellow friends on Twitter and other developer communities
Follow me on Twiter