For OpenCV to use CUDA acceleration on the NVIDIA Jetson TX1 running L4T 28.2 (JetPack 3.2), you will need to build OpenCV from source. Looky here:
With the latest release of L4T, 28.2, OpenCV version 3.3 may be installed through the JetPack installer. At the time of the L4T release, OpenCV did not provide support for CUDA 9.0 with which L4T 28.2 ships. Over the next couple of months, version OpenCV 3.4 added CUDA 9.0 support.
So what does that mean? Well, if you want OpenCV CUDA support under L4T 28.2 you need to compile it from source. Fortunately we have some convenience scripts to help with that task in the JetsonHacks repository buildOpenCVTX1 on Github.
You should note that OpenCV is a rich environment, and can be custom tailored to your needs. As such, some of the more common options are in the build command, but are not comprehensive. Modify the options to suit your needs.
If you saw the previous article about building OpenCV on the Jetson TX2, the process is similar. However, there is one difference which is very important.
The Jetson TX1 has a 16GB internal eMMC flash drive. Along with the operating system and usual programs/libraries, this does not leave enough room on the eMMC to build the OpenCV library. As a result, you will need to build OpenCV on external media, such as a SD card, USB flash drive/disk, or SATA disk.
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.
The external drive must be formatted as Ext4, otherwise the operating system will get all confused when it tries to do things such as make symbolic links.
With this script release, the script now installs OpenCV in /usr/local. Earlier versions of this script installed in /usr. You may have to set your include and libraries and/or PYTHONPATH to point to the new version. See the Examples folder. Alternatively, you may want to change the script to install into the /usr directory.
All of this may lead to a conflict. You may consider removing OpenCV installed by JetPack before performing this script installation:
$ sudo apt-get purge libopencv*
The buildOpenCV 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:
$ ./buildOpenCV.sh -s <file directory>
This will build OpenCV is the given file directory and install OpenCV is the /usr/local directory.
Make sure to read through the install script. In the script, here are some of the options that were included:
- Fast Math (cuBLAS)
- GStreamer 1.0
- Video 4 Linux (V4L)
- Python 2.7 and Python 3.5 support
Build and Install
To download the source, build and install OpenCV:
$ git clone https://github.com/jetsonhacks/buildOpenCVTX1.git
$ cd buildOpenCVTX1
$ git checkout v2.0OpenCV3.4
$ ./buildOpenCV.sh -s <file directory>
You can remove the sources and build files after you are done:
$ ./removeOpenCVSources.sh -s <file directory>
where the <file directory> is the same as in the buildOpenCV command. This will remove the OpenCV source, as well as the opencv_extras directories.
There are a couple of demos in the Examples folder.
There are two example programs here. Both programs require OpenCV to be installed with GStreamer support enabled. Both of these examples were last tested with L4T 28.2, OpenCV 3.4.1
The first is a simple C++ program to view the onboard camera feed from the Jetson Dev Kit.
To compile gstreamer_view.cpp:
$ 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
to run the program:
The second is a Python program that reads the onboard camera feed from the Jetson Dev Kit and does Canny Edge Detection.
To run the Canny detection demo (Python 2.7):
$ python cannyDetection.py
With Python 3.3:
$ python3 cannyDetection.py
With the Canny detection demo, use the less than () to adjust the edge detection parameters. You can pass the command line flags —video_device=<videoDeviceNumber> to use a USB camera instead of the built in camera.
- This is meant to be a template for building your own custom version of OpenCV, pick and choose your own modules and options
- Most people do NOT have both the JetPack installed and the source built OpenCV on their system. Some people have noted success using both however, check the forums.
- Different modules and setting may require different dependencies, make sure to look for error messages when building.
The information for this script was gathered from several places: