Reference for ultralytics/trackers/byte_tracker.py
Note
This file is available at https://github.com/ultralytics/ultralytics/blob/main/ultralytics/trackers/byte_tracker.py. If you spot a problem please help fix it by contributing a Pull Request 🛠️. Thank you 🙏!
ultralytics.trackers.byte_tracker.STrack
Bases: BaseTrack
Single object tracking representation that uses Kalman filtering for state estimation.
This class is responsible for storing all the information regarding individual tracklets and performs state updates and predictions based on Kalman filter.
Attributes:
Name | Type | Description |
---|---|---|
shared_kalman | KalmanFilterXYAH | Shared Kalman filter that is used across all STrack instances for prediction. |
_tlwh | ndarray | Private attribute to store top-left corner coordinates and width and height of bounding box. |
kalman_filter | KalmanFilterXYAH | Instance of Kalman filter used for this particular object track. |
mean | ndarray | Mean state estimate vector. |
covariance | ndarray | Covariance of state estimate. |
is_activated | bool | Boolean flag indicating if the track has been activated. |
score | float | Confidence score of the track. |
tracklet_len | int | Length of the tracklet. |
cls | Any | Class label for the object. |
idx | int | Index or identifier for the object. |
frame_id | int | Current frame ID. |
start_frame | int | Frame where the object was first detected. |
Methods:
Name | Description |
---|---|
predict | Predict the next state of the object using Kalman filter. |
multi_predict | Predict the next states for multiple tracks. |
multi_gmc | Update multiple track states using a homography matrix. |
activate | Activate a new tracklet. |
re_activate | Reactivate a previously lost tracklet. |
update | Update the state of a matched track. |
convert_coords | Convert bounding box to x-y-aspect-height format. |
tlwh_to_xyah | Convert tlwh bounding box to xyah format. |
Examples:
Initialize and activate a new track
>>> track = STrack(xywh=[100, 200, 50, 80, 0], score=0.9, cls="person")
>>> track.activate(kalman_filter=KalmanFilterXYAH(), frame_id=1)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
xywh | List[float] | Bounding box coordinates and dimensions in the format (x, y, w, h, [a], idx), where (x, y) is the center, (w, h) are width and height, [a] is optional aspect ratio, and idx is the id. | required |
score | float | Confidence score of the detection. | required |
cls | Any | Class label for the detected object. | required |
Examples:
>>> xywh = [100.0, 150.0, 50.0, 75.0, 1]
>>> score = 0.9
>>> cls = "person"
>>> track = STrack(xywh, score, cls)
Source code in ultralytics/trackers/byte_tracker.py
tlwh property
Returns the bounding box in top-left-width-height format from the current state estimate.
xywh property
Returns the current position of the bounding box in (center x, center y, width, height) format.
xywha property
Returns position in (center x, center y, width, height, angle) format, warning if angle is missing.
xyxy property
Converts bounding box from (top left x, top left y, width, height) to (min x, min y, max x, max y) format.
__repr__
Returns a string representation of the STrack object including start frame, end frame, and track ID.
activate
Activate a new tracklet using the provided Kalman filter and initialize its state and covariance.
Source code in ultralytics/trackers/byte_tracker.py
convert_coords
Convert a bounding box's top-left-width-height format to its x-y-aspect-height equivalent.
multi_gmc staticmethod
Update state tracks positions and covariances using a homography matrix for multiple tracks.
Source code in ultralytics/trackers/byte_tracker.py
multi_predict staticmethod
Perform multi-object predictive tracking using Kalman filter for the provided list of STrack instances.
Source code in ultralytics/trackers/byte_tracker.py
predict
Predicts the next state (mean and covariance) of the object using the Kalman filter.
Source code in ultralytics/trackers/byte_tracker.py
re_activate
Reactivates a previously lost track using new detection data and updates its state and attributes.
Source code in ultralytics/trackers/byte_tracker.py
tlwh_to_xyah staticmethod
Convert bounding box from tlwh format to center-x-center-y-aspect-height (xyah) format.
update
Update the state of a matched track.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
new_track | STrack | The new track containing updated information. | required |
frame_id | int | The ID of the current frame. | required |
Examples:
Update the state of a track with new detection information
>>> track = STrack([100, 200, 50, 80, 0.9, 1])
>>> new_track = STrack([105, 205, 55, 85, 0.95, 1])
>>> track.update(new_track, 2)
Source code in ultralytics/trackers/byte_tracker.py
ultralytics.trackers.byte_tracker.BYTETracker
BYTETracker: A tracking algorithm built on top of YOLOv8 for object detection and tracking.
Responsible for initializing, updating, and managing the tracks for detected objects in a video sequence. It maintains the state of tracked, lost, and removed tracks over frames, utilizes Kalman filtering for predicting the new object locations, and performs data association.
Attributes:
Name | Type | Description |
---|---|---|
tracked_stracks | List[STrack] | List of successfully activated tracks. |
lost_stracks | List[STrack] | List of lost tracks. |
removed_stracks | List[STrack] | List of removed tracks. |
frame_id | int | The current frame ID. |
args | Namespace | Command-line arguments. |
max_time_lost | int | The maximum frames for a track to be considered as 'lost'. |
kalman_filter | KalmanFilterXYAH | Kalman Filter object. |
Methods:
Name | Description |
---|---|
update | Updates object tracker with new detections. |
get_kalmanfilter | Returns a Kalman filter object for tracking bounding boxes. |
init_track | Initialize object tracking with detections. |
get_dists | Calculates the distance between tracks and detections. |
multi_predict | Predicts the location of tracks. |
reset_id | Resets the ID counter of STrack. |
joint_stracks | Combines two lists of stracks. |
sub_stracks | Filters out the stracks present in the second list from the first list. |
remove_duplicate_stracks | Removes duplicate stracks based on IoU. |
Examples:
Initialize BYTETracker and update with detection results
>>> tracker = BYTETracker(args, frame_rate=30)
>>> results = yolo_model.detect(image)
>>> tracked_objects = tracker.update(results)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
args | Namespace | Command-line arguments containing tracking parameters. | required |
frame_rate | int | Frame rate of the video sequence. | 30 |
Examples:
Initialize BYTETracker with command-line arguments and a frame rate of 30
Source code in ultralytics/trackers/byte_tracker.py
get_dists
Calculates the distance between tracks and detections using IoU and optionally fuses scores.
Source code in ultralytics/trackers/byte_tracker.py
get_kalmanfilter
init_track
Initializes object tracking with given detections, scores, and class labels using the STrack algorithm.
Source code in ultralytics/trackers/byte_tracker.py
joint_stracks staticmethod
Combines two lists of STrack objects into a single list, ensuring no duplicates based on track IDs.
Source code in ultralytics/trackers/byte_tracker.py
multi_predict
remove_duplicate_stracks staticmethod
Removes duplicate stracks from two lists based on Intersection over Union (IoU) distance.
Source code in ultralytics/trackers/byte_tracker.py
reset
Resets the tracker by clearing all tracked, lost, and removed tracks and reinitializing the Kalman filter.
Source code in ultralytics/trackers/byte_tracker.py
reset_id staticmethod
Resets the ID counter for STrack instances to ensure unique track IDs across tracking sessions.
sub_stracks staticmethod
Filters out the stracks present in the second list from the first list.
update
Updates the tracker with new detections and returns the current list of tracked objects.
Source code in ultralytics/trackers/byte_tracker.py
293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 |
|