Ultralytics/ultralytics (2024)

Ultralytics/ultralytics (1)

Object tracking in the realm of video analytics is a critical task that not only identifies the location and class of objects within the frame but also maintains a unique ID for each detected object as the video progresses. The applications are limitless—ranging from surveillance and security to real-time sports analytics.

Why Choose Ultralytics YOLO for Object Tracking?

The output from Ultralytics trackers is consistent with standard object detection but has the added value of object IDs. This makes it easy to track objects in video streams and perform subsequent analytics. Here's why you should consider using Ultralytics YOLO for your object tracking needs:

  • Efficiency: Process video streams in real-time without compromising accuracy.
  • Flexibility: Supports multiple tracking algorithms and configurations.
  • Ease of Use: Simple Python API and CLI options for quick integration and deployment.
  • Customizability: Easy to use with custom trained YOLO models, allowing integration into domain-specific applications.

Video Tutorial: Object Detection and Tracking with Ultralytics YOLOv8.

Features at a Glance

Ultralytics YOLO extends its object detection features to provide robust and versatile object tracking:

  • Real-Time Tracking: Seamlessly track objects in high-frame-rate videos.
  • Multiple Tracker Support: Choose from a variety of established tracking algorithms.
  • Customizable Tracker Configurations: Tailor the tracking algorithm to meet specific requirements by adjusting various parameters.

Available Trackers

Ultralytics YOLO supports the following tracking algorithms. They can be enabled by passing the relevant YAML configuration file such as tracker=tracker_type.yaml:

  • BoT-SORT - Use botsort.yaml to enable this tracker.
  • ByteTrack - Use bytetrack.yaml to enable this tracker.

The default tracker is BoT-SORT.

Tracking

To run the tracker on video streams, use a trained Detect, Segment or Pose model such as YOLOv8n, YOLOv8n-seg and YOLOv8n-pose.

Python

from ultralytics import YOLO# Load an official or custom modelmodel = YOLO("yolov8n.pt") # Load an official Detect modelmodel = YOLO("yolov8n-seg.pt") # Load an official Segment modelmodel = YOLO("yolov8n-pose.pt") # Load an official Pose modelmodel = YOLO("path/to/best.pt") # Load a custom trained model# Perform tracking with the modelresults = model.track(source="https://youtu.be/LNwODJXcvt4", show=True) # Tracking with default trackerresults = model.track( source="https://youtu.be/LNwODJXcvt4", show=True, tracker="bytetrack.yaml") # Tracking with ByteTrack tracker

CLI

# Perform tracking with various models using the command line interfaceyolo track model=yolov8n.pt source="https://youtu.be/LNwODJXcvt4" # Official Detect modelyolo track model=yolov8n-seg.pt source="https://youtu.be/LNwODJXcvt4" # Official Segment modelyolo track model=yolov8n-pose.pt source="https://youtu.be/LNwODJXcvt4" # Official Pose modelyolo track model=path/to/best.pt source="https://youtu.be/LNwODJXcvt4" # Custom trained model# Track using ByteTrack trackeryolo track model=path/to/best.pt tracker="bytetrack.yaml"

As can be seen in the above usage, tracking is available for all Detect, Segment and Pose models run on videos or streaming sources.

Configuration

Tracking Arguments

Tracking configuration shares properties with Predict mode, such as conf, iou, and show. For further configurations, refer to the Predict model page.

Python

from ultralytics import YOLO# Configure the tracking parameters and run the trackermodel = YOLO("yolov8n.pt")results = model.track(source="https://youtu.be/LNwODJXcvt4", conf=0.3, iou=0.5, show=True)

CLI

# Configure tracking parameters and run the tracker using the command line interfaceyolo track model=yolov8n.pt source="https://youtu.be/LNwODJXcvt4" conf=0.3, iou=0.5 show

Tracker Selection

Ultralytics also allows you to use a modified tracker configuration file. To do this, simply make a copy of a tracker config file (for example, custom_tracker.yaml) from ultralytics/cfg/trackers and modify any configurations (except the tracker_type) as per your needs.

Python

from ultralytics import YOLO# Load the model and run the tracker with a custom configuration filemodel = YOLO("yolov8n.pt")results = model.track(source="https://youtu.be/LNwODJXcvt4", tracker="custom_tracker.yaml")

CLI

# Load the model and run the tracker with a custom configuration file using the command line interfaceyolo track model=yolov8n.pt source="https://youtu.be/LNwODJXcvt4" tracker='custom_tracker.yaml'

For a comprehensive list of tracking arguments, refer to the ultralytics/cfg/trackers page.

Python Examples

Persisting Tracks Loop

