In this Python OpenCV article we are going to learn about Object Tracking with Python and OpenCV, so first of all let’s talk about Object Tracking, Object tracking is the process of locating and following an object in video or image sequence over time. this technique different use cases, from surveillance systems to robotics and autonomous vehicles. In this article we are going to learn how to perform object tracking using Python and OpenCV.
What is Python OpenCV ?
OpenCV (Open Source Computer Vision Library) is an open source computer vision and machine learning software library. it provides different functions for image and video processing including object detection and tracking. OpenCV is written in C++ and has Python bindings and this makes it easy to use in Python applications.
For this article we need these libraries to be installed.
1 2 |
pip install opencv-python pip3 install opencv-contrib-python |
Object Tracking with Python and OpenCV
Now let’s write some code to perform object tracking using frame by frame tracking approach. we are going to use builtin cv2.Tracker class in OpenCV to perform the tracking. cv2.Tracker class provides several tracking algorithms including MOSSE, KCF, and MIL. for this example, we will use the KCF (Kernelized Correlation Filters) algorithm.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
import cv2 # Open video file video = cv2.VideoCapture('car.mp4') # Read first frame of the video ret, frame = video.read() # Define initial bounding box bbox = cv2.selectROI(frame, False) # Initialize tracker tracker = cv2.TrackerKCF_create() tracker.init(frame, bbox) # Loop over frames of the video while True: # Read next frame of the video ret, frame = video.read() # Break if video has ended if not ret: break # Update the tracker success, bbox = tracker.update(frame) # Draw bounding box around the object if success: x, y, w, h = [int(i) for i in bbox] cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2) # Display frame cv2.imshow('Object Tracking', frame) # Exit if the user presses 'q' if cv2.waitKey(1) == ord('q'): break # Release the video capture object and close all windows video.release() cv2.destroyAllWindows() |
Now let’s talk about above code, we open video file using the cv2.VideoCapture() function. after that we read the first frame of the video and use the cv2.selectROI() function to define the initial bounding box around the object.
1 2 3 |
video = cv2.VideoCapture('car.mp4') ret, frame = video.read() bbox = cv2.selectROI(frame, False) |
After that we initialize the tracker using the cv2.TrackerKCF_create() function and tracker.init() method. we pass in the first frame of the video and the initial bounding box.
1 2 |
tracker = cv2.TrackerKCF_create() tracker.init(frame, bbox) |
After that we enter our loop that reads each frame of the video using video.read() function. if there are no more frames to read we break out of the loop. after we update the tracker using tracker.update() method and current frame of the video. tracker.update() method returns boolean value indicating whether tracking was successful and the new bounding box coordinates.
If the tracking was successful, we draw new bounding box on the current frame using the cv2.rectangle() function. and after that we display the frame using the cv2.imshow() function.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
while True: ret, frame = video.read() if not ret: break success, bbox = tracker.update(frame) if success: x, y, w, h = [int(i) for i in bbox] cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2) cv2.imshow('Object Tracking', frame) if cv2.waitKey(1) == ord('q'): break |
and finally we have checked if the user has pressed q key. if so, we break out of the loop and release the video capture object using video.release() function. also we close all windows using the cv2.destroyAllWindows() function.
This is the result
Learn More on Python GUI
- How to Use Stylesheets in PyQt5
- How to Build Custom Widgets in PyQt6
- How to Create CheckButton in Python TKinter
- How to Add Icon to PySide6 Window
- How to Load UI in Python PySide6
- How to Create RadioButton in PySide6
- How to Create ComboBox in PySide6
- How to Create CheckBox in Python PySide6
- Responsive Applications with PyQt6 Multithreading
- Event Handling in Python and PyQt6
- How to Use Stylesheets in Python PyQt6