Imagine you’re trying to follow a Formula 1 car as it races around a track. You don’t care where the car started—you just want to keep your eyes glued to it as it zooms around corners. Now think of doing that with a computer, but instead of eyes, it has a camera. That’s what object tracking is all about, and it looks something like this:
In this blog, we’re going to strap a GoPro to your Python code and dive into the thrilling world of object tracking using OpenCV. Whether you want to:
- Track your dog doing laps in the yard,
- Monitor traffic like a budget version of Google Maps,
- Or catch your roommate in the act of fridge robbery at 2 AM…
And don’t worry—we won’t just dump a bunch of theoretical concepts on you. We’ll explore hands-on examples, code breakdowns, and even a few fun mini-projects. This tutorial is aimed at intermediate-level Python programmers who are curious about opencv object tracking python.
Table of Contents
What is Object Tracking?
Let’s not overcomplicate this. Object tracking is when your computer picks a target in a video and then… just doesn’t let it out of its sight. Think of object detection as that nosy neighbor who checks if your car is in the driveway right now. Now think of object tracking as a private investigator following your car from your house to Taco Bell and back—with live updates.
The goal? Once you’ve locked onto something in one frame (say, your cat), you want to follow it across every frame that follows—whether it’s walking, jumping, or teleporting (as cats tend to do). Once we locate an object in the first frame, we want to follow it across subsequent frames, adjusting the bounding box as the object moves, changes size, or rotates.
Why Use OpenCV for Object Tracking?
If you are reading this blog on object tracking, then you definitely have written some code in Python and certainly have come across the word OpenCV. But in case you are unaware, let me just say that whenever you will work with images or video frames in Python, the first line of your code will most probably be import cv2
.
OpenCV (Open Source Computer Vision Library) is a go-to tool for computer vision tasks. It’s powerful, lightweight, well-documented, and—best of all—free. Here’s why it is perfect for object tracking:
- It comes with built-in tracking algorithms.
- Real-time performance is achievable, even on modest machines.
- Supports multiple tracker types to fit different use cases.
- Easy integration with webcams, IP cameras, and video files.
- Compatible with Python, which means less boilerplate code and more results.
Show me the Code already!
Enough of introductions! I know you can’t wait to copy paste the code and show it off to your friends so here we go.
Installing OpenCV (if needed)
If you haven’t installed OpenCV yet, take exactly two minutes to reflect on your life choices and then install it using the following code:
pip install opencv-python
pip install opencv-contrib-python
Basic Tracker Setup in Python
If you want to track some object in a pre-existing video, keep reading further. If you want to run it in real-time using your webcam, skip to the next section. Now comes the code:
import cv2
# Load video
video = cv2.VideoCapture("video.mp4")
# Read first frame
ret, frame = video.read()
if not ret:
print("Cannot read video")
exit()
# Select ROI manually
bbox = cv2.selectROI("Tracking", frame, fromCenter=False, showCrosshair=True)
# Initialize tracker
tracker = cv2.TrackerCSRT_create() # You can replace with TrackerKCF_create(), etc.
tracker.init(frame, bbox)
while True:
ret, frame = video.read()
if not ret:
break
# Update tracker
success, bbox = tracker.update(frame)
if success:
# Draw bounding box
x, y, w, h = [int(v) for v in bbox]
cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)
else:
cv2.putText(frame, "Tracking Failed", (20, 80),
cv2.FONT_HERSHEY_SIMPLEX, 0.75, (0, 0, 255), 2)
cv2.imshow("Tracking", frame)
# Exit on ESC
if cv2.waitKey(1) == 27:
break
video.release()
cv2.destroyAllWindows()
Boom! That’s your first object tracker code right there. You’ve got the code, the skills, and hopefully a working webcam. Now, to make the code truly run, you need to do the final and most important step: follow me on Instagram @machinelearningsite – because you want to build cool projects, but also scroll through memes without feeling guilty. Followed me? Thanks, now go ahead and copy-paste it in your code editor (yes, I know its notepad, I won’t judge you…out loud!) and run it. BUT, I am certain it will throw some errors so in the line video = cv2.VideoCapture("video.mp4")
, just provide the absolute path of your video instead of that video.mp4.
Once you run it, the first frame of the video will freeze in which you draw a box around the object you want to monitor. Press enter or space bar and the tracker will follow it across the video. But want all this to happen real time?
Real-Time Object Tracking with Webcam
Let’s use our webcam for real-time tracking.
import cv2
cap = cv2.VideoCapture(0)
ret, frame = cap.read()
bbox = cv2.selectROI("Tracking", frame, fromCenter=False, showCrosshair=True)
tracker = cv2.TrackerCSRT_create()
tracker.init(frame, bbox)
while True:
ret, frame = cap.read()
if not ret:
break
success, bbox = tracker.update(frame)
if success:
x, y, w, h = [int(v) for v in bbox]
cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)
else:
cv2.putText(frame, "Lost!", (50, 80), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (0, 0, 255), 2)
cv2.imshow("Webcam Object Tracking", frame)
if cv2.waitKey(1) & 0xFF == 27:
break
cap.release()
cv2.destroyAllWindows()
Similar to the previous case, the code will start the webcam and capture an image first. So make sure you already hold your object in front of the camera before running the code (and yes, I know how that sounds – moving on). You then select an object in frame by drawing a bounding box and ultimately press enter.
The Object Tracking Algorithms in OpenCV
If your code’s up and running, you’re good to go. But if you’re the type who likes to dig deeper, experiment wildly, and occasionally blow things up (in code, not real life – hopefully), here’s an important piece of information about the code. The object tracking algorithm we used was TrackerCSRT
of OpenCV. OpenCV also provides several other pre-built tracking algorithms under its API:
- BOOSTING Tracker – Based on AdaBoost. Slower and outdated.
- MIL Tracker – Multiple Instance Learning. Decent accuracy.
- KCF Tracker – Kernelized Correlation Filters. Fast and fairly accurate.
- MOSSE Tracker – Minimum Output Sum of Squared Error. Super fast but not very accurate.
- TLD Tracker – Tracks and detects objects. Doesn’t perform well with occlusions.
- MedianFlow Tracker – Great for tracking with minimal movement.
In case you want to try any of these out, here are the two important links that will help you: OpenCV and ChatGPT. And if object tracking didn’t completely fry your brain (or your webcam), check out these next rabbit holes:
- Creating Augmented Reality Filters with OpenCV in Python – Because why not turn your face into a banana on camera?
- Python for Beginners: Creating an App – For those who like their code with a little less math and a little more “look what I built.”
- Image Processing Using Python – Exercises – Practice makes perfect. Or at least tolerably buggy.
Summary
We kicked off this wild ride by nailing down what object tracking actually is (spoiler: it’s not the same as object detection), then dove into why “OpenCV + Python” is basically the Batman & Robin of real-time tracking. We went from processing dusty old video files to live webcam feeds and slapped together face-detection hacks. Whether you’re rigging up a “keep an eye on Fluffy” system, turning your drone into a high-tech tail, or building a novelty face-following-your-nose app, you’ve now got the chops to make it happen.
Feeling restless? Here’s some post-tutorial fuel for your next late-night coding spree:
- Track a single player throughout a sports match (because who needs VAR when you have code?).
- Build a motion-triggered alarm that freaks out whenever your cat decides to moonlight as a ninja.
- Pair OpenCV with a Raspberry Pi and create your own “I see everything” security camera.
And that’s it for object tracking. Still reading? Either you’re really into object tracking or just avoiding work. Either way, follow me on Instagram @machinelearningsite for more code tutorials, weird project ideas, and developer memes that understand your pain.
Every now and then, I remember I should probably tell people about the stuff I build. Most of it is glorified chaos, but occasionally something cool sneaks through – like this project. And if I ever blow up my laptop doing something questionable, I want you to know exactly which project caused it. So go ahead and subscribe to my newsletter. I might send you one or two updates. Maybe. If you’re lucky.