コード例 #1
0
def osd_sink_pad_buffer_probe(pad, info, u_data):
    frame_number = 0
    #Intiallizing object counter with 0.
    obj_counter = {
        PGIE_CLASS_ID_VEHICLE: 0,
        PGIE_CLASS_ID_PERSON: 0,
        PGIE_CLASS_ID_BICYCLE: 0,
        PGIE_CLASS_ID_ROADSIGN: 0
    }
    is_first_object = True
    gst_buffer = info.get_buffer()
    if not gst_buffer:
        print("Unable to get GstBuffer ")
        return

    # Retrieve batch metadata from the gst_buffer
    # Note that pyds.gst_buffer_get_nvds_batch_meta() expects the
    # C address of gst_buffer as input, which is obtained with hash(gst_buffer)
    batch_meta = pyds.gst_buffer_get_nvds_batch_meta(hash(gst_buffer))
    if not batch_meta:
        return Gst.PadProbeReturn.OK
    l_frame = batch_meta.frame_meta_list
    while l_frame is not None:
        try:
            # Note that l_frame.data needs a cast to pyds.NvDsFrameMeta
            # The casting is done by pyds.glist_get_nvds_frame_meta()
            # The casting also keeps ownership of the underlying memory
            # in the C code, so the Python garbage collector will leave
            # it alone.
            frame_meta = pyds.glist_get_nvds_frame_meta(l_frame.data)
        except StopIteration:
            continue
        is_first_object = True
        '''
        print("Frame Number is ", frame_meta.frame_num)
        print("Source id is ", frame_meta.source_id)
        print("Batch id is ", frame_meta.batch_id)
        print("Source Frame Width ", frame_meta.source_frame_width)
        print("Source Frame Height ", frame_meta.source_frame_height)
        print("Num object meta ", frame_meta.num_obj_meta)
        '''
        frame_number = frame_meta.frame_num
        l_obj = frame_meta.obj_meta_list
        while l_obj is not None:
            try:
                obj_meta = pyds.glist_get_nvds_object_meta(l_obj.data)
            except StopIteration:
                continue

            # Update the object text display
            txt_params = obj_meta.text_params
            if (txt_params.display_text):
                pyds.free_buffer(txt_params.display_text)

            txt_params.display_text = pgie_classes_str[obj_meta.class_id]

            obj_counter[obj_meta.class_id] += 1

            # Font , font-color and font-size
            txt_params.font_params.font_name = "Serif"
            txt_params.font_params.font_size = 10
            # set(red, green, blue, alpha); set to White
            txt_params.font_params.font_color.set(1.0, 1.0, 1.0, 1.0)

            # Text background color
            txt_params.set_bg_clr = 1
            # set(red, green, blue, alpha); set to Black
            txt_params.text_bg_clr.set(0.0, 0.0, 0.0, 1.0)

            # Ideally NVDS_EVENT_MSG_META should be attached to buffer by the
            # component implementing detection / recognition logic.
            # Here it demonstrates how to use / attach that meta data.
            if (is_first_object and not (frame_number % 30)):
                # Frequency of messages to be send will be based on use case.
                # Here message is being sent for first object every 30 frames.

                # Allocating an NvDsEventMsgMeta instance and getting reference
                # to it. The underlying memory is not manged by Python so that
                # downstream plugins can access it. Otherwise the garbage collector
                # will free it when this probe exits.
                msg_meta = pyds.alloc_nvds_event_msg_meta()
                msg_meta.bbox.top = obj_meta.rect_params.top
                msg_meta.bbox.left = obj_meta.rect_params.left
                msg_meta.bbox.width = obj_meta.rect_params.width
                msg_meta.bbox.height = obj_meta.rect_params.height
                msg_meta.frameId = frame_number
                msg_meta.trackingId = long_to_int(obj_meta.object_id)
                msg_meta.confidence = obj_meta.confidence
                msg_meta = generate_event_msg_meta(msg_meta, obj_meta.class_id)
                user_event_meta = pyds.nvds_acquire_user_meta_from_pool(
                    batch_meta)
                if (user_event_meta):
                    user_event_meta.user_meta_data = msg_meta
                    user_event_meta.base_meta.meta_type = pyds.NvDsMetaType.NVDS_EVENT_MSG_META
                    # Setting callbacks in the event msg meta. The bindings layer
                    # will wrap these callables in C functions. Currently only one
                    # set of callbacks is supported.
                    pyds.set_user_copyfunc(user_event_meta, meta_copy_func)
                    pyds.set_user_releasefunc(user_event_meta, meta_free_func)
                    pyds.nvds_add_user_meta_to_frame(frame_meta,
                                                     user_event_meta)
                else:
                    print("Error in attaching event meta to buffer\n")

                is_first_object = False
            try:
                l_obj = l_obj.next
            except StopIteration:
                break
        try:
            l_frame = l_frame.next
        except StopIteration:
            break

    print("Frame Number =", frame_number, "Vehicle Count =",
          obj_counter[PGIE_CLASS_ID_VEHICLE], "Person Count =",
          obj_counter[PGIE_CLASS_ID_PERSON])
    return Gst.PadProbeReturn.OK
