JetsonHacks

Developing on NVIDIA® Jetson™ for AI on the Edge

OpenCV 4 + CUDA on Jetson Nano

Building OpenCV 4 with CUDA support on the NVIDIA Jetson Nano Developer Kit can be a bit of a chore. Here’s some help. Looky here:

Background

In an earlier article, 5 Things about OpenCV on Jetson, we discuss some of the reasons which you may want to build OpenCV from source. The reasons include:

  • You need a specific version of OpenCV (the default on the Jetson is 3.3.1)
  • You would like to use OpenCV version 4
  • You would like to have CUDA acceleration
  • You would like to have a different configuration than the default version, such as support for Qt, OpenGL, and so on.

Note: While we mention why you may want to switch to CUDA enabled algorithms, reader Patrick pointed out that a real world example of when you want CUDA acceleration is when using the OpenCV DNN module. To do so, you may need to set the CMake flag OPENCV_DNN_CUDA to YES.

In the video, we use:

  • Jetson Nano
  • A Samsung T5 USB drive
  • A RPi V2 camera
  • A 5V, 4A power supply

You can find these items on the JetsonHacks Amazon store. Buying through the link does not cost you anything additional, and sends a small commission to the channel. Thank you for your support!

Resources

There are already some good resources for building OpenCV on the Jetson. First, there is the “official” NVIDIA script in this Github repository: https://github.com/AastaNV/JEP As of this writing, the OpenCV script is in the scripts directory, the name of the script is install_opencv4.1.1_Jetson.sh

NVIDIA updates this script frequently to support newer versions of OpenCV and L4T. A good choice if you want something to work and do not want to spend the time specifying your configuration. In the video, we go over the script.

Michael de Gans is a frequent Jetson forum contributor. He knows what’s up! Michael has a Github repository with the name nano_build_opencv which contains a script to build OpenCV. The script has much more structure than the NVIDIA one, you will benefit reading through/using it.

Sergio Canu’s excellent article on building OpenCV 4.1 discusses how to increase the size of the system swap memory when building OpenCV. Here’s a JetsonHacks article on how to set the swap memory size. This should cut your build time dramatically when building OpenCV. Setting the swap memory size to 8GB cut the time as shown in the video of 2.5 hours to 1.5 hours.

Another great resource is JK Jung’s blog. The blog covers many aspects of using Jetsons in machine learning, certainly one of the best Jetson resources out there. There are articles on different Machine Learning frameworks, how to speed up your projects with TensorRT, ML optimizing and more.

By going through JK Jung’s blog post, you will begin to understand some of the decisions that you may need to make for any given environment. OpenCV is a very rich environment. In computer speak that usually means that it is so flexible that it takes a lot of knowledge to configure it for best performance for any given application.

Which leads us to the gist of this post. The JetsonHacks take on build OpenCV. In the broad sense, this is version updates previous work we have done on the other members of Jetson family.

Building OpenCV 4

The version of OpenCV 4 that we are building has the following features:

  • CUDA support
  • Gstreamer support
  • Video for Linux support (V4L2)
  • Qt support
  • OpenCV version 4.1.1
  • Python 2 and Python 3 support
  • Build an OpenCV package with installer
  • Build for Jetson Nano

In the video, we are using a Jetson Nano running L4T 32.2.1/JetPack 4.2.2. The Nano is running with the rootfs on a USB drive. This speeds up the build time considerably. As in Sergio Canu’s article, you can increase the size of the swap file to reduce memory thrashing. Here’s a JetsonHacks article on how to set the swap memory size. This should cut your build time dramatically when building OpenCV. Setting the swap memory size to 8GB cut the time as shown in the video of 2.5 hours down to 1.5 hours.

Note: The swap file in the video is only 2GB, and results in a lot of memory thrashing when building the Python wrappers.

Let’s build this puppy

As we discuss in the video, you will want to set the CMAKE flags to reflect the OpenCV configuration you want to build. We have a quick walk through in the video, but it is certainly worth exploring on your own. To build from a Terminal:

$ git clone https://github.com/JetsonHacksNano/buildOpenCV

$ cd buildOpenCV

$ ./buildOpenCV.sh |& tee openCV_build.log

The last command will build OpenCV, and send the results of the build to the file openCV_build.log. This is useful when you want to review your build configuration, or help hunt down issues that may occur during the build.

The resulting Package of OpenCV will end up in the opencv directory: ~/opencv/build/_CPACK_Packages/Linux/STGZ with a name similar to OpenCV-4.1.1-<commit>-aarch64.sh, where commit is the name of the Github version.

We can run a couple of demos to make sure things are working. Please see the Examples folder and follow the README instructions (as shown in the video). The demos expect that you have a RPi V2 camera, you may have to change some code for a USB camera.

