Custom video source

Plug custom video sources into PySDK using predict_batch—ideal for cameras, SDKs, GStreamer, and advanced use cases needing per-frame control or metadata.

Estimated read time: 6 minutes

This guide shows how to plug non-standard video sources into DeGirum PySDK—things like PiCamera2, GStreamer appsink, proprietary SDKs, image sequences, screen captures, or preprocessed frames.

For common sources (webcam, file path, RTSP URL), use degirum_tools.predict_stream(model, source). When you need custom capture, inline processing, or per-frame metadata, use predict_batch with your own generator.

How predict_batch works

model.predict_batch(source_iterable) accepts any Python iterator or generator that yields either:

  • frame: a NumPy array shaped H×W×3, dtype=uint8, BGR format.

  • (frame, frame_info): same frame plus a free-form dict (frame_info) that is returned as result.info.

This allows you to:

  • Attach metadata: for syncing, routing, or auditing (e.g., {"camera_id":"dock-3","frame_index":42,"ts_ms":1712345678901}).

  • Preprocess inline: rotate, resize, crop, denoise, or convert colors before yielding.

  • Use any source: PiCamera2, GStreamer, PyAV/FFmpeg, SDK callbacks, image folders, or synthetic frames.

Lifecycle & flow

  • One result frame: outputs are returned in order.

  • An analyzer (if used): tile, track, and zone analyzers return a single merged result per frame.

  • Back-pressure aware: predict_batch pulls frames at the model’s pace. Don’t busy—if you capture asynchronously, use a bounded queue and yield from it.

  • Termination: stop iteration to end the stream. Always release devices and pipelines in a finally block inside your generator.

  • Errors handling: skip bad frames, log, and continue. For a stuck backend, signal failure in frame_info or break and restart the pipeline.

Common setup

Example: OpenCV source (with frame_info)

Example: Raspberry Pi Camera (PiCamera2)

Example: GStreamer via OpenCV (simple)

Make sure your OpenCV build has GStreamer support.

RTSP variant (OpenCV):

Example: GStreamer via PyGObject (appsink, fine control)

* **When to use** `predict_stream`: for webcam, video files, or RTSP, `degirum_tools.predict_stream(model, source)` already performs capture, iteration, and drawing—use it unless you need custom sources, extra processing, or `frame_info` metadata. * `frame_info` **round-trip**: yield `(frame, info)` → read back as `result.info` (e.g., timestamps, camera IDs, sequence numbers, shard IDs, etc.). * **Inline transforms**: rotate, resize, or crop frames inside your generator before yielding:

  • Resource safety: release VideoCapture or pipelines in a finally block inside your generator.

  • Performance: keep per-frame Python work minimal. Push heavy decoding to capture backends. To reduce clutter and computation, set output_class_set in ModelSpec.model_properties if you only need specific labels.

Last updated

Was this helpful?