Meet YOLO26: next-gen vision AI.

Link to this section使用 Ultralytics YOLO 进行多目标跟踪#

YOLO multi-object tracking with trajectory paths

视频分析领域中的目标跟踪是一项关键任务,它不仅能识别帧内目标的位置和类别,还能在视频播放过程中为每个检测到的目标维持一个唯一的 ID。其应用场景极其广泛——从监控和安全到实时体育分析,应有尽有。

Link to this section为何选择 Ultralytics YOLO 进行目标跟踪?#

Ultralytics 跟踪器的输出与标准 目标检测 一致,但增加了目标 ID 的额外价值。这使得在视频流中跟踪目标并进行后续分析变得轻而易举。以下是你应该考虑使用 Ultralytics YOLO 来满足目标跟踪需求的原因:

  • 效率: 实时处理视频流,且不会影响 准确性
  • 灵活性: 支持多种跟踪算法和配置。
  • 易用性: 提供简单的 Python API 和 CLI 选项,以便快速集成和部署。
  • 可定制性: 易于与自定义训练的 YOLO 模型配合使用,从而集成到特定领域的应用程序中。


Watch: How to Run Multi-Object Tracking with Ultralytics YOLO26 | BoT-SORT & ByteTrack | VisionAI 🚀

Link to this section实际应用#

交通零售水产养殖
车辆跟踪人员跟踪鱼类跟踪
车辆跟踪人员跟踪鱼类跟踪

Link to this section功能概览#

Ultralytics YOLO 扩展了其目标检测功能,以提供强大且通用的目标跟踪:

  • 实时跟踪: 在高帧率视频中实现无缝跟踪。
  • 多种跟踪器支持: 从各种成熟的跟踪算法中进行选择。
  • 可定制的跟踪器配置: 通过调整各项参数,定制跟踪算法以满足特定需求。

Link to this section可用跟踪器#

Ultralytics YOLO 支持以下跟踪算法。可以通过传递相关的 YAML 配置文件(例如 tracker=tracker_type.yaml)来启用它们:

  • BoT-SORT - 使用 botsort.yaml 启用此跟踪器。
  • ByteTrack - 使用 bytetrack.yaml 启用此跟踪器。

默认跟踪器是 BoT-SORT。

Link to this section跟踪#

要在视频流上运行跟踪器,请使用经过训练的检测 (Detect)、分割 (Segment) 或姿态 (Pose) 模型,例如 YOLO26n、YOLO26n-seg 或 YOLO26n-pose。你可以通过 Ultralytics Platform 在本地或云端 GPU 上训练自定义模型。

示例
from ultralytics import YOLO

# Load an official or custom model
model = YOLO("yolo26n.pt")  # Load an official Detect model
model = YOLO("yolo26n-seg.pt")  # Load an official Segment model
model = YOLO("yolo26n-pose.pt")  # Load an official Pose model
model = YOLO("path/to/best.pt")  # Load a custom-trained model

# Perform tracking with the model
results = model.track("https://youtu.be/LNwODJXcvt4", show=True)  # Tracking with default tracker
results = model.track("https://youtu.be/LNwODJXcvt4", show=True, tracker="bytetrack.yaml")  # with ByteTrack

如上述用法所示,所有在视频或流媒体源上运行的检测、分割和姿态模型均支持跟踪功能。

Link to this section配置#

Link to this section跟踪参数#

跟踪配置与预测模式 (Predict mode) 共享部分属性,如 confioushow。有关进一步配置,请参考 Predict 模型页面。

示例
from ultralytics import YOLO

# Configure the tracking parameters and run the tracker
model = YOLO("yolo26n.pt")
results = model.track(source="https://youtu.be/LNwODJXcvt4", conf=0.1, iou=0.7, show=True)

Link to this section跟踪器选择#

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.

示例
from ultralytics import YOLO

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

有关每个参数的详细说明,请参阅 跟踪器参数 部分。

Link to this section跟踪器参数#

一些跟踪行为可以通过编辑特定于每种跟踪算法的 YAML 配置文件进行微调。这些文件定义了诸如阈值、缓冲区和匹配逻辑等参数:

下表提供了每个参数的描述:

跟踪器阈值信息

如果检测的置信度分数低于 track_high_thresh,跟踪器将不会更新该目标,导致没有活跃的跟踪。

参数有效值或范围描述
tracker_typebotsort, bytetrack指定跟踪器类型。选项为 botsortbytetrack
track_high_thresh0.0-1.0跟踪期间第一次关联所使用的阈值。影响将检测结果匹配到现有轨迹的置信度。
track_low_thresh0.0-1.0跟踪期间第二次关联的阈值。当第一次关联失败时使用,标准更为宽松。
new_track_thresh0.0-1.0如果检测结果与任何现有轨迹不匹配,用于初始化新轨迹的阈值。控制何时认为新目标出现。
track_buffer>=0缓冲区,用于指示丢失轨迹在被移除前应保持存活的帧数。值越大,对遮挡的容忍度越高。
match_thresh0.0-1.0轨迹匹配阈值。较高的值使匹配更宽松。
fuse_scoreTrue, False确定在匹配前是否将置信度分数与 IoU 距离融合。有助于在关联时平衡空间信息和置信度信息。
gmc_methodorb, sift, ecc, sparseOptFlow, None用于全局运动补偿的方法。有助于补偿摄像机移动以改进跟踪效果。
proximity_thresh0.0-1.0与 ReID(重识别)进行有效匹配所需的最小 IoU。在利用外观线索之前确保空间上的接近度。
appearance_thresh0.0-1.0ReID 所需的最小外观相似度。设置两个检测结果在视觉上需要有多相似才能被链接。
with_reidTrue, False指示是否使用 ReID。启用基于外观的匹配,以实现遮挡情况下的更好跟踪。仅由 BoTSORT 支持。
modelauto, yolo26[nsmlx]-cls.pt指定要使用的模型。默认为 auto,如果检测器是 YOLO,则使用原生特征,否则使用 yolo26n-cls.pt

Link to this section启用重识别 (ReID)#

By default, ReID is turned off to minimize performance overhead. Enabling it is simple—just set with_reid: True in the tracker configuration. You can also customize the model used for ReID, allowing you to trade off accuracy and speed depending on your use case:

  • 原生特征 (model: auto):这直接利用来自 YOLO 检测器的特征进行 ReID,增加的开销最小。当你需要在不显著影响性能的情况下实现一定水平的 ReID 时,这是理想的选择。如果检测器不支持原生特征,它会自动回退到使用 yolo26n-cls.pt
  • YOLO 分类模型:你可以明确设置一个分类模型(例如 yolo26n-cls.pt)用于 ReID 特征提取。这提供了更具辨别力的嵌入,但由于额外的推理步骤,会引入额外的延迟。

为了获得更好的性能,特别是当使用单独的分类模型进行 ReID 时,你可以将其导出到 TensorRT 等更快的后端:

将 ReID 模型导出到 TensorRT
from torch import nn

from ultralytics import YOLO

# Load the classification model
model = YOLO("yolo26n-cls.pt")

# Add average pooling layer
head = model.model.model[-1]
pool = nn.Sequential(nn.AdaptiveAvgPool2d((1, 1)), nn.Flatten(start_dim=1))
pool.f, pool.i = head.f, head.i
model.model.model[-1] = pool

# Export to TensorRT
model.export(format="engine", half=True, dynamic=True, batch=32)

导出后,你可以在跟踪器配置中指向 TensorRT 模型路径,它将在跟踪期间用于 ReID。

Link to this sectionPython 示例#



Watch: How to Build Interactive Object Tracking with Ultralytics YOLO | Click to Crop & Display ⚡

Link to this section持久化轨迹循环#

这是一个使用 OpenCV (cv2) 和 YOLO26 在视频帧上运行目标跟踪的 Python 脚本。此脚本假设必要的包(opencv-pythonultralytics)已经安装。persist=True 参数告诉跟踪器,当前图像或帧是序列中的下一张,并预期存在来自前一张图像的轨迹。

带有跟踪功能的流式 for 循环
import cv2

from ultralytics import YOLO

# Load the YOLO26 model
model = YOLO("yolo26n.pt")

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

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

    if success:
        # Run YOLO26 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("YOLO26 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 window
cap.release()
cv2.destroyAllWindows()

请注意从 model(frame)model.track(frame) 的变化,这启用了目标跟踪而非简单的检测。此修改后的脚本将在视频的每一帧上运行跟踪器,可视化结果,并将其显示在一个窗口中。按 'q' 可退出循环。

Link to this section随时间绘制轨迹#

在连续帧上可视化目标轨迹可以为视频中检测到的目标的移动模式和行为提供有价值的洞察。借助 Ultralytics YOLO26,绘制这些轨迹是一个无缝且高效的过程。

在接下来的示例中,我们将演示如何利用 YOLO26 的跟踪功能来绘制检测到的目标在多个视频帧上的移动轨迹。此脚本涉及打开一个视频文件,逐帧读取,并利用 YOLO 模型来识别和跟踪各种目标。通过保留检测到的边界框的中心点并将它们连接起来,我们可以绘制出表示被跟踪目标所遵循路径的线条。

在多个视频帧上绘制轨迹
from collections import defaultdict

import cv2
import numpy as np

from ultralytics import YOLO

# Load the YOLO26 model
model = YOLO("yolo26n.pt")

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

# Store the track history
track_history = defaultdict(lambda: [])

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

    if success:
        # Run YOLO26 tracking on the frame, persisting tracks between frames
        result = model.track(frame, persist=True)[0]

        # Get the boxes and track IDs
        if result.boxes and result.boxes.is_track:
            boxes = result.boxes.xywh.cpu()
            track_ids = result.boxes.id.int().cpu().tolist()

            # Visualize the result on the frame
            frame = result.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 30 tracks for 30 frames
                    track.pop(0)

                # Draw the tracking lines
                points = np.hstack(track).astype(np.int32).reshape((-1, 1, 2))
                cv2.polylines(frame, [points], isClosed=False, color=(230, 230, 230), thickness=10)

        # Display the annotated frame
        cv2.imshow("YOLO26 Tracking", 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 window
cap.release()
cv2.destroyAllWindows()

Link to this section多线程跟踪#

多线程跟踪提供了同时在多个视频流上运行目标跟踪的能力。这在处理多个视频输入(如来自多个监控摄像头的输入)时特别有用,并发处理可以极大地提高效率和性能。

在提供的 Python 脚本中,我们利用 Python 的 threading 模块同时运行多个跟踪器实例。每个线程负责在一个视频文件上运行跟踪器,并且所有线程都在后台同时运行。

为了确保每个线程接收到正确的参数(视频文件、要使用的模型和文件索引),我们定义了一个 run_tracker_in_thread 函数,该函数接受这些参数并包含主跟踪循环。此函数逐帧读取视频,运行跟踪器,并显示结果。

此示例使用了两个不同的模型:yolo26n.ptyolo26n-seg.pt,每个模型分别在不同的视频文件中跟踪目标。视频文件在 SOURCES 中指定。

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.

最后,在所有线程完成任务后,使用 cv2.destroyAllWindows() 关闭显示结果的窗口。

多线程跟踪实现
import threading

import cv2

from ultralytics import YOLO

# Define model names and video sources
MODEL_NAMES = ["yolo26n.pt", "yolo26n-seg.pt"]
SOURCES = ["path/to/video.mp4", "0"]  # local video, 0 for webcam

def run_tracker_in_thread(model_name, filename):
    """Run YOLO tracker in its own thread for concurrent processing.

    Args:
        model_name (str): The YOLO26 model object.
        filename (str): The path to the video file or the identifier for the webcam/external camera source.
    """
    model = YOLO(model_name)
    results = model.track(filename, save=True, stream=True)
    for r in results:
        pass

# Create and start tracker threads using a for loop
tracker_threads = []
for video_file, model_name in zip(SOURCES, MODEL_NAMES):
    thread = threading.Thread(target=run_tracker_in_thread, args=(model_name, video_file), daemon=True)
    tracker_threads.append(thread)
    thread.start()

# Wait for all tracker threads to finish
for thread in tracker_threads:
    thread.join()

# Clean up and close windows
cv2.destroyAllWindows()

通过创建更多的线程并应用相同的方法,此示例可以轻松扩展以处理更多的视频文件和模型。

Link to this section贡献新的追踪器#

你是否精通多目标追踪,并成功使用 Ultralytics YOLO 实现或适配过追踪算法?我们诚邀你向 ultralytics/cfg/trackers 中的 Trackers 部分贡献代码!你的实际应用和解决方案对于从事追踪任务的用户来说将极具参考价值。

通过贡献这一部分,你将帮助扩展 Ultralytics YOLO 框架中可用的追踪解决方案范围,为社区增加更多的功能和实用性。

要开始你的贡献,请查阅我们的 贡献指南,了解有关提交 Pull Request (PR) 🛠️ 的详细说明。我们很期待看到你能带来的成果!

让我们共同提升 Ultralytics YOLO 生态系统的追踪能力 🙏!

Link to this section常见问题解答#

Link to this section什么是多目标追踪,Ultralytics YOLO 是如何支持它的?#

视频分析中的多目标追踪包括识别物体,并为视频帧中每个检测到的物体维护一个唯一 ID。Ultralytics YOLO 通过提供实时追踪和物体 ID 来支持此功能,从而促进安全监控和体育分析等任务。该系统使用诸如 BoT-SORTByteTrack 之类的追踪器,这些追踪器可以通过 YAML 文件进行配置。

Link to this section如何为 Ultralytics YOLO 配置自定义追踪器?#

You can configure a custom tracker by copying an existing tracker configuration file (e.g., custom_tracker.yaml) from the Ultralytics tracker configuration directory and modifying parameters as needed, except for the tracker_type. Use this file in your tracking model like so:

示例
from ultralytics import YOLO

model = YOLO("yolo26n.pt")
results = model.track(source="https://youtu.be/LNwODJXcvt4", tracker="custom_tracker.yaml")

Link to this section如何同时在多个视频流上运行目标追踪?#

要同时在多个视频流上运行目标追踪,你可以使用 Python 的 threading 模块。每个线程将处理一个单独的视频流。以下是如何设置的示例:

多线程跟踪
import threading

import cv2

from ultralytics import YOLO

# Define model names and video sources
MODEL_NAMES = ["yolo26n.pt", "yolo26n-seg.pt"]
SOURCES = ["path/to/video.mp4", "0"]  # local video, 0 for webcam

def run_tracker_in_thread(model_name, filename):
    """Run YOLO tracker in its own thread for concurrent processing.

    Args:
        model_name (str): The YOLO26 model object.
        filename (str): The path to the video file or the identifier for the webcam/external camera source.
    """
    model = YOLO(model_name)
    results = model.track(filename, save=True, stream=True)
    for r in results:
        pass

# Create and start tracker threads using a for loop
tracker_threads = []
for video_file, model_name in zip(SOURCES, MODEL_NAMES):
    thread = threading.Thread(target=run_tracker_in_thread, args=(model_name, video_file), daemon=True)
    tracker_threads.append(thread)
    thread.start()

# Wait for all tracker threads to finish
for thread in tracker_threads:
    thread.join()

# Clean up and close windows
cv2.destroyAllWindows()

Link to this sectionUltralytics YOLO 多目标追踪有哪些实际应用?#

Ultralytics YOLO 的多目标追踪有许多应用,包括:

  • 交通: 用于交通管理和 自动驾驶 的车辆追踪。
  • 零售: 用于店内分析和安全的人员追踪。
  • 水产养殖: 用于监测水生环境的鱼类追踪。
  • 体育分析: 用于性能分析的运动员和设备追踪。
  • 安全系统: 监控可疑活动 并创建 安全警报

这些应用受益于 Ultralytics YOLO 实时处理高帧率视频的能力,且具有极高的准确性。

Link to this section如何使用 Ultralytics YOLO 可视化跨多个视频帧的物体轨迹?#

要可视化跨多个视频帧的物体轨迹,你可以结合使用 YOLO 模型的追踪功能和 OpenCV 来绘制检测到物体的路径。这是一个演示此过程的示例脚本:

在多个视频帧上绘制轨迹
from collections import defaultdict

import cv2
import numpy as np

from ultralytics import YOLO

model = YOLO("yolo26n.pt")
video_path = "path/to/video.mp4"
cap = cv2.VideoCapture(video_path)
track_history = defaultdict(lambda: [])

while cap.isOpened():
    success, frame = cap.read()
    if success:
        results = model.track(frame, persist=True)
        boxes = results[0].boxes.xywh.cpu()
        track_ids = results[0].boxes.id.int().cpu().tolist()
        annotated_frame = results[0].plot()
        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)))
            if len(track) > 30:
                track.pop(0)
            points = np.hstack(track).astype(np.int32).reshape((-1, 1, 2))
            cv2.polylines(annotated_frame, [points], isClosed=False, color=(230, 230, 230), thickness=10)
        cv2.imshow("YOLO26 Tracking", annotated_frame)
        if cv2.waitKey(1) & 0xFF == ord("q"):
            break
    else:
        break
cap.release()
cv2.destroyAllWindows()

此脚本将绘制追踪线,显示被追踪物体随时间的移动路径,从而提供有关物体行为和模式的宝贵见解。

评论