How to include opencv to deepstream-test5-app

How can i access opencv on deepstream test5 app

• Hardware Platform Jetson
• DeepStream 5.0
• JetPack Version 4.4.1

i am use

#include “opencv2/imgproc/imgproc.hpp”
#include “opencv2/highgui/highgui.hpp”

and code below to makefile

ifeq ($(TARGET_DEVICE),aarch64)
	PKGS:= gstreamer-1.0 gstreamer-base-1.0 gstreamer-video-1.0 opencv4
else
	PKGS:= gstreamer-1.0 gstreamer-base-1.0 gstreamer-video-1.0 opencv
endif

i has receive error

In file included from /usr/include/opencv4/opencv2/imgproc.hpp:46:0,
                 from /usr/include/opencv4/opencv2/imgproc/imgproc.hpp:48,
                 from deepstream_test5_app_main.c:55:
/usr/include/opencv4/opencv2/core.hpp:49:4: error: #error core.hpp header must be compiled as C++
 #  error core.hpp header must be compiled as C++
    ^~~~~
In file included from /usr/include/opencv4/opencv2/core.hpp:52:0,
                 from /usr/include/opencv4/opencv2/imgproc.hpp:46,
                 from /usr/include/opencv4/opencv2/imgproc/imgproc.hpp:48,
                 from deepstream_test5_app_main.c:55:
/usr/include/opencv4/opencv2/core/cvdef.h:690:4: error: #error "OpenCV 4.x+ requires enabled C++11 support"
 #  error "OpenCV 4.x+ requires enabled C++11 support"
    ^~~~~
/usr/include/opencv4/opencv2/core/cvdef.h:695:10: fatal error: array: No such file or directory
 #include <array>

help me

Hi,
We have sample code of using OpenCV in dsexample plugins. Please take a look at

/opt/nvidia/deepstream/deepstream-5.0/sources/gst-plugins/gst-dsexample/README
1 Like

Hi, i know dxexample sample code of using OpenCV, but i want to custom bbox_generated_probe_after_analytics() in deepstream-test5-app to save object become image file and put path of this image to generate_event_msg_meta()
thank you

i have seen path code at Uridecodebin with filesink

#if 1
  static int dump = 0;
  int idx = 1;
  if (dump < 150) {
    GstMapInfo in_map_info;
    NvBufSurface *surface = NULL, ip_surf;

    memset (&in_map_info, 0, sizeof (in_map_info));
    if (!gst_buffer_map (buf, &in_map_info, GST_MAP_READ)) {
      g_print ("Error: Failed to map gst buffer\n");
      gst_buffer_unmap (buf, &in_map_info);
      return GST_PAD_PROBE_OK;
    }
    cudaError_t cuda_err;

    NvBufSurfTransformRect src_rect, dst_rect;
    surface = (NvBufSurface *) in_map_info.data;

    ip_surf = *surface;

    ip_surf.numFilled = ip_surf.batchSize = 1;
    ip_surf.surfaceList = &(surface->surfaceList[idx]);
  
    int batch_size= surface->batchSize;
    printf("\nBatch Size : %d, resolution : %dx%d \n",batch_size,
        surface->surfaceList[idx].width, surface->surfaceList[idx].height);

    src_rect.top   = 0;
    src_rect.left  = 0;
    src_rect.width = (guint) surface->surfaceList[idx].width;
    src_rect.height= (guint) surface->surfaceList[idx].height;

    dst_rect.top   = 0;
    dst_rect.left  = 0;
    dst_rect.width = (guint) surface->surfaceList[idx].width;
    dst_rect.height= (guint) surface->surfaceList[idx].height;

    NvBufSurfTransformParams nvbufsurface_params;
    nvbufsurface_params.src_rect = &src_rect;
    nvbufsurface_params.dst_rect = &dst_rect;
    nvbufsurface_params.transform_flag =  NVBUFSURF_TRANSFORM_CROP_SRC | NVBUFSURF_TRANSFORM_CROP_DST;
    nvbufsurface_params.transform_filter = NvBufSurfTransformInter_Default;
  
    NvBufSurface *dst_surface = NULL;
    NvBufSurfaceCreateParams nvbufsurface_create_params;

    /* An intermediate buffer for NV12/RGBA to BGR conversion  will be
     * required. Can be skipped if custom algorithm can work directly on NV12/RGBA. */
    nvbufsurface_create_params.gpuId  = surface->gpuId;
    nvbufsurface_create_params.width  = (gint) surface->surfaceList[idx].width;
    nvbufsurface_create_params.height = (gint) surface->surfaceList[idx].height;
    nvbufsurface_create_params.size = 0;
    nvbufsurface_create_params.colorFormat = NVBUF_COLOR_FORMAT_RGBA;
    nvbufsurface_create_params.layout = NVBUF_LAYOUT_PITCH;
    nvbufsurface_create_params.memType = NVBUF_MEM_DEFAULT;

    cuda_err = cudaSetDevice (surface->gpuId);

    cudaStream_t cuda_stream;

    cuda_err=cudaStreamCreate (&cuda_stream);

    int create_result = NvBufSurfaceCreate(&dst_surface,1,&nvbufsurface_create_params);	

    NvBufSurfTransformConfigParams transform_config_params;
    NvBufSurfTransform_Error err;

    transform_config_params.compute_mode = NvBufSurfTransformCompute_Default;
    transform_config_params.gpu_id = surface->gpuId;
    transform_config_params.cuda_stream = cuda_stream;
    err = NvBufSurfTransformSetSessionParams (&transform_config_params);

    NvBufSurfaceMemSet (dst_surface, 0, 0, 0);
    err = NvBufSurfTransform (&ip_surf, dst_surface, &nvbufsurface_params);
    if (err != NvBufSurfTransformError_Success) {
  	  g_print ("NvBufSurfTransform failed with error %d while converting buffer\n", err);
    }
    NvBufSurfaceMap (dst_surface, 0, 0, NVBUF_MAP_READ);
    NvBufSurfaceSyncForCpu (dst_surface, 0, 0);

    cv::Mat bgr_frame = cv::Mat (cv::Size(nvbufsurface_create_params.width, nvbufsurface_create_params.height), CV_8UC3);

    cv::Mat in_mat =
        cv::Mat (nvbufsurface_create_params.height, nvbufsurface_create_params.width,
        CV_8UC4, dst_surface->surfaceList[0].mappedAddr.addr[0],
        dst_surface->surfaceList[0].pitch);

    cv::cvtColor (in_mat, bgr_frame, CV_RGBA2BGR);

    char filename[64];
    snprintf(filename, 64, "/tmp/image%03d.jpg", dump);
    cv::imwrite(filename,bgr_frame);
    dump ++;

    NvBufSurfaceUnMap (dst_surface, 0, 0);
    NvBufSurfaceDestroy (dst_surface);
    cudaStreamDestroy (cuda_stream);
    gst_buffer_unmap (buf, &in_map_info);
  }
