OpenCV + CUDA on Jetson Nano

Hi! Ive been trying to modify some Gstreamer Plugins (mainly cvsmooth) so it does some other stuff too but mainly for it to be accelerated on the Jetson Nano’s GPU

Ive installed OpenCV 4.5 with CUDA support

pkg-config --modversion opencv

The plugin compiles with no problem but whenever i try to run it i get this:

GST_ARGUS: Setup Complete, Starting captures for 0 seconds
GST_ARGUS: Starting repeat capture requests.
CONSUMER: Producer has connected; continuing.
terminate called after throwing an instance of 'cv::Exception'
  what():  OpenCV(4.1.1) /home/nvidia/host/build_opencv/nv_opencv/modules/core/include/opencv2/core/private.cuda.hpp:107: error: (-216:No CUDA support) The library is compiled without CUDA support in function 'throw_no_cuda'

Seems the opencv-4.1.1 without CUDA support shipped with JetPack is used instead of your own opencv-4.5.0 build.

Your would build your plugin using:

-I <path_to_opencv-4.5.0>/include/opencv4
-L <path_to_opencv-4.5.0>/lib

or use pkg-config if it gives correct 4.5.0 flags.

Then, for execution, you would set environment variable LD_LIBRARY_PATH before running:

export LD_LIBRARY_PATH=/usr/local/cuda/lib64:<path_to_opencv-4.5.0>/lib
gst-launch-1.0 <your pipeline_with_cvsmooth>

I still dont get it to work

I cloned and built opencv in the home directory and added it to LD_LIBRAY_PATH

echo $LD_LIBRARY_PATH                                    

Also i get this when i search for opencv libraries

dpkg -l | grep libopencv
ii  libopencv                                     4.1.1-2-gd5a58aa75                               arm64        Open Computer Vision Library
ii  libopencv-core3.2:arm64                       3.2.0+dfsg-4ubuntu0.1                            arm64        computer vision core library
ii  libopencv-dev                                 4.1.1-2-gd5a58aa75                               arm64        Development files for Open Source Computer Vision Library
ii  libopencv-python                              4.1.1-2-gd5a58aa75                               arm64        Python bindings for Open Source Computer Vision Library
ii  libopencv-samples                             4.1.1-2-gd5a58aa75                               arm64        Samples for Open Source Computer Vision Library

Contents of /home/devkit/opencv/build/lib

libade.a                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    python3             

Also i think this is important, since im modifying an existing plugin im using meson and ninja to build it. Im really new to meson and ninja, maybe im missing something there but i wouldnt know where to start.

When you’ve configured your opencv build with cmake, it has set CMAKE_INSTALL_PREFIX (for example /usr/local).
After building opencv, when you run:

sudo make install

…your opencv build will be installed at that location. pkg-config would use that location for opencv flags.
Check with:

pkg-config opencv4 --cflags --libs

I cannot tell about meson/ninja build.

I’m sure that if building your app or plugin with correct include paths and libs paths, and set these in LD_LIBRARY_PATH as you did it should work…for a C++ application or plugin.

Note that running from python may be different. If this is your case, try to display the opencv version that your python install uses:

Python 2.7.17 (default, Jul 20 2020, 15:37:01) 
[GCC 7.5.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
>>> print(cv2.__version__)

This is the output i get from pkg-config opencv4 --cflags --libs

-I/usr/include/opencv4/opencv -I/usr/include/opencv4 -lopencv_dnn -lopencv_gapi -lopencv_highgui -lopencv_ml -lopencv_objdetect -lopencv_photo -lopencv_stitching -lopencv_video -lopencv_calib3d -lopencv_features2d -lopencv_flann -lopenc
v_videoio -lopencv_imgcodecs -lopencv_imgproc -lopencv_core

And as far as python goes:

Python 3.6.9 (default, Jul 17 2020, 12:50:27) 
[GCC 8.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
>>> print(cv2.__version__)

Looks ok so far, but you’ve installed into /usr where the opencv-4.1.1 was. Also noted some 3.2 version above. Did you purge before ?
I use to install into /usr/local/my-version to get rid of that.
What gives:

ls -l /usr/include/opencv4/opencv2
ls /usr/lib/libopencv*

if you see mixed files (dates in headers, or suffixs for libs), you would purge and reinstall.