Here is a Python script using OpenCV (cv2) and YOLOv8 to run object tracking on video frames. This script still assumes you have already installed the necessary packages (opencv-python and ultralytics). The persist=True argument tells the tracker than the current image or frame is the next in a sequence and to expect tracks from the previous image in the current image.

Python

import cv2from ultralytics import YOLO# Load the YOLOv8 modelmodel = YOLO("yolov8n.pt")# Open the video filevideo_path = "path/to/video.mp4"cap = cv2.VideoCapture(video_path)# Loop through the video frameswhile cap.isOpened(): # Read a frame from the video success, frame = cap.read() if success: # Run YOLOv8 tracking on the frame, persisting tracks between frames results = model.track(frame, persist=True) # Visualize the results on the frame annotated_frame = results[0].plot() # Display the annotated frame cv2.imshow("YOLOv8 Tracking", annotated_frame) # Break the loop if 'q' is pressed if cv2.waitKey(1) & 0xFF == ord("q"): break else: # Break the loop if the end of the video is reached break# Release the video capture object and close the display windowcap.release()cv2.destroyAllWindows()

Please note the change from model(frame) to model.track(frame), which enables object tracking instead of simple detection. This modified script will run the tracker on each frame of the video, visualize the results, and display them in a window. The loop can be exited by pressing 'q'.

Plotting Tracks Over Time

Visualizing object tracks over consecutive frames can provide valuable insights into the movement patterns and behavior of detected objects within a video. With Ultralytics YOLOv8, plotting these tracks is a seamless and efficient process.

In the following example, we demonstrate how to utilize YOLOv8's tracking capabilities to plot the movement of detected objects across multiple video frames. This script involves opening a video file, reading it frame by frame, and utilizing the YOLO model to identify and track various objects. By retaining the center points of the detected bounding boxes and connecting them, we can draw lines that represent the paths followed by the tracked objects.

Python

from collections import defaultdictimport cv2import numpy as npfrom ultralytics import YOLO# Load the YOLOv8 modelmodel = YOLO("yolov8n.pt")# Open the video filevideo_path = "path/to/video.mp4"cap = cv2.VideoCapture(video_path)# Store the track historytrack_history = defaultdict(lambda: [])# Loop through the video frameswhile cap.isOpened(): # Read a frame from the video success, frame = cap.read() if success: # Run YOLOv8 tracking on the frame, persisting tracks between frames results = model.track(frame, persist=True) # Get the boxes and track IDs boxes = results[0].boxes.xywh.cpu() track_ids = results[0].boxes.id.int().cpu().tolist() # Visualize the results on the frame annotated_frame = results[0].plot() # Plot the tracks for box, track_id in zip(boxes, track_ids): x, y, w, h = box track = track_history[track_id] track.append((float(x), float(y))) # x, y center point if len(track) > 30: # retain 90 tracks for 90 frames track.pop(0) # Draw the tracking lines points = np.hstack(track).astype(np.int32).reshape((-1, 1, 2)) cv2.polylines( annotated_frame, [points], isClosed=False, color=(230, 230, 230), thickness=10, ) # Display the annotated frame cv2.imshow("YOLOv8 Tracking", annotated_frame) # Break the loop if 'q' is pressed if cv2.waitKey(1) & 0xFF == ord("q"): break else: # Break the loop if the end of the video is reached break# Release the video capture object and close the display windowcap.release()cv2.destroyAllWindows()

Multithreaded Tracking

Multithreaded tracking provides the capability to run object tracking on multiple video streams simultaneously. This is particularly useful when handling multiple video inputs, such as from multiple surveillance cameras, where concurrent processing can greatly enhance efficiency and performance.

In the provided Python script, we make use of Python's threading module to run multiple instances of the tracker concurrently. Each thread is responsible for running the tracker on one video file, and all the threads run simultaneously in the background.

To ensure that each thread receives the correct parameters (the video file and the model to use), we define a function run_tracker_in_thread that accepts these parameters and contains the main tracking loop. This function reads the video frame by frame, runs the tracker, and displays the results.

Two different models are used in this example: yolov8n.pt and yolov8n-seg.pt, each tracking objects in a different video file. The video files are specified in video_file1 and video_file2.

The daemon=True parameter in threading.Thread means that these threads will be closed as soon as the main program finishes. We then start the threads with start() and use join() to make the main thread wait until both tracker threads have finished.

Finally, after all threads have completed their task, the windows displaying the results are closed using cv2.destroyAllWindows().

Python

