Make deepstream-app save cropped images like deepstream-image-meta-test

Hello I’m trying to make deepstream-app to crop and save images like what deepstream-image-meta-test do.

What I’ve done so far :
added the dependencies to code and makefile.
and this is process_meta after editing.

static void
process_meta (AppCtx * appCtx, NvDsBatchMeta * batch_meta)
{
  gpointer *ctx = (gpointer *) appCtx;
  guint person_count = 0;
  NvDsObjectMeta *obj = NULL;
  char folder_name[] = "images";
  struct stat st = {0};
  if (stat(folder_name, &st) == -1) {
    mkdir(folder_name, 0700);
    }
  GstMapInfo inmap = GST_MAP_INFO_INIT;
  NvBufSurface *ip_surf = (NvBufSurface *) inmap.data;

  for (NvDsMetaList * l_frame = batch_meta->frame_meta_list; l_frame != NULL;
      l_frame = l_frame->next) {
    
    guint num_rects = 0;

    if(frame_number%1==0){
    /* Create sub directory for this time stamp */
    char time_stamp[50];
    snprintf(time_stamp, 50,"%s//%d",folder_name,(int)floor(frame_number) );
    if (stat(time_stamp, &st) == -1) {
      mkdir(time_stamp, 0700);
      }
    }
    // g_print ("frame(%d): ",frame_number );
    NvDsFrameMeta *frame_meta = l_frame->data;

    for (NvDsMetaList * l_obj = frame_meta->obj_meta_list; l_obj != NULL;
        l_obj = l_obj->next) {
      
      obj = (NvDsObjectMeta *) l_obj->data;
      
      
      gint class_index = obj->class_id;

      NvDsGieConfig *gie_config = NULL;
      gchar *str_ins_pos = 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;

      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;
        }
      }
      // g_print("testing ...");
      if (obj->class_id == PGIE_CLASS_ID_PERSON) {
        // g_print("OBJECT ID : %d ",obj->class_id );
        person_count++;
        people_num++;
        num_rects++;
        NvDsObjEncUsrArgs userData = { 0 };
        /* To be set by user */
        userData.saveImg = save_img;
        userData.attachUsrMeta = attach_user_meta;
        /* Set if Image scaling Required */
        userData.scaleImg = FALSE;
        userData.scaledWidth = 0;
        userData.scaledHeight = 0;
        /* Preset */
        userData.objNum = num_rects;
        char fileNameString[FILE_NAME_SIZE];
        snprintf( fileNameString, 50, "%s//%d//%d_%d_%d_%d_%d_%d.jpg",folder_name,(int)floor(frame_number), frame_meta->source_id,(int)obj->rect_params.left,(int)obj->rect_params.top,(int)obj->rect_params.width,(int)obj->rect_params.height,person_count);
        strcpy(userData.fileNameImg, fileNameString);
        // g_print(fileNameString);
        /*Main Function Call */
        // nvds_obj_enc_process (ctx, &userData, ip_surf, obj, frame_meta);
      }
       
    }
  }
  frame_number++;
  // nvds_obj_enc_finish (ctx);
  // g_print ("There are (%d) people ",people_num );
}

However now whenever I uncomment “nvds_obj_enc_process” and “nvds_obj_enc_finish” the app freezes and doesn’t save any images.

What I’m doing wrong here or what am I missing.
Thanks

• Hardware Platform ( GPU )
**• DeepStream Version 6.0 **
**• TensorRT Version 8.2.3-1+cuda11.4 **
• NVIDIA GPU Driver Version (470.129.06)

deepstream_app.c (51.7 KB)

Here is the code, I tried to use and replicate what Running image save in deepstream5 app issue

and Running image save in deepstream5 app issue

tried to do, I dont get segmentation fault, the app just freezes at the function nvds_obj_enc_process

Hi, @a7medhermas.
Can you run deepstream-image-meta-test app well and save the image successfully?
Cause you uncommand 2 functions, Could you help to confirm which function that cause the issue, “nvds_obj_enc_process” or “nvds_obj_enc_finish”?

In your code num_rect should be set to 0 inside each frame (now it’s done once for a batch).
Generally, it looks a bit overcomplicated and patchy so anything can cause issues as those two functions are very sensitive to context.

1 Like

Hello sir,
Yes, image_meta_test app is running with no issues.
“nvds_obj_enc_process” is what causes the app to freeze.

Hi, @a7medhermas .
Since image_meta_test app is OK in your env. The root reason maybe the API how you use. Here are some ways you can check:

1.You should ensure the sequnce of the 4 fucntions (nvds_obj_enc_create_context, nvds_obj_enc_process, nvds_obj_enc_finish, nvds_obj_enc_destroy_context), you can refer to deepstream_image_meta_test.c and
https://docs.nvidia.com/metropolis/deepstream/sdk-api/group__ee__object__encoder.html

2.Print the metadata info what you use in the 4 functions and the deepstream_image_meta_test.c use,then compare them. (e.g., the image color format metadata and so on)

There is no update from you for a period, assuming this is not an issue anymore.
Hence we are closing this topic. If need further support, please open a new one.
Thanks

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