def add_obj_meta_to_frame(frame_object, batch_meta, frame_meta, label_names): """ Inserts an object into the metadata """ # this is a good place to insert objects into the metadata. # Here's an example of inserting a single object. obj_meta = pyds.nvds_acquire_obj_meta_from_pool(batch_meta) # Set bbox properties. These are in input resolution. rect_params = obj_meta.rect_params rect_params.left = int(IMAGE_WIDTH * frame_object.left) rect_params.top = int(IMAGE_HEIGHT * frame_object.top) rect_params.width = int(IMAGE_WIDTH * frame_object.width) rect_params.height = int(IMAGE_HEIGHT * frame_object.height) # Semi-transparent yellow backgroud rect_params.has_bg_color = 0 rect_params.bg_color.set(1, 1, 0, 0.4) # Red border of width 3 rect_params.border_width = 3 rect_params.border_color.set(1, 0, 0, 1) # Set object info including class, detection confidence, etc. obj_meta.confidence = frame_object.detectionConfidence obj_meta.class_id = frame_object.classId # There is no tracking ID upon detection. The tracker will # assign an ID. obj_meta.object_id = UNTRACKED_OBJECT_ID lbl_id = frame_object.classId if lbl_id >= len(label_names): lbl_id = 0 # Set the object classification label. obj_meta.obj_label = label_names[lbl_id] # Set display text for the object. txt_params = obj_meta.text_params if txt_params.display_text: pyds.free_buffer(txt_params.display_text) txt_params.x_offset = int(rect_params.left) txt_params.y_offset = max(0, int(rect_params.top) - 10) txt_params.display_text = ( label_names[lbl_id] + " " + "{:04.3f}".format(frame_object.detectionConfidence)) # 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) # Inser the object into current frame meta # This object has no parent pyds.nvds_add_obj_meta_to_frame(frame_meta, obj_meta, None)
def meta_free_func(data, user_data): user_meta = pyds.NvDsUserMeta.cast(data) srcmeta = pyds.NvDsEventMsgMeta.cast(user_meta.user_meta_data) # pyds.free_buffer takes C address of a buffer and frees the memory # It's a NOP if the address is NULL pyds.free_buffer(srcmeta.ts) pyds.free_buffer(srcmeta.sensorStr) if (srcmeta.objSignature.size > 0): pyds.free_buffer(srcmeta.objSignature.signature) srcmeta.objSignature.size = 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
def meta_free_func(data, user_data): user_meta = pyds.glist_get_nvds_user_meta(data) srcmeta = pyds.glist_get_nvds_event_msg_meta(user_meta.user_meta_data) # pyds.free_buffer takes C address of a buffer and frees the memory # It's a NOP if the address is NULL pyds.free_buffer(srcmeta.ts) pyds.free_buffer(srcmeta.sensorStr) if (srcmeta.objSignature.size > 0): pyds.free_buffer(srcmeta.objSignature.signature) srcmeta.objSignature.size = 0 if (srcmeta.extMsgSize > 0): if (srcmeta.objType == pyds.NvDsObjectType.NVDS_OBJECT_TYPE_VEHICLE): obj = pyds.glist_get_nvds_vehicle_object(srcmeta.extMsg) pyds.free_buffer(obj.type) pyds.free_buffer(obj.color) pyds.free_buffer(obj.make) pyds.free_buffer(obj.model) pyds.free_buffer(obj.license) pyds.free_buffer(obj.region) if (srcmeta.objType == pyds.NvDsObjectType.NVDS_OBJECT_TYPE_PERSON): obj = pyds.glist_get_nvds_person_object(srcmeta.extMsg) pyds.free_buffer(obj.gender) pyds.free_buffer(obj.cap) pyds.free_buffer(obj.hair) pyds.free_buffer(obj.apparel) pyds.free_gbuffer(srcmeta.extMsg) srcmeta.extMsgSize = 0
def meta_free_func(data, user_data): logging.debug("meta_free_func: BEGIN") user_meta = pyds.NvDsUserMeta.cast(data) srcmeta = pyds.NvDsEventMsgMeta.cast(user_meta.user_meta_data) # pyds.free_buffer takes C address of a buffer and frees the memory # It's a NOP if the address is NULL pyds.free_buffer(srcmeta.ts) pyds.free_buffer(srcmeta.sensorStr) if srcmeta.objSignature.size > 0: pyds.free_buffer(srcmeta.objSignature.signature) srcmeta.objSignature.size = 0 if srcmeta.extMsgSize > 0: if srcmeta.objType == pyds.NvDsObjectType.NVDS_OBJECT_TYPE_VEHICLE: obj = pyds.NvDsVehicleObject.cast(srcmeta.extMsg) pyds.free_buffer(obj.type) pyds.free_buffer(obj.color) pyds.free_buffer(obj.make) pyds.free_buffer(obj.model) pyds.free_buffer(obj.license) pyds.free_buffer(obj.region) if srcmeta.objType == pyds.NvDsObjectType.NVDS_OBJECT_TYPE_PERSON: obj = pyds.NvDsPersonObject.cast(srcmeta.extMsg) pyds.free_buffer(obj.gender) pyds.free_buffer(obj.cap) pyds.free_buffer(obj.hair) pyds.free_buffer(obj.apparel) pyds.free_gbuffer(srcmeta.extMsg) srcmeta.extMsgSize = 0 logging.debug("meta_free_func: END")