import threadingimport cv2from ultralytics import YOLOdef run_tracker_in_thread(filename, model): """Starts multi-thread tracking on video from `filename` using `model` and displays results frame by frame.""" video = cv2.VideoCapture(filename) frames = int(video.get(cv2.CAP_PROP_FRAME_COUNT)) for _ in range(frames): ret, frame = video.read() if ret: results = model.track(source=frame, persist=True) res_plotted = results[0].plot() cv2.imshow("p", res_plotted) if cv2.waitKey(1) == ord("q"): break# Load the modelsmodel1 = YOLO("yolov8n.pt")model2 = YOLO("yolov8n-seg.pt")# Define the video files for the trackersvideo_file1 = "path/to/video1.mp4"video_file2 = "path/to/video2.mp4"# Create the tracker threadstracker_thread1 = threading.Thread(target=run_tracker_in_thread, args=(video_file1, model1), daemon=True)tracker_thread2 = threading.Thread(target=run_tracker_in_thread, args=(video_file2, model2), daemon=True)# Start the tracker threadstracker_thread1.start()tracker_thread2.start()# Wait for the tracker threads to finishtracker_thread1.join()tracker_thread2.join()# Clean up and close windowscv2.destroyAllWindows()

This example can easily be extended to handle more video files and models by creating more threads and applying the same methodology.

Contribute New Trackers

Are you proficient in multi-object tracking and have successfully implemented or adapted a tracking algorithm with Ultralytics YOLO? We invite you to contribute to our Trackers section in ultralytics/cfg/trackers! Your real-world applications and solutions could be invaluable for users working on tracking tasks.

By contributing to this section, you help expand the scope of tracking solutions available within the Ultralytics YOLO framework, adding another layer of functionality and utility for the community.

To initiate your contribution, please refer to our Contributing Guide for comprehensive instructions on submitting a Pull Request (PR) 🛠️. We are excited to see what you bring to the table!

Together, let's enhance the tracking capabilities of the Ultralytics YOLO ecosystem 🙏!

Ultralytics/ultralytics (2024)

References

Top Articles
Electric Storage Heaters - Eliminate Backup Fossil Fuel Heating System
Thousands chatted with this AI 'virtual girlfriend.' Then things got even weirder
neither of the twins was arrested,传说中的800句记7000词
No Hard Feelings (2023) Tickets & Showtimes
Worcester Weather Underground
Paris 2024: Kellie Harrington has 'no more mountains' as double Olympic champion retires
Body Rubs Austin Texas
Naturalization Ceremonies Can I Pick Up Citizenship Certificate Before Ceremony
Cosentyx® 75 mg Injektionslösung in einer Fertigspritze - PatientenInfo-Service
Corporate Homepage | Publix Super Markets
Campaign Homecoming Queen Posters
Ucf Event Calendar
Edible Arrangements Keller
Bc Hyundai Tupelo Ms
Best Fare Finder Avanti
Marion County Wv Tax Maps
6001 Canadian Ct Orlando Fl
Q Management Inc
U Break It Near Me
Apply for a credit card
Never Give Up Quotes to Keep You Going
U Of Arizona Phonebook
Galaxy Fold 4 im Test: Kauftipp trotz Nachfolger?
4Oxfun
Stephanie Bowe Downey Ca
Housing Intranet Unt
Guide to Cost-Benefit Analysis of Investment Projects Economic appraisal tool for Cohesion Policy 2014-2020
Vip Lounge Odu
Frequently Asked Questions - Hy-Vee PERKS
Star News Mugshots
Math Minor Umn
Mbi Auto Discount Code
Tamilrockers Movies 2023 Download
Old Peterbilt For Sale Craigslist
Why The Boogeyman Is Rated PG-13
CVS Near Me | Somersworth, NH
Umiami Sorority Rankings
Whitehall Preparatory And Fitness Academy Calendar
How Many Dogs Can You Have in Idaho | GetJerry.com
Worcester County Circuit Court
Avance Primary Care Morrisville
Costco Gas Foster City
Sour OG is a chill recreational strain -- just have healthy snacks nearby (cannabis review)
Patricia And Aaron Toro
Fluffy Jacket Walmart
Willkommen an der Uni Würzburg | WueStart
Lorton Transfer Station
Www Pig11 Net
Congruent Triangles Coloring Activity Dinosaur Answer Key
Ics 400 Test Answers 2022
Island Vibes Cafe Exeter Nh
Latest Posts
Article information

Author: Merrill Bechtelar CPA

Last Updated:

Views: 5788

Rating: 5 / 5 (50 voted)

Reviews: 81% of readers found this page helpful

Author information

Name: Merrill Bechtelar CPA

Birthday: 1996-05-19

Address: Apt. 114 873 White Lodge, Libbyfurt, CA 93006

Phone: +5983010455207

Job: Legacy Representative

Hobby: Blacksmithing, Urban exploration, Sudoku, Slacklining, Creative writing, Community, Letterboxing

Introduction: My name is Merrill Bechtelar CPA, I am a clean, agreeable, glorious, magnificent, witty, enchanting, comfortable person who loves writing and wants to share my knowledge and understanding with you.