From 65b8f9b9a8f41051b70a1ee213a8e46ef4becc14 Mon Sep 17 00:00:00 2001 From: Barzan Hayati Date: Sun, 24 Aug 2025 09:18:13 +0000 Subject: [PATCH] Recator draw imprecise face --- src/nv_infer_server_manager.cpp | 335 +++++++++++++++----------------- src/nv_infer_server_manager.hpp | 3 + 2 files changed, 161 insertions(+), 177 deletions(-) diff --git a/src/nv_infer_server_manager.cpp b/src/nv_infer_server_manager.cpp index 8daf173..9629bf4 100644 --- a/src/nv_infer_server_manager.cpp +++ b/src/nv_infer_server_manager.cpp @@ -242,7 +242,6 @@ GstPadProbeReturn NvInferServerManager::pgie_pad_buffer_probe( (void)stream_height; (void)stream_width; - // float source_id = (float)frame_meta->source_id; /* Iterate user metadata in frames to search PGIE's tensor metadata */ for (NvDsMetaList *l_user = frame_meta->frame_user_meta_list; l_user != NULL; l_user = l_user->next) { @@ -312,190 +311,172 @@ GstPadProbeReturn NvInferServerManager::pgie_pad_buffer_probe( jkl++) { // 100 persons for each frame if (data[jkl * 57 + 4] > threshold_body_detection) { detected_persons++; - // std::cout - // << "nvinferserver first for x = " << data[jkl * 57 + - // 0] - // << " y = " << data[jkl * 57 + 1] - // << " w = " << data[jkl * 57 + 2] - // << " h = " << data[jkl * 57 + 3] - // << " score = " << data[jkl * 57 + 4] << std::endl; - // for (unsigned int mno = 0; mno < 57; ++mno) { - // float value = data[jkl * 57 + mno]; - // (void)value; - // // std::cout << "data[" << jkl << "][" << mno - // // << "] = " << value << std::endl; - // } } } - - for (uint index = 0; index < detected_persons; index++) { - // imprecise_face_obj_meta is the imprecise face - NvDsObjectMeta *imprecise_face_obj_meta = - nvds_acquire_obj_meta_from_pool(batch_meta); - - // meta->unique_id in NvDsInferTensorMeta - // This is the unique ID of the inference component (PGIE/SGIE) - // that produced the tensor output. It comes directly from the - // unique-id property in the [property] section of your - // config_infer_primary.txt or config_infer_secondary.txt. A - // pipeline can have multiple inference components (1 PGIE + - // many SGIEs). Each inference element might output tensors - // (NvDsInferTensorMeta) that are attached as user metadata. - // unique_id lets you know which inference element the tensor - // belongs to. meta->unique_id → The unique-id you assigned in - // the config for the inference component that produced these - // tensor outputs. - - imprecise_face_obj_meta->unique_component_id = meta->unique_id; - - // imprecise_face_obj_meta->unique_component_id - // Meaning: The ID of the component (PGIE, SGIE, Tracker, - // Custom, etc.) that generated this metadata. Source: Assigned - // by DeepStream when metadata is attached by a specific element - // in the pipeline. Example: PGIE might be assigned - // unique_component_id = 1 SGIE might be assigned - // unique_component_id = 2 Tracker usually doesn’t overwrite - // PGIE’s class_id but may extend metadata (like assigning - // object_id). You normally don’t set this manually. DeepStream - // sets it when a particular component (PGIE/SGIE) attaches - // object metadata. You might override it only if you’re - // injecting your own custom objects into the pipeline and need - // to differentiate your component from PGIE/SGIE. - - imprecise_face_obj_meta->confidence = data[index * 57 + 4]; - // imprecise_face_obj_meta->object_id = UNTRACKED_OBJECT_ID; - imprecise_face_obj_meta->class_id = - PGIE_CLASS_ID_PERSON; // 0 for body detection - NvOSD_RectParams &rect_params_imprecise_face = - imprecise_face_obj_meta->rect_params; - NvOSD_TextParams &text_params_imprecise_face = - imprecise_face_obj_meta->text_params; - /* Assign bounding box coordinates. */ - rect_params_imprecise_face.left = int( - data[index * 57 + 0] * MUXER_OUTPUT_WIDTH / PGIE_NET_WIDTH); - rect_params_imprecise_face.top = - int(data[index * 57 + 1] * MUXER_OUTPUT_HEIGHT / - PGIE_NET_HEIGHT); - float x_shoulder; - float y_shoulder; - if (data[index * 57 + 21] > data[index * 57 + 24]) { - x_shoulder = data[index * 57 + 21]; - y_shoulder = data[index * 57 + 22]; - } else { - x_shoulder = data[index * 57 + 24]; - y_shoulder = data[index * 57 + 25]; - } - rect_params_imprecise_face.width = - int((x_shoulder - data[index * 57 + 0]) * - MUXER_OUTPUT_WIDTH / PGIE_NET_WIDTH); - rect_params_imprecise_face.height = - int((y_shoulder - data[index * 57 + 1]) * - MUXER_OUTPUT_HEIGHT / PGIE_NET_HEIGHT); - - // std::cout << "nvinferserver imprecise face for x = " << - // rect_params_imprecise_face.left - // << " y = " << rect_params_imprecise_face.top - // << " w = " << rect_params_imprecise_face.width - // << " h = " << rect_params_imprecise_face.height - // << " score = " << - // imprecise_face_obj_meta->confidence << std::endl; - - /* Border of width 3. */ - rect_params_imprecise_face.border_width = 3; - rect_params_imprecise_face.has_bg_color = 0; - rect_params_imprecise_face.border_color = - NvOSD_ColorParams{0, 0, 1, 1}; - /* display_text requires heap allocated memory. */ - text_params_imprecise_face.display_text = g_strdup( - imprecise_face_str[0]); // g_strdup(pgie_class_str[0]); - /* Display text above the left top corner of the object. */ - text_params_imprecise_face.x_offset = - rect_params_imprecise_face.left; - text_params_imprecise_face.y_offset = - rect_params_imprecise_face.top - 10; - /* Set black background for the text. */ - text_params_imprecise_face.set_bg_clr = 1; - text_params_imprecise_face.text_bg_clr = - NvOSD_ColorParams{0, 0, 0, 1}; - /* Font face, size and color. */ - text_params_imprecise_face.font_params.font_name = - (gchar *)"Serif"; - text_params_imprecise_face.font_params.font_size = 11; - text_params_imprecise_face.font_params.font_color = - NvOSD_ColorParams{1, 1, 1, 1}; - // adding landmarks to imprecise_face_obj_meta as user_meta - NvDsUserMeta *um1 = - nvds_acquire_user_meta_from_pool(batch_meta); - um1->user_meta_data = set_metadata_ptr( - &(data[index * 57])); // Add landmarks here - um1->base_meta.meta_type = - NVDS_USER_OBJECT_META_LANDMARKS_AND_SOURCE_ID; - um1->base_meta.copy_func = (NvDsMetaCopyFunc)copy_user_meta; - um1->base_meta.release_func = - (NvDsMetaReleaseFunc)release_user_meta; - nvds_add_user_meta_to_obj(imprecise_face_obj_meta, um1); - nvds_add_obj_meta_to_frame(frame_meta, imprecise_face_obj_meta, - NULL); - - NvDsObjectMeta *body_obj_meta = - nvds_acquire_obj_meta_from_pool(batch_meta); - - body_obj_meta->unique_component_id = meta->unique_id; - body_obj_meta->confidence = data[index * 57 + 4]; - // body_obj_meta->object_id = UNTRACKED_OBJECT_ID; - body_obj_meta->class_id = - PGIE_CLASS_ID_PERSON; // 0 for body detection - NvOSD_RectParams &rect_params_body = body_obj_meta->rect_params; - NvOSD_TextParams &text_params_body = body_obj_meta->text_params; - /* Assign bounding box coordinates. */ - rect_params_body.left = int( - data[index * 57 + 0] * MUXER_OUTPUT_WIDTH / PGIE_NET_WIDTH); - rect_params_body.top = - int(data[index * 57 + 1] * MUXER_OUTPUT_HEIGHT / - PGIE_NET_HEIGHT); - rect_params_body.width = - int((data[index * 57 + 2] - data[index * 57 + 0]) * - MUXER_OUTPUT_WIDTH / PGIE_NET_WIDTH); - rect_params_body.height = - int((data[index * 57 + 3] - data[index * 57 + 1]) * - MUXER_OUTPUT_HEIGHT / PGIE_NET_HEIGHT); - - /* Border of width 3. */ - rect_params_body.border_width = 3; - rect_params_body.has_bg_color = 0; - rect_params_body.border_color = NvOSD_ColorParams{1, 0, 0, 1}; - /* display_text requires heap allocated memory. */ - text_params_body.display_text = g_strdup(pgie_class_str[0]); - /* Display text above the left top corner of the object. */ - text_params_body.x_offset = rect_params_body.left; - text_params_body.y_offset = rect_params_body.top - 30; - /* Set black background for the text. */ - text_params_body.set_bg_clr = 1; - text_params_body.text_bg_clr = NvOSD_ColorParams{0, 0, 0, 1}; - /* Font face, size and color. */ - text_params_body.font_params.font_name = (gchar *)"Serif"; - text_params_body.font_params.font_size = 11; - text_params_body.font_params.font_color = - NvOSD_ColorParams{1, 1, 1, 1}; - // // adding landmarks to body_obj_meta as user_meta - // NvDsUserMeta *um1 = - // nvds_acquire_user_meta_from_pool(batch_meta); - // um1->user_meta_data = set_metadata_ptr( - // &(data[index * 57])); // Add landmarks here - // um1->base_meta.meta_type = - // NVDS_USER_OBJECT_META_LANDMARKS_AND_SOURCE_ID; - // um1->base_meta.copy_func = (NvDsMetaCopyFunc)copy_user_meta; - // um1->base_meta.release_func = - // (NvDsMetaReleaseFunc)release_user_meta; - // nvds_add_user_meta_to_obj(body_obj_meta, um1); - nvds_add_obj_meta_to_frame(frame_meta, body_obj_meta, NULL); - } + update_frame_with_face_body_meta(detected_persons, batch_meta, meta, + data, frame_meta); } } // use_device_mem = 1 - use_device_mem; return GST_PAD_PROBE_OK; } +void NvInferServerManager::update_frame_with_face_body_meta( + uint detected_persons, NvDsBatchMeta *batch_meta, NvDsInferTensorMeta *meta, + float *data, NvDsFrameMeta *frame_meta) { + for (uint index = 0; index < detected_persons; index++) { + // imprecise_face_obj_meta is the imprecise face + NvDsObjectMeta *imprecise_face_obj_meta = + nvds_acquire_obj_meta_from_pool(batch_meta); + + // meta->unique_id in NvDsInferTensorMeta + // This is the unique ID of the inference component (PGIE/SGIE) + // that produced the tensor output. It comes directly from the + // unique-id property in the [property] section of your + // config_infer_primary.txt or config_infer_secondary.txt. A + // pipeline can have multiple inference components (1 PGIE + + // many SGIEs). Each inference element might output tensors + // (NvDsInferTensorMeta) that are attached as user metadata. + // unique_id lets you know which inference element the tensor + // belongs to. meta->unique_id → The unique-id you assigned in + // the config for the inference component that produced these + // tensor outputs. + + imprecise_face_obj_meta->unique_component_id = meta->unique_id; + + // imprecise_face_obj_meta->unique_component_id + // Meaning: The ID of the component (PGIE, SGIE, Tracker, + // Custom, etc.) that generated this metadata. Source: Assigned + // by DeepStream when metadata is attached by a specific element + // in the pipeline. Example: PGIE might be assigned + // unique_component_id = 1 SGIE might be assigned + // unique_component_id = 2 Tracker usually doesn’t overwrite + // PGIE’s class_id but may extend metadata (like assigning + // object_id). You normally don’t set this manually. DeepStream + // sets it when a particular component (PGIE/SGIE) attaches + // object metadata. You might override it only if you’re + // injecting your own custom objects into the pipeline and need + // to differentiate your component from PGIE/SGIE. + + imprecise_face_obj_meta->confidence = data[index * 57 + 4]; + // imprecise_face_obj_meta->object_id = UNTRACKED_OBJECT_ID; + imprecise_face_obj_meta->class_id = + PGIE_CLASS_ID_PERSON; // 0 for body detection + NvOSD_RectParams &rect_params_imprecise_face = + imprecise_face_obj_meta->rect_params; + NvOSD_TextParams &text_params_imprecise_face = + imprecise_face_obj_meta->text_params; + /* Assign bounding box coordinates. */ + rect_params_imprecise_face.left = + int(data[index * 57 + 0] * MUXER_OUTPUT_WIDTH / PGIE_NET_WIDTH); + rect_params_imprecise_face.top = + int(data[index * 57 + 1] * MUXER_OUTPUT_HEIGHT / PGIE_NET_HEIGHT); + float x_shoulder; + float y_shoulder; + if (data[index * 57 + 21] > data[index * 57 + 24]) { + x_shoulder = data[index * 57 + 21]; + y_shoulder = data[index * 57 + 22]; + } else { + x_shoulder = data[index * 57 + 24]; + y_shoulder = data[index * 57 + 25]; + } + rect_params_imprecise_face.width = + int((x_shoulder - data[index * 57 + 0]) * MUXER_OUTPUT_WIDTH / + PGIE_NET_WIDTH); + rect_params_imprecise_face.height = + int((y_shoulder - data[index * 57 + 1]) * MUXER_OUTPUT_HEIGHT / + PGIE_NET_HEIGHT); + + // std::cout << "nvinferserver imprecise face for x = " << + // rect_params_imprecise_face.left + // << " y = " << rect_params_imprecise_face.top + // << " w = " << rect_params_imprecise_face.width + // << " h = " << rect_params_imprecise_face.height + // << " score = " << + // imprecise_face_obj_meta->confidence << std::endl; + + /* Border of width 3. */ + rect_params_imprecise_face.border_width = 3; + rect_params_imprecise_face.has_bg_color = 0; + rect_params_imprecise_face.border_color = NvOSD_ColorParams{0, 0, 1, 1}; + /* display_text requires heap allocated memory. */ + text_params_imprecise_face.display_text = + g_strdup(imprecise_face_str[0]); // g_strdup(pgie_class_str[0]); + /* Display text above the left top corner of the object. */ + text_params_imprecise_face.x_offset = rect_params_imprecise_face.left; + text_params_imprecise_face.y_offset = + rect_params_imprecise_face.top - 10; + /* Set black background for the text. */ + text_params_imprecise_face.set_bg_clr = 1; + text_params_imprecise_face.text_bg_clr = NvOSD_ColorParams{0, 0, 0, 1}; + /* Font face, size and color. */ + text_params_imprecise_face.font_params.font_name = (gchar *)"Serif"; + text_params_imprecise_face.font_params.font_size = 11; + text_params_imprecise_face.font_params.font_color = + NvOSD_ColorParams{1, 1, 1, 1}; + // adding landmarks to imprecise_face_obj_meta as user_meta + NvDsUserMeta *um1 = nvds_acquire_user_meta_from_pool(batch_meta); + um1->user_meta_data = + set_metadata_ptr(&(data[index * 57])); // Add landmarks here + um1->base_meta.meta_type = + NVDS_USER_OBJECT_META_LANDMARKS_AND_SOURCE_ID; + um1->base_meta.copy_func = (NvDsMetaCopyFunc)copy_user_meta; + um1->base_meta.release_func = (NvDsMetaReleaseFunc)release_user_meta; + nvds_add_user_meta_to_obj(imprecise_face_obj_meta, um1); + nvds_add_obj_meta_to_frame(frame_meta, imprecise_face_obj_meta, NULL); + + NvDsObjectMeta *body_obj_meta = + nvds_acquire_obj_meta_from_pool(batch_meta); + + body_obj_meta->unique_component_id = meta->unique_id; + body_obj_meta->confidence = data[index * 57 + 4]; + // body_obj_meta->object_id = UNTRACKED_OBJECT_ID; + body_obj_meta->class_id = PGIE_CLASS_ID_PERSON; // 0 for body detection + NvOSD_RectParams &rect_params_body = body_obj_meta->rect_params; + NvOSD_TextParams &text_params_body = body_obj_meta->text_params; + /* Assign bounding box coordinates. */ + rect_params_body.left = + int(data[index * 57 + 0] * MUXER_OUTPUT_WIDTH / PGIE_NET_WIDTH); + rect_params_body.top = + int(data[index * 57 + 1] * MUXER_OUTPUT_HEIGHT / PGIE_NET_HEIGHT); + rect_params_body.width = + int((data[index * 57 + 2] - data[index * 57 + 0]) * + MUXER_OUTPUT_WIDTH / PGIE_NET_WIDTH); + rect_params_body.height = + int((data[index * 57 + 3] - data[index * 57 + 1]) * + MUXER_OUTPUT_HEIGHT / PGIE_NET_HEIGHT); + + /* Border of width 3. */ + rect_params_body.border_width = 3; + rect_params_body.has_bg_color = 0; + rect_params_body.border_color = NvOSD_ColorParams{1, 0, 0, 1}; + /* display_text requires heap allocated memory. */ + text_params_body.display_text = g_strdup(pgie_class_str[0]); + /* Display text above the left top corner of the object. */ + text_params_body.x_offset = rect_params_body.left; + text_params_body.y_offset = rect_params_body.top - 30; + /* Set black background for the text. */ + text_params_body.set_bg_clr = 1; + text_params_body.text_bg_clr = NvOSD_ColorParams{0, 0, 0, 1}; + /* Font face, size and color. */ + text_params_body.font_params.font_name = (gchar *)"Serif"; + text_params_body.font_params.font_size = 11; + text_params_body.font_params.font_color = NvOSD_ColorParams{1, 1, 1, 1}; + // // adding landmarks to body_obj_meta as user_meta + // NvDsUserMeta *um1 = + // nvds_acquire_user_meta_from_pool(batch_meta); + // um1->user_meta_data = set_metadata_ptr( + // &(data[index * 57])); // Add landmarks here + // um1->base_meta.meta_type = + // NVDS_USER_OBJECT_META_LANDMARKS_AND_SOURCE_ID; + // um1->base_meta.copy_func = (NvDsMetaCopyFunc)copy_user_meta; + // um1->base_meta.release_func = + // (NvDsMetaReleaseFunc)release_user_meta; + // nvds_add_user_meta_to_obj(body_obj_meta, um1); + nvds_add_obj_meta_to_frame(frame_meta, body_obj_meta, NULL); + } +} + // add custom infromation to metadata by: set_metadata_ptr, copy_user_meta, // release_user_meta void *NvInferServerManager::set_metadata_ptr(float *arr) { diff --git a/src/nv_infer_server_manager.hpp b/src/nv_infer_server_manager.hpp index 5eaa8df..cbc35c4 100644 --- a/src/nv_infer_server_manager.hpp +++ b/src/nv_infer_server_manager.hpp @@ -40,4 +40,7 @@ class NvInferServerManager { static void *set_metadata_ptr(float *); static gpointer copy_user_meta(gpointer, gpointer); static void release_user_meta(gpointer, gpointer); + static void update_frame_with_face_body_meta(uint, NvDsBatchMeta *, + NvDsInferTensorMeta *, float *, + NvDsFrameMeta *); }; \ No newline at end of file