Vehicle detection in Python using OpenCV
In this tutorial, we will learn how to detect multiple vehicles in the same frame. We will use the OpenCV Python library to perform this task. Install Python OpenCV library on your Python environment if you have not installed it yet.
Secondly, we need the haar cascaded files which contains the features of the vehicles which we want to detect. Here we are using cars.xml and bicycle.xml.
This program will detect the cars and the bicycles from the given footage and draws rectangle over it.
Now we will proceed towards our coding section.
Python code Vehicle detection using OpenCV
Let’s start our code:
import cv2
Import Python OpenCV library.
cap = cv2.VideoCapture('traffic.mp4') #Path to footage
car_cascade = cv2.CascadeClassifier('cars.xml') #Path to cars.xml
bicycle_cascade= cv2.CascadeClassifier('bicycle.xml') #Path to bicycle.xmlUse the VideoCapture method to process the video or footage. Give the path of vehicle models to the CascadeClassifier function.
while True:
ret, img = cap.read()
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
cars=car_cascade.detectMultiScale(gray,1.8,2)
bicycle = bicycle_cascade.detectMultiScale(gray, 1.8, 2)cap.read() function reads every frame and gives an img array. cvtColor changes image into grayscale. We do not need a colored frame to detect objects moreover it decreases the processing speed. cars and bicycle stores (x,y,w,h) for detected objects. (x,y) are location and w stands for width and h for height.
#Drawing rectangles on detected cars
for (x,y,w,h) in cars:
cv2.rectangle(img,(x,y),(x+w,y+h),(225,0,0),2)
# Drawing rectangles on detected bicycles
for (x,y,w,h) in bicycle:
cv2.rectangle(img,(x,y),(x+w,y+h),(225,0,0),2)For every (x,y,w,h) in cars or bicycle cv2.rectangle line draws a rectangle. The first argument of the rectangle is image on which we will draw the rectangle. Another two are associated with coordinates. Then color and after it the width of a line.
cv2.imshow('img',img) #Shows the frameEnter imshow line to display the image with the label ‘img’.
if cv2.waitKey(20) & 0xFF == ord('q'):
breakIf we press ‘q’ on the keyboard then the program will terminate.
cap.release() cv2.destroyAllWindows()
Then come out of while loop and write the lines described in the upper code block. These lines will release the camera device if any and de-allocate memory which was used earlier.
Now, have a look at the below box which contains the full code of this given problem.
import cv2
cap = cv2.VideoCapture('traffic.mp4') #Path to footage
car_cascade = cv2.CascadeClassifier('cars.xml') #Path to cars.xml
bicycle_cascade= cv2.CascadeClassifier('bicycle.xml') #Path to bicycle.xml
while True:
ret, img = cap.read()
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
cars=car_cascade.detectMultiScale(gray,1.8,2)
bicycle = bicycle_cascade.detectMultiScale(gray, 1.8, 2)
#Drawing rectangles on detected cars
for (x,y,w,h) in cars:
cv2.rectangle(img,(x,y),(x+w,y+h),(225,0,0),2)
# Drawing rectangles on detected bicycles
for (x,y,w,h) in bicycle:
cv2.rectangle(img,(x,y),(x+w,y+h),(225,0,0),2)
cv2.imshow('img',img) #Shows the frame
if cv2.waitKey(20) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()If everything goes fine then it will show the blue rectangles around detected cars and bicycles for given footage. So in this way, we have successfully detected vehicles using Python OpenCV.
Dear sir or madam,
My name is Ger from the Netherlands and will ask you if you can tell me where to find the cars.xml and bicycle.xml.
The program is nice and easy and just what I need.
Many thanks for an answer
Greetings Ger