コード例 #2
0
def set_event_message_meta_probe(pad, info, u_data):
    logging.info("set_event_message_meta_probe: BEGIN")
    add_message_when_no_objects_found = True
    gst_buffer = info.get_buffer()
    if gst_buffer:
        batch_meta = pyds.gst_buffer_get_nvds_batch_meta(hash(gst_buffer))
        if batch_meta:
            for frame_meta_raw in glist_iterator(batch_meta.frame_meta_list):
                frame_meta = pyds.NvDsFrameMeta.cast(frame_meta_raw)
                logging.info(
                    "set_event_message_meta_probe: %20s:%-8s: pts=%23s, dts=%23s, duration=%23s, size=%8d"
                    % (pad.get_parent_element().name, pad.name,
                       format_clock_time(
                           gst_buffer.pts), format_clock_time(gst_buffer.dts),
                       format_clock_time(
                           gst_buffer.duration), gst_buffer.get_size()))
                pravega_timestamp = PravegaTimestamp.from_nanoseconds(
                    frame_meta.buf_pts)
                logging.info(
                    "set_event_message_meta_probe: %20s:%-8s: buf_pts=%s, pravega_timestamp=%s, ntp_timestamp=%s"
                    % (pad.get_parent_element().name, pad.name,
                       format_clock_time(frame_meta.buf_pts),
                       pravega_timestamp, str(frame_meta.ntp_timestamp)))
                if not pravega_timestamp.is_valid():
                    logging.info(
                        "set_event_message_meta_probe: Timestamp %s is invalid."
                        % pravega_timestamp)
                else:
                    added_message = False
                    for obj_meta_raw in glist_iterator(
                            frame_meta.obj_meta_list):
                        obj_meta = pyds.NvDsObjectMeta.cast(obj_meta_raw)
                        logging.info(
                            "set_event_message_meta_probe: obj_meta.class_id=%d"
                            % (obj_meta.class_id, ))
                        # We can only identify a single object in an NvDsEventMsgMeta.
                        # For now, we identify the first object in the frame.
                        # TODO: Create multiple NvDsEventMsgMeta instances per frame or use a custom user metadata class to identify multiple objects.
                        if not added_message:
                            # Allocating an NvDsEventMsgMeta instance and getting reference
                            # to it. The underlying memory is not manged by Python so that
                            # downstream plugins can access it. Otherwise the garbage collector
                            # will free it when this probe exits.
                            msg_meta = pyds.alloc_nvds_event_msg_meta()
                            msg_meta.bbox.top = obj_meta.rect_params.top
                            msg_meta.bbox.left = obj_meta.rect_params.left
                            msg_meta.bbox.width = obj_meta.rect_params.width
                            msg_meta.bbox.height = obj_meta.rect_params.height
                            msg_meta.frameId = frame_meta.frame_num
                            msg_meta.trackingId = long_to_int(
                                obj_meta.object_id)
                            msg_meta.confidence = obj_meta.confidence
                            msg_meta = generate_event_msg_meta(
                                msg_meta, obj_meta.class_id, pravega_timestamp)
                            user_event_meta = pyds.nvds_acquire_user_meta_from_pool(
                                batch_meta)
                            if user_event_meta is None:
                                raise Exception(
                                    "Error in attaching event meta to buffer")
                            user_event_meta.user_meta_data = msg_meta
                            user_event_meta.base_meta.meta_type = pyds.NvDsMetaType.NVDS_EVENT_MSG_META
                            # Setting callbacks in the event msg meta. The bindings layer
                            # will wrap these callables in C functions. Currently only one
                            # set of callbacks is supported.
                            pyds.set_user_copyfunc(user_event_meta,
                                                   meta_copy_func)
                            pyds.set_user_releasefunc(user_event_meta,
                                                      meta_free_func)
                            pyds.nvds_add_user_meta_to_frame(
                                frame_meta, user_event_meta)
                            added_message = True
                    if add_message_when_no_objects_found and not added_message:
                        msg_meta = pyds.alloc_nvds_event_msg_meta()
                        msg_meta.frameId = frame_meta.frame_num
                        msg_meta = generate_event_msg_meta(
                            msg_meta, PGIE_CLASS_ID_NONE, pravega_timestamp)
                        user_event_meta = pyds.nvds_acquire_user_meta_from_pool(
                            batch_meta)
                        if user_event_meta is None:
                            raise Exception(
                                "Error in attaching event meta to buffer")
                        user_event_meta.user_meta_data = msg_meta
                        user_event_meta.base_meta.meta_type = pyds.NvDsMetaType.NVDS_EVENT_MSG_META
                        pyds.set_user_copyfunc(user_event_meta, meta_copy_func)
                        pyds.set_user_releasefunc(user_event_meta,
                                                  meta_free_func)
                        pyds.nvds_add_user_meta_to_frame(
                            frame_meta, user_event_meta)
                        added_message = True

    logging.info("set_event_message_meta_probe: END")
    return Gst.PadProbeReturn.OK
