How to custom server maker python kafka data?

please refer to the following code. otherAttrs should be added before using and unreferring. then please rebuild and replace the so file.

  json_object_set_string_member (rootObj, "otherAttrs", meta->otherAttrs);
  json_node_set_object (rootNode, rootObj);
  message = json_to_string (rootNode, TRUE);
......
1 Like

thank you very much, here , I have learned how to customize some content.

but objects is a list, and box in objects also is a list

How can this section be implemented or simplified?

deepstream_schema/eventmsg_payload.cpp:780:57: error: 'NvDsEventMsgMeta' {aka 'struct NvDsEventMsgMeta'} has no member named 'sourceId'
  780 |   json_object_set_int_member(rootObj, "sourceId", meta->sourceId);
      |                                                         ^~~~~~~~
deepstream_schema/eventmsg_payload.cpp:781:52: error: 'NvDsEventMsgMeta' {aka 'struct NvDsEventMsgMeta'} has no member named 'pts'; did you mean 'ts'?
  781 |   json_object_set_int_member(rootObj, "pts", meta->pts);
      |                                                    ^~~
      |                                                    ts
deepstream_schema/eventmsg_payload.cpp:782:52: error: 'NvDsEventMsgMeta' {aka 'struct NvDsEventMsgMeta'} has no member named 'ntp'
  782 |   json_object_set_int_member(rootObj, "ntp", meta->ntp);
      |                                                    ^~~
deepstream_schema/eventmsg_payload.cpp:783:54: error: 'NvDsEventMsgMeta' {aka 'struct NvDsEventMsgMeta'} has no member named 'width'
  783 |   json_object_set_int_member(rootObj, "width", meta->width);
      |                                                      ^~~~~
deepstream_schema/eventmsg_payload.cpp:784:55: error: 'NvDsEventMsgMeta' {aka 'struct NvDsEventMsgMeta'} has no member named 'height'
  784 |   json_object_set_int_member(rootObj, "height", meta->height);
      |                                                       ^~~~~~
deepstream_schema/eventmsg_payload.cpp:785:59: error: 'NvDsEventMsgMeta' {aka 'struct NvDsEventMsgMeta'} has no member named 'objects'; did you mean 'objectId'?
  785 |   json_object_set_string_member(rootObj, "objects", meta->objects);
      |                                                           ^~~~~~~
      |                                                           objectId

It seems that these custom fields cannot be added.

As the error shown, there is not sourceId and other data in the struct NvDsEventMsgMeta. did you succeed to add msg_meta.otherAttrs = “11ttttt”? if yes, you might set all needed data to one msg_meta.otherAttrs.

1 Like

Here is a summary of the solutions outlined above.
1 Edit file. eventmsg_payload.cpp ,

cd /opt/nvidia/deepstream/deepstream/sources/libs/nvmsgconv/deepstream_schema/
vim eventmsg_payload.cpp

in generate_event_message func

gchar* generate_event_message (void *privData, NvDsEventMsgMeta *meta)
{
  JsonNode *rootNode;
  JsonObject *rootObj;
  JsonObject *placeObj;
  JsonObject *sensorObj;
  JsonObject *analyticsObj;
  JsonObject *eventObj;
  JsonObject *objectObj;
  gchar *message;

  uuid_t msgId;
  gchar msgIdStr[37];

  uuid_generate_random (msgId);
  uuid_unparse_lower(msgId, msgIdStr);

  // place object
//  placeObj = generate_place_object (privData, meta);

  // sensor object
//  sensorObj = generate_sensor_object (privData, meta);

  // analytics object
//  analyticsObj = generate_analytics_module_object (privData, meta);

  // object object
//  objectObj = generate_object_object (privData, meta);

  // event object
//  eventObj = generate_event_object (privData, meta);

  // root object
  rootObj = json_object_new ();
  json_object_set_string_member (rootObj, "messageid", msgIdStr);
  json_object_set_string_member (rootObj, "mdsversion", "1.0");
  json_object_set_string_member (rootObj, "@timestamp", meta->ts);
//  json_object_set_object_member (rootObj, "place", placeObj);
//  json_object_set_object_member (rootObj, "sensor", sensorObj);
//  json_object_set_object_member (rootObj, "analyticsModule", analyticsObj);
//  json_object_set_object_member (rootObj, "object", objectObj);
//  json_object_set_object_member (rootObj, "event", eventObj);

//  if (meta->videoPath)
//    json_object_set_string_member (rootObj, "videoPath", meta->videoPath);
//  else
//    json_object_set_string_member (rootObj, "videoPath", "");

  rootNode = json_node_new (JSON_NODE_OBJECT);

  json_object_set_string_member (rootObj, "otherAttrs", meta->otherAttrs);
  json_node_set_object (rootNode, rootObj);

  message = json_to_string (rootNode, TRUE);
  json_node_free (rootNode);
  json_object_unref (rootObj);
  return message;
}
cd ..
make && make install

2, in your python code,

        json_str = json.dumps(payload).encode('utf-8')
        user_event_meta = pyds.nvds_acquire_user_meta_from_pool(batch_meta)
        if user_event_meta:
            msg_meta = pyds.alloc_nvds_event_msg_meta(user_event_meta)
            msg_meta = pyds.NvDsEventMsgMeta.cast(msg_meta)
            msg_meta.otherAttrs = json_str
            msg_meta.ts = pyds.alloc_buffer(MAX_TIME_STAMP_LEN + 1)
            pyds.generate_ts_rfc3339(msg_meta.ts, MAX_TIME_STAMP_LEN)
            user_event_meta.user_meta_data = msg_meta
            user_event_meta.base_meta.meta_type = pyds.NvDsMetaType.NVDS_EVENT_MSG_META

            pyds.nvds_add_user_meta_to_frame(frame_meta, user_event_meta)
        else:
            logger.error("Unable to get user meta")

you will get msg

{
  "messageid": "c3518967-0de4-4c82-9e10-369407f3225e", 
  "mdsversion": "1.0",
  "@timestamp": "2024-11-27T11:28:51.407Z",
  "otherAttrs": "{\"source_id\": 2, \"pts\": 62601937403, \"ntp\": 1732706667686326123, \"width\": 2560, \"height\": 1440, \"frame_num\": 1411, \"objects\": [{\"class_id\": 34, \"class_name\": \"Car\", \"confidence\": \"0.98\", \"box\": [815, 485, 182, 274]}, {\"class_id\": 27, \"class_name\": \"Car\", \"confidence\": \"1.00\", \"box\": [1007, 492, 187, 270]}]}",
}

If we implement custom message content like this, do we still need to implement

event_msg_meta_copy_func()
event_msg_meta_release_func()