Wouldn’t it be nice to be able to package OpenCV into an installable after a build such as in our previous article, Build OpenCV 3.4 with CUDA on Jetson TX2? Well you can! In fact, the OpenCV build system makes this pretty simple.
Note: There’s also an article for the Jetson TX1: Build OpenCV 3.4 with CUDA – Jetson TX1. Both of the repositories are basically the same, but set the architecture for the particular processor in use.
The advantage of this approach is that you will only need to build OpenCV to the configuration you desire once, and can reinstall it easily when you need to recreate or reflash your Jetson environment.
Building the Package
Here’s a walk through video for building the OpenCV package. This is for a Jetson TX2, but the instructions for a Jetson TX1 are similar. Looky here:
Note: In the video we used this USB Flash Drive to build the package. We also have been using the Samsung T5 Portable SSD here lately, and really liking it. The Samsung T5 is currently at a great price/performance point, and don’t tend to get lost as easily as the thumb drives. The USB drive is connected via an Amazon 7 Port USB 3.0 Hub, and the webcam is a Logitech c920.
In the buildOpenCVTX2 repository on the JetsonHacks account on Github (for the Jetson TX1 buildOpenCVTX1 repository) there is a script called ‘buildAndPackageOpenCV.sh’. The script includes the commands in the buildOpenCV.sh script, along with the commands to package the build.
The flag CPACK_BINARY_DEB tells CMAKE to package the build. If you read the script, you will see that after OpenCV is built and installed, there is a command:
$ sudo make package
This tells the system to build the binary package files. These files are in three flavors; .deb files, a .tar file, and a .sh shell script which is a self contained installer.
The buildAndPackageOpenCV script has two optional command line parameters:
-s | –sourcedir Directory in which to place the opencv sources (default $HOME)
-i | –installdir Directory in which to install opencv libraries (default /usr/local)
For example, to run the the build file:
$ ./buildAndPackageOpenCV.sh -s <file directory>
This example will build OpenCV in the given file directory and install OpenCV in the /usr/local directory.
The corresponding .deb files will be in the <file directory>/opencv/build directory in .deb file and compressed forms.
As shown in the video, the source directory can be set to external media such as a USB drive or SATA drive.
Note: On L4T 28.2, the default installation location for the OpenCV libraries is /usr. The default location for the OpenCV 3.4 build presented here is /usr/local.
Installing the Package
While OpenCV 3.4 is installed in the preceding procedure, there will be times that you want to generate a new system and install OpenCV 3.4 without having to compile it from source. With the OpenCV package files from the above build process, you can simply use the package files as installers. For the simplest way, looky here (first few minutes):
You will probably want to copy the package files to another directory. In the <file directory>/opencv/build directory, the OpenCV packages will start as ‘OpenCV*’
There will be some .deb files, a .tar file, and a .sh file. These files may differ from the video depending on the build options you select for OpenCV.
If you are familiar with Linux, you probably already know what to do with the .deb file or .tar files. If not, you can use the .sh file.
First, you will want to remove the current OpenCV installation:
$ sudo apt-get remove lib opencv
Note: You may want to ‘purge’ instead of ‘remove’. The purge command will remove the corresponding .deb files of the older versions from the system if installed.
Next, switch over to the place where you have your OpenCV package shell script and execute:
$ ./OpenCV-3.4.1-<version>.sh –prefix=/usr
The ‘prefix’ flag tells the installer where to place OpenCV. In a standard L4T installation, OpenCV is installed in /usr.
This is just one way to install OpenCV, you may want to install it using different methods or locations.
Testing with YOLO
In the installation video, we install and test with YOLO. Looky here:
The instructions are taken from JK Jung’s excellent blog: https://jkjung-avt.github.io/yolov3/. If you haven’t run across that blog before, take the time to stop and look around. There is a lot of great Jetson related content!
YOLO itself means “You Only Look Once”. YOLO is a state-of-the-art, real-time object detection system. Checkout the YOLO website: https://pjreddie.com/darknet/yolo/
There’s quite a lot of information there, take your time and absorb it.
Conclusion
I think this is a pretty useful addition to the build toolbox. I use it quite a bit. Have fun!
The post How To Package OpenCV 3.4 with CUDA – NVIDIA Jetson TX Dev Kits appeared first on JetsonHacks.