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): sameframeplus a free-formdict(frame_info) that is returned asresult.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_batchpulls frames at the model’s pace. Don’t busy—if you capture asynchronously, use a bounded queue andyieldfrom it.Termination: stop iteration to end the stream. Always release devices and pipelines in a
finallyblock inside your generator.Errors handling: skip bad frames, log, and continue. For a stuck backend, signal failure in
frame_infoor 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)
Last updated
Was this helpful?