Note about pkg-config

By default, OpenCV does not build a .pc file for use by pkg-config. In order to build the file, the correct Cmake option is:

OPENCV_GENERATE_PKGCONFIG=YES

When installation is complete, you will need to relocate the .pc file (assuming the script default install path here) /usr/local/lib/pkgconfig/ and edit the file paths in the .pc file appropriately. Make sure you run:

$ sudo ldconfig -v

afterwards so that pkg-config knows where to look.

Conclusion

That’s a lot of reading for three simple lines of code! However, when undertaking a project like this it make sense to understand why you are doing it, what your options are, and what benefit you expect to receive after completion.

Notes

In the video:

  • Jetson Nano
  • Build OpenCV 4.1.1
  • L4T 32.2.1/JetPack 4.2.2
Facebook
Twitter
LinkedIn
Reddit
Email
Print

46 Responses

    1. I seem to recall seeing that. But I think you make a good point that we should be more direct in pointing it out. I’ve added a note to the article pointing it out. Thank you very much.

  1. Good evening, I was wondering if you had any thoughts on ways to reduce the 300-400ms latency that you mention in the video? On the Nvidia Jetson forums I’ve been told there is a 2-3 frame delay in the CSI pipeline but you are clearly seeing much greater.

    (Thank you for all your efforts to make Jetson Nano easier to use)

    1. I haven’t really looked into it, you can probably get better advice in the forums. I would advise you to perform your own tests, there are lots of parameters to tweak that I may not have set for optimal performance.

  2. hi Jim, opencv compiled okay, but canny code didnt detect a camera? Did you use the csi cam or a usb cam? if you used the csi cam, is there a driver that needs to be installed or any setting changed?

  3. Anyone wanting to use DNNs (yolo etc.):
    CUDA_DNN requires latest master branch (script uses a different branch). Ran into a build fail a few hours in due to missing libwebp-dev module. Install the module before (or add to script):
    $ sudo apt-get install libwebp-dev

  4. Phil,
    sudo apt-get install libwebp-dev
    above cmd Is it mandatory to run before building the opencv?

    Is it ok if i run the “sudo apt-get install libwebp-dev” cmd after build?

    please advise

    note:
    I already built the opencv4.1.1, just now I see your comments.

  5. I have a question regarding opencv supporting CUDA. The latest Jetpack 4.3 (and also the image I have on my jetson nano) has OpenCV 4.1.1 already installed. I did the print cv2.getBuildInformation() that you indicated in your other post but this time it did not indicate that CUDA was not supported but it did not indicate that CUDA was supported either.

    So my question is, is CUDA supported by default in OpenCV 4.1.1 or should I build it from source anyway?

    1. It depends on your needs. The default OpenCV 4.1.1 does not have CUDA support. However, make sure that the OpenCV algorithms that you are going to use have CUDA support before recompiling OpenCV with CUDA support. Thanks for reading!

  6. Where and how can I utilise the package when using the magic flag?

    I’m looking to install into a venv in a separate location

      1. I used the script and it installed most of the files to /usr/src/OpenCV-4.1.1-dirty-aarch64/. I’m guessing I would have to manually place the residing bin, include, lib, share folders in the respective system directories? I’m using this as part of a docker build script

  7. Thanks for the great tutorial! Can I use this same script to install opencv4.1.2 by only changing the OPENCV VERSION in the file?

    1. mmm, eventhough cuda is activated, cv::gpu::getCudaEnabledDeviceCount() returns 0.
      and your python example fails because cv2.namedWindow(windowName, cv2.WINDOW_NORMAL) is not implemented

  8. mmm, eventhough cuda is activated, cv::gpu::getCudaEnabledDeviceCount() returns 0.
    and your python example fails because cv2.namedWindow(windowName, cv2.WINDOW_NORMAL) is not implemented

  9. Great write up. Thank you. I’m trying to build 4.1.1 but get the following error
    fatal error: sys/videoio.h: No such file or directory
    Not sure what to do about it, see other have the same issue but I can not locate a definitive solution. Do you have an idea?

  10. 64G micro SD, 4G swap, 1 core, OpenCV 4.3.0.
    Just over 4 hours to build.
    Appears 4.3.0 does not support cuDNN.
    Also to build ‘gstreamer_veiw’, had to change
    –cfags opencv to –cflags opencv4
    and –libs opencv to –libs opencv4
    cv2 is now very cuda’y in Python 🙂

  11. can anyone please explain the “Note about pkg-config” steps I am new to this field,.. installation is complete but I am getting a fatal error(no file named OpenCV is found).

  12. Thanks for this. It works great on my jetson Nano. I just received my Xavier NX kit, and the opencv 4.1 installed does not include the contrib libs. Would it work for me to use this same procedure on the Xavier? I assume i would first uninstall the included version.

  13. thanks for the advice,ill try it out. do you have any other advice for uninstalling the version i currently have under the jetpack preview, without causing any problems, both on the uninstall and also on its impact on the new install?

  14. Hi,
    I did the installation, and seemed to work. But when i test it, i can import cv2, but i cant use cv2.__version__ or any other attributes of cv2. Ant addvice/ tips

    1. I tried:
      sudo apt-get -y purge libopencv-dev libopencv-python libopencv-samples libopencv*
      sudo apt-get purge ‘*opencv*’
      sudo apt-get autoremove
      sudo apt-get update

      You can double check with:
      sudo find / -name “*opencv*”

  15. I was building opencv with this script and got this error.
    please help me to resolve it.

    :0:0: fatal error: cannot read PCH file: No such file or directory
    compilation terminated.
    modules/cudaarithm/CMakeFiles/opencv_cudaarithm.dir/build.make:14463: recipe for target ‘modules/cudaarithm/CMakeFiles/opencv_cudaarithm.dir/src/arithm.cpp.o’ failed
    make[2]: *** [modules/cudaarithm/CMakeFiles/opencv_cudaarithm.dir/src/arithm.cpp.o] Error 1
    CMakeFiles/Makefile2:2873: recipe for target ‘modules/cudaarithm/CMakeFiles/opencv_cudaarithm.dir/all’ failed
    make[1]: *** [modules/cudaarithm/CMakeFiles/opencv_cudaarithm.dir/all] Error 2
    Makefile:162: recipe for target ‘all’ failed
    make: *** [all] Error 2
    Make did not successfully build
    Please fix issues and retry build

  16. I am using jetson nano which is booted with Jetpack4.5.1 . I need to build opencv 4.5.1 with CUDA.When I run your .sh file it automatically changes the version to 4.1.1.What is the procedure to change the version of opencv to build with cuda?

  17. Hi! Thank you for the tutorial. After installation, opencv2/cudawarping.hpp is missing. What can I do? Do I have to purge the previous install coming with the SDK? Thanks!

  18. I installed opencv using this script, but i have error: AttributeError: ‘module’ object has no attribute ‘DNN_BACKEND_CUDA’. I noticed what in cv2.getBuildInformation, line cuDNN was NO. So i decided to add in config files “-D WITH_CUDNN=ON” according Michael De Gans script, but i still have error: AttributeError: ‘module’ object has no attribute ‘DNN_BACKEND_CUDA’. Why if i run script by default “-D WITH_CUDNN” missing? Why i can’t use DNN_BACKEND_CUDA overall?

  19. Hello,
    I successfully installed ./buildOpenCV.sh |& tee openCV_build.log
    but am not able to locate any .pc file. I looked under /usr/local/lib – there is missing the directory pkgconfig
    I would be grateful for any suggestions

    1. Started again after fresh Jetson image flashing. This time I altered the install script adding – D OPENCV_GENERATE_PKGCONFIG=ON \
      I am not sure what I am doing here 🙂
      After script execution I went to examples. Python script works great, but trying to compile with gcc -std=c++11 `pkg-config –cflags opencv` `pkg-config –libs opencv` gstreamer_view.cpp -o gstreamer_view -lstdc++ -lopencv_core -lopencv_highgui -lopencv_videoio shows missing opencv

      just replaced the occurrences of opencv with opencv4 and it works now

  20. Hello,

    I’m having a hard compiling this script in a Jetson Nano 2GB ram.

    The error is:

    collect2: error: ld returned 1 exit status
    modules/xobjdetect/tools/waldboost_detector/CMakeFiles/opencv_waldboost_detector.dir/build.make:105: recipe for target ‘bin/opencv_waldboost_detector’ failed
    make[2]: *** [bin/opencv_waldboost_detector] Error 1
    CMakeFiles/Makefile2:8066: recipe for target ‘modules/xobjdetect/tools/waldboost_detector/CMakeFiles/opencv_waldboost_detector.dir/all’ failed
    make[1]: *** [modules/xobjdetect/tools/waldboost_detector/CMakeFiles/opencv_waldboost_detector.dir/all] Error 2
    Makefile:162: recipe for target ‘all’ failed
    make: *** [all] Error 2
    Make did not successfully build
    Please fix issues and retry build

    Thank you in advance !

Leave a Reply

Your email address will not be published. Required fields are marked *

Disclaimer

Some links here are affiliate links. If you purchase through these links I will receive a small commission at no additional cost to you. As an Amazon Associate, I earn from qualifying purchases.

Books, Ideas & Other Curiosities