How to attach custom metadata objects?

The example provided in deepstream-test-4 shows how to attach additional metadata, but these are not user-defined. What if I want to attach a custom Python object? Is there any way to do so using the bindings?

Hi @amr.mustafa

An easy way we found to attach custom meta to DeepStream meta was by adding a JSON string as NvDsEventMsgMeta

In this answer I added the code of how we do it on C:

But we also have a python element that adds custom metadata this way:

def custom_meta_copy_func(data, user_data):
    user_meta = pyds.NvDsUserMeta.cast(data)
    src_meta_data = user_meta.user_meta_data
    srcmeta = pyds.NvDsEventMsgMeta.cast(src_meta_data)
    dstmeta_ptr = pyds.memdup(
        pyds.get_ptr(srcmeta), sys.getsizeof(
            pyds.NvDsEventMsgMeta))
    dstmeta = pyds.NvDsEventMsgMeta.cast(dstmeta_ptr)
    dstmeta.otherAttrs = pyds.get_string(srcmeta.otherAttrs)
    dstmeta.objectId = pyds.get_string(srcmeta.objectId)
    return dstmeta


def custom_meta_free_func(data, user_data):
    user_meta = pyds.NvDsUserMeta.cast(data)
    srcmeta = pyds.NvDsEventMsgMeta.cast(user_meta.user_meta_data)
    pyds.free_buffer(srcmeta.otherAttrs)
    pyds.free_buffer(srcmeta.objectId)

...

    def _add_custom_meta_to_buffer(self, meta_str, buf):
        batch_meta = pyds.gst_buffer_get_nvds_batch_meta(hash(buf))
        user_event_meta = pyds.nvds_acquire_user_meta_from_pool(batch_meta)
        msg_meta = pyds.alloc_nvds_event_msg_meta()
        msg_meta = pyds.NvDsEventMsgMeta.cast(msg_meta)

        # We are using the otherAttrs field of NvDsEventMsgMeta to pass the
        # custom JSON meta string downstream
        msg_meta.otherAttrs = meta_str
        # We distinguish our event meta from other DeepStream events by
        # setting the objectId field
        msg_meta.objectId = "custom"

        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
            # Set callbacks for the event msg meta. These are needed to
            # avoid memory leaks.
            pyds.set_user_copyfunc(user_event_meta, custom_meta_copy_func)
            pyds.set_user_releasefunc(
                user_event_meta, custom_meta_free_func)
            pyds.nvds_add_user_meta_to_batch(batch_meta, user_event_meta)
        else:
            Gst.error("Error in attaching event meta to buffer\n")

Hope this helps

This is so cool! I will give it a try when I get back to work. Let’s mark this as the solution. Thank you!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.