def do_transform_ip(self, buffer: Gst.Buffer) -> Gst.FlowReturn:

        if self.model is None:
            Gst.warning(
                f"No model speficied for {self}. Plugin working in passthrough mode"
            )
            return Gst.FlowReturn.OK

        try:
            # Convert Gst.Buffer to np.ndarray
            image = utils.gst_buffer_with_caps_to_ndarray(
                buffer, self.sinkpad.get_current_caps())

            # model inference
            objects = self.model.process_single(image)

            Gst.debug(
                f"Frame id ({buffer.pts // buffer.duration}). Detected {str(objects)}"
            )

            # write objects to as Gst.Buffer's metadata
            # Explained: http://lifestyletransfer.com/how-to-add-metadata-to-gstreamer-buffer-in-python/
            gst_meta_write(buffer, objects)
        except Exception as err:
            logging.error("Error %s: %s", self, err)

        return Gst.FlowReturn.OK
示例#2
0
    def do_transform_ip(self, buffer: Gst.Buffer):
        try:
            caps = self.sinkpad.get_current_caps()
            image = utils.gst_buffer_with_caps_to_ndarray(buffer, caps)
            detections = self.process(image)
            gst_meta_write(buffer, detections)

        except Exception as err:
            Gst.error(f'Error {self}: {traceback.format_exc()}')

        return Gst.FlowReturn.OK
示例#3
0
def test_metadata():
    np_buffer = np.random.randint(low=0,
                                  high=255,
                                  size=(HEIGHT, WIDTH, 3),
                                  dtype=np.uint8)

    gst_buffer = gst.ndarray_to_gst_buffer(np_buffer)

    from gstreamer.gst_objects_info_meta import gst_meta_write, gst_meta_get, gst_meta_remove

    objects = [
        {
            'class_name': "person",
            'bounding_box': [8, 10, 100, 100],
            'confidence': 0.6,
            'track_id': 1
        },
        {
            'class_name': "person",
            'bounding_box': [10, 9, 120, 110],
            'confidence': 0.67,
            'track_id': 2
        },
    ]

    # no metadata at the beginning
    assert len(gst_meta_get(gst_buffer)) == 0

    # write metadata
    gst_meta_write(gst_buffer, objects)

    # read metadata
    meta_objects = gst_meta_get(gst_buffer)
    assert len(gst_meta_get(gst_buffer)) == len(objects)

    for gst_meta_obj, py_obj in zip(meta_objects, objects):
        for key, val in py_obj.items():
            if isinstance(gst_meta_obj[key], float):
                assert math.isclose(gst_meta_obj[key], val, rel_tol=1e-07)
            else:
                assert gst_meta_obj[key] == val

    # remove metadata
    gst_meta_remove(gst_buffer)
    assert len(gst_meta_get(gst_buffer)) == 0