def tiler_src_pad_buffer_probe(pad, info, u_data):
    frame_number = 0

    obj_counter = {PGIE_CLASS_ID_PERSON: 0}
    is_first_object = True

    num_rects = 0
    gst_buffer = info.get_buffer()
    if not gst_buffer:
        print("Unable to get GstBuffer ")
        return

    # Retrieve batch metadata from the gst_buffer
    # Note that pyds.gst_buffer_get_nvds_batch_meta() expects the
    # C address of gst_buffer as input, which is obtained with hash(gst_buffer)
    batch_meta = pyds.gst_buffer_get_nvds_batch_meta(hash(gst_buffer))
    if not batch_meta:
        return Gst.PadProbeReturn.OK
    l_frame = batch_meta.frame_meta_list
    while l_frame is not None:
        try:
            # Note that l_frame.data needs a cast to pyds.NvDsFrameMeta
            # The casting is done by pyds.NvDsFrameMeta.cast()
            # The casting also keeps ownership of the underlying memory
            # in the C code, so the Python garbage collector will leave
            # it alone.
            frame_meta = pyds.NvDsFrameMeta.cast(l_frame.data)
        except StopIteration:
            continue
        '''
        print("Frame Number is ", frame_meta.frame_num)
        print("Source id is ", frame_meta.source_id)
        print("Batch id is ", frame_meta.batch_id)
        print("Source Frame Width ", frame_meta.source_frame_width)
        print("Source Frame Height ", frame_meta.source_frame_height)
        print("Num object meta ", frame_meta.num_obj_meta)
        '''
        frame_number = frame_meta.frame_num
        l_obj = frame_meta.obj_meta_list
        num_rects = frame_meta.num_obj_meta
        is_first_object = True

        while l_obj is not None:
            try:
                # Casting l_obj.data to pyds.NvDsObjectMeta
                obj_meta = pyds.NvDsObjectMeta.cast(l_obj.data)
            except StopIteration:
                continue
            obj_counter[obj_meta.class_id] += 1

            # Ideally NVDS_EVENT_MSG_META should be attached to buffer by the
            # component implementing detection / recognition logic.
            # Here it demonstrates how to use / attach that meta data.
            # print("########################")
            # print("before entering is first object", is_first_object)
            if (is_first_object and not (frame_number % 30)):
                # print("Entered is first object")
                # Frequency of messages to be send will be based on use case.
                # Here message is being sent for first object every 30 frames.

                # Allocating an NvDsEventMsgMeta instance and getting reference
                # to it. The underlying memory is not manged by Python so that
                # downstream plugins can access it. Otherwise the garbage collector
                # will free it when this probe exits.
                msg_meta = pyds.alloc_nvds_event_msg_meta()
                msg_meta.bbox.top = obj_meta.rect_params.top
                msg_meta.bbox.left = obj_meta.rect_params.left
                msg_meta.bbox.width = obj_meta.rect_params.width
                msg_meta.bbox.height = obj_meta.rect_params.height
                msg_meta.frameId = frame_number
                msg_meta.trackingId = long_to_int(obj_meta.object_id)
                msg_meta.confidence = obj_meta.confidence
                msg_meta = generate_event_msg_meta(msg_meta, obj_meta.class_id)
                user_event_meta = pyds.nvds_acquire_user_meta_from_pool(
                    batch_meta)
                if (user_event_meta):
                    user_event_meta.user_meta_data = msg_meta
                    user_event_meta.base_meta.meta_type = pyds.NvDsMetaType.NVDS_EVENT_MSG_META
                    # Setting callbacks in the event msg meta. The bindings layer
                    # will wrap these callables in C functions. Currently only one
                    # set of callbacks is supported.
                    pyds.set_user_copyfunc(user_event_meta, meta_copy_func)
                    pyds.set_user_releasefunc(user_event_meta, meta_free_func)
                    pyds.nvds_add_user_meta_to_frame(frame_meta,
                                                     user_event_meta)
                else:
                    print("Error in attaching event meta to buffer\n")

                is_first_object = False

            try:
                l_obj = l_obj.next
            except StopIteration:
                break
        """display_meta=pyds.nvds_acquire_display_meta_from_pool(batch_meta)
        display_meta.num_labels = 1
        py_nvosd_text_params = display_meta.text_params[0]
        py_nvosd_text_params.display_text = "Frame Number={} Number of Objects={} Vehicle_count={} Person_count={}".format(frame_number, num_rects, vehicle_count, person)
        py_nvosd_text_params.x_offset = 10;
        py_nvosd_text_params.y_offset = 12;
        py_nvosd_text_params.font_params.font_name = "Serif"
        py_nvosd_text_params.font_params.font_size = 10
        py_nvosd_text_params.font_params.font_color.red = 1.0
        py_nvosd_text_params.font_params.font_color.green = 1.0
        py_nvosd_text_params.font_params.font_color.blue = 1.0
        py_nvosd_text_params.font_params.font_color.alpha = 1.0
        py_nvosd_text_params.set_bg_clr = 1
        py_nvosd_text_params.text_bg_clr.red = 0.0
        py_nvosd_text_params.text_bg_clr.green = 0.0
        py_nvosd_text_params.text_bg_clr.blue = 0.0
        py_nvosd_text_params.text_bg_clr.alpha = 1.0
        #print("Frame Number=", frame_number, "Number of Objects=",num_rects,"Vehicle_count=",vehicle_count,"Person_count=",person)
        pyds.nvds_add_display_meta_to_frame(frame_meta, display_meta)"""

        # Get frame rate through this probe
        fps_streams["stream{0}".format(frame_meta.pad_index)].get_fps()
        try:
            l_frame = l_frame.next
        except StopIteration:
            break
    print("Frame Number=", frame_number, "Number of Objects=", num_rects,
          "Face=", obj_counter[PGIE_CLASS_ID_PERSON])
    return Gst.PadProbeReturn.OK
def set_event_message_meta_probe(pad, info, u_data):
    logging.info("set_event_message_meta_probe: BEGIN")
    gst_buffer = info.get_buffer()
    if gst_buffer:
        batch_meta = pyds.gst_buffer_get_nvds_batch_meta(hash(gst_buffer))
        if batch_meta:
            for frame_meta_raw in glist_iterator(batch_meta.frame_meta_list):
                frame_meta = pyds.NvDsFrameMeta.cast(frame_meta_raw)
                # TODO: It appears that the timestamp may be incorrect by up to 1 second.
                pravega_video_timestamp = pts_to_pravega_video_timestamp(
                    frame_meta.buf_pts)
                logging.info(
                    "set_event_message_meta_probe: pad=%s:%s, pts=%s, buf_pts=%s, pravega_video_timestamp=%d, ntp_timestamp=%s"
                    % (pad.get_parent_element().name, pad.name,
                       format_clock_time(gst_buffer.pts),
                       format_clock_time(frame_meta.buf_pts),
                       pravega_video_timestamp, str(frame_meta.ntp_timestamp)))
                if pravega_video_timestamp <= 0:
                    logging.info(
                        "set_event_message_meta_probe: Timestamp is invalid. It may take a few seconds for RTSP timestamps to be valid."
                    )
                else:
                    is_first_object = True
                    for obj_meta_raw in glist_iterator(
                            frame_meta.obj_meta_list):
                        obj_meta = pyds.NvDsObjectMeta.cast(obj_meta_raw)
                        logging.info(
                            "set_event_message_meta_probe: obj_meta.class_id=%d"
                            % (obj_meta.class_id, ))
                        # We can only identify a single object in an NvDsEventMsgMeta.
                        # For now, we identify the first object in the frame.
                        # TODO: Create multiple NvDsEventMsgMeta instances per frame or use a custom user metadata class to identify multiple objects.
                        if is_first_object:
                            # Allocating an NvDsEventMsgMeta instance and getting reference
                            # to it. The underlying memory is not manged by Python so that
                            # downstream plugins can access it. Otherwise the garbage collector
                            # will free it when this probe exits.
                            msg_meta = pyds.alloc_nvds_event_msg_meta()
                            msg_meta.bbox.top = obj_meta.rect_params.top
                            msg_meta.bbox.left = obj_meta.rect_params.left
                            msg_meta.bbox.width = obj_meta.rect_params.width
                            msg_meta.bbox.height = obj_meta.rect_params.height
                            msg_meta.frameId = frame_meta.frame_num
                            msg_meta.trackingId = long_to_int(
                                obj_meta.object_id)
                            msg_meta.confidence = obj_meta.confidence
                            msg_meta = generate_event_msg_meta(
                                msg_meta, obj_meta.class_id,
                                pravega_video_timestamp)
                            user_event_meta = pyds.nvds_acquire_user_meta_from_pool(
                                batch_meta)
                            if user_event_meta:
                                user_event_meta.user_meta_data = msg_meta
                                user_event_meta.base_meta.meta_type = pyds.NvDsMetaType.NVDS_EVENT_MSG_META
                                # Setting callbacks in the event msg meta. The bindings layer
                                # will wrap these callables in C functions. Currently only one
                                # set of callbacks is supported.
                                pyds.set_user_copyfunc(user_event_meta,
                                                       meta_copy_func)
                                pyds.set_user_releasefunc(
                                    user_event_meta, meta_free_func)
                                pyds.nvds_add_user_meta_to_frame(
                                    frame_meta, user_event_meta)
                            else:
                                raise Exception(
                                    "Error in attaching event meta to buffer")
                            is_first_object = False
    logging.info("set_event_message_meta_probe: END")
    return Gst.PadProbeReturn.OK