#endif

i try to make it, but get error above

Makefile

CUDA_VER?=10.2
APP:= deepstream-test5-app

#TARGET_DEVICE = $(shell gcc -dumpmachine | cut -f1 -d -)
TARGET_DEVICE = $(shell gcc -dumpmachine | cut -f1 -d -)
CXX:= g++

NVDS_VERSION:=5.0

LIB_INSTALL_DIR?=/opt/nvidia/deepstream/deepstream-$(NVDS_VERSION)/lib/
APP_INSTALL_DIR?=/opt/nvidia/deepstream/deepstream-$(NVDS_VERSION)/bin/

ifeq ($(TARGET_DEVICE),aarch64)
  CFLAGS:= -DPLATFORM_TEGRA
endif

SRCS:= deepstream_test5_app_main.c deepstream_utc.c
SRCS+= ../deepstream-app/deepstream_app.c ../deepstream-app/deepstream_app_config_parser.c
SRCS+= $(wildcard ../../apps-common/src/*.c)

INCS:= $(wildcard *.h)

PKGS:= gstreamer-1.0 gstreamer-video-1.0 x11 json-glib-1.0 opencv4

OBJS:= $(SRCS:.c=.o)

CFLAGS+= -I../../apps-common/includes -I../../../includes -I../deepstream-app/ -DDS_VERSION_MINOR=0 -DDS_VERSION_MAJOR=5 -I /usr/local/cuda-$(CUDA_VER)/include \

LIBS+= -L$(LIB_INSTALL_DIR) -lnvdsgst_meta -lnvds_meta -lnvbufsurface -lnvbufsurftransform -lnvdsgst_helper -lnvdsgst_smartrecord -lnvds_utils -lnvds_msgbroker -lm \
       -lgstrtspserver-1.0 -ldl -Wl,-rpath,$(LIB_INSTALL_DIR) -L/usr/local/cuda-$(CUDA_VER)/lib64/ -lcudart -ldl \

CFLAGS+= `pkg-config --cflags $(PKGS)`

LIBS+= `pkg-config --libs $(PKGS)`

all: $(APP)

%.o: %.c $(INCS) Makefile
	$(CC) -c -o $@ $(CFLAGS) $<

$(APP): $(OBJS) Makefile
	$(CC) -o $(APP) $(OBJS) $(LIBS)

install: $(APP)
	cp -rv $(APP) $(APP_INSTALL_DIR)

clean:
	rm -rf $(OBJS) $(APP)

help me

can you help me

Hi,
It looks like you need to add -std=c++11. You may add it and try again.

1 Like

sorry, where i need put it in make file

Hi,
Another user has shared a Makefile. Please take a look at:
RTSP camera access frame issue
The user uses g++ instead of gcc. You can try this.

i had try it but not worked, this user use opencv not opencv4
i has refer to make file of gst-dsexample but not solve the error

Hi,
It works for us by adding/modifying

TARGET_DEVICE = $(shell g++ -dumpmachine | cut -f1 -d -)
CFLAGS += -fpermissive -Wnarrowing
CC := g++

Works for both opencv and opencv4.

1 Like

are you can share for me your make file

i get the error with that modify

In function ‘void* meta_copy_func(gpointer, gpointer)’:
deepstream_test5_app_main.c:340:22: error: invalid conversion from ‘gpointer {aka void*}’ to ‘NvDsEventMsgMeta*’ [-fpermissive]
   dstMeta = g_memdup (srcMeta, sizeof (NvDsEventMsgMeta));
             ~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
deepstream_test5_app_main.c:346:48: error: invalid conversion from ‘gpointer {aka void*}’ to ‘gdouble* {aka double*}’ [-fpermissive]
     dstMeta->objSignature.signature = g_memdup (srcMeta->objSignature.signature,
                                       ~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
         srcMeta->objSignature.size);
         ~~~~~~~~~~~~~~~~~~~~~~~~~~~             
deepstream_test5_app_main.c: In function ‘void bbox_generated_probe_after_analytics(AppCtx*, GstBuffer*, NvDsBatchMeta*, guint)’:
deepstream_test5_app_main.c:593:42: error: invalid conversion from ‘gpointer {aka void*}’ to ‘NvDsFrameMeta* {aka _NvDsFrameMeta*}’ [-fpermissive]
     NvDsFrameMeta *frame_meta = l_frame->data;
                                 ~~~~~~~~~^~~~
deepstream_test5_app_main.c: In function ‘int main(int, char**)’:
deepstream_test5_app_main.c:1390:36: error: invalid conversion from ‘int’ to ‘GFileTest’ [-fpermissive]
             G_FILE_TEST_IS_REGULAR | G_FILE_TEST_IS_SYMLINK))
             ~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
In file included from /usr/include/glib-2.0/glib.h:48:0,
                 from /usr/include/gstreamer-1.0/gst/gst.h:27,
                 from deepstream_test5_app_main.c:23:
/usr/include/glib-2.0/glib/gfileutils.h:82:10: note:   initializing argument 2 of ‘gboolean g_file_test(const gchar*, GFileTest)’
 gboolean g_file_test         (const gchar  *filename,
          ^~~~~~~~~~~
Makefile:61: recipe for target 'deepstream_test5_app_main.o' failed

are you add header of opencv to test5 app ?

#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"

Hi,
Please add

CFLAGS += -fpermissive -Wnarrowing

i get warring

deepstream_test5_app_main.c: In function ‘void* meta_copy_func(gpointer, gpointer)’:
deepstream_test5_app_main.c:340:22: warning: invalid conversion from ‘gpointer {aka void*}’ to ‘NvDsEventMsgMeta*’ [-fpermissive]
   dstMeta = g_memdup (srcMeta, sizeof (NvDsEventMsgMeta));
             ~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
deepstream_test5_app_main.c:346:48: warning: invalid conversion from ‘gpointer {aka void*}’ to ‘gdouble* {aka double*}’ [-fpermissive]
     dstMeta->objSignature.signature = g_memdup (srcMeta->objSignature.signature,
                                       ~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
         srcMeta->objSignature.size);
         ~~~~~~~~~~~~~~~~~~~~~~~~~~~             
deepstream_test5_app_main.c: In function ‘void bbox_generated_probe_after_analytics(AppCtx*, GstBuffer*, NvDsBatchMeta*, guint)’:
deepstream_test5_app_main.c:593:42: warning: invalid conversion from ‘gpointer {aka void*}’ to ‘NvDsFrameMeta* {aka _NvDsFrameMeta*}’ [-fpermissive]
     NvDsFrameMeta *frame_meta = l_frame->data;
                                 ~~~~~~~~~^~~~
deepstream_test5_app_main.c: In function ‘int main(int, char**)’:
deepstream_test5_app_main.c:1390:36: warning: invalid conversion from ‘int’ to ‘GFileTest’ [-fpermissive]
             G_FILE_TEST_IS_REGULAR | G_FILE_TEST_IS_SYMLINK))
             ~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
In file included from /usr/include/glib-2.0/glib.h:48:0,
                 from /usr/include/gstreamer-1.0/gst/gst.h:27,
                 from deepstream_test5_app_main.c:23:
/usr/include/glib-2.0/glib/gfileutils.h:82:10: note:   initializing argument 2 of ‘gboolean g_file_test(const gchar*, GFileTest)’
 gboolean g_file_test         (const gchar  *filename,
          ^~~~~~~~~~~

thank you

hello @DaneLLL
i flow this post Uridecodebin with filesink why has no image save

please help me how to get object frame in

 bbox_generated_probe_after_analytics (AppCtx * appCtx, GstBuffer * buf, NvDsBatchMeta * batch_meta, guint index)

of deepstream_test5_app_main.c, thank so much