Video#
Optional helper for reading video files or a live camera feed, writing the
annotated output, and showing a progress bar during processing. It is a thin
wrapper around OpenCV's VideoCapture / VideoWriter — use it for quick
pipelines, and reach for OpenCV directly when you need more control.
To use this module install Norfair with the video extra:
1 | |
Context manager usage (recommended)#
Video implements the context-manager protocol so file
handles and writers are released deterministically — even if the loop is
interrupted by an exception or an early break:
1 2 3 4 5 6 7 8 9 10 | |
Reading from a webcam#
Pass camera=<device id> instead of input_path:
1 2 3 | |
camera and input_path are mutually exclusive — you must pick one.
API#
Video I/O helpers: frame iteration, display, and output writing.
Video
#
Simple pythonic wrapper around an OpenCV video source and sink.
Yields raw OpenCV frames so the full OpenCV toolbox is available for frame-level processing, while taking care of capture setup, progress-bar rendering and writer initialization.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
camera
|
int
|
Device id of the camera to read from. Webcams typically use
|
None
|
input_path
|
str
|
Path to the video file to read. Mutually exclusive with
|
None
|
output_path
|
str
|
Where the output video should be written. May be a directory (an output filename is then auto-generated) or a full file path. |
'.'
|
output_fps
|
float
|
Frames per second for the output file. Defaults to the input source's fps. This is useful with live cameras when the effective processing rate is lower than the camera's native rate. |
None
|
label
|
str
|
Optional label appended to the progress-bar description. |
''
|
output_fourcc
|
str
|
OpenCV codec for the output file. Defaults to |
None
|
output_extension
|
str
|
File extension used when |
'mp4'
|
Examples:
Read, process and write a video::
1 2 3 4 | |
The context manager releases the underlying VideoCapture and
VideoWriter even if the loop is interrupted. Iterating without
with also works — resources are freed when the iterator is
exhausted or when :meth:close is called.
Raises:
| Type | Description |
|---|---|
ValueError
|
If neither or both of |
Source code in norfair/video.py
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 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 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 406 407 408 409 410 411 412 413 414 415 416 417 | |
__init__(camera=None, input_path=None, output_path='.', output_fps=None, label='', output_fourcc=None, output_extension='mp4')
#
Open the capture and set up the progress bar.
Source code in norfair/video.py
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 | |
__enter__()
#
Return self so Video can be used in a with block.
Source code in norfair/video.py
184 185 186 | |
__exit__(exc_type, exc_val, exc_tb)
#
Release video resources when leaving a with block.
Source code in norfair/video.py
188 189 190 191 | |
close()
#
Release video resources (VideoCapture, VideoWriter, GUI windows).
Safe to call multiple times; subsequent calls are no-ops.
Source code in norfair/video.py
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 | |
__iter__()
#
Yield successive OpenCV frames from the underlying capture.
The progress bar is updated for every frame. When the
generator is exhausted (or the consumer loop exits for any
reason), :meth:close is invoked to release resources.
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If the |
Source code in norfair/video.py
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 | |
write(frame)
#
Write one frame to the output video.
Lazily initializes the underlying cv2.VideoWriter on the
first call, using frame's shape to determine the output
resolution.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
frame
|
ndarray
|
The OpenCV frame to write to file. |
required |
Returns:
| Type | Description |
|---|---|
int
|
The key code from |
Source code in norfair/video.py
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 | |
show(frame, downsample_ratio=1.0)
#
Display frame in a GUI window.
Typically called inside a video-inference loop to preview the output.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
frame
|
ndarray
|
The OpenCV frame to be displayed. |
required |
downsample_ratio
|
float
|
Factor by which to downsample the frame before displaying. Useful when streaming the GUI video display over a slow connection (e.g. X11 forwarding over SSH). |
1.0
|
Returns:
| Type | Description |
|---|---|
int
|
The key code from |
Source code in norfair/video.py
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 | |
get_output_file_path()
#
Return the resolved output-file path for written frames.
When output_path is a directory, Norfair auto-generates a
filename based on the input source. This method returns that
resolved path so callers can locate the rendered video.
Returns:
| Type | Description |
|---|---|
str
|
The absolute (or relative) path to the output file. |
Source code in norfair/video.py
329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 | |
get_codec_fourcc(filename)
#
Resolve the fourcc codec name to use when writing filename.
Returns :attr:output_fourcc when set; otherwise derives a
default from filename's extension ("XVID" for
.avi, "mp4v" for .mp4).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filename
|
str
|
Destination file whose extension is used for codec auto-selection. |
required |
Returns:
| Type | Description |
|---|---|
str
|
Name of the fourcc codec to feed to
:class: |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If no codec can be resolved from |
Source code in norfair/video.py
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 | |
abbreviate_description(description)
#
Shorten description so the progress bar fits small terminals.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
description
|
str
|
The full description string to abbreviate. |
required |
Returns:
| Type | Description |
|---|---|
str
|
The original |
Source code in norfair/video.py
396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 | |
VideoFromFrames
#
Iterate over an MOT-style dataset that stores frames as images.
Reads seqinfo.ini to determine frame count, resolution and
file-naming convention, then yields successive frames via the
iterator protocol. Optionally encodes the frames into an .mp4
file alongside the iteration via :meth:update.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
input_path
|
str
|
Path to the MOT sequence directory (containing
|
required |
save_path
|
str
|
Directory where the rendered |
'.'
|
information_file
|
InformationFile
|
Pre-parsed |
None
|
make_video
|
bool
|
If |
True
|
Source code in norfair/video.py
420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 | |
__init__(input_path, save_path='.', information_file=None, make_video=True)
#
Parse seqinfo.ini and optionally open the output writer.
Source code in norfair/video.py
445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 | |
__iter__()
#
Reset the frame counter and return self as an iterator.
Source code in norfair/video.py
487 488 489 490 | |
__next__()
#
Return the next image frame from the sequence.
Source code in norfair/video.py
492 493 494 495 496 497 498 499 500 501 502 503 | |
update(frame)
#
Append frame to the output video and release on the last frame.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
frame
|
ndarray
|
The OpenCV frame to write to the output video. |
required |
Source code in norfair/video.py
505 506 507 508 509 510 511 512 513 514 515 516 517 518 | |