Skip to content

Reference for ultralytics/trackers/basetrack.py

Note

This file is available at https://github.com/ultralytics/ultralytics/blob/main/ultralytics/trackers/basetrack.py. If you spot a problem please help fix it by contributing a Pull Request 🛠️. Thank you 🙏!


ultralytics.trackers.basetrack.TrackState

Enumeration class representing the possible states of an object being tracked.

Attributes:

Name Type Description
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.

Examples:

>>> state = TrackState.New
>>> if state == TrackState.New:
>>>     print("Object is newly detected.")





ultralytics.trackers.basetrack.BaseTrack

BaseTrack()

Base class for object tracking, providing foundational attributes and methods.

Attributes:

Name Type Description
_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:

Name Description
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.

Examples:

Initialize a new track and mark it as lost:

>>> track = BaseTrack()
>>> track.mark_lost()
>>> print(track.state)  # Output: 2 (TrackState.Lost)
Source code in ultralytics/trackers/basetrack.py
69
70
71
72
73
74
75
76
77
78
79
80
81
def __init__(self):
    """Initialize a new track with a 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)

end_frame property

end_frame: int

Return the ID of the most recent frame where the object was tracked.

activate

activate(*args: Any) -> None

Activate the track with provided arguments, initializing necessary attributes for tracking.

Source code in ultralytics/trackers/basetrack.py
94
95
96
def activate(self, *args: Any) -> None:
    """Activate the track with provided arguments, initializing necessary attributes for tracking."""
    raise NotImplementedError

mark_lost

mark_lost() -> None

Mark the track as lost by updating its state to TrackState.Lost.

Source code in ultralytics/trackers/basetrack.py
106
107
108
def mark_lost(self) -> None:
    """Mark the track as lost by updating its state to TrackState.Lost."""
    self.state = TrackState.Lost

mark_removed

mark_removed() -> None

Mark the track as removed by setting its state to TrackState.Removed.

Source code in ultralytics/trackers/basetrack.py
110
111
112
def mark_removed(self) -> None:
    """Mark the track as removed by setting its state to TrackState.Removed."""
    self.state = TrackState.Removed

next_id staticmethod

next_id() -> int

Increment and return the next unique global track ID for object tracking.

Source code in ultralytics/trackers/basetrack.py
88
89
90
91
92
@staticmethod
def next_id() -> int:
    """Increment and return the next unique global track ID for object tracking."""
    BaseTrack._count += 1
    return BaseTrack._count

predict

predict() -> None

Predict the next state of the track based on the current state and tracking model.

Source code in ultralytics/trackers/basetrack.py
 98
 99
100
def predict(self) -> None:
    """Predict the next state of the track based on the current state and tracking model."""
    raise NotImplementedError

reset_id staticmethod

reset_id() -> None

Reset the global track ID counter to its initial value.

Source code in ultralytics/trackers/basetrack.py
114
115
116
117
@staticmethod
def reset_id() -> None:
    """Reset the global track ID counter to its initial value."""
    BaseTrack._count = 0

update

update(*args: Any, **kwargs: Any) -> None

Update the track with new observations and data, modifying its state and attributes accordingly.

Source code in ultralytics/trackers/basetrack.py
102
103
104
def update(self, *args: Any, **kwargs: Any) -> None:
    """Update the track with new observations and data, modifying its state and attributes accordingly."""
    raise NotImplementedError





📅 Created 1 year ago ✏️ Updated 8 months ago