Skip to content

Reference for ultralytics/trackers/basetrack.py

Improvements

This page is sourced from https://github.com/ultralytics/ultralytics/blob/main/ultralytics/trackers/basetrack.py. Have an improvement or example to add? Open a Pull Request — thank you! 🙏


class ultralytics.trackers.basetrack.TrackState

TrackState()

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

Attributes

NameTypeDescription
NewintState when the object is newly detected.
TrackedintState when the object is successfully tracked in subsequent frames.
LostintState when the object is no longer tracked.
RemovedintState when the object is removed from tracking.

Examples

>>> state = TrackState.New
>>> if state == TrackState.New:
>>>     print("Object is newly detected.")
Source code in ultralytics/trackers/basetrack.pyView on GitHub
class TrackState:





class ultralytics.trackers.basetrack.BaseTrack

BaseTrack(self)

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

Attributes

NameTypeDescription
_countintClass-level counter for unique track IDs.
track_idintUnique identifier for the track.
is_activatedboolFlag indicating whether the track is currently active.
stateTrackStateCurrent state of the track.
historyOrderedDictOrdered history of the track's states.
featureslistList of features extracted from the object for tracking.
curr_featureAnyThe current feature of the object being tracked.
scorefloatThe confidence score of the tracking.
start_frameintThe frame number where tracking started.
frame_idintThe most recent frame ID processed by the track.
time_since_updateintFrames passed since the last update.
locationtupleThe location of the object in the context of multi-camera tracking.

Methods

NameDescription
end_frameReturn the ID of the most recent frame where the object was tracked.
activateActivate the track with provided arguments, initializing necessary attributes for tracking.
mark_lostMark the track as lost by updating its state to TrackState.Lost.
mark_removedMark the track as removed by setting its state to TrackState.Removed.
next_idIncrement and return the next unique global track ID for object tracking.
predictPredict the next state of the track based on the current state and tracking model.
reset_idReset the global track ID counter to its initial value.
updateUpdate the track with new observations and data, modifying its state and attributes accordingly.

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.pyView on GitHub
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.

    Examples:
        Initialize a new track and mark it as lost:
        >>> track = BaseTrack()
        >>> track.mark_lost()
        >>> print(track.state)  # Output: 2 (TrackState.Lost)
    """

    _count = 0

    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)


property ultralytics.trackers.basetrack.BaseTrack.end_frame

def end_frame(self) -> int

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

Source code in ultralytics/trackers/basetrack.pyView on GitHub
@property
def end_frame(self) -> int:
    """Return the ID of the most recent frame where the object was tracked."""
    return self.frame_id


method ultralytics.trackers.basetrack.BaseTrack.activate

def activate(self, *args: Any) -> None

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

Args

NameTypeDescriptionDefault
*argsAnyrequired
Source code in ultralytics/trackers/basetrack.pyView on GitHub
def activate(self, *args: Any) -> None:
    """Activate the track with provided arguments, initializing necessary attributes for tracking."""
    raise NotImplementedError


method ultralytics.trackers.basetrack.BaseTrack.mark_lost

def mark_lost(self) -> None

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

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


method ultralytics.trackers.basetrack.BaseTrack.mark_removed

def mark_removed(self) -> None

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

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


method ultralytics.trackers.basetrack.BaseTrack.next_id

def next_id() -> int

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

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


method ultralytics.trackers.basetrack.BaseTrack.predict

def predict(self) -> None

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

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


method ultralytics.trackers.basetrack.BaseTrack.reset_id

def reset_id() -> None

Reset the global track ID counter to its initial value.

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


method ultralytics.trackers.basetrack.BaseTrack.update

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

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

Args

NameTypeDescriptionDefault
*argsAnyrequired
**kwargsAnyrequired
Source code in ultralytics/trackers/basetrack.pyView on GitHub
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 2 years ago ✏️ Updated 18 days ago
glenn-jocherjk4eBurhan-Q