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:
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!
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:
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.
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.
In the video:
- Jetson Nano
- Build OpenCV 4.1.1
- L4T 32.2.1/JetPack 4.2.2