Video Utils¶
supervision.utils.video.VideoInfo
dataclass
¶
A class to store video information, including width, height, fps and total number of frames.
Attributes:
| Name | Type | Description |
|---|---|---|
width |
int
|
width of the video in pixels |
height |
int
|
height of the video in pixels |
fps |
int
|
frames per second of the video |
total_frames |
Optional[int]
|
total number of frames in the video, default is None |
Examples:
import supervision as sv
video_info = sv.VideoInfo.from_video_path(video_path=<SOURCE_VIDEO_FILE>)
video_info
# VideoInfo(width=3840, height=2160, fps=25, total_frames=538)
video_info.resolution_wh
# (3840, 2160)
Source code in supervision/utils/video.py
supervision.utils.video.VideoSink
¶
Context manager that saves video frames to a file using OpenCV.
Attributes:
| Name | Type | Description |
|---|---|---|
target_path |
str
|
The path to the output file where the video will be saved. |
video_info |
VideoInfo
|
Information about the video resolution, fps, and total frame count. |
codec |
str
|
FOURCC code for video format |
Example
import supervision as sv
video_info = sv.VideoInfo.from_video_path(<SOURCE_VIDEO_PATH>)
frames_generator = sv.get_video_frames_generator(<SOURCE_VIDEO_PATH>)
with sv.VideoSink(target_path=<TARGET_VIDEO_PATH>, video_info=video_info) as sink:
for frame in frames_generator:
sink.write_frame(frame=frame)
Source code in supervision/utils/video.py
supervision.utils.video.FPSMonitor
¶
A class for monitoring frames per second (FPS) to benchmark latency.
Source code in supervision/utils/video.py
Attributes¶
fps
property
¶
Computes and returns the average FPS based on the stored time stamps.
Returns:
| Name | Type | Description |
|---|---|---|
float |
float
|
The average FPS. Returns 0.0 if no time stamps are stored. |
Functions¶
__init__(sample_size=30)
¶
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
int
|
The maximum number of observations for latency benchmarking. |
30
|
Examples:
import supervision as sv
frames_generator = sv.get_video_frames_generator(source_path=<SOURCE_FILE_PATH>)
fps_monitor = sv.FPSMonitor()
for frame in frames_generator:
# your processing code here
fps_monitor.tick()
fps = fps_monitor.fps
Source code in supervision/utils/video.py
reset()
¶
supervision.utils.video.get_video_frames_generator(source_path, stride=1, start=0, end=None, iterative_seek=False)
¶
Get a generator that yields the frames of the video.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
str
|
The path of the video file. |
required |
|
int
|
Indicates the interval at which frames are returned, skipping stride - 1 frames between each. |
1
|
|
int
|
Indicates the starting position from which video should generate frames |
0
|
|
Optional[int]
|
Indicates the ending position at which video should stop generating frames. If None, video will be read to the end. |
None
|
|
bool
|
If True, the generator will seek to the
|
False
|
Returns:
| Type | Description |
|---|---|
Generator[ndarray, None, None]
|
A generator that yields the frames of the video. |
Examples:
import supervision as sv
for frame in sv.get_video_frames_generator(source_path=<SOURCE_VIDEO_PATH>):
...
Source code in supervision/utils/video.py
supervision.utils.video.process_video(source_path, target_path, callback, *, max_frames=None, prefetch=32, writer_buffer=32, show_progress=False, progress_message='Processing video')
¶
Process video frames asynchronously using a threaded pipeline.
This function orchestrates a three-stage pipeline to optimize video processing throughput:
- Reader thread: Continuously reads frames from the source video file and
enqueues them into a bounded queue (
frame_read_queue). The queue size is limited by theprefetchparameter to control memory usage. - Main thread (Processor): Dequeues frames from
frame_read_queue, applies the user-definedcallbackfunction to process each frame, then enqueues the processed frames into another bounded queue (frame_write_queue) for writing. The processing happens in the main thread, simplifying use of stateful objects without synchronization. - Writer thread: Dequeues processed frames from
frame_write_queueand writes them sequentially to the output video file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
str
|
Path to the input video file. |
required |
|
str
|
Path where the processed video will be saved. |
required |
|
Callable[[ndarray, int], ndarray]
|
Function called for each frame, accepting the frame as a numpy array and its zero-based index, returning the processed frame. |
required |
|
int | None
|
Optional maximum number of frames to process. If None, the entire video is processed (default). |
None
|
|
int
|
Maximum number of frames buffered by the reader thread. Controls memory use; default is 32. |
32
|
|
int
|
Maximum number of frames buffered before writing. Controls output buffer size; default is 32. |
32
|
|
bool
|
Whether to display a tqdm progress bar during processing. Default is False. |
False
|
|
str
|
Description shown in the progress bar. |
'Processing video'
|
Returns:
| Type | Description |
|---|---|
None
|
None |
Example
Source code in supervision/utils/video.py
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 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 | |