Hi guys, In this article, we are going to learn how to track cars and pedestrians in real-time using OpenCV
ok, first of all,
what is OpenCV?
OpenCV is a cross-platform library using which we can develop real-time computer vision applications. It mainly focuses on image processing, video capture, and analysis including features like face detection and object detection.
Here we are focused on car and pedestrian tracking.
HAAR CASCADE ALGORITHM
The inner algorithm of the detection process used here is the Haar Cascade algorithm.
The algorithm uses edge or line detection features proposed by Viola and Jones in their research paper โRapid Object Detection using a Boosted Cascade of Simple Featuresโ published in 2001. The algorithm is given a lot of positive images consisting of faces, and a lot of negative images not consisting of any face to train on them.
Features of Haar Cascade:
The first contribution to the research was the introduction of the haar features shown above. These features on the image make it easy to find out the edges and the lines in the image or to pick areas where there is a sudden change in the intensities of the pixels.
The objective here is to find out the sum of all the image pixels lying in the darker area of the haar feature and the sum of all the image pixels lying in the lighter area of the haar feature. And then find out their difference.
The haar feature continuously traverses from the top left of the image to the bottom right to search for the particular feature. This is just a representation of the whole concept of the haar feature traversal. In its actual work, the haar feature would traverse pixel by pixel in the image. Also, all possible sizes of the haar features will be applied
I hope you understood the inner working and the algorithm used behind these detections.
Now let's go and code to Track cars and pedestrians in real-time.
To use OpenCV first you should install OpenCV.
pip install opencv-python
If you have installed it already let's go...
We also need a trained model so that we can use it to detect cars and pedestrians. you can use the haar cascade data from here for PEDESTRIANS and CARS
copy the whole thing and paste it into an XML file in the editor you are using and name it as 'haarcascade_fullbody.xml' and 'haarcascade_cars.xml' respectively.
lets code...
import cv2
pedestrian_tracker = cv2.CascadeClassifier(haarcascade_fullbody.xml)
car_tracker = cv2.CascadeClassifier(haarcascade_cars.xml)
#get video footage either from real-time or a recorded video.
#to open webcam
video = cv2.VideoCapture(0)
# video = cv2.VideoCapture(video.mp4)
#iterate forever over frames
while True:
#read the current frame
read_successful, frame = video.read()
if read_successful:
#convert into grayscale
grayscaled_frame = cv2.cvtColor(frame,COLOR_BGR2GRAY)
else:
break
#detect cars
cars = car_tracker.detectMultiScale(grayscaled_frame)
#detect pedestrians
pedestrians = pedestrian_tracker.detectMultiScale(grayscaled_frame)
#draw rectangles around cars
for (x ,y ,w ,h ) in cars:
cv2.reactangle(frame , (x+1, y+2), (x+w, y+h), (255,0,0), 2)
cv2.reactangle(frame , (x, y), (x+w, y+h), (0,0,255), 2)
#draw rectangles around pedestrians
for (x ,y ,w ,h ) in pedestrians:
cv2.reactangle(frame , (x, y), (x+w, y+h), (0,255,255), 2)
#display
cv2.imshow('Cars and pedestrain tracking', frame)
#listen for a key for 1 milli sec and move on
key = cv2.waitKey(1)
#stop if q/Q is pressed
if(key == 81 or key == 113):
break
#release video capture object
video.release()
OUTRO:
I hope you understood the code and the inner algorithms of the detection process in this article.
This is the underlying technology used in self-driving cars to detect the cars and people moving in front and back of them with some advanced deep learning techniques.
What we saw is a simple application of opencv, we can explore tons of models in it. Be creative and come up with new ideas.
related sources:
Thank you for reading ๐
HAPPY LEARNING
-JHA