Install OpenCV 4.5 on Jetson Nano
Last updated: June 27, 2021
Why would you install OpenCV on the Jetson Nano, when your operating system has already a version pre-installed? The main reason is that the shipped version has no CUDA support. And after all, wasn't the CUDA accelerator the main reason you bought the Jetson Nano in the first place?
Regularly, we get the question if we have an SD image of a Jetson Nano with pre-installed OpenCV, TensorFlow and PyTorch.
We are happy to comply with this request. Please, find a complete Jetson Nano image on our GitHub page. Download the zip file from our Mega site, unzip and flash the image on a 32 GB SD-card, and enjoy! The zip file is 13 Gbyte and Mega.nz has a three hour limitation on the download time. You will need a fast internet connection.
On December 22, 2020, OpenCV released version 4.5.1. The most important improvement in this version is the work on the G-API framework and the RISC-V port. Neither will affect the Jetson Nano.
On April 2, 2021, OpenCV released version 4.5.2. Find the significant changes in version in the log file here. Important changes are made in the imgproc, videoio and DNN module.
It may be advisable to install the lightdm desktop instead of the gdm3 when prompted during the initial installation, saving about 500 MByte of precious memory space. It can make all the difference, especially with memory-hungry deep learning models. Although some of you probably prefer the Ubuntu look and feel to the somewhat archaic lxdm experience.
Before installing OpenCV 4.5.0 on your Jetson Nano, consider overclocking. When the CUDA accelerator is not used, which is in most daily applications, the Jetson Nano has a quad ARM Cortex-A57 core running at 1.4 GHz. Compared to the quad Cortex-A72 at 1.5 GHz of the Raspberry Pi 4, there isn't that great a difference. Especially with an overclocked RPi. In that case, it will beat the Jetson in most tasks. Except, of course, when the CUDA kicks in. On this page the overclocking of the Jetson Nano is covered.
So, time to reinstall OpenCV on the Jetson Nano with the CUDA and cuDNN support this time.
Enlarge memory swap.
Building the full OpenCV 4.5.0 package requires more than 4 Gbytes of RAM on your Jetson Nano and the extra 2 Gbytes of swap space delivered by zram. We have to install dphys-swapfile to get the additional space from your SD card temporarily. After the compilation, the mechanism will be removed, eliminating swapping to the SD card. Please follow the next commands. Note also the installation of nano, a tiny text editor.
Version 4.5.2 needs even more memory than the previous version. In this case, you need to increase the dphys swap beyond the regular 2048 limit. It is done by first changing the maximum boundary in /sbin/dphys-swapfile to 4096. Next, set the /etc/dphys-swapfile. The slideshow will guide you. If there is not enough swap memory, the compilation will crash at 100% without notification.
# a fresh start, so check for updates
$ sudo apt-get update
$ sudo apt-get upgrade
# install nano
$ sudo apt-get install nano
# install dphys-swapfile
$ sudo apt-get install dphys-swapfile
# enlarge the boundary (4.5.2 only)
$ sudo nano /sbin/dphys-swapfile
# give the required memory size
$ sudo nano /etc/dphys-swapfile
# reboot afterwards
$ sudo reboot.
Installing OpenCV on your Jetson Nano is not that complicated. With its 68 command lines, it is more of an administrative task. That's why we created an installation script that executes all commands in this guide at once. Use it if you want, it shouldn't cause any problems. The whole installation will take two hours to complete. It starts with the installation of the dependencies and ends with the ldconfig.
If you want to beautify your OpenCV with the Qt5 GUI, follow the instructions on the GitHub page, or in the section below.
Cleaning and removing dphys-swap should now be done manually according to these instructions.
Not using the script? Here's the full guide.
The OpenCV software uses other third party software libraries. Install these first. Some come with the Jetson Tegra operating system, others you may already have installed. Better to be safe than sorry, so here's the full list. Only the latest packages are installed according to the procedure.
The installation will take up an additional 800 MByte on your disk, if not already present.
# reveal the CUDA location
$ sudo sh -c "echo '/usr/local/cuda/lib64' >> /etc/ld.so.conf.d/nvidia-tegra.conf"
$ sudo ldconfig
# third-party libraries
$ sudo apt-get install build-essential cmake git unzip pkg-config
$ sudo apt-get install libjpeg-dev libpng-dev libtiff-dev
$ sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev
$ sudo apt-get install libgtk2.0-dev libcanberra-gtk*
$ sudo apt-get install python3-dev python3-numpy python3-pip
$ sudo apt-get install libxvidcore-dev libx264-dev libgtk-3-dev
$ sudo apt-get install libtbb2 libtbb-dev libdc1394-22-dev
$ sudo apt-get install gstreamer1.0-tools libv4l-dev v4l-utils
$ sudo apt-get install libgstreamer1.0-dev libgstreamer-plugins-base1.0-dev
$ sudo apt-get install libavresample-dev libvorbis-dev libxine2-dev
$ sudo apt-get install libfaac-dev libmp3lame-dev libtheora-dev
$ sudo apt-get install libopencore-amrnb-dev libopencore-amrwb-dev
$ sudo apt-get install libopenblas-dev libatlas-base-dev libblas-dev
$ sudo apt-get install liblapack-dev libeigen3-dev gfortran
$ sudo apt-get install libhdf5-dev protobuf-compiler
$ sudo apt-get install libprotobuf-dev libgoogle-glog-dev libgflags-dev
Qt is an open-source toolkit for the development of cross-platform graphical user interfaces. It also works on the Linux Tegra operating system. The software can be used to beautify OpenCV windows and other user interfaces, such as sliders and checkboxes. It is absolutely not mandatory for the working of OpenCV, only to embellish the look. Must be said, that using Qt5 will slow down your OpenCV by a few percent. If you go for the fastest possible apps, don't use it.
If you want the Qt5 support enabled in OpenCV, you have to download the library as shown in the command below. The next step is to set the -D WITH_QT flag during the build.
# only install if you want Qt5
# to beautify your OpenCV GUI
$ sudo apt-get install qt5-default
When all third-party software is installed, OpenCV itself can be downloaded. There are two packages needed; the basic version and the additional contributions. After downloading, you can unzip the files. Please be aware of line wrapping in the text boxes. The two commands are starting with wget and ending with zip.
Before we begin with the actual build of the library, there is one small step to go. You have to make a directory where all the build files can be located.
$ cd ~/opencv
$ mkdir build
$ cd build
Now it is time for an important step. Here you tell CMake what, where and how to make OpenCV on your Raspberry. There are many flags involved. The most you will recognize. You will probably notice the -D WITH_QT=OFF line. Here Qt5 support is disabled. Set -D WITH_QT=ON if you choose to use the Qt5 software for the GUI. We save space by excluding any (Python) examples or tests.
There are only bare spaces before the -D flags, not tabs. By the way, the two last dots are no typo. It tells CMake where it can find its CMakeLists.txt (the large recipe file); one directory up.
Also, note the /usr directory where all header files and OpenCV libraries will reside. We have a slight preference for the /usr/local folder. For now, we'll be following the Tegra installation directory to keep pre-existing software running smoothly with the reinstalled OpenCV. Keep it in mind when you port some of our Raspberry Pi software on GitHub to your Jetson Nano.