Hello @Fiona.Chen , thank you for your replies.
I’m trying to access the NvDsInferSegmentationMeta in my osd_sink_pad_buffer_probe
function, it is my interest to access the segmentation output in OpenCV. If you look at my code, where could I find the metadata of the mask?
With this code, I’m able to access the original pipeline frames in OpenCV. I’d like to access the segmentation output with OpenCV too, or if not possible in OpenCV some other way to access it.
static GstPadProbeReturn osd_sink_pad_buffer_probe (GstPad * pad, GstPadProbeInfo * info, gpointer u_data){
GstBuffer *buf = (GstBuffer *) info->data;
guint num_rects = 0;
NvDsObjectMeta *obj_meta = NULL;
guint vehicle_count = 0;
guint person_count = 0;
NvDsMetaList * l_frame = NULL;
NvDsMetaList * l_obj = NULL;
NvDsDisplayMeta *display_meta = NULL;
NvDsFrameMeta *frame_meta = NULL;
NvDsBatchMeta *batch_meta = gst_buffer_get_nvds_batch_meta (buf);
GstMapInfo in_map_info;
NvBufSurface *surface = NULL;
memset (&in_map_info, 0, sizeof (in_map_info));
if (gst_buffer_map (buf, &in_map_info, GST_MAP_READWRITE)) {
surface = (NvBufSurface *) in_map_info.data;
NvBufSurfaceMap(surface, -1, -1, NVBUF_MAP_READ_WRITE);
NvBufSurfaceSyncForCpu(surface, -1, -1);
batch_meta = gst_buffer_get_nvds_batch_meta(buf);
for (l_frame = batch_meta->frame_meta_list; l_frame != NULL; l_frame = l_frame->next) {
frame_meta = (NvDsFrameMeta *) (l_frame->data);
int offset = 0;
NvDsFrameMeta *usr_meta = (NvDsFrameMeta *) frame_meta->frame_user_meta_list->data; // Here Im trying to access the segmentation output
gint frame_width = (gint) surface->surfaceList[frame_meta->batch_id].width;
gint frame_height = (gint) surface->surfaceList[frame_meta->batch_id].height;
#ifdef PLATFORM_TEGRA
NvBufSurfTransformRect src_rect;
NvBufSurfTransformRect dst_rect;
NvBufSurfTransformParams transform_params;
NvBufSurface ip_surf;
ip_surf = *surface;
ip_surf.numFilled = ip_surf.batchSize = 1;
ip_surf.surfaceList = &(surface->surfaceList[frame_meta->batch_id]);
NvOSD_RectParams rect_params;
rect_params.left = 0;
rect_params.top = 0;
rect_params.width = frame_width;
rect_params.height = frame_height;
gint src_left = GST_ROUND_UP_2((unsigned int)rect_params.left);
gint src_top = GST_ROUND_UP_2((unsigned int)rect_params.top);
gint src_width = GST_ROUND_DOWN_2((unsigned int)rect_params.width);
gint src_height = GST_ROUND_DOWN_2((unsigned int)rect_params.height);
/* Maintain aspect ratio */
double hdest = frame_width * src_height / (double) src_width;
double wdest = frame_height * src_width / (double) src_height;
guint dest_width, dest_height;
if (hdest <= frame_height) {
dest_width = frame_width;
dest_height = hdest;
} else {
dest_width = wdest;
dest_height = frame_height;
}
src_rect = {(guint)src_top, (guint)src_left, (guint)src_width, (guint)src_height};
dst_rect = {0, 0, (guint)dest_width, (guint)dest_height};
if (NvBufSurfaceMapEglImage(surface, frame_meta->batch_id) != 0){
cout << "ERROR on converting the EGL Buffer." <inter_buf->surfaceList[0].mappedAddr.eglImage
// Use interop APIs cuGraphicsEGLRegisterImage and
// cuGraphicsResourceGetMappedEglFrame to access the buffer in CUDA
static bool create_filter = true;
static cv::Ptr< cv::cuda::Filter > filter;
CUresult status;
CUeglFrame eglFrame;
CUgraphicsResource pResource = NULL;
cudaFree(0);
status = cuGraphicsEGLRegisterImage(&pResource, surface->surfaceList[0].mappedAddr.eglImage,
CU_GRAPHICS_MAP_RESOURCE_FLAGS_NONE);
status = cuGraphicsResourceGetMappedEglFrame(&eglFrame, pResource, 0, 0);
status = cuCtxSynchronize();
if (create_filter) {
filter = cv::cuda::createSobelFilter(CV_8UC4, CV_8UC4, 1, 0, 3, 1,
cv::BORDER_DEFAULT);
//filter = cv::cuda::createGaussianFilter(CV_8UC4, CV_8UC4, cv::Size(31,31), 0, 0, cv::BORDER_DEFAULT);
create_filter = false;
}
cv::cuda::GpuMat d_mat(frame_height, frame_width, CV_8UC4, eglFrame.frame.pPitch[0]);
cv::cuda::GpuMat d_mat_out;
filter->apply (d_mat, d_mat_out);
cv::Mat cpuDstMat;
//d_mat.download(cpuDstMat);
d_mat_out.download(cpuDstMat);
string filename = "cv_frames/frame_" + to_string(frame_number) + ".jpg";
cv::imwrite(filename, cpuDstMat);
status = cuCtxSynchronize();
status = cuGraphicsUnregisterResource(pResource);
// apply back to the original buffer
transform_params.src_rect = &dst_rect;
transform_params.dst_rect = &src_rect;
NvBufSurfTransform (surface, &ip_surf, &transform_params);
// Destroy the EGLImage
NvBufSurfaceUnMapEglImage (surface, 0);
#else
cv::cuda::GpuMat frame;
frame = cv::cuda::GpuMat(frame_height, frame_width, CV_8UC4,
(void *) surface->surfaceList[frame_meta->batch_id].dataPtr,
surface->surfaceList[frame_meta->batch_id].pitch);
cv::Mat src_mat_BGRA;
frame.download(src_mat_BGRA);
cv::cvtColor(src_mat_BGRA, src_mat_BGRA, COLOR_RGBA2BGR);
string filename = "cv_frames/frame_" + to_string(frame_number) + ".jpg";
cv::imwrite(filename, src_mat_BGRA);
#endif
for (l_obj = frame_meta->obj_meta_list; l_obj != NULL;
l_obj = l_obj->next) {
obj_meta = (NvDsObjectMeta *) (l_obj->data);
if (obj_meta->class_id == PGIE_CLASS_ID_VEHICLE) {
vehicle_count++;
num_rects++;
}
if (obj_meta->class_id == PGIE_CLASS_ID_PERSON) {
person_count++;
num_rects++;
}
}
display_meta = nvds_acquire_display_meta_from_pool(batch_meta);
NvOSD_TextParams *txt_params = &display_meta->text_params[0];
display_meta->num_labels = 1;
txt_params->display_text = static_cast(g_malloc0(MAX_DISPLAY_LEN));
offset = snprintf(txt_params->display_text, MAX_DISPLAY_LEN, "Person = %d ", person_count);
offset = snprintf(txt_params->display_text + offset, MAX_DISPLAY_LEN, "Vehicle = %d ", vehicle_count);
/* Now set the offsets where the string should appear */
txt_params->x_offset = 10;
txt_params->y_offset = 12;
/* Font , font-color and font-size */
txt_params->font_params.font_name = "Serif";
txt_params->font_params.font_size = 10;
txt_params->font_params.font_color.red = 1.0;
txt_params->font_params.font_color.green = 1.0;
txt_params->font_params.font_color.blue = 1.0;
txt_params->font_params.font_color.alpha = 1.0;
/* Text background color */
txt_params->set_bg_clr = 1;
txt_params->text_bg_clr.red = 0.0;
txt_params->text_bg_clr.green = 0.0;
txt_params->text_bg_clr.blue = 0.0;
txt_params->text_bg_clr.alpha = 1.0;
nvds_add_display_meta_to_frame(frame_meta, display_meta);
}
NvBufSurfaceUnMap(surface, -1, -1);
}
gst_buffer_unmap (buf, &in_map_info);
g_print ("Frame Number = %d Number of objects = %d "
"Vehicle Count = %d Person Count = %d\n",
frame_number, num_rects, vehicle_count, person_count);
frame_number++;
return GST_PAD_PROBE_OK;
}
Thanks in advance!