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:
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.
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.
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*
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
- 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
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:
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 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:
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.
- 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.
Do your installation scripts on GitHub work on JetPack 4.1.1DP?
I have not encountered any issues using them with 4.1.1. Thanks for reading!
What camera are you using and how is it attached to the Jetson AGX Xavier?
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!
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`
The ~/opencv folder takes up 10GB on disk space. Is it safe to delete after installation?
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
Please ask this question on the official NVIDIA Jetson forum where a large number of developers and NVIDIA engineers share their experience. The forum is here: https://devtalk.nvidia.com/default/board/326/jetson-agx-xavier/
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.
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.
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!
I am glad you were able to get it to work. Hopefully the rest of your projects are much easier.
fatal error: opencv2/xfeatures2d/cuda.hpp: No such file or directory
Hi, I’m getting this message. how do I add sift-surf libraries?
thanx for your great posts..
I’m working with the JetPack 4.3 on jetson Nano with OpenCV cv2 verison 4.1.1. Unfortunately gstream is not working with my raspy 2 cam on Jetson Nano. Any ideas?
It is difficult to diagnose your issue with the information provided. Do you have a Gstreamer pipeline that you are using? Did you compile OpenCV with Gstreamer on?
Hi, Is it possible to build OpenCV on the host and then install it on the Xavier?
Yes. Thanks for reading!
Thanks for your reply. So the steps are the same except for make install. Correct? And should we specify arm64 option while compiling it on host?
You will need to set up your host compilation environment to fit the Jetson. I do not have anything to share on this particular subject. Please ask this question on the official NVIDIA Jetson AGX Xavier forum, where a large group of developers and NVIDIA engineers share their experience: https://forums.developer.nvidia.com/c/agx-autonomous-machines/jetson-embedded-systems/jetson-agx-xavier/75
How can we install OpenCV inside a docker container so that we don’t have to do it everytime we want to use a different Xavier?
Trying this installation at a Xavier NX.
Get this error when trying to run the compiled code
./gstreamer_view: error while loading shared libraries: libopencv_highgui.so.3.4: cannot open shared object file: No such file or directory
Thank you for your posts and videos.
I am trying to run this on an Xavier board running Jetpack 4.3. Without changing anything in the installation script, I was getting errors about some packages being deprecated (not too suprised). OpenCV 4.1.1 was the one installed with Jetpack 4.3. I tried changing OPENCV_VERSION to 4.1.1 and am getting errors.
Do you have any advice on how to change the buildOpenCV.sh script?
Thank you for the posts. Running into a bit of a problem, as it seems that I’m running out of free space on a 32 gig Xavier with mostly only the base Jetpack (4.4) installed. Just how big is the build?
You can try these scripts: https://github.com/mdegans/nano_build_opencv
Thank you for your posts. They have been a great resource for getting the hang of the Jetson platforms.
Thank you for the kind words, and thanks for reading!
Why is not VFPV3 or NEON activated? (I noticed TBB is activated)
Feel free to use whatever extensions you like, that’s the point of the article. Typically GCC will handle extensions that match the processor. ARMv7 had different processor versions, some had VFPV3 and NEON support, thus the flags. All ARMv8, like the Xavier AGX, have those built in so GCC is smart enough to use them/compile them when encountered.
Thanks for the tutorial!. I finally managed to install OpenCV 3.4 on my Jetson AGX Xavier.
However the final test failed, because previously it had Opencv 4 installed. Even though I did the initial purging, it seems that it is still there.
The situation is the following:
When I do opencv_version, it shows 3.4 correctly.
But when I do from inside python import cv2, cv2.__version__ it shows 4.1
And also if in a CMakelists.txt file I put find_package(OpenCV 4.0 QUIET), it finds the V4 package.
My questions are:
1. Where does python ‘s import cv2 search for installed opencv?
2. How can I get rid of that version of opencv
Thanks in advance!