Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

How to use YOLOv9 and ByteTrack for Object-Tracking

Mert
3 min readApr 18, 2024

--

Imagine being able to track objects in real time, such as analysing player movement in a sports game or monitoring traffic flow at a busy intersection. This is the power of object tracking, an area of computer vision that goes beyond simple detection. It allows us not only to identify objects in a scene, but also to track their paths and movements over time.

In this blog, we’ll explore the world of object tracking using two powerful tools: YOLOv9 and ByteTrack.

  • YOLOv9 (You Only Look Once, version 9) is a state-of-the-art object detection algorithm. Think of YOLOv9 as our eyes — it can quickly and accurately identify objects in an image or video frame.
  • ByteTrack comes in after YOLOv9 has done its job. It’s a state-of-the-art tracking algorithm that takes YOLOv9’s detections and connects them across frames to create smooth object trajectories. Think of ByteTrack as the brain — it analyses the detections and keeps track of each object’s movement.

How to use it for images

Step 1: Install the libraries

pip install opencv-python ultralytics

Step 2: Import the libraries

import cv2
from ultralytics import YOLO

Step 3: Load your model

# Load the YOLOv9 model
model = YOLO('yolov9e-seg.pt') # Load an official Segment model

You can use any YOLOv9 model here. You could also use a YOLOv9 model for object detection or pose detection.

Step 4: Read your image

image_path = "YourImagePath"
image = cv2.imread(image_path)

Step 5: Use your model

# min confidence for detecting an object
conf = 0.2
# min Intersection over Union for overlapping in consecutive frames
# to be considered the same frame
iou = 0.5
# if you set show to True, then it will show the result image
results = model.track(image, persist=True, conf=conf, iou=iou, show=True, tracker="bytetrack.yaml")
print(results)
cv2.waitKey(0)

How to use it for videos

Step 1: Install the libraries

pip install opencv-python ultralytics

Step 2: Import the libraries

import cv2
from ultralytics import YOLO

Step 3: Load your model

# Load the YOLOv9 model
model = YOLO('yolov9e-seg.pt') # Load an official Segment model

You can use any YOLOv9 model here. You could also use a YOLOv9 model for object detection or pose detection.

Step 4: Read your video path

# open the video file
video_path = r"YourVideoPath"
cap = cv2.VideoCapture(video_path)

Step 5: Loop through the video frames

while cap.isOpened():
# Read a frame from the video
success, frame = cap.read()

if success:
# Run YOLOv9 tracking on the frame, persisting tracks between frames
conf = 0.2
iou = 0.5
results = model.track(frame, persist=True, conf=conf, iou=iou, show=True, tracker="bytetrack.yaml")

# Visualize the results on the frame
annotated_frame = results[0].plot()

# Display the annotated frame
cv2.imshow("YOLOv9 Tracking", annotated_frame)

Conclusion

In this tutorial, we learned how to use YOLOv9 and Bytetrack for Object Tracking. If you found this code helpful, please clap your hands and comment on this post! I would also love for you to follow me to learn more about data science and other related topics. Thanks for reading!

--

--

Mert

Bioinformatics grad, now Master's in Informatics. Passionate about Computer Vision & Deep Learning.