Object Tracker Using OpenCV and Python

Chandra Shekar Reddy
AI Mind
Published in
3 min readJul 4, 2023

--

Object Tracking: Select a bounding box and track it throughout the video

My main goal in this project is to track an object in a real-time video stream from our webcam.

If you are new to OpenCV and looking for a simple project to start with, then this project is for you.

Why do we need object tracking?

we need to track objects in videos for a variety of reasons:

1. To track a person for surveillance

2. To track a ball during a football match

3. To track the movement of a car (as a self-driving car does) etc.

The applications are endless.

Object tracking is the process of:

1. Taking an initial set of object detections (such as an input set of bounding box coordinates)

2. Creating a unique ID for each of the initial detections

3. And then tracking each of the objects as they move around frames in a video, maintaining the assignment of unique IDs.

Luckily, OpenCV provides a tracker API that contains implementations of many single object tracking algorithms which we can use to simplify the object tracking task.

In this project we are using OpenCV which has 8 different object trackers available:

1. BOOSTING Tracker

2. MIL Tracker

3. KCF Tracker

4. TLD Tracker

5. MEDIANFLOW Tracker

6. GOTURN Tracker

7. MOSSE Tracker

8. CSRT Tracker

For this project we are using CSRT Tracker.

The CSRT tracker uses spatial reliability to correct the tracker if it begins to fail. It also uses a Gaussian function to penalize spatial inconsistencies between frames. The CSRT tracker is more accurate than the KCF or other trackers but more expensive computationally

You can find more information about the trackers and their performance here.

The same implementation could easily be applied to other object trackers as well.

Let’s go ahead and implement object tracking with OpenCV!

Please install the openCV package using the following command:

python -m pip install opencv-contrib-python

opencv-contrib-python package contains both the main modules of OpenCV and the contrib modules, which includes other algorithms that are not in the main modules.

we will begin by importing our required Python packages:

import cv2 as cv

cv2 is the only package we would need for this project.

Next, we’ll initialize our video stream, tracker, and the bounding box:

CAMERA_INDEX = 1 # 0 for built-in camera, 1 for external camera
cap = cv.VideoCapture(CAMERA_INDEX)
tracker = cv.TrackerCSRT_create()
BB = None

cap: Initialized camera capture with the camera index

tracker: Initialized tracker using CSRT algorithm.

BB: Bounding Box

Now let’s implement our main tracing function track:

def track(frame):
(success, box) = tracker.update(frame)
if success:
(x, y, w, h) = [int(v) for v in box]
cv.rectangle(frame, (x, y), (x+w, y+h), (255, 0, 0), 2)
return success, frame

The track function takes a single argument, frame, which is the current frame of the video stream we are processing.

Next, we’ll call the update method of our tracker object, passing in our frame:

while True:
ret, frame = cap.read()

if BB is not None:
success, frame = track(frame)

cv.imshow("Frame", frame)
key = cv.waitKey(1) & 0xFF

if key == ord("c"):
BB = cv.selectROI("Frame", frame, fromCenter=False, showCrosshair=True)
tracker.init(frame, BB)

elif key == ord("q"):
break
cap.release()
cv.destroyAllWindows()

You should see a window open with the video stream displayed inside of it. Press the c key to select a bounding box around the object you want to track. Once you have selected the bounding box, press the ENTER or SPACE key to confirm the selection. The object will then be tracked in subsequent frames of the video stream.

Press the q key to stop the video stream and exit the application.

You can press c key to re-initialize the tracker with new Bounding Box.

That’s the end of this project. I hope you enjoyed it and learned something new.

You can find the full implementation of this project in my GitHub.

--

--