It takes several hours to build OpenCV with CUDA support on a NVIDIA Jetson. In the normal build process, the finished library is installed on the build machine. But what if you want to install it on another machine also? The make build system for OpenCV has a package option! Looky here:
Tips and Tricks
Building the package for OpenCV is pretty simple. Go to the build directory and then make the package. In the video, we’re using a Jetson Nano, but this works with any Jetson. Assuming that you were following the previous articles and have just finished your build, the commands will look like this:
$ cd /tmp/build_opencv/opencv/build
$ make package
The make build system will run, and produce a shell script for installation, along with a couple of compressed .tar files both of which contain only the OpenCV libraries and associated files. The file names will start with ‘OpenCV-‘. Copy those files somewhere that you can share with other machines. Typically I use a USB SSD for the task.
Saving the Build
Because the build system uses make, it’s important to keep the timestamps intact when copying the build artifacts. If you do not, then make will rebuild the entire system because it thinks the source files have been updated.
When you copy files using the file manager GUI, the default is to keep the same timestamps on the copied files. However, if you use the command line ‘cp‘ command, the timestamps are updated to reflect when the files were copied. In order to prevent this, use the preserve flag. For example, to copy the folder build_opencv :
$ cp -r –preserve=timestamps build_opencv /path/to/destination
It’s source to destination. The -r means copy everything including subdirectories. Use the preserve flag whenever copying build artifacts.
Install
Installing the new package is straightforward. Copy the OpenCV script file to your home directory, then run the script. For example:
$ ./OpenCV-4.8.0-aarch64.sh –prefix=/usr/local
This will install the OpenCV library into /usr/local because of the ‘prefix‘ flag. Default installation is into the current directory. Some people will install into .local in the home directory if they want only the current user to have access.
Missing Dependencies
Unfortunately some of the runtime libraries are missing after the install. When packaging, the development libraries are used. However, when installed on a new system these libraries have not been installed. The runtime libraries are not as extensive as the ones needed for development.
The video goes over how to find the dependencies using the ‘ldd’ and ‘apt-file’ utilities. The following ldd command finds the files that the dynamic linker cannot find, and apt-file attempts to locate which package holds them in the apt repository.
$ ldd cv2.cpython-36m-aarch64-linux-gnu.so | grep found
$ apt-file search libtesseract
You will need to install apt-file and update it’s index before use:
$ sudo apt install apt-file
$ sudo apt-file update
The libtesseract and libcblas libraries are not present in the default system. In order to install them:
$ sudo apt install libtesseract4
$ sudo apt install libatlas3-base
Once these libraries are installed, you are ready to enjoy OpenCV goodness! If you’re using Python to import cv2, then make sure to set the PYTHONPATH to match your install spot.
6 Responses
Build opencv takes long time and I got error at 98%, I’m very sad.
Could you share package so I can install without compile. Thanks
I can’t supplied precompile packages, but you can look here: https://forums.developer.nvidia.com/t/pytorch-and-torchvision-problem-on-jetson-nano/273176/6?u=kangalow
Good luck on your project, and thanks for reading!
Once you’ll have built opencv with CUDA support, you may want to use it 😉
A way to leverage Jetson HW with many open source SW is from gstreamer
You may see:
https://forums.developer.nvidia.com/t/opencv-cuda-processing-from-gstreamer-pipeline-jp4-jp5/273745
Currently, only EGL pitch format is supported for RGBA, I420 and NV12. Block linear or other memory buffer types than NVBUF_MEM_SURFACE_ARRAY are unsupported.
Though, it may be enough or a good starting point for trying most cases with Jetsons.
Hope it helps.
Most excellent! Thank you for sharing this code.
Oh boy !! more next level complexity with Gstreamer. When does the Nvidia bamboo slivers under the finger nails end for us mere mortals ?
Sorry to hear about the script kiddie attack on your site. Civility is deteriorating at an alarming pace. Thanks for all your charitable education . Most of us appreciate your talent and generosity.
Thank you for the kind words, and thanks for reading!