跳至内容

参考资料 ultralytics/trackers/basetrack.py

备注

该文件可在https://github.com/ultralytics/ultralytics/blob/main/ ultralytics/trackers/basetrack .py 上获取。如果您发现问题,请通过提交 Pull Request🛠️ 帮助修复。谢谢🙏!



ultralytics.trackers.basetrack.TrackState

枚举类,代表被跟踪对象的可能状态。

属性

名称 类型 说明
New int

新检测到对象时的状态。

Tracked int

在后续帧中成功跟踪到目标时的状态。

Lost int

对象不再被跟踪时的状态。

Removed int

从跟踪中删除对象时的状态。

源代码 ultralytics/trackers/basetrack.py
class TrackState:
    """
    Enumeration class representing the possible states of an object being tracked.

    Attributes:
        New (int): State when the object is newly detected.
        Tracked (int): State when the object is successfully tracked in subsequent frames.
        Lost (int): State when the object is no longer tracked.
        Removed (int): State when the object is removed from tracking.
    """

    New = 0
    Tracked = 1
    Lost = 2
    Removed = 3



ultralytics.trackers.basetrack.BaseTrack

对象跟踪基类,提供基本属性和方法。

属性

名称 类型 说明
_count int

唯一轨道 ID 的类别级计数器。

track_id int

轨道的唯一标识符。

is_activated bool

指示轨道当前是否处于活动状态的标志。

state TrackState

赛道现状

history OrderedDict

按顺序排列的轨道状态历史。

features list

从物体中提取的用于跟踪的特征列表。

curr_feature any

被跟踪物体的当前特征。

score float

跟踪的置信度得分。

start_frame int

开始跟踪的帧编号。

frame_id int

轨道最近处理的帧 ID。

time_since_update int

上次更新后的帧数。

location tuple

多摄像头跟踪背景下的目标位置。

方法

名称 说明
end_frame

返回跟踪对象的最后一帧的 ID。

next_id

递增并返回下一个全局轨道 ID。

activate

激活轨道的抽象方法。

predict

预测轨道下一状态的抽象方法。

update

用新数据更新轨迹的抽象方法。

mark_lost

将轨道标记为丢失。

mark_removed

标记轨道已移除。

reset_id

重置全局轨道 ID 计数器。

源代码 ultralytics/trackers/basetrack.py
class BaseTrack:
    """
    Base class for object tracking, providing foundational attributes and methods.

    Attributes:
        _count (int): Class-level counter for unique track IDs.
        track_id (int): Unique identifier for the track.
        is_activated (bool): Flag indicating whether the track is currently active.
        state (TrackState): Current state of the track.
        history (OrderedDict): Ordered history of the track's states.
        features (list): List of features extracted from the object for tracking.
        curr_feature (any): The current feature of the object being tracked.
        score (float): The confidence score of the tracking.
        start_frame (int): The frame number where tracking started.
        frame_id (int): The most recent frame ID processed by the track.
        time_since_update (int): Frames passed since the last update.
        location (tuple): The location of the object in the context of multi-camera tracking.

    Methods:
        end_frame: Returns the ID of the last frame where the object was tracked.
        next_id: Increments and returns the next global track ID.
        activate: Abstract method to activate the track.
        predict: Abstract method to predict the next state of the track.
        update: Abstract method to update the track with new data.
        mark_lost: Marks the track as lost.
        mark_removed: Marks the track as removed.
        reset_id: Resets the global track ID counter.
    """

    _count = 0

    def __init__(self):
        """Initializes a new track with unique ID and foundational tracking attributes."""
        self.track_id = 0
        self.is_activated = False
        self.state = TrackState.New
        self.history = OrderedDict()
        self.features = []
        self.curr_feature = None
        self.score = 0
        self.start_frame = 0
        self.frame_id = 0
        self.time_since_update = 0
        self.location = (np.inf, np.inf)

    @property
    def end_frame(self):
        """Return the last frame ID of the track."""
        return self.frame_id

    @staticmethod
    def next_id():
        """Increment and return the global track ID counter."""
        BaseTrack._count += 1
        return BaseTrack._count

    def activate(self, *args):
        """Abstract method to activate the track with provided arguments."""
        raise NotImplementedError

    def predict(self):
        """Abstract method to predict the next state of the track."""
        raise NotImplementedError

    def update(self, *args, **kwargs):
        """Abstract method to update the track with new observations."""
        raise NotImplementedError

    def mark_lost(self):
        """Mark the track as lost."""
        self.state = TrackState.Lost

    def mark_removed(self):
        """Mark the track as removed."""
        self.state = TrackState.Removed

    @staticmethod
    def reset_id():
        """Reset the global track ID counter."""
        BaseTrack._count = 0

end_frame property

返回轨道的最后一帧 ID。

__init__()

初始化具有唯一 ID 和基本跟踪属性的新跟踪。

源代码 ultralytics/trackers/basetrack.py
def __init__(self):
    """Initializes a new track with unique ID and foundational tracking attributes."""
    self.track_id = 0
    self.is_activated = False
    self.state = TrackState.New
    self.history = OrderedDict()
    self.features = []
    self.curr_feature = None
    self.score = 0
    self.start_frame = 0
    self.frame_id = 0
    self.time_since_update = 0
    self.location = (np.inf, np.inf)

activate(*args)

抽象方法,使用提供的参数激活轨道。

源代码 ultralytics/trackers/basetrack.py
def activate(self, *args):
    """Abstract method to activate the track with provided arguments."""
    raise NotImplementedError

mark_lost()

将轨道标记为丢失。

源代码 ultralytics/trackers/basetrack.py
def mark_lost(self):
    """Mark the track as lost."""
    self.state = TrackState.Lost

mark_removed()

标记轨道已移除。

源代码 ultralytics/trackers/basetrack.py
def mark_removed(self):
    """Mark the track as removed."""
    self.state = TrackState.Removed

next_id() staticmethod

递增并返回全局轨道 ID 计数器。

源代码 ultralytics/trackers/basetrack.py
@staticmethod
def next_id():
    """Increment and return the global track ID counter."""
    BaseTrack._count += 1
    return BaseTrack._count

predict()

预测轨道下一状态的抽象方法。

源代码 ultralytics/trackers/basetrack.py
def predict(self):
    """Abstract method to predict the next state of the track."""
    raise NotImplementedError

reset_id() staticmethod

重置全局轨道 ID 计数器。

源代码 ultralytics/trackers/basetrack.py
@staticmethod
def reset_id():
    """Reset the global track ID counter."""
    BaseTrack._count = 0

update(*args, **kwargs)

根据新的观测结果更新轨迹的抽象方法。

源代码 ultralytics/trackers/basetrack.py
def update(self, *args, **kwargs):
    """Abstract method to update the track with new observations."""
    raise NotImplementedError





Created 2023-11-12, Updated 2024-06-02
Authors: glenn-jocher (5), Burhan-Q (1)