Drawn rectangle is not available on encoded frame

I am using deepstream version 5.0.
I am trying to draw rectangles on the frame using NvDsDisplayMeta and I can see these rectangles on eglsink display but when I am trying to encode this frame and write to the disk then those images don’t contain these drawn rectangles.

Below is the code

/**
 * Called when element recieves an input buffer from upstream element.
 */
static GstFlowReturn gst_processor_transform_ip (GstBaseTransform * btrans, GstBuffer * inbuf)
{
GstProcessor *processor = GST_PROCESSOR (btrans);
GstMapInfo in_map_info;
GstFlowReturn flow_ret = GST_FLOW_ERROR;
gdouble scale_ratio = 1.0;

NvBufSurface *surface = NULL;
NvDsBatchMeta *batch_meta = NULL;
NvDsFrameMeta *frame_meta = NULL;
NvDsMetaList * l_frame = NULL;
guint i = 0;

processor->frame_num++;
CHECK_CUDA_STATUS (cudaSetDevice (processor->gpu_id),
                   "Unable to set cuda device");

memset (&in_map_info, 0, sizeof (in_map_info));
if (!gst_buffer_map (inbuf, &in_map_info, GST_MAP_READ)) {
    g_print ("Error: Failed to map gst buffer\n");
    goto error;
}

surface = (NvBufSurface *) in_map_info.data;

if (CHECK_NVDS_MEMORY_AND_GPUID (processor, surface))
    goto error;

batch_meta = gst_buffer_get_nvds_batch_meta (inbuf);
if (batch_meta == nullptr) {
    GST_ELEMENT_ERROR (processor, STREAM, FAILED,
                       ("NvDsBatchMeta not found for input buffer."), (NULL));
    return GST_FLOW_ERROR;
}

for (l_frame = batch_meta->frame_meta_list; l_frame != NULL; l_frame = l_frame->next) 
{
    frame_meta = (NvDsFrameMeta *) (l_frame->data);

    // process frame_meta for detections

    //remove all previous objects
    nvds_clear_obj_meta_list(frame_meta, frame_meta->obj_meta_list);

    NvDsBatchMeta *frame_batch_meta = frame_meta->base_meta.batch_meta;

    NvDsDisplayMeta * display_meta = nvds_acquire_display_meta_from_pool(frame_batch_meta); 
    NvDsObjectMeta *object_meta = nvds_acquire_obj_meta_from_pool(frame_batch_meta);

    NvOSD_RectParams * rect_params = NULL;

  **// Code to draw rectangles on  the image.**

    rect_params = &display_meta->rect_params[0];
    rect_params->left = 100;
    rect_params->top = 100;
    rect_params->width = 300;
    rect_params->height = 300;
    rect_params->has_bg_color = 0;
    rect_params->bg_color = (NvOSD_ColorParams) { 1, 1, 0, 0.4 };
    rect_params->border_width = 10;
    rect_params->border_color = (NvOSD_ColorParams) {1, 0.03921568627, 0.05882352941, 0.98};;

    display_meta->num_rects = 1;

    nvds_add_display_meta_to_frame(frame_meta, display_meta);

    auto enc_ctx_handle = nvds_obj_enc_create_context();

    NvOSD_RectParams & encoding_params = object_meta->rect_params;
    encoding_params.left = 5;
    encoding_params.top = 5;
    encoding_params.width = 500
    encoding_params.height = 500;
    encoding_params.has_bg_color = 0;
    encoding_params.bg_color = (NvOSD_ColorParams) { 1, 1, 0, 0.4 };
    encoding_params.border_width = 2;
    encoding_params.border_color = (NvOSD_ColorParams) {1, 0.03921568627, 0.05882352941, 0.98};

    **// Code to encode and write this frame to disk but rectangles aren't in the image.**

    nvds_add_obj_meta_to_frame(frame_meta, object_meta, NULL);

    NvDsObjEncUsrArgs userData = { 0 };
    userData.saveImg = TRUE;
    userData.attachUsrMeta = TRUE;
    userData.objNum = 1;
    std::string save_path = "user_data/EventImages/basic/basic_" + std::to_string(rand()) + ".jpg";
    strcpy(userData.fileNameImg, save_path.c_str());
    nvds_obj_enc_process(enc_ctx_handle, &userData, surface, object_meta, frame_meta);
    nvds_obj_enc_finish(enc_ctx_handle);

    i++;
}
flow_ret = GST_FLOW_OK;
error:
gst_buffer_unmap (inbuf, &in_map_info);
return flow_ret;

}

Any idea what I am doing wrong here?

Where did you add these code?

@bcao I have update my question with minimum code.

Oh, the rectangle is drawned by nvdsosd, so that’s why you cannot see them in the upstream osd companents.

@bcao how can I draw rectangles on the encoded frame?
And how does nvdsosd draw rectangles. Does it make a copy of the encoded frame to draw?

OSD has done that when you add the display meta

you just need to install a probe on the osd src pad, and save the image in the probe, refer deepstream_image_meta_test.c