Packaging OpenCV with CUDA for Jetson

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.

The post Packaging OpenCV with CUDA for Jetson appeared first on JetsonHacks.

Leave a Reply

Your email address will not be published.

Previous post 702 – Super Mario Wonder Deep Dive
Next post NVIDIA and Scaleway Speed Development for European Startups and Enterprises