OpenCV build on Jetson Nano

opencv OpenCV

This article explains how to build OpenCV on Jetson Nano. This article explains how to build PyTorch on Jetson Nano, but as for OpenCV, you need to build OpenCV by yourself since no CUDA, cuDNN enabled package is provided. The default python3 is usually used when OpenCV is built, but this article explains how to build OpenCV for any python3 version.

This article also uses python 3.8 as an example. Note that the scripts used in the description are based on the https://github.com/mdegans/nano_build_opencv repository with additional configuration and modifications for illustration purposes. OpenCV 4.6.0 is used. The entire script can be found at https://github.com/otamajakusi/build_jetson_nano_libraries.

install package

Install the necessary packages. nvidia-cuda and nvidia-cudnn8 are also installed.

sudo apt-get update sudo apt-get install -y \ build-essential \ cmake \ git \ gfortran \ libatlas-base-dev \ libavcodec-dev \ libavformat-dev \ libavresample-dev \ libcanberra-gtk3-module \ libdc1394-22-dev \ libeigen3-dev \ libglew-dev \ libgstreamer-plugins-base1.0-dev \ libgstreamer-plugins-good1.0-dev \ libgstreamer1.0-dev \ libgtk-3-dev \ libjpeg-dev \ libjpeg8-dev \ libjpeg-turbo8-dev \ liblapack-dev \ liblapacke-dev \ libopenblas-dev \ libpng-dev \ libpostproc-dev \ libswscale-dev \ libtbb-dev \ libtbb2 \ libtesseract-dev \ libtiff-dev \ libv4l-dev \ libxine2-dev \ libxvidcore-dev \ libx264-dev \ pkg-config \ python3.8-dev \ python3-numpy \ python3-matplotlib \ python3-pip \ qv4l2 \ v4l-utils \ v4l2ucp \ zlib1g-dev \ nvidia-cuda \ nvidia-cudnn8
Code language: Bash (bash)

Next, install the python3.8 package. The key point is to uninstall the pre-installed opencv package and python3 numpy and install the python3.8 numpy.

apt list --installed | grep -i opencv | awk -F/ '{print $1}'| xargs sudo apt purge -y sudo python3 -m pip install -U pip sudo python3 -m pip uninstall -y numpy sudo python3.8 -m pip install -U pip sudo python3.8 -m pip install setuptools sudo python3.8 -m pip install numpy
Code language: Bash (bash)

build and install OpenCV

Clone OpenCV.

git clone --depth 1 --branch 4.6.0 https://github.com/opencv/opencv.git git clone --depth 1 --branch 4.6.0 https://github.com/opencv/opencv_contrib.git
Code language: Bash (bash)

Set CMAKFLAGS.

CMAKEFLAGS=" -D BUILD_EXAMPLES=OFF -D BUILD_opencv_python2=OFF -D BUILD_opencv_python3=ON -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D CUDA_ARCH_BIN=5.3,6.2,7.2 -D CUDA_ARCH_PTX= -D CUDA_FAST_MATH=ON -D CUDNN_VERSION='8.0' -D EIGEN_INCLUDE_PATH=/usr/include/eigen3 -D ENABLE_NEON=ON -D OPENCV_DNN_CUDA=ON -D OPENCV_ENABLE_NONFREE=ON -D OPENCV_EXTRA_MODULES_PATH=../../opencv_contrib/modules -D OPENCV_GENERATE_PKGCONFIG=ON -D WITH_CUBLAS=ON -D WITH_CUDA=ON -D WITH_CUDNN=ON -D WITH_GSTREAMER=ON -D WITH_LIBV4L=ON -D WITH_OPENGL=ON -D PYTHON3_EXECUTABLE=python3.8 -D PYTHON3_INCLUDE_PATH=$(python3.8 -c "from distutils.sysconfig import get_python_inc; print(get_python_inc())") -D PYTHON3_PACKAGES_PATH=$(python3.8 -c "from distutils.sysconfig import get_python_lib; print(get_python_lib())") -D PYTHON3_LIBRARY=/usr/lib/aarch64-linux-gnu/libpython3.8.so"
Code language: Bash (bash)

The point is that PYTHON3_EXECUTABLE, PYTHON3_INCLUDE_PATH, PYTHON3_PACKAGES_PATH, and PYTHON3_LIBRARY should be set respectively, since the default python3 is not used.

Build & install. Jetson Nano core number 4 is specified as make -j$(nproc), it builds fine in my environment. if you want to run with another OpenCV version other than 4.6.0, please refer to Please refer to https://qengineering.eu/install-opencv-4.5-on-jetson-nano.html.

cd opencv mkdir build cd build cmake ${CMAKEFLAGS} .. make -j$(nproc) sudo make install
Code language: Bash (bash)

It takes about 2 hours and 40 minutes.

That’s all.

Reference