Detection Smoother¶
supervision.detection.tools.smoother.DetectionsSmoother
¶
A utility class for smoothing detections over multiple frames in video tracking. It maintains a history of detections for each track and provides smoothed predictions based on these histories.
Warning
DetectionsSmootherrequires thetracker_idfor each detection. Refer to Roboflow Trackers for information on integrating tracking into your inference pipeline.- This class is not compatible with segmentation models.
- When detections in a frame disagree on confidence presence — some tracks
carry confidence scores and others do not —
confidenceis set toNonefor all smoothed detections in that frame.
Example
>>> import numpy as np
>>> import supervision as sv
>>> smoother = sv.DetectionsSmoother(length=3)
>>> detections_1 = sv.Detections(
... xyxy=np.array([[0, 0, 10, 10]]),
... confidence=np.array([0.5]),
... tracker_id=np.array([1])
... )
>>> detections_2 = sv.Detections(
... xyxy=np.array([[2, 2, 12, 12]]),
... confidence=np.array([0.7]),
... tracker_id=np.array([1])
... )
>>> smoothed = smoother.update_with_detections(detections_1)
>>> smoothed.xyxy
array([[ 0., 0., 10., 10.]])
>>> smoothed = smoother.update_with_detections(detections_2)
>>> smoothed.xyxy
array([[ 1., 1., 11., 11.]])
>>> smoothed.confidence
array([0.6])
import supervision as sv
from ultralytics import YOLO
video_info = sv.VideoInfo.from_video_path(video_path="<SOURCE_FILE_PATH>")
frame_generator = sv.get_video_frames_generator(
source_path="<SOURCE_FILE_PATH>")
model = YOLO("<MODEL_PATH>")
tracker = sv.ByteTrack(frame_rate=video_info.fps)
smoother = sv.DetectionsSmoother()
box_annotator = sv.BoxAnnotator()
with sv.VideoSink("<TARGET_FILE_PATH>", video_info=video_info) as sink:
for frame in frame_generator:
result = model(frame)[0]
detections = sv.Detections.from_ultralytics(result)
detections = tracker.update_with_detections(detections)
detections = smoother.update_with_detections(detections)
annotated_frame = box_annotator.annotate(frame.copy(), detections)
sink.write_frame(annotated_frame)
Source code in src/supervision/detection/tools/smoother.py
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 | |
Methods:¶
__init__(length: int = 5) -> None
¶
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
int
|
The maximum number of frames to consider for smoothing detections. Defaults to 5. |
5
|
Source code in src/supervision/detection/tools/smoother.py
get_smoothed_detections(track_ids: set[int] | None = None) -> Detections
¶
Return the smoothed detections for the requested active tracks.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
set[int] | None
|
Optional set of track IDs to include in the output. When provided, tracks absent from the current frame are excluded from the emitted detections but their history stays cached. |
None
|
Source code in src/supervision/detection/tools/smoother.py
get_track(track_id: int) -> Detections | None
¶
Return the smoothed Detections for a single track.
Averages xyxy over all valid (non-None) frames in the track window.
confidence is averaged only over frames that carry it; frames with
confidence=None are excluded. Returns None when the track is unknown
or its entire window is empty.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
int
|
The tracker ID whose smoothed detection to retrieve. |
required |
Returns:
| Type | Description |
|---|---|
Detections | None
|
Smoothed |
Detections | None
|
unknown or all frames in its window are empty. |
Source code in src/supervision/detection/tools/smoother.py
reset() -> None
¶
Clears the per-track detection history so the smoother can be reused
across independent streams without carrying over frames from a
previous stream. The configured window length is preserved.
Examples:
>>> import numpy as np
>>> import supervision as sv
>>> smoother = sv.DetectionsSmoother()
>>> detections = sv.Detections(
... xyxy=np.array([[0, 0, 10, 10]]),
... confidence=np.array([0.5]),
... tracker_id=np.array([1])
... )
>>> _ = smoother.update_with_detections(detections)
>>> len(smoother.tracks)
1
>>> smoother.reset()
>>> len(smoother.tracks)
0
Source code in src/supervision/detection/tools/smoother.py
update_with_detections(detections: Detections) -> Detections
¶
Updates the smoother with a new set of detections from a frame.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
Detections
|
The detections to add to the smoother. |
required |