JetsonHacks

Developing on NVIDIA® Jetson™ for AI on the Edge

Build OpenCV 3.4 with CUDA on NVIDIA Jetson TX1

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:

Background

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.

Installation

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.

Library location

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*

Options

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:

  • CUDA
  • Fast Math (cuBLAS)
  • OpenGL
  • 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.

Examples

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:

$ ./gstreamer_view

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 (<) and greater 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.

Notes

  • 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:

Facebook
Twitter
LinkedIn
Reddit
Email
Print

17 Responses

  1. Awesome. Do the build options include SIFT, SURF and the Tracker modules that are part of Opencv Contrib?

      1. Can you explain more in depth how to do this? I’m not sure I know how to do it and fear I may ruin something along the way. Thank you!

  2. When using:

    git clone https://github.com/jetsonhacks/buildOpenCVTX1.git

    I noticed that there is a C compilation error. When I compared cloned repo buildOpenCV script and the online version, there are:

    # For 3.4.1, use this to fix C compilation issues (like in YOLO)
    git cherry-pick 549b5df

    Lines, which are not in my version of the script. Added them and trying to compile OpenCV again. However, do I must uninstall somehow the previous build? The:

    sudo apt-get purge libopencv*

    does not work.

    1. Usually it’s something like ‘sudo make uninstall’ from the build folder for the OpenCV from source build. apt-get purge is for the version installed by JetPack.

  3. Thank you very much for your instruction.
    I have just updated Python to 3.7. Is there a way to install opencv for Python3.7? I tried to use update-alternatives to switch python3 to python3.7 but it still does not work.
    Do you have instruction to install pytorch too?

      1. kangalow: I did ask question there but nobody have answered my question yet. Somehow I have the feeling that they switch to Jetson TX2 and AGX. From your script, I thought that I can just change Python3 to Python3.7 but it still not work.

  4. Thanks.. worked first go…. Is it easy to change to the latest version of openCV4.1.0?

    Also, the python3 canny edge detection example worked perfectly. Does it automatically use the cuda cores or do they need to called to differentiate code from cpu?

    1. Did you try to change the version number in the scripts, and see if it works?

      If there is a CUDA enhanced code in a given OpenCV function, it will be called automatically if OpenCV is compiled with CUDA support. Thanks for reading!

            1. Hi, Could you please let me know how this error was fixed? I am trying with 4.1.1 and am getting the same error.

  5. What is the best Opencv version to use now? I have the Oak-D stereo camera and want to use that with Jetson TX1 and ROS. The Jetson has Ubuntu 18.04 now. Should I stick with the version 3x or go to 4x of Opencv? I certainly want to include Cuda processing!

Leave a Reply

Your email address will not be published. Required fields are marked *

Disclaimer

Some links here are affiliate links. If you purchase through these links I will receive a small commission at no additional cost to you. As an Amazon Associate, I earn from qualifying purchases.

Books, Ideas & Other Curiosities