Introduction
Hi guys ,
In this article, you’re going to cover some basics object detection in python by building your own vehicle detection script in python using an open-source computer vision library (OpenCV).
what is object detection?
Object detection is a computer technology related to computer vision and image processing that deals with detecting instances of semantic objects of a certain class in digital images and videos.
In this tutorial, we will learn how to perform Real-time vehicle detection in a video or from camera streams using OpenCV Library using a pre-trained vehicle cascade model.
Requirements
To able to follow through this tutorial you’re supposed to have the following;
- OpenCV library
- Pre-trained cascade classifier
- Sample Video with cars in it
Installation
$ 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
Sample 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 you can download it through the below link.
Project Directory
Your Project directory should look like this
. ├── app.py ├── cars.mp4 └── haarcascade_car.xml
Coding our Python vehicle detection script
Now let’s get hand dirty by building what we have just talked about, make use you have the model XML file and sample 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
- scaleFactor
- minNeighbors
ScaleFactor specifying 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
cars = cars_cascade.detectMultiScale(frame, scaleFactor, minNeighbors)
When you run the above line of code it will perform detection in the frame and 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 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)
detect_cars ( ) function
We also need a function which receives image frames and then draw rectangles around it using the detected coordinates 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 Simulator function ( )
Finally Let’s build a simulator function to load the video, perform vehicle detection by calling 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()
Running our app as a script
Add the below two lines to restrict to run your app as a script
if __name__ == '__main__': Simulator()
Final Code (Vehicle_detector.py)
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()
To download the full code check it on My Github Profile
Don’t forget to subscribe to this blog to stay updated on upcoming Python tutorials
I also recommend you to read this;
- How to convert picture to sound in Python
- Build a Real-time barcode reader in Python
- Getting started with image processing using a pillow
- How to detect Edges in a picture using OpenCV Canny algorithm
In case of comment, suggestion, or difficulties drop it in the comment box below and I will get back to you ASAP
One thought on “Real-time vehicle detection in Python”