def meta_copy_func(data, user_data): logging.debug("meta_copy_func: BEGIN") # Cast data to pyds.NvDsUserMeta user_meta = pyds.NvDsUserMeta.cast(data) src_meta_data = user_meta.user_meta_data # Cast src_meta_data to pyds.NvDsEventMsgMeta srcmeta = pyds.NvDsEventMsgMeta.cast(src_meta_data) # Duplicate the memory contents of srcmeta to dstmeta # First use pyds.get_ptr() to get the C address of srcmeta, then # use pyds.memdup() to allocate dstmeta and copy srcmeta into it. # pyds.memdup returns C address of the allocated duplicate. dstmeta_ptr = pyds.memdup(pyds.get_ptr(srcmeta), sys.getsizeof(pyds.NvDsEventMsgMeta)) # Cast the duplicated memory to pyds.NvDsEventMsgMeta dstmeta = pyds.NvDsEventMsgMeta.cast(dstmeta_ptr) # Duplicate contents of ts field. Note that reading srcmeta.ts # returns its C address. This allows to memory operations to be # performed on it. dstmeta.ts = pyds.strdup(srcmeta.ts) # Copy the sensorStr. This field is a string property. # The getter (read) returns its C address. The setter (write) # takes string as input, allocates a string buffer and copies # the input string into it. # pyds.get_string() takes C address of a string and returns # the reference to a string object and the assignment inside the binder copies content. dstmeta.sensorStr = pyds.get_string(srcmeta.sensorStr) if srcmeta.objSignature.size > 0: dstmeta.objSignature.signature = pyds.memdup( srcmeta.objSignature.signature, srcMeta.objSignature.size) dstmeta.objSignature.size = srcmeta.objSignature.size if srcmeta.extMsgSize > 0: if srcmeta.objType == pyds.NvDsObjectType.NVDS_OBJECT_TYPE_VEHICLE: srcobj = pyds.NvDsVehicleObject.cast(srcmeta.extMsg) obj = pyds.alloc_nvds_vehicle_object() obj.type = pyds.get_string(srcobj.type) obj.make = pyds.get_string(srcobj.make) obj.model = pyds.get_string(srcobj.model) obj.color = pyds.get_string(srcobj.color) obj.license = pyds.get_string(srcobj.license) obj.region = pyds.get_string(srcobj.region) dstmeta.extMsg = obj dstmeta.extMsgSize = sys.getsizeof(pyds.NvDsVehicleObject) elif srcmeta.objType == pyds.NvDsObjectType.NVDS_OBJECT_TYPE_PERSON: srcobj = pyds.NvDsPersonObject.cast(srcmeta.extMsg) obj = pyds.alloc_nvds_person_object() obj.age = srcobj.age obj.gender = pyds.get_string(srcobj.gender) obj.cap = pyds.get_string(srcobj.cap) obj.hair = pyds.get_string(srcobj.hair) obj.apparel = pyds.get_string(srcobj.apparel) dstmeta.extMsg = obj dstmeta.extMsgSize = sys.getsizeof(pyds.NvDsVehicleObject) logging.debug("meta_copy_func: END") return dstmeta
def create_pyds_string(s): """Create a C zero-terminated string from the provided Python string. The caller is responsible for freeing the string.""" sb = ctypes.create_string_buffer(s.encode("utf-8")) return pyds.strdup(ctypes.addressof(sb))