Build OpenCV 3.4 on NVIDIA Jetson AGX Xavier Developer Kit

In order to get access to cameras using OpenCV on the NVIDIA Jetson AGX Xavier, you need to build the library from source. Looky here:

Background

Using JetPack 4.1, you may install OpenCV as an option. However, that particular version of OpenCV does not support CUDA or camera input.

What to do? Build it yourself of course!

Fortunately we have some convenience scripts to help with that task in the JetsonHacks repository buildOpenCVXavier 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.

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

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
  • TBB (Threading Build Blocks)

Build and Install

To download the source, build and install OpenCV:

$ git clone https://github.com/jetsonhacks/buildOpenCVXavier.git
$ cd buildOpenCVXavier
$ git checkout v1.0
$ ./buildOpenCV.sh

Note: You can also use buildAndPackageOpenCV.sh, which will build .deb installers for your OpenCV configuration. This is useful when you want to build OpenCV once, and then have it available for other installations.

Another Note: Check out the README file for instructions on how to set where the scripts download and build the OpenCV source. You can also query the scripts using the –help command line flag.

You can remove the sources and build files after you are done:

$ ./removeOpenCVSources.sh

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 31.0.2, OpenCV 3.4.3

The first is a simple C++ program to view the onboard camera feed from the Jetson Dev Kit.

To compile gstreamer_view.cpp:

$ g++ -o gstreamer_view -Wall -std=c++11 gstreamer_view.cpp $(pkg-config –libs opencv)

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.
  • In the video, there are two cameras. One is a camera from a Jetson TX1 Development Kit plugged in to the AGX Xavier camera port (located on the underside of the carrier board). The second camera is a Logitech C920 webcam connected via USB.

14 Comments

    • There are two cameras. One is a camera from a Jetson TX1 Development Kit, plugged in to the AGX Xavier camera port (located on the underside of the carrier board). The second camera is a Logitech C920 webcam connected via USB. Thanks for reading!

  1. I found that with this installation openCV would not work in Python 3. To fix this I had to run: `sudo apt install python3-opencv` after above installation was complete.

    Furthermore, cv2 would still not work in a virtual environment. To fix this I had to create a symbolic link to the main python env cv2 file:
    `ln -s /usr/lib/python3/dist-packages/cv2.cpython-36m-aarch64-linux-gnu.so .venv/lib/python3.6/site-packages/cv2.cpython-36m-aarch64-linux-gnu.so`

    • Disregard my previous comment re: installing opencv for python3. Running `sudo apt install python3-opencv` will not install gstreamer.

      The correct way to install for python 3 requires adding the following option for cmake in buildOpenCV.sh line 161:
      `-D PYTHON_DEFAULT_EXECUTABLE=/usr/bin/python3 \`

      Then create a link to cv2 if using a virtual env:
      `ln /usr/local/lib/python3.6/site-packages/cv2.cpython-36m-aarch64-linux-gnu.so .venv/lib/python3.6/site-packages/cv2.so`

  2. I encountered problems on Jetpack 4.2.1
    The with that version my camera is really slow..
    I have also build opencv 4.1.1 without problem while compiling or installation,
    but sadly the camera was also really slow..

    Any one have experience with that?
    Thanks in advance

  3. I had a problem with a circular link to libGL.so.
    The build failed at about 30% because it could not find libGL.so.
    Worse than that when I rebooted, the GUI could not put up window boarders or move windows.
    I am not sure if I overwrote the actual libGL.so.x.x or if the script did. I recovered by getting the file from https://developer.nvidia.com/embedded/dlc/r32-2-1_Release_v1.0/TX2-AGX/Tegra_Linux_Sample-Root-Filesystem_R32.2.1_aarch64.tbz2got
    I had to replace it while the build was in progress.

  4. I grow tired of trying 50 times now. Can someone please share how to use this with Python 3.7 ?
    python points to 3.7
    python3 points to 3.6
    added `-D PYTHON_DEFAULT_EXECUTABLE=/usr/bin/python3.7 \`

    Still doesn’t work. Been at this for 3 days now….

    Any help would be much appriciated

    • Not sure what issue you are encountering from your description. My understanding is that python should point to where your version 2.X is, and python3 should point to where your Python 3.X is located. So something like python points to Python 2.7 and python 3 would point to Python 3.7.

  5. Okay I used sudo update-alternatives –config python and sudo update-alternatives –config python3

    python -V = 2.7
    python3 -V = 3.7

    I removed the old opencv folder it had build on the SSD and ran the build again.After that it started working, much thanks!

Leave a Reply

Your email address will not be published.


*