Python Bindings#

1. GStreamer Python bindings#

GStreamer provides Python bindings in the module called pygst. On Ubuntu you can install it through apt package manager as

sudo apt-get install python-gst-1.0

GStreamer Python interface has one-to-one mapping with C interface as Python wrappers generated automatically via GObject introspection. Similar to C/C++ the Python application can construct pipeline (either from pipeline string description in gst-launch format or creating and connecting elements programmatically), set pad probe callback(s) on source or sink pad of any element in the pipeline, etc.

Please see Python sample as one of examples.

2. Video-analytics specific Python bindings#

As GVA plugin registers inference specific metadata, another Python library gstgva in this repository is complimentary to pygst and additionally provides Python bindings for GVA specific types such as GstGVATensorMeta and GstGVAJSONMeta and access to inference specific fields in GstVideoRegionOfInterestMeta. Unlike gstpy, C to Python wrappers in gstgva implemented via Python ctypes mechanism and designed with the goal of one-to-one mapping with GVA C++ interface.

You can find the following Python callback function example of accessing list of detected objects and objects attributes very similar to C++ function example on Metadata page.

def pad_probe_callback(pad, info):
    with gstgva.util.GST_PAD_PROBE_INFO_BUFFER(info) as buffer:
        caps = pad.get_current_caps()
        frame = gstgva.VideoFrame(buffer, caps)
        for roi in frame.regions():
            for tensor in roi.tensors():
                print("  Attribute ",
                print("    label=", tensor.label())
                print("    confidence=", tensor.confidence())
    return Gst.PadProbeReturn.OK

3. gvapython element#

Besides Python binding for standard GStreamer and GVA plugin interfaces, the GStreamer element gvapython from this repository could be used for pipeline customization with code in Python language. The gvapython inserted at any place of pipeline and takes reference to Python file and function/class name as element property, and invokes provided callback function on every frame.

The gvapython element could be used for post-processing of metadata generated by inference elements (for example, extract list of detected bounding boxes in case of detection model format not supported by GVA), or perform some analytics based on inference results (for example, compare face embeddings versus gallery of known faces).

The gvapython element implemented in “C” language as normal GStreamer element and invokes Python functions via “C” interface (Python.h).

Please see samples for gvapython element in this folder

4. Performance considerations#

Python code performance is typically significantly slower than C/C++ code compiled into native instruction set. The parallelization is also limited as all Python code executed in single thread. If Python callback contains compute intensive operations executed every frame, it may impact overall performance and production usage may require migration from Python to C implementation.