Unable to install opencv with CUDA in Jetson Nano

I am trying to install opencv with CUDA in Jetson Nano device but I get the following error shown below. I am running the shell script given in this link:
https://github.com/jetsonhacks/buildOpenCVTX1

Error:
*** No rule to make target ‘/usr/lib/aarch64-linux-gnu/libGL.so’, needed by ‘lib/libopencv_cudev.so.3.4.1’. Stop.

I’d suggest to use the script from here. It will automatically install required tools or libraries, download opencv-4.0.0, build and install.

1 Like

Thank you!!!

I managed to install opencv using above script
Unfortunately the samples es cpp were not installed.
I tried cmake with BUILD_EXAMPLES defined but cmake failed.

Error log shows
/usr/share/opencvinstall/opencv-4.0.0/build/CMakeFiles/CMakeTmp/CheckIncludeFile.c:1:10: fatal error: sys/videoio.h: No such file or directory

Any advise will be greatly appreciated

You might want to check the post I replied to. Your problem is most likely not related to a library. And it’s not possible to successfully compile without swap on the nano.

Thanks
We had added 5G of swap space when we tried
cmake -D BUILD_EXAMPLES=1 …
and got above error in the error log
thanks

You may post the command you’ve used for configuring with cmake.
If you’ve set INSTALL_C_EXAMPLES to ON, you may try if disabling this (-D INSTALL_C_EXAMPLES=OFF) improves.

Thanks for your assistance.
Final cmake
richard-desktop:/usr/share/opencvinstall/opencv-4.0.0/build# cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D WITH_CUDA=ON -D ENABLE_FAST_MATH=1 -D CUDA_FAST_MATH=1 -D WITH_CUBLAS=1 -D INSTALL_PYTHON_EXAMPLES=ON -D OPENCV_EXTRA_MODULES_PATH=…/…/opencv_contrib-3.1.0/modules -D BUILD_EXAMPLES=ON -D WITH_V4L=OFF -D INSTALL_C_EXAMPLES=OFF …

Result Tail

Python 3:
– Interpreter: /usr/bin/python3 (ver 3.6.7)
– Libraries: /usr/lib/aarch64-linux-gnu/libpython3.6m.so (ver 3.6.7)
– numpy: /root/.local/lib/python3.6/site-packages/numpy/core/include (ver 1.16.2)
– packages path: lib/python3.6/dist-packages

– Python (for build): /usr/bin/python2.7

– Java:
– ant: NO
– JNI: NO
– Java wrappers: NO
– Java tests: NO

– Install to: /usr/local



– Configuring incomplete, errors occurred!
See also “/usr/share/opencvinstall/opencv-4.0.0/build/CMakeFiles/CMakeOutput.log”.
See also “/usr/share/opencvinstall/opencv-4.0.0/build/CMakeFiles/CMakeError.log”.

Seems your are mixing opencv_contrib-3.1.0 with opencv-4.0.0.
You should use OPENCV_EXTRA_MODULES_PATH=…/…/opencv_contrib-4.0.0/modules

Thanks greatly.
got mixed up
changed to 4.0.0 and cmaked
got following error now

===
Building C object CMakeFiles/cmTC_1f64a.dir/CheckSymbolExists.c.o
/usr/bin/cc -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wuninitialized -Winit-self -Wno-narrowing -Wno-comment -Wimplicit-fallthrough=3 -Wno-strict-overflow -fdiagnostics-show-option -pthread -fomit-frame-pointer -ffast-math -ffunction-sections -fdata-sections -fvisibility=hidden -O3 -DNDEBUG -fPIE -o CMakeFiles/cmTC_1f64a.dir/CheckSymbolExists.c.o -c /usr/share/opencvinstall/opencv-4.0.0/build/CMakeFiles/CMakeTmp/CheckSymbolExists.c
/usr/share/opencvinstall/opencv-4.0.0/build/CMakeFiles/CMakeTmp/CheckSymbolExists.c:2:10: fatal error: OpenCL_INCLUDE_DIR-NOTFOUND/CL/cl.h: No such file or directory
#include <OpenCL_INCLUDE_DIR-NOTFOUND/CL/cl.h>
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
compilation terminated.

Maybe some bad config remains. I’d suggest to rebuild in a brand new folder.

thanks again.

what I did was

cd build
rm *
tried to remove …/cache if any, but there were none

then cmake
then got above error

re-install opencv all over?

cl.h is here
richard-desktop:/usr/share/opencvinstall/opencv-4.0.0/newbuild# find /usr -name cl.h -print
/usr/share/opencvinstall/opencv-4.0.0/3rdparty/include/opencl/1.2/CL/cl.h

how to help cmake to find it?

Use recursive rm, otherwise subfolders would remain:

cd build
rm -rf *

Thank you for your assistance
Yes done similar

mkdir newbuild
cd newbuild

same error

Thanks for assistance.
Finally managed to compile the examples by re-installing opencv with about script
https://github.com/AastaNV/JEP/blob/master/script/install_opencv4.0.0_Nano.sh

and editing cmake line from BUILD_EXAMPES=OFF to BUILD_EXAMPLES=ON

Tried to run a few samples like facedetect and hough transform but find that they are not
using the GPU (using tegrastats)

Anyone tried and confirm that the GPU is being utilised?

Thanks

Anyone knows which Opencv sample that will use the GPU?

If you’re just looking for one example, you may try example_gpu_houghlines.

Thank you very much.

Now why didnt I see these gpu examples???

Run above houglines program but it happens too fast to see the gpu load

Tried
./example_gpu_hog --folder …/…/samples/data/
and was able to see the gpu being loaded.
Is there a way for above program to input a rtsp video stream?

Also tried

./example_gpu_cascadeclassifier --cascade …/…/data/haarcascades/haarcascade_russian_plate_number.xml “rtsp://admin:gth12345@192.168.1.51:554/live”
Device 0: “NVIDIA Tegra X1” 3965Mb, sm_53, Driver/Runtime ver.10.0/10.0
terminate called after throwing an instance of ‘cv::Exception’
what(): OpenCV(4.0.0) /usr/share/OpenCV/opencvnano/opencv-4.0.0/modules/core/include/opencv2/core/private.cuda.hpp:113: error: (-213:The function/feature is not implemented) The called functionality is disabled for current build or platform in function ‘throw_no_cuda’

Abort (core dumped)

but it crashed.

Anyway to resolve this?

Thanks

Here is a quick (and not optimal, but I won’t do more) adaptation for reading a RTSP stream instead:

#include <cmath>
#include <iostream>

#include "opencv2/core.hpp"
#include <opencv2/core/utility.hpp>
#include "opencv2/videoio.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/imgproc.hpp"
#include "opencv2/cudaimgproc.hpp"

using namespace std;
using namespace cv;
using namespace cv::cuda;


int main()
{
    const char* gst_cap = "rtspsrc location=rtsp://127.0.0.1:8554/test ! decodebin ! videoconvert ! video/x-raw,format=GRAY8 ! appsink";

    VideoCapture cap(gst_cap);
    if( !cap.isOpened() )
    {
        cout << "Error: Cv::VideoCapture.open() failed" << endl;
        return 0;
    }

    Mat src;
    Mat mask;
    Mat dst_cpu;
    Mat dst_gpu;
    
    GpuMat d_lines;

    Ptr<cuda::HoughSegmentDetector> hough = cuda::createHoughSegmentDetector(1.0f, (float) (CV_PI / 180.0f), 50, 5);

    namedWindow("source", WINDOW_AUTOSIZE);
    namedWindow("detected lines [CPU]", WINDOW_AUTOSIZE);
    namedWindow("detected lines [GPU]", WINDOW_AUTOSIZE);

    for(;;) { 
         if (!cap.read(src)) {
             cout<<"Capture read error"<<endl;
             break;
         }
 
         cv::Canny(src, mask, 100, 200, 3);
    	 cv::cvtColor(mask, dst_cpu, COLOR_GRAY2BGR);
         dst_gpu = dst_cpu.clone();

         vector<Vec4i> lines_cpu;
         {
             const int64 start = getTickCount();

             cv::HoughLinesP(mask, lines_cpu, 1, CV_PI / 180, 50, 60, 5);

             const double timeSec = (getTickCount() - start) / getTickFrequency();
             cout << "CPU Time : " << timeSec * 1000 << " ms" << endl;
             cout << "CPU Found : " << lines_cpu.size() << endl;
         }

         for (size_t i = 0; i < lines_cpu.size(); ++i)
         {
             Vec4i l = lines_cpu[i];
             line(dst_cpu, Point(l[0], l[1]), Point(l[2], l[3]), Scalar(0, 0, 255), 3, LINE_AA);
         }

         GpuMat d_src(mask);
         {
              const int64 start = getTickCount();

              hough->detect(d_src, d_lines);

              const double timeSec = (getTickCount() - start) / getTickFrequency();
              cout << "GPU Time : " << timeSec * 1000 << " ms" << endl;
              cout << "GPU Found : " << d_lines.cols << endl;
         }
         vector<Vec4i> lines_gpu;
         if (!d_lines.empty())
	 {
		lines_gpu.resize(d_lines.cols);
		Mat h_lines(1, d_lines.cols, CV_32SC4, &lines_gpu[0]);
		d_lines.download(h_lines);
	 }

	 for (size_t i = 0; i < lines_gpu.size(); ++i)
	 {
		Vec4i l = lines_gpu[i];
		line(dst_gpu, Point(l[0], l[1]), Point(l[2], l[3]), Scalar(0, 0, 255), 3, LINE_AA);
	 }

	 imshow("source", src);
	 imshow("detected lines [CPU]", dst_cpu);
	 imshow("detected lines [GPU]", dst_gpu);
	 waitKey(1);
    }

    return 0;
}

Thank you very much.
Please do contact me if you need any assistance / drink/meal in SG.

Replaced original houghlines.cpp with above ,
modified gst_cap to argv[1]
and run the program
The result shows GPU is only 2 to 3 times faster although GPU did find more lines.
Thanks.
Please do inform when more cuba functions available, especially haarscascade detection.
Thanks.

=== Output of modified sample houghlines program =====
CPU Time : 286.127 ms
CPU Found : 28
GPU Time : 162.679 ms
GPU Found : 2728
CPU Time : 295.719 ms
CPU Found : 33
GPU Time : 115.552 ms
GPU Found : 3208
CPU Time : 287.503 ms
CPU Found : 29
GPU Time : 181.23 ms
GPU Found : 3102
CPU Time : 305.856 ms
CPU Found : 41
GPU Time : 119.34 ms
GPU Found : 3351
CPU Time : 329.245 ms
CPU Found : 47
GPU Time : 138.867 ms
GPU Found : 3606
CPU Time : 255.881 ms
CPU Found : 38
GPU Time : 193.132 ms
GPU Found : 2587
CPU Time : 310.359 ms
CPU Found : 39
GPU Time : 148.919 ms
GPU Found : 3151
CPU Time : 298.087 ms
CPU Found : 33
GPU Time : 122.628 ms
GPU Found : 2953
CPU Time : 236.007 ms
CPU Found : 31
GPU Time : 153.455 ms
GPU Found : 2217