Trying to dsiplay the new info for each object

Hardware Platform: Jetson Orin Nano
DeepStream Version: 6.3
JetPack Version: 5.1

I am trying to display a result of a cosine similarity between two objects in deepstream, one of them is provided in a file and the other is the bbox of each object.

I could do everything and print the result of the similarty on the terminal.
I tried to display the result of the similarity on the bbox but its blinking and only shows on only one object at the time.

This is the part of my code to process the metadata and display it:

NvDsObjectMeta *obj_meta = NULL;
  // For single source always display text either with demuxer or with tiler
  if (!appCtx->config.tiled_display_config.enable ||
      appCtx->config.num_source_sub_bins == 1) {
    appCtx->show_bbox_text = 1;
  }

  for (NvDsMetaList * l_frame = batch_meta->frame_meta_list; l_frame != NULL;l_frame = l_frame->next) {
    NvDsFrameMeta *frame_meta = (NvDsFrameMeta *) l_frame->data;
    for (NvDsMetaList * l_obj = frame_meta->obj_meta_list; l_obj != NULL;l_obj = l_obj->next) {
      NvDsObjectMeta *obj_meta = (NvDsObjectMeta *)(l_obj->data);
            
      for (NvDsMetaList *l_user = obj_meta->obj_user_meta_list; l_user != NULL; l_user = l_user->next) {
          NvDsUserMeta *user_meta = (NvDsUserMeta *)l_user->data;
          if (user_meta->base_meta.meta_type == NVDSINFER_TENSOR_OUTPUT_META) {
            
          NvDsInferTensorMeta *tensor_meta = (NvDsInferTensorMeta *)user_meta->user_meta_data;
                  float *embedding_vector = (float *)tensor_meta->out_buf_ptrs_host[0];

          NvDsInferDims embedding_dims = tensor_meta->output_layers_info[0].inferDims;

        int numElements = 1;
          for (int i = 0; i < embedding_dims.numDims; i++) {
            numElements *= embedding_dims.d[i];
          }

          int width = embedding_dims.d[2];
          int height = embedding_dims.d[1];

        similarity = cosine_similarity(array, embedding_vector, numElements);
        
        id = obj_meta->object_id;
        printf("class_id: %d \n", id);
        printf("Similarity: %f \n", similarity);



        }}


      NvDsObjectMeta *obj = (NvDsObjectMeta *) l_obj->data;
      gint class_index = obj->class_id;
      NvDsGieConfig *gie_config = NULL;
      


      
  




      if (obj->unique_component_id ==
          (gint) appCtx->config.primary_gie_config.unique_id) {
        gie_config = &appCtx->config.primary_gie_config;
      } else {
        for (gint i = 0; i < (gint) appCtx->config.num_secondary_gie_sub_bins;
            i++) {
          gie_config = &appCtx->config.secondary_gie_sub_bin_config[i];
          if (obj->unique_component_id == (gint) gie_config->unique_id) {
            break;
          }
          gie_config = NULL;
        }
      }
      g_free (obj->text_params.display_text);
      obj->text_params.display_text = NULL;

      g_free (obj_meta->text_params.display_text);
      obj_meta->text_params.display_text = NULL;



      if (gie_config != NULL) {
        if (g_hash_table_contains (gie_config->bbox_border_color_table,
                class_index + (gchar *) NULL)) {
          obj->rect_params.border_color = *((NvOSD_ColorParams *)
              g_hash_table_lookup (gie_config->bbox_border_color_table,
                  class_index + (gchar *) NULL));
        } else {
          obj->rect_params.border_color = gie_config->bbox_border_color;
        }
        obj->rect_params.border_width = appCtx->config.osd_config.border_width;

        if (g_hash_table_contains (gie_config->bbox_bg_color_table,
                class_index + (gchar *) NULL)) {
          obj->rect_params.has_bg_color = 1;
          obj->rect_params.bg_color = *((NvOSD_ColorParams *)
              g_hash_table_lookup (gie_config->bbox_bg_color_table,
                  class_index + (gchar *) NULL));
        } else {
          obj->rect_params.has_bg_color = 0;
        }
      }

      if (!appCtx->show_bbox_text)
        continue;
      
      


      obj->text_params.x_offset = obj->rect_params.left;
      obj->text_params.y_offset = obj->rect_params.top - 30;
      obj->text_params.font_params.font_color =
          appCtx->config.osd_config.text_color;
      obj->text_params.font_params.font_size =
          appCtx->config.osd_config.text_size;
      obj->text_params.font_params.font_name = appCtx->config.osd_config.font;
      if (appCtx->config.osd_config.text_has_bg) {
        obj->text_params.set_bg_clr = 1;
        obj->text_params.text_bg_clr = appCtx->config.osd_config.text_bg_color;
      }

      obj->text_params.display_text = (char *) g_malloc (256);
      obj->text_params.display_text[0] = '\0';
 
      str_ins_pos = obj->text_params.display_text;



 


        // printf("class_id: %d \n", id);
        // printf("Similarity: %f \n", similarity);
        

      if (obj->obj_label[0] != '\0')
        {
        sprintf (str_ins_pos, "%s", obj->obj_label);
        str_ins_pos += strlen (str_ins_pos);
        }
      // printf("\n obj->obj_label: %s \n",obj->obj_label);
      // printf("\n Test2: %d \n",id);

      if (obj->object_id != UNTRACKED_OBJECT_ID) {
        /** object_id is a 64-bit sequential value;
         * but considering the display aesthetic,
         * trimming to lower 32-bits */
        if (appCtx->config.tracker_config.display_tracking_id) {
          guint64 const LOW_32_MASK = 0x00000000FFFFFFFF;
          sprintf (str_ins_pos, " %lu", (obj->object_id & LOW_32_MASK));
          str_ins_pos += strlen (str_ins_pos);

  
     
        }

        if (id == obj->object_id) {
        snprintf(str_ins_pos, 256 - (str_ins_pos - obj->text_params.display_text),
                 " Similarity: %.5f", similarity);
        str_ins_pos += strlen(str_ins_pos);
    }
      
      }}
      for (NvDsMetaList *l_user = obj_meta->obj_user_meta_list; l_user != NULL; l_user = l_user->next) {
          NvDsUserMeta *user_meta = (NvDsUserMeta *)l_user->data;
          if (user_meta->base_meta.meta_type == NVDSINFER_TENSOR_OUTPUT_META) {
            
          NvDsInferTensorMeta *tensor_meta = (NvDsInferTensorMeta *)user_meta->user_meta_data;
                  float *embedding_vector = (float *)tensor_meta->out_buf_ptrs_host[0];

          NvDsInferDims embedding_dims = tensor_meta->output_layers_info[0].inferDims;

        int numElements = 1;
          for (int i = 0; i < embedding_dims.numDims; i++) {
            numElements *= embedding_dims.d[i];
          }

          int width = embedding_dims.d[2];
          int height = embedding_dims.d[1];

        similarity = cosine_similarity(array, embedding_vector, numElements);
        
        id = obj_meta->object_id;
        printf("class_id: %d \n", id);
        printf("Similarity: %f \n", similarity);
        }}

In the for loop of your code, you just get the last similarity value. The previously obtained values are overwritten on the next loop.

What you suggest to edit?

Actually I am displaying with the condition meets the ID:

 if (id == obj->object_id) {
        snprintf(str_ins_pos, 256 - (str_ins_pos - obj->text_params.display_text),
                 " Similarity: %.5f", newSimilarityValue);
        str_ins_pos += strlen(str_ins_pos);
      }

If I print it without the condition, it will display the same result for all objects.

You need to attach the similarity immediately after you get this value. Instead of getting all the values and operating only once outside the for loop in your code.

I am doing that here:

for (NvDsMetaList *l_user = obj_meta->obj_user_meta_list; l_user != NULL; l_user = l_user->next) {
          NvDsUserMeta *user_meta = (NvDsUserMeta *)l_user->data;
          if (user_meta->base_meta.meta_type == NVDSINFER_TENSOR_OUTPUT_META) {
            
          NvDsInferTensorMeta *tensor_meta = (NvDsInferTensorMeta *)user_meta->user_meta_data;
                  float *embedding_vector = (float *)tensor_meta->out_buf_ptrs_host[0];

          NvDsInferDims embedding_dims = tensor_meta->output_layers_info[0].inferDims;

        int numElements = 1;
          for (int i = 0; i < embedding_dims.numDims; i++) {
            numElements *= embedding_dims.d[i];
          }

          int width = embedding_dims.d[2];
          int height = embedding_dims.d[1];

        similarity = cosine_similarity(array, embedding_vector, numElements);
        
        id = obj_meta->object_id;

        // printf("class_id: %d \n", id);
        printf("Similarity: %f \n", similarity);

        }

The output of my code is like:

I want the similarity to be displayed for all objects.

I solved it by creating a map data structure and saved the similarity with the corresponding key.

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