Track Objects¶
Leverage Supervision's advanced capabilities for enhancing your video analysis by seamlessly tracking objects recognized by a multitude of object detection, segmentation and keypoint models. This comprehensive guide will take you through the steps to perform inference using native RF-DETR, with Inference and Ultralytics alternatives. Following this, you'll discover how to track these objects efficiently and annotate your video content for a deeper analysis.
Prerequisites¶
This tutorial requires Python 3.10 or later. Install Supervision and the external trackers package, which provides ByteTrackTracker:
Then install the inference framework you plan to use. The examples for each framework are alternatives, so you only need one:
- RF-DETR:
pip install "rfdetr>=1.8.0"(providesRFDETRMediumandRFDETRKeypointPreview; the keypoint preview model was added in 1.8.0) - Inference:
pip install inference(supports Python 3.10 to 3.13) - Ultralytics:
pip install ultralytics
The Inference examples need a Roboflow API key. Replace <ROBOFLOW_API_KEY> with your key, which you can find in your Roboflow settings. Keep the key private and do not commit it to source control. The RF-DETR and Ultralytics examples do not need an API key.
Object Detection & Segmentation¶
To make it easier for you to follow our tutorial download the video we will use as an example. You can do this using the supervision.assets module included in the base package.
This section demonstrates how to detect and segment objects in video frames using native RF-DETR first, with Inference and YOLOv8 alternatives. You will download a sample video, define a per-frame callback function that runs model prediction, and process the entire video to produce an annotated output file.
from supervision.assets import download_assets, VideoAssets
download_assets(VideoAssets.PEOPLE_WALKING)
Run Inference¶
First, you'll need to obtain predictions from your object detection or segmentation model. In this tutorial, we are using RF-DETR as an example, with YOLOv8 as an alternative. However, Supervision is versatile and compatible with various models. Check this link for guidance on how to plug in other models.
We will define a callback function, which will process each frame of the video by obtaining model predictions and then annotating the frame based on these predictions. This callback function will be essential in the subsequent steps of the tutorial, as it will be modified to include tracking, labeling, and trace annotations.
Tip
Both object detection and segmentation models are supported. Try it with native RFDETRMedium or rfdetr-seg-medium, or use their Inference aliases.
import numpy as np
import supervision as sv
from rfdetr import RFDETRMedium
model = RFDETRMedium()
box_annotator = sv.BoxAnnotator()
def callback(frame: np.ndarray, _: int) -> np.ndarray:
detections = model.predict(frame[:, :, ::-1])
return box_annotator.annotate(frame.copy(), detections=detections)
sv.process_video(
source_path="people-walking.mp4",
target_path="result.mp4",
callback=callback
)
import numpy as np
import supervision as sv
from inference.models.utils import get_roboflow_model
model = get_roboflow_model(model_id="rfdetr-small", api_key="<ROBOFLOW_API_KEY>")
box_annotator = sv.BoxAnnotator()
def callback(frame: np.ndarray, _: int) -> np.ndarray:
results = model.infer(frame)[0]
detections = sv.Detections.from_inference(results)
return box_annotator.annotate(frame.copy(), detections=detections)
sv.process_video(
source_path="people-walking.mp4",
target_path="result.mp4",
callback=callback
)
import numpy as np
import supervision as sv
from ultralytics import YOLO
model = YOLO("yolov8n.pt")
box_annotator = sv.BoxAnnotator()
def callback(frame: np.ndarray, _: int) -> np.ndarray:
results = model(frame)[0]
detections = sv.Detections.from_ultralytics(results)
return box_annotator.annotate(frame.copy(), detections=detections)
sv.process_video(
source_path="people-walking.mp4",
target_path="result.mp4",
callback=callback
)
Tracking¶
After running inference and obtaining predictions, the next step is to track the detected objects throughout the video. The code samples below use ByteTrackTracker (see the removal note below), each detected object is assigned a unique tracker ID, enabling the continuous following of the object's motion path across different frames.
Removed tracker wrapper
sv.ByteTrack was removed as of supervision-0.31.0. The samples below already use ByteTrackTracker from the external trackers package — install it with pip install trackers before running them. They preserve the wrapper's track_activation_threshold=0.25 and minimum_consecutive_frames=1 settings. If you're migrating your own code from sv.ByteTrack, note that the external tracker's update method is update(), not update_with_detections().
import numpy as np
import supervision as sv
from rfdetr import RFDETRMedium
from trackers import ByteTrackTracker
model = RFDETRMedium()
tracker = ByteTrackTracker(track_activation_threshold=0.25, minimum_consecutive_frames=1)
box_annotator = sv.BoxAnnotator()
def callback(frame: np.ndarray, _: int) -> np.ndarray:
detections = model.predict(frame[:, :, ::-1])
detections = tracker.update(detections)
return box_annotator.annotate(frame.copy(), detections=detections)
sv.process_video(
source_path="people-walking.mp4",
target_path="result.mp4",
callback=callback
)
import numpy as np
import supervision as sv
from inference.models.utils import get_roboflow_model
from trackers import ByteTrackTracker
model = get_roboflow_model(model_id="rfdetr-small", api_key="<ROBOFLOW_API_KEY>")
tracker = ByteTrackTracker(track_activation_threshold=0.25, minimum_consecutive_frames=1)
box_annotator = sv.BoxAnnotator()
def callback(frame: np.ndarray, _: int) -> np.ndarray:
results = model.infer(frame)[0]
detections = sv.Detections.from_inference(results)
detections = tracker.update(detections)
return box_annotator.annotate(frame.copy(), detections=detections)
sv.process_video(
source_path="people-walking.mp4",
target_path="result.mp4",
callback=callback
)
import numpy as np
import supervision as sv
from trackers import ByteTrackTracker
from ultralytics import YOLO
model = YOLO("yolov8n.pt")
tracker = ByteTrackTracker(track_activation_threshold=0.25, minimum_consecutive_frames=1)
box_annotator = sv.BoxAnnotator()
def callback(frame: np.ndarray, _: int) -> np.ndarray:
results = model(frame)[0]
detections = sv.Detections.from_ultralytics(results)
detections = tracker.update(detections)
return box_annotator.annotate(frame.copy(), detections=detections)
sv.process_video(
source_path="people-walking.mp4",
target_path="result.mp4",
callback=callback
)
Annotate Video with Tracking IDs¶
Annotating the video with tracking IDs helps in distinguishing and following each object distinctly. With the sv.LabelAnnotator in Supervision, we can overlay the tracker IDs and class labels on the detected objects, offering a clear visual representation of each object's class and unique identifier.
import numpy as np
import supervision as sv
from rfdetr import RFDETRMedium
from trackers import ByteTrackTracker
model = RFDETRMedium()
tracker = ByteTrackTracker(track_activation_threshold=0.25, minimum_consecutive_frames=1)
box_annotator = sv.BoxAnnotator()
label_annotator = sv.LabelAnnotator()
def callback(frame: np.ndarray, _: int) -> np.ndarray:
detections = model.predict(frame[:, :, ::-1])
detections = tracker.update(detections)
detections = detections[detections.tracker_id != -1]
labels = [
f"#{tracker_id} {class_name}"
for class_name, tracker_id
in zip(detections.data["class_name"], detections.tracker_id)
]
annotated_frame = box_annotator.annotate(
frame.copy(), detections=detections)
return label_annotator.annotate(
annotated_frame, detections=detections, labels=labels)
sv.process_video(
source_path="people-walking.mp4",
target_path="result.mp4",
callback=callback
)
import numpy as np
import supervision as sv
from inference.models.utils import get_roboflow_model
from trackers import ByteTrackTracker
model = get_roboflow_model(model_id="rfdetr-small", api_key="<ROBOFLOW_API_KEY>")
tracker = ByteTrackTracker(track_activation_threshold=0.25, minimum_consecutive_frames=1)
box_annotator = sv.BoxAnnotator()
label_annotator = sv.LabelAnnotator()
def callback(frame: np.ndarray, _: int) -> np.ndarray:
results = model.infer(frame)[0]
detections = sv.Detections.from_inference(results)
detections = tracker.update(detections)
detections = detections[detections.tracker_id != -1]
labels = [
f"#{tracker_id} {class_name}"
for class_name, tracker_id
in zip(detections.data["class_name"], detections.tracker_id)
]
annotated_frame = box_annotator.annotate(
frame.copy(), detections=detections)
return label_annotator.annotate(
annotated_frame, detections=detections, labels=labels)
sv.process_video(
source_path="people-walking.mp4",
target_path="result.mp4",
callback=callback
)
import numpy as np
import supervision as sv
from trackers import ByteTrackTracker
from ultralytics import YOLO
model = YOLO("yolov8n.pt")
tracker = ByteTrackTracker(track_activation_threshold=0.25, minimum_consecutive_frames=1)
box_annotator = sv.BoxAnnotator()
label_annotator = sv.LabelAnnotator()
def callback(frame: np.ndarray, _: int) -> np.ndarray:
results = model(frame)[0]
detections = sv.Detections.from_ultralytics(results)
detections = tracker.update(detections)
detections = detections[detections.tracker_id != -1]
labels = [
f"#{tracker_id} {class_name}"
for class_name, tracker_id
in zip(detections.data["class_name"], detections.tracker_id)
]
annotated_frame = box_annotator.annotate(
frame.copy(), detections=detections)
return label_annotator.annotate(
annotated_frame, detections=detections, labels=labels)
sv.process_video(
source_path="people-walking.mp4",
target_path="result.mp4",
callback=callback
)
Annotate Video with Traces¶
Adding traces to the video involves overlaying the historical paths of the detected objects. This feature, powered by the sv.TraceAnnotator, allows for visualizing the trajectories of objects, helping in understanding the movement patterns and interactions between objects in the video.
import numpy as np
import supervision as sv
from rfdetr import RFDETRMedium
from trackers import ByteTrackTracker
model = RFDETRMedium()
tracker = ByteTrackTracker(track_activation_threshold=0.25, minimum_consecutive_frames=1)
box_annotator = sv.BoxAnnotator()
label_annotator = sv.LabelAnnotator()
trace_annotator = sv.TraceAnnotator()
def callback(frame: np.ndarray, _: int) -> np.ndarray:
detections = model.predict(frame[:, :, ::-1])
detections = tracker.update(detections)
detections = detections[detections.tracker_id != -1]
labels = [
f"#{tracker_id} {class_name}"
for class_name, tracker_id
in zip(detections.data["class_name"], detections.tracker_id)
]
annotated_frame = box_annotator.annotate(
frame.copy(), detections=detections)
annotated_frame = label_annotator.annotate(
annotated_frame, detections=detections, labels=labels)
return trace_annotator.annotate(
annotated_frame, detections=detections)
sv.process_video(
source_path="people-walking.mp4",
target_path="result.mp4",
callback=callback
)
import numpy as np
import supervision as sv
from inference.models.utils import get_roboflow_model
from trackers import ByteTrackTracker
model = get_roboflow_model(model_id="rfdetr-small", api_key="<ROBOFLOW_API_KEY>")
tracker = ByteTrackTracker(track_activation_threshold=0.25, minimum_consecutive_frames=1)
box_annotator = sv.BoxAnnotator()
label_annotator = sv.LabelAnnotator()
trace_annotator = sv.TraceAnnotator()
def callback(frame: np.ndarray, _: int) -> np.ndarray:
results = model.infer(frame)[0]
detections = sv.Detections.from_inference(results)
detections = tracker.update(detections)
detections = detections[detections.tracker_id != -1]
labels = [
f"#{tracker_id} {class_name}"
for class_name, tracker_id
in zip(detections.data["class_name"], detections.tracker_id)
]
annotated_frame = box_annotator.annotate(
frame.copy(), detections=detections)
annotated_frame = label_annotator.annotate(
annotated_frame, detections=detections, labels=labels)
return trace_annotator.annotate(
annotated_frame, detections=detections)
sv.process_video(
source_path="people-walking.mp4",
target_path="result.mp4",
callback=callback
)
import numpy as np
import supervision as sv
from trackers import ByteTrackTracker
from ultralytics import YOLO
model = YOLO("yolov8n.pt")
tracker = ByteTrackTracker(track_activation_threshold=0.25, minimum_consecutive_frames=1)
box_annotator = sv.BoxAnnotator()
label_annotator = sv.LabelAnnotator()
trace_annotator = sv.TraceAnnotator()
def callback(frame: np.ndarray, _: int) -> np.ndarray:
results = model(frame)[0]
detections = sv.Detections.from_ultralytics(results)
detections = tracker.update(detections)
detections = detections[detections.tracker_id != -1]
labels = [
f"#{tracker_id} {class_name}"
for class_name, tracker_id
in zip(detections.data["class_name"], detections.tracker_id)
]
annotated_frame = box_annotator.annotate(
frame.copy(), detections=detections)
annotated_frame = label_annotator.annotate(
annotated_frame, detections=detections, labels=labels)
return trace_annotator.annotate(
annotated_frame, detections=detections)
sv.process_video(
source_path="people-walking.mp4",
target_path="result.mp4",
callback=callback
)
Keypoints¶
Tip
To compare pose models, colour joints and measure joint angles with sv.joint_angles, see the keypoint detection cookbook.
Models aren't limited to object detection and segmentation. Keypoint detection allows for detailed analysis of body joints and connections, especially valuable for applications like human pose estimation. This section introduces keypoint tracking. We'll walk through the steps of annotating keypoints, converting them into bounding box detections compatible with ByteTrack, and applying detection smoothing for enhanced stability.
To make it easier for you to follow our tutorial, let's download the video we will use as an example. You can do this using the supervision.assets module included in the base package.
Keypoint Detection¶
First, obtain predictions from your keypoint detection model. This tutorial uses the RF-DETR keypoint preview model, with Roboflow Inference and Ultralytics alternatives. Install RF-DETR with pip install rfdetr. See the KeyPoints documentation for other supported models.
We will define a callback function, which will process each frame of the video by obtaining model predictions and then annotating the frame based on these predictions.
Let's immediately visualize the results with our EdgeAnnotator and VertexAnnotator.
import numpy as np
import supervision as sv
from rfdetr import RFDETRKeypointPreview
model = RFDETRKeypointPreview()
edge_annotator = sv.EdgeAnnotator()
vertex_annotator = sv.VertexAnnotator()
def callback(frame: np.ndarray, _: int) -> np.ndarray:
key_points = model.predict(frame[:, :, ::-1], threshold=0.5)
annotated_frame = edge_annotator.annotate(
frame.copy(), key_points=key_points)
return vertex_annotator.annotate(
annotated_frame, key_points=key_points)
sv.process_video(
source_path="skiing.mp4",
target_path="result.mp4",
callback=callback
)
import numpy as np
import supervision as sv
from inference.models.utils import get_roboflow_model
from supervision.key_points.skeletons import Skeleton
model = get_roboflow_model(
model_id="yolov8m-pose-640", api_key="<ROBOFLOW_API_KEY>")
vertex_annotator = sv.VertexAnnotator()
def callback(frame: np.ndarray, _: int) -> np.ndarray:
results = model.infer(frame)[0]
key_points = sv.KeyPoints.from_inference(results)
# Inference may omit trailing joints, so keep only edges within the array.
keypoint_count = key_points.xy.shape[1]
edges = [edge for edge in Skeleton.COCO.value if max(edge) <= keypoint_count]
annotated_frame = frame.copy()
if edges:
annotated_frame = sv.EdgeAnnotator(edges=edges).annotate(
annotated_frame, key_points=key_points)
return vertex_annotator.annotate(
annotated_frame, key_points=key_points)
sv.process_video(
source_path="skiing.mp4",
target_path="result.mp4",
callback=callback
)
import numpy as np
import supervision as sv
from ultralytics import YOLO
model = YOLO("yolov8m-pose.pt")
edge_annotator = sv.EdgeAnnotator()
vertex_annotator = sv.VertexAnnotator()
def callback(frame: np.ndarray, _: int) -> np.ndarray:
results = model(frame)[0]
key_points = sv.KeyPoints.from_ultralytics(results)
annotated_frame = edge_annotator.annotate(
frame.copy(), key_points=key_points)
return vertex_annotator.annotate(
annotated_frame, key_points=key_points)
sv.process_video(
source_path="skiing.mp4",
target_path="result.mp4",
callback=callback
)
Convert to Detections¶
Keypoint tracking is currently supported via the conversion of KeyPoints to Detections. This is achieved with the KeyPoints.as_detections() function.
Let's convert to detections and visualize the results with our BoxAnnotator.
Tip
You may use the selected_keypoint_indices argument to specify a subset of keypoints to convert. This is useful when some keypoints could be occluded. For example: a person might swing their arm, causing the elbow to be occluded by the torso sometimes.
import numpy as np
import supervision as sv
from rfdetr import RFDETRKeypointPreview
model = RFDETRKeypointPreview()
edge_annotator = sv.EdgeAnnotator()
vertex_annotator = sv.VertexAnnotator()
box_annotator = sv.BoxAnnotator()
def callback(frame: np.ndarray, _: int) -> np.ndarray:
key_points = model.predict(frame[:, :, ::-1], threshold=0.5)
detections = key_points.as_detections()
annotated_frame = edge_annotator.annotate(
frame.copy(), key_points=key_points)
annotated_frame = vertex_annotator.annotate(
annotated_frame, key_points=key_points)
return box_annotator.annotate(
annotated_frame, detections=detections)
sv.process_video(
source_path="skiing.mp4",
target_path="result.mp4",
callback=callback
)
import numpy as np
import supervision as sv
from inference.models.utils import get_roboflow_model
from supervision.key_points.skeletons import Skeleton
model = get_roboflow_model(
model_id="yolov8m-pose-640", api_key="<ROBOFLOW_API_KEY>")
vertex_annotator = sv.VertexAnnotator()
box_annotator = sv.BoxAnnotator()
def callback(frame: np.ndarray, _: int) -> np.ndarray:
results = model.infer(frame)[0]
key_points = sv.KeyPoints.from_inference(results)
detections = key_points.as_detections()
# Inference may omit trailing joints, so keep only edges within the array.
keypoint_count = key_points.xy.shape[1]
edges = [edge for edge in Skeleton.COCO.value if max(edge) <= keypoint_count]
annotated_frame = frame.copy()
if edges:
annotated_frame = sv.EdgeAnnotator(edges=edges).annotate(
annotated_frame, key_points=key_points)
annotated_frame = vertex_annotator.annotate(
annotated_frame, key_points=key_points)
return box_annotator.annotate(
annotated_frame, detections=detections)
sv.process_video(
source_path="skiing.mp4",
target_path="result.mp4",
callback=callback
)
import numpy as np
import supervision as sv
from ultralytics import YOLO
model = YOLO("yolov8m-pose.pt")
edge_annotator = sv.EdgeAnnotator()
vertex_annotator = sv.VertexAnnotator()
box_annotator = sv.BoxAnnotator()
def callback(frame: np.ndarray, _: int) -> np.ndarray:
results = model(frame)[0]
key_points = sv.KeyPoints.from_ultralytics(results)
detections = key_points.as_detections()
annotated_frame = edge_annotator.annotate(
frame.copy(), key_points=key_points)
annotated_frame = vertex_annotator.annotate(
annotated_frame, key_points=key_points)
return box_annotator.annotate(
annotated_frame, detections=detections)
sv.process_video(
source_path="skiing.mp4",
target_path="result.mp4",
callback=callback
)
Keypoint Tracking¶
Now that we have a Detections object, we can track it throughout the video. The code samples below use ByteTrackTracker (see the removal note above), each detected object is assigned a unique tracker ID, enabling the continuous following of the object's motion path across different frames. We shall visualize the result with TraceAnnotator.
import numpy as np
import supervision as sv
from trackers import ByteTrackTracker
from rfdetr import RFDETRKeypointPreview
model = RFDETRKeypointPreview()
edge_annotator = sv.EdgeAnnotator()
vertex_annotator = sv.VertexAnnotator()
box_annotator = sv.BoxAnnotator()
tracker = ByteTrackTracker(track_activation_threshold=0.25, minimum_consecutive_frames=1)
trace_annotator = sv.TraceAnnotator()
def callback(frame: np.ndarray, _: int) -> np.ndarray:
key_points = model.predict(frame[:, :, ::-1], threshold=0.5)
detections = key_points.as_detections()
detections = tracker.update(detections)
detections = detections[detections.tracker_id != -1]
annotated_frame = edge_annotator.annotate(
frame.copy(), key_points=key_points)
annotated_frame = vertex_annotator.annotate(
annotated_frame, key_points=key_points)
annotated_frame = box_annotator.annotate(
annotated_frame, detections=detections)
return trace_annotator.annotate(
annotated_frame, detections=detections)
sv.process_video(
source_path="skiing.mp4",
target_path="result.mp4",
callback=callback
)
import numpy as np
import supervision as sv
from inference.models.utils import get_roboflow_model
from supervision.key_points.skeletons import Skeleton
from trackers import ByteTrackTracker
model = get_roboflow_model(
model_id="yolov8m-pose-640", api_key="<ROBOFLOW_API_KEY>")
vertex_annotator = sv.VertexAnnotator()
box_annotator = sv.BoxAnnotator()
tracker = ByteTrackTracker(track_activation_threshold=0.25, minimum_consecutive_frames=1)
trace_annotator = sv.TraceAnnotator()
def callback(frame: np.ndarray, _: int) -> np.ndarray:
results = model.infer(frame)[0]
key_points = sv.KeyPoints.from_inference(results)
detections = key_points.as_detections()
detections = tracker.update(detections)
detections = detections[detections.tracker_id != -1]
# Inference may omit trailing joints, so keep only edges within the array.
keypoint_count = key_points.xy.shape[1]
edges = [edge for edge in Skeleton.COCO.value if max(edge) <= keypoint_count]
annotated_frame = frame.copy()
if edges:
annotated_frame = sv.EdgeAnnotator(edges=edges).annotate(
annotated_frame, key_points=key_points)
annotated_frame = vertex_annotator.annotate(
annotated_frame, key_points=key_points)
annotated_frame = box_annotator.annotate(
annotated_frame, detections=detections)
return trace_annotator.annotate(
annotated_frame, detections=detections)
sv.process_video(
source_path="skiing.mp4",
target_path="result.mp4",
callback=callback
)
import numpy as np
import supervision as sv
from trackers import ByteTrackTracker
from ultralytics import YOLO
model = YOLO("yolov8m-pose.pt")
edge_annotator = sv.EdgeAnnotator()
vertex_annotator = sv.VertexAnnotator()
box_annotator = sv.BoxAnnotator()
tracker = ByteTrackTracker(track_activation_threshold=0.25, minimum_consecutive_frames=1)
trace_annotator = sv.TraceAnnotator()
def callback(frame: np.ndarray, _: int) -> np.ndarray:
results = model(frame)[0]
key_points = sv.KeyPoints.from_ultralytics(results)
detections = key_points.as_detections()
detections = tracker.update(detections)
detections = detections[detections.tracker_id != -1]
annotated_frame = edge_annotator.annotate(
frame.copy(), key_points=key_points)
annotated_frame = vertex_annotator.annotate(
annotated_frame, key_points=key_points)
annotated_frame = box_annotator.annotate(
annotated_frame, detections=detections)
return trace_annotator.annotate(
annotated_frame, detections=detections)
sv.process_video(
source_path="skiing.mp4",
target_path="result.mp4",
callback=callback
)
Bonus: Smoothing¶
We could stop here as we have successfully tracked the object detected by the keypoint model. However, we can further enhance the stability of the boxes by applying DetectionsSmoother. This tool helps in stabilizing the boxes by smoothing the bounding box coordinates across frames. It is very simple to use:
import numpy as np
import supervision as sv
from trackers import ByteTrackTracker
from rfdetr import RFDETRKeypointPreview
model = RFDETRKeypointPreview()
edge_annotator = sv.EdgeAnnotator()
vertex_annotator = sv.VertexAnnotator()
box_annotator = sv.BoxAnnotator()
tracker = ByteTrackTracker(track_activation_threshold=0.25, minimum_consecutive_frames=1)
smoother = sv.DetectionsSmoother()
trace_annotator = sv.TraceAnnotator()
def callback(frame: np.ndarray, _: int) -> np.ndarray:
key_points = model.predict(frame[:, :, ::-1], threshold=0.5)
detections = key_points.as_detections()
detections = tracker.update(detections)
detections = detections[detections.tracker_id != -1]
detections = smoother.update_with_detections(detections)
annotated_frame = edge_annotator.annotate(
frame.copy(), key_points=key_points)
annotated_frame = vertex_annotator.annotate(
annotated_frame, key_points=key_points)
annotated_frame = box_annotator.annotate(
annotated_frame, detections=detections)
return trace_annotator.annotate(
annotated_frame, detections=detections)
sv.process_video(
source_path="skiing.mp4",
target_path="result.mp4",
callback=callback
)
import numpy as np
import supervision as sv
from inference.models.utils import get_roboflow_model
from supervision.key_points.skeletons import Skeleton
from trackers import ByteTrackTracker
model = get_roboflow_model(
model_id="yolov8m-pose-640", api_key="<ROBOFLOW_API_KEY>")
vertex_annotator = sv.VertexAnnotator()
box_annotator = sv.BoxAnnotator()
tracker = ByteTrackTracker(track_activation_threshold=0.25, minimum_consecutive_frames=1)
smoother = sv.DetectionsSmoother()
trace_annotator = sv.TraceAnnotator()
def callback(frame: np.ndarray, _: int) -> np.ndarray:
results = model.infer(frame)[0]
key_points = sv.KeyPoints.from_inference(results)
detections = key_points.as_detections()
detections = tracker.update(detections)
detections = detections[detections.tracker_id != -1]
detections = smoother.update_with_detections(detections)
# Inference may omit trailing joints, so keep only edges within the array.
keypoint_count = key_points.xy.shape[1]
edges = [edge for edge in Skeleton.COCO.value if max(edge) <= keypoint_count]
annotated_frame = frame.copy()
if edges:
annotated_frame = sv.EdgeAnnotator(edges=edges).annotate(
annotated_frame, key_points=key_points)
annotated_frame = vertex_annotator.annotate(
annotated_frame, key_points=key_points)
annotated_frame = box_annotator.annotate(
annotated_frame, detections=detections)
return trace_annotator.annotate(
annotated_frame, detections=detections)
sv.process_video(
source_path="skiing.mp4",
target_path="result.mp4",
callback=callback
)
import numpy as np
import supervision as sv
from trackers import ByteTrackTracker
from ultralytics import YOLO
model = YOLO("yolov8m-pose.pt")
edge_annotator = sv.EdgeAnnotator()
vertex_annotator = sv.VertexAnnotator()
box_annotator = sv.BoxAnnotator()
tracker = ByteTrackTracker(track_activation_threshold=0.25, minimum_consecutive_frames=1)
smoother = sv.DetectionsSmoother()
trace_annotator = sv.TraceAnnotator()
def callback(frame: np.ndarray, _: int) -> np.ndarray:
results = model(frame)[0]
key_points = sv.KeyPoints.from_ultralytics(results)
detections = key_points.as_detections()
detections = tracker.update(detections)
detections = detections[detections.tracker_id != -1]
detections = smoother.update_with_detections(detections)
annotated_frame = edge_annotator.annotate(
frame.copy(), key_points=key_points)
annotated_frame = vertex_annotator.annotate(
annotated_frame, key_points=key_points)
annotated_frame = box_annotator.annotate(
annotated_frame, detections=detections)
return trace_annotator.annotate(
annotated_frame, detections=detections)
sv.process_video(
source_path="skiing.mp4",
target_path="result.mp4",
callback=callback
)
Attach Tracker IDs to Keypoints¶
The samples above track boxes. For per-person pose analysis you need the tracker ID on each skeleton. Do not copy detections.tracker_id into key_points by position: as_detections() leaves out skeletons without any usable keypoint, and the tracker may return rows in a different order (matched tracks first), so positional copies can put IDs on the wrong person. Instead, store each skeleton's row index in key_points.data before the conversion. as_detections() copies data to the detections, so the index comes back with every tracked row.
import numpy as np
import supervision as sv
from trackers import ByteTrackTracker
from rfdetr import RFDETRKeypointPreview
model = RFDETRKeypointPreview()
edge_annotator = sv.EdgeAnnotator()
vertex_annotator = sv.VertexAnnotator()
tracker = ByteTrackTracker(track_activation_threshold=0.25, minimum_consecutive_frames=1)
def callback(frame: np.ndarray, _: int) -> np.ndarray:
key_points = model.predict(frame[:, :, ::-1], threshold=0.5)
key_points.data["row"] = np.arange(len(key_points))
detections = tracker.update(key_points.as_detections())
key_points.data["tracker_id"] = np.full(len(key_points), -1)
if len(detections):
key_points.data["tracker_id"][detections.data["row"]] = detections.tracker_id
key_points = key_points[key_points.data["tracker_id"] != -1]
annotated_frame = edge_annotator.annotate(
frame.copy(), key_points=key_points)
return vertex_annotator.annotate(
annotated_frame, key_points=key_points)
sv.process_video(
source_path="skiing.mp4",
target_path="result.mp4",
callback=callback
)
import numpy as np
import supervision as sv
from inference.models.utils import get_roboflow_model
from supervision.key_points.skeletons import Skeleton
from trackers import ByteTrackTracker
model = get_roboflow_model(
model_id="yolov8m-pose-640", api_key="<ROBOFLOW_API_KEY>")
vertex_annotator = sv.VertexAnnotator()
tracker = ByteTrackTracker(track_activation_threshold=0.25, minimum_consecutive_frames=1)
def callback(frame: np.ndarray, _: int) -> np.ndarray:
results = model.infer(frame)[0]
key_points = sv.KeyPoints.from_inference(results)
key_points.data["row"] = np.arange(len(key_points))
detections = tracker.update(key_points.as_detections())
key_points.data["tracker_id"] = np.full(len(key_points), -1)
if len(detections):
key_points.data["tracker_id"][detections.data["row"]] = detections.tracker_id
key_points = key_points[key_points.data["tracker_id"] != -1]
# Inference may omit trailing joints, so keep only edges within the array.
keypoint_count = key_points.xy.shape[1]
edges = [edge for edge in Skeleton.COCO.value if max(edge) <= keypoint_count]
annotated_frame = frame.copy()
if edges:
annotated_frame = sv.EdgeAnnotator(edges=edges).annotate(
annotated_frame, key_points=key_points)
return vertex_annotator.annotate(
annotated_frame, key_points=key_points)
sv.process_video(
source_path="skiing.mp4",
target_path="result.mp4",
callback=callback
)
import numpy as np
import supervision as sv
from trackers import ByteTrackTracker
from ultralytics import YOLO
model = YOLO("yolov8m-pose.pt")
edge_annotator = sv.EdgeAnnotator()
vertex_annotator = sv.VertexAnnotator()
tracker = ByteTrackTracker(track_activation_threshold=0.25, minimum_consecutive_frames=1)
def callback(frame: np.ndarray, _: int) -> np.ndarray:
results = model(frame)[0]
key_points = sv.KeyPoints.from_ultralytics(results)
key_points.data["row"] = np.arange(len(key_points))
detections = tracker.update(key_points.as_detections())
key_points.data["tracker_id"] = np.full(len(key_points), -1)
if len(detections):
key_points.data["tracker_id"][detections.data["row"]] = detections.tracker_id
key_points = key_points[key_points.data["tracker_id"] != -1]
annotated_frame = edge_annotator.annotate(
frame.copy(), key_points=key_points)
return vertex_annotator.annotate(
annotated_frame, key_points=key_points)
sv.process_video(
source_path="skiing.mp4",
target_path="result.mp4",
callback=callback
)
On frames without tracked detections, the assignment is skipped because older Supervision releases can omit the row field from empty results. Each skeleton in key_points now has its ID in key_points.data["tracker_id"]; new tracks start with tracker_id=-1. With these tracker settings, a track started by a detection that meets the activation threshold can receive an ID on its next successfully matched frame. The filter keeps only skeletons that have been assigned an ID. Inference can omit low-confidence joints, including the last COCO joint; the example filters the COCO edges to the available keypoint indices so that the remaining edges can still be drawn.
This structured walkthrough should give a detailed pathway to annotate videos effectively using Supervision’s various functionalities, including object tracking and trace annotations.
Frequently Asked Questions¶
How do I track objects across video frames with supervision?¶
Pass Detections to ByteTrackTracker.update() (from the external trackers package, pip install trackers) on each frame. The tracker assigns persistent IDs. Combine with sv.TraceAnnotator to visualize trajectories. Supervision's built-in sv.ByteTrack wrapper, which used update_with_detections(), was removed as of supervision-0.31.0.
What should I know about ByteTrack?¶
ByteTrack uses low-confidence detections during association, which can improve continuity during missed or weak detections. Supervision's built-in ByteTrack wrapper was removed as of supervision-0.31.0 in favor of the external trackers package.
Can I track instances instead of bounding boxes?¶
Yes. ByteTrack tracks bounding boxes. For instance masks, use sv.MaskAnnotator with the tracker IDs to color-code each tracked object consistently.
Does ByteTrack work with any detection model?¶
Yes. ByteTrack is model-agnostic - it accepts any Detections object with bounding boxes, regardless of the supported converter or model output that produced it.
Authors¶
- Piotr Skalski — Computer Vision Engineer, Roboflow
- Soumik Mandal — ML Engineer, Roboflow