Back to Ultralytics

Setup

examples/object_tracking.ipynb

8.4.468.8 KB
Original Source
<div align="center"> <a href="https://ultralytics.com/yolo" target="_blank"> </a>

中文 | 한국어 | 日本語 | Русский | Deutsch | Français | Español | Português | Türkçe | Tiếng Việt | العربية

<a href="https://github.com/ultralytics/ultralytics/actions/workflows/ci.yml"></a> <a href="https://console.paperspace.com/github/ultralytics/ultralytics"></a> <a href="https://colab.research.google.com/github/ultralytics/ultralytics/blob/main/examples/object_tracking.ipynb"></a> <a href="https://www.kaggle.com/models/ultralytics/yolo26"></a> <a href="https://ultralytics.com/discord"></a>

Welcome to the Ultralytics YOLO26 🚀 notebook! <a href="https://github.com/ultralytics/ultralytics">YOLO26</a> is the latest version of the YOLO (You Only Look Once) AI models developed by <a href="https://ultralytics.com">Ultralytics</a>. This notebook serves as the starting point for exploring the various resources available to help you get started with YOLO26 and understand its features and capabilities.

YOLO26 models are fast, accurate, and easy to use, making them ideal for various object detection and image segmentation tasks. They can be trained on large datasets and run on diverse hardware platforms, from CPUs to GPUs.

We hope that the resources in this notebook will help you get the most out of YOLO26. Please browse the YOLO26 <a href="https://docs.ultralytics.com/modes/track/"> Tracking Docs</a> for details, raise an issue on <a href="https://github.com/ultralytics/ultralytics">GitHub</a> for support, and join our <a href="https://ultralytics.com/discord">Discord</a> community for questions and discussions!

</div>

Setup

pip install ultralytics and dependencies and check software and hardware.

python
!uv pip install ultralytics
import ultralytics

ultralytics.checks()

Ultralytics Object Tracking

Ultralytics YOLO11 instance segmentation involves identifying and outlining individual objects in an image, providing a detailed understanding of spatial distribution. Unlike semantic segmentation, it uniquely labels and precisely delineates each object, crucial for tasks like object detection and medical imaging.

There are two types of instance segmentation tracking available in the Ultralytics package:

  • Instance Segmentation with Class Objects: Each class object is assigned a unique color for clear visual separation.

  • Instance Segmentation with Object Tracks: Every track is represented by a distinct color, facilitating easy identification and tracking.

Samples

Instance SegmentationInstance Segmentation + Object Tracking
Ultralytics Instance Segmentation 😍Ultralytics Instance Segmentation with Object Tracking 🔥

CLI

Command-Line Interface (CLI) example.

python
!yolo track source="/path/to/video.mp4" save=True

Python

Python Instance Segmentation and Object tracking example.

python
from collections import defaultdict

import cv2

from ultralytics import YOLO

# Dictionary to store tracking history with default empty lists
track_history = defaultdict(lambda: [])

# Load the YOLO model with segmentation capabilities
model = YOLO("yolo26n-seg.pt")

# Open the video file
cap = cv2.VideoCapture("path/to/video.mp4")

# Retrieve video properties: width, height, and frames per second
w, h, fps = (int(cap.get(x)) for x in (cv2.CAP_PROP_FRAME_WIDTH, cv2.CAP_PROP_FRAME_HEIGHT, cv2.CAP_PROP_FPS))

# Initialize video writer to save the output video with the specified properties
out = cv2.VideoWriter("instance-segmentation-object-tracking.avi", cv2.VideoWriter_fourcc(*"MJPG"), fps, (w, h))

while True:
    # Read a frame from the video
    ret, im0 = cap.read()
    if not ret:
        print("Video frame is empty or video processing has been successfully completed.")
        break

    # Perform object tracking on the current frame
    results = model.track(im0, persist=True)

    # Plot results with instance-colored masks (each track_id gets a unique color)
    im0 = results[0].plot(color_mode="instance", line_width=2)

    # Write the annotated frame to the output video
    out.write(im0)
    # Display the annotated frame
    cv2.imshow("instance-segmentation-object-tracking", im0)

    # Exit the loop if 'q' is pressed
    if cv2.waitKey(1) & 0xFF == ord("q"):
        break

# Release the video writer and capture objects, and close all OpenCV windows
out.release()
cap.release()
cv2.destroyAllWindows()

Additional Resources

Community Support

For more information on using tracking with Ultralytics, you can explore the comprehensive Ultralytics Tracking Docs. This guide covers everything from basic concepts to advanced techniques, ensuring you get the most out of tracking and visualization.

Ultralytics ⚡ Resources

At Ultralytics, we are committed to providing cutting-edge AI solutions. Here are some key resources to learn more about our company and get involved with our community:

  • Ultralytics HUB: Simplify your AI projects with Ultralytics HUB, our no-code tool for effortless YOLO training and deployment.
  • Ultralytics Licensing: Review our licensing terms to understand how you can use our software in your projects.
  • About Us: Discover our mission, vision, and the story behind Ultralytics.
  • Join Our Team: Explore career opportunities and join our team of talented professionals.

YOLO26 🚀 Resources

YOLO26 is the latest evolution in the YOLO series, offering state-of-the-art performance in object detection and image segmentation. Here are some essential resources to help you get started with YOLO26:

  • GitHub: Access the YOLO26 repository on GitHub, where you can find the source code, contribute to the project, and report issues.
  • Docs: Explore the official documentation for YOLO26, including installation guides, tutorials, and detailed API references.
  • Discord: Join our Discord community to connect with other users, share your projects, and get help from the Ultralytics team.

These resources are designed to help you leverage the full potential of Ultralytics' offerings and YOLO26. Whether you're a beginner or an experienced developer, you'll find the information and support you need to succeed.