Object Tracking Across Multiple Streams using Ultralytics YOLOv8

Ultralytics
5 min readOct 3, 2023

--

Object tracking across multiple streams plays an important role in computer vision, particularly in real-world applications. It extends beyond mere problem-solving, offering valuable insights and serving as a prerequisite for object-counting modules. It empowers systems to observe and analyze the movements of objects, a fundamental requirement in scenarios such as surveillance, autonomous vehicles, and retail analytics. Ultimately, object tracking enriches comprehension and decision-making in complex environments.

Ultralytics YOLOv8 Object Tracking Across Multiple Streams
Fig-1.1: Ultralytics YOLOv8 Object Tracking Across Multiple Streams

In this article, you will learn how to perform object tracking on multiple streams using Ultralytics YOLOv8. The key components covered include:

  • Importing Required Libraries
  • Object Tracking Code for Multistreaming
  • Inference

Let’s dive in!

Import Required Libraries

Before diving into object tracking with Ultralytics YOLOv8, we need to set up the necessary libraries and dependencies. Follow these steps:

Step 1.1: Install the Ultralytics Package

Ensure you have the Ultralytics package installed by running the following command:

pip install ultralytics # Install Ultralytics Package

Step 1.2: Create a Python File and Import Libraries

Create a Python file for your project and import the essential libraries. In this project, we’ll be using OpenCV for displaying frames and Ultralytics for object detection, segmentation, and tracking.

import cv2 # Import OpenCV Library
from ultralytics import YOLO # Import Ultralytics Package

Step 1.3: Import the Threading Module

To facilitate multi-streaming, we’ll be using threading. Import the threading module as follows:

import threading # Threading module import

With these initial steps completed, we’re now ready to proceed with object tracking on multiple streams.

Fig-1.2: The coding party is starting at this moment. 😃😃😃

Object Tracking Code for Multistreaming

In this section, we’ll focus on setting up object tracking for two video streams: one from a video file and another from a webcam or IP camera.

Step 2.1: Define Video File Paths

Now, let’s define the file paths for the video streams we want to track objects on. In this example, we’ll use one video file and one webcam feed.

# Define the video files for the trackers
video_file1 = ‘ultralytics\\test.mp4’ # Video file path
video_file2 = 0 # WebCam Path

Step 2.2: Load YOLOv8 Models

First, we need to load the YOLOv8 models, which will be the backbone of our object-tracking system. We will load two models: one for object detection and another for object segmentation. The models are closely linked to the video sources; the more the number of video sources, the more models will be utilized in parallel.

# Load the YOLOv8 models
model1 = YOLO(‘yolov8n.pt’) # YOLOv8n Model
model2 = YOLO(‘yolov8s.pt’) # YOLOv8s Model

Step 2.3: Target Function for Thread

Next, let’s establish a target for our threading operation. This target will be a function responsible for running the video file and applying the YOLOv8 object-tracking algorithm. The mentioned code will act as the target function for our thread.

def run_tracker_in_thread(filename, model, file_index):
"""
This function is designed to run a video file or webcam stream
concurrently with the YOLOv8 model, utilizing threading.

- filename: The path to the video file or the webcam/external
camera source.
- model: The file path to the YOLOv8 model.
- file_index: An argument to specify the count of the
file being processed.
"""

video = cv2.VideoCapture(filename) # Read the video file

while True:
ret, frame = video.read() # Read the video frames

# Exit the loop if no more frames in either video
if not ret:
break

# Track objects in frames if available
results = model.track(frame, persist=True)
res_plotted = results[0].plot()
cv2.imshow("Tracking_Stream_"+str(file_index), res_plotted)

key = cv2.waitKey(1)
if key == ord('q'):
break

# Release video sources
video.release()

Note: The file_index parameter represents the index of the file that can be assigned to each thread. For example, if you are processing two videos concurrently, the first thread will be assigned file_index as 1, while the second thread will use file_index as 2. In the case of a third thread, it will have file_index set to 3.

Hint: If you want to do Object Detection on Multiple streams, you can replace model.track with model.predict in the above function.

Step-2.4: Create the Object Tracking Threads

Now, let’s generate the threads for object tracking. The number of threads will correspond to the number of video sources you intend to run. The provided code will create two threads to handle two video streams.

# Create the tracker thread

# Thread used for the video file
tracker_thread1 = threading.Thread(target=run_tracker_in_thread,
args=(video_file1, model1, 1),
daemon=True)

# Thread used for the webcam
tracker_thread2 = threading.Thread(target=run_tracker_in_thread,
args=(video_file2, model2, 2),
daemon=True)

Step-2.5: Start the Object Tracking Threads

Everything is set up and ready to go. Now, it’s time to initiate the object tracking threads.

# Start the tracker thread

# Start thread that run video file
tracker_thread1.start()

# Start thread that run webcam
tracker_thread2.start()

Step-2.6: Thread Handling and Destroy Windows

Following the thread’s activation, it’s equally important to manage it when it concludes its task. The mentioned code snippet serves the purpose of waiting for the object tracking thread to complete its execution.

# Wait for the tracker thread to finish

# Tracker thread 1
tracker_thread1.join()

# Tracker thread 2
tracker_thread2.join()

# Clean up and close windows
cv2.destroyAllWindows()

Note: If you need the complete code for Object Tracking Across Multiple Streams using YOLOv8, you can check https://docs.ultralytics.com/

Inference

We have now crafted a comprehensive code for handling multiple streams with YOLOv8 for object tracking. The next step is to put this code into action. Save the file where you’ve incorporated the provided code as ‘ultralytics_multi_stream_object_tracking.py’ and proceed to execute it using the following command.

# run the code file
python 'ultralytics_multi_stream_object_tracking.py'

In a matter of seconds, the processing will start, and you’ll observe the inference results on both the video file and the webcam feed.

Fig-1.3: Ultralytics YOLOv8 Object Tracking on Video Feed and WebCam

Thank you for journeying with us through the realms of “Object Tracking Across Multiple Streams using Ultralytics YOLOv8.” Take it for a spin on your content. We’re all ears (and eyes!) for your creations! 🚀

Our Trending Articles

Ultralytics

Ultralytics is on a mission to empower people and companies to unleash the positive potential of vision AI. With a commitment to simplicity, flexibility, and usability, Ultralytics YOLO and HUB lower the barriers to getting started solving the world’s challenges. Ultralytics technology enables businesses to transform their operations, enhance customer experiences, and drive innovation by seamlessly integrating state-of-the-art models and user-friendly platforms. It’s been a remarkable journey, but we’re just getting started.

--

--