This article was published as a part of the Data Science Blogathon.
In this article, we will learn how to make an object tracker using OpenCV in Python and using, and we will build an object tracker and make a counter system.
A tracker keeps track of moving objects in the frame; In OpenCV, we can build a tracker class using Euclidean distance tracking or centroid tracking.
Object tracking and the counter system are only used in video or camera feed. Our tracker will assign a specific id to the moving objects, which will not change throughout the feed.
Generally, there could be two types of object trackers based on their capabilities.
In this article, we will design a Multiple Object Tracker, and using that; we will make a Vehicle Counter System using Python.
There are various tracking algorithms available in the market. They have different working algorithms and complexity.
Some Popular tracking algorithms are given below-
Step 1. In the first step, moving objects will be detected, and a bounding box for each and every moving object will be found. Using the bounding box coordinates, the centroid points will be calculated, which will be the point of diagonal intersection of the bounding box rectangle.
Centroids calculation
Step 2. After calculating the centroid points, assign them a specific unique id and then calculate the euclidean distance between every possible centroid in our frame.
Euclidean Distance between centroids
Step 3. The main Idea of Centroid is that the same object will be moved a minimum distance compared to others points in the subsequent frames. Using this idea,, whichever centroid has the minimum distance pair will be assigned with the same id as the previous centroid with the least euclidean distance.
Centroid distance difference
Step 4. We can assign previous IDs to the object in the subsequent frame based on the euclidean distance difference.
Points being Tracked
We are using the subtraction technique on subsequent frames to capture the moving points ( F(t+1) -F(t)). However, we are free to use any object detector algorithms based on our objective.
The involvement of deep learning in Object tracking makes it more robust daily. Modern object trackers are far better than what we have seen previously. These Object Trackers are being deployed extensively.
Euclidean Distance Tracker in Python
By Combining all the Processes we discussed, we made a EuclideanDistTracker
class In Python. This Tracker Object will return me the tracking Id and coordinates of the moving objects.
This Class Contains all the math involved in Object Tracking.
All the source codes used in this article can be downloaded through this link. I encourage you to download the source code files in order to avoid any typing mistakes in your workbook.
After downloading the tracker file, save it as tracker.py
To import in python as a module. We will import the tracker file later. Download the file tracker.py
using this link.
Functions Available in our Tracker Class
update
→ It inputs bounding box coordinates in an array.object_id
].object_id
is the unique id given to the objects?Before Starting, we will import the tracker object and initiate a video feed; it could be a camera feed or a video feed.
import cv2 import numpy as np from tracker import EuclideanDistTracker tracker = EuclideanDistTracker() cap = cv2.VideoCapture('RoadRage.mp4') ret, frame1 = cap.read() ret, frame2 = cap.read()
We are using the sample highway video provided by OpenCV as a sample.
cap.read()
It returns the frame and a boolean value after reading the feed.
In this part, we will implement object detection using subsequent frame subtraction in OpenCV and use the detected objects for tracking using the tracker class.
Object detection might catch some noise due to unwanted moving objects like trees, leaves etc. we will filter all unwanted moving objects by defining a minimum threshold area.
while cap.isOpened(): diff = cv2.absdiff(frame1, frame2) gray = cv2.cvtColor(diff, cv2.COLOR_BGR2GRAY) blur = cv2.GaussianBlur(gray, (5,5), 0 ) height, width = blur.shape print(height, width) dilated = cv2.dilate(threshold, (1,1), iterations=1) contours, _, = cv2.findContours(dilated, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) detections = [] for contour in contours: (x,y,w,h) = cv2.boundingRect(contour) if cv2.contourArea(contour) <300: continue detections.append([x,y,w,h]) boxes_ids = tracker.update(detections) for box_id in boxes_ids: x,y,w,h,id = box_id cv2.putText(frame1, str(id),(x,y-15), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0,0,255), 2) cv2.rectangle(frame1, (x,y),(x+w, y+h), (0,255,0), 2) cv2.imshow('frame',frame1)
frame1 = frame2 ret, frame2 = cap.read() key = cv2.waitKey(30) if key == ord('q): break cv2.destroyAllWindows()
cv2.absdiff
This method is used to find the difference between two subsequent frames and based on the difference, we will catch moving objects.boxes_ids
It contains the associated object Ids and coordinates of bounding boxes.Output Frame:
In this article, we discussed Object Detection algorithms and learned the process of Euclidean Tracker. Using all the knowledge, we made a Tracker Class and implemented a Tracker in OpenCV.
We made a vehicle counter system using the Euclidean Tracker and object detection using frame subtraction.
Centroid Tracker is easy to implement but inaccurate compared to modern Tracking algorithms. Euclidean and Centroid Tracker Algorithms will fail if objects travel at different speeds. Hence it doesn’t suit the real-world scenario.
Thanks for Reading!!
The media shown in this article is not owned by Analytics Vidhya and is used at the Author’s discretion.
Lorem ipsum dolor sit amet, consectetur adipiscing elit,