Available for personal testing - opencv configured with cuda acceleration on jetson nano b01

The previous article has written about how to install the image and basic configuration.
It is available for personal testing - jetson nano B01 image installation and configuration.

3. Configure opencv to support cuda acceleration

3.1 Uninstall the built-in opencv

The official image of jetson nano comes with opencv, but it does not support graphics card acceleration.

input the command

sudo jtop

Press number 7 to view the INFO interface, you can see

Insert image description here

Therefore, the one that comes with it by default does not support CUDA acceleration (GPU), and there is no way to fully utilize the performance of the GPU on Jetson.

How to uninstall the built-in opencv

sudo apt-get purge libopencv*
sudo apt-get autoremove
sudo apt-get update

3.1 Compile and install opencv

Install dependent libraries

Enter the following command

sudo add-apt-repository "deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ xenial main multiverse restricted universe"
sudo apt update
sudo apt install libjasper1 libjasper-dev
sudo apt install -y libjpeg8-dev libjasper-dev libpng12-dev libtiff5-dev libavcodec-dev libavformat-dev libswscale-dev libdc1394-22-dev libxine2-dev libv4l-dev
sudo apt install -y libgstreamer1.0-dev libgstreamer-plugins-base1.0-dev libgtk2.0-dev libtbb-dev libatlas-base-dev libfaac-dev libmp3lame-dev libtheora-dev libvorbis-dev libxvidcore-dev libopencore-amrnb-dev libopencore-amrwb-dev x264 v4l-utils
sudo apt install python-dev python-numpy libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libjasper-dev libdc1394-22-dev

There may be duplicates among the above seven commands, so you can ignore them. If you forgot to record them when troubleshooting the problem, pressing them again will have no effect.

Download opencv and opencv_contrib source code

Go to opencv and opencv_contrib to download the source code respectively . (If you need to access the Internet scientifically, you can use Jetson's own browser or download it on a win computer and use a USB flash drive to copy it)

opencv download

Do not click code-Download ZIP directly!!! Because what you download is not necessarily what you want to use! ! !

Click on the image below and select the version you need under Tags. I will take 4.5.3 as an example here. I failed to install version 4.0.0 successfully and found no problem.

Insert image description here

After switching to version 4.5.3, click code-Download ZIP to download

Insert image description here

opencv_contrib download

Do not click code-Download ZIP directly!!! Because what you download is not necessarily what you want to use! ! !

Click on the image below and select the version consistent with opencv under Tags. Be sure to ensure that the two versions are consistent .

Insert image description here

After switching to version 4.5.3, click code-Download ZIP to download

Insert image description here

Compile and install opencv

Put opencv-4.5.3.zip and opencv_contrib-4.5.3.zip in the same folder and unzip it, and put the unzipped opencv_contrib-4.5.3 in the opencv-4.5.3 file! (You can use the visual interface on the Jetson side to decompress and move operations, which will be more convenient)

Insert image description here

Install cmake (#If it is installed, ignore this. If you don’t know whether it is installed, just write it again. If you repeat the installation, you will be prompted. It will not affect)

sudo apt-get install make	

View the value of CUDA_ARCH_BIN

sudo jtop

View and record in the INFO page, you will need it later

Insert image description here

View the address of opencv_contrib

Execute the command and view the complete address based on your own address

cd opencv400/opencv_contrib-4.x/modules
pwd

Insert image description here

Excuting an order

cd opencv400	#注意这里进入到的文件夹中必须包含opencv解压后的所有文件和opencv_contrib文件夹
mkdir build
cd build

Copy the cudev folder in the modules in opencv_contrib to opencv/modules (you can use the visual interface on the Linux side, and the command line operation will not be repeated)

Execute cmake

It is necessary to change the number in the following command -DCUDA_ARCH_BIN= 5.3 to the value of CUDA_ARCH_BIN found earlier.

You need to change the path after the following command -DOPENCV_EXTRA_MODULES_PATH= to the address of opencv_contrib found earlier (generally no need to change)

If you get an error in this step, check to see if the version is correct.

cmake \
-DCMAKE_BUILD_TYPE=Release \
-DCMAKE_INSTALL_PREFIX=/usr/local \
-DOPENCV_ENABLE_NONFREE=1 \
-DBUILD_opencv_python2=1 \
-DBUILD_opencv_python3=1 \
-DWITH_FFMPEG=1 \
-DCUDA_TOOLKIT_ROOT_DIR=/usr/local/cuda \
-DCUDA_ARCH_BIN=5.3 \
-DCUDA_ARCH_PTX=7.2 \
-DWITH_CUDA=1 \
-DENABLE_FAST_MATH=1 \
-DCUDA_FAST_MATH=1 \
-DWITH_CUBLAS=1 \
-DOPENCV_GENERATE_PKGCONFIG=1 \
-DOPENCV_EXTRA_MODULES_PATH=../opencv_contrib-4.5.3/modules \
..

Run make, then wait and check the number of threads

sudo cat /proc/stat | grep cpu[0-9] -c

Compilation takes about 2-3 hours. The number after -j in this step depends on the number of threads found in the previous one!

sudo make -j4

You may get stuck at the end of the progress, just restart it. If you can't restart, try hard work and unplug it directly.

After make compilation is completed, install it

sudo make install

So far the installation is successful

Verify installation results

Check the opencv version number and whether it supports cuda in jtop

Insert image description here

Some error reports and solutions

Insert image description here

I scrolled up and found this error, which prompted that the file was missing. I found the answer by consulting online information to compile OpenCV and openc_contrib, which prompted that the boostdesc_bgm.i file was missing . Follow this blog for successful resolution. The file address mentioned there belongs to another big boss.

Baidu Cloud link: https://pan.baidu.com/s/1BeYF8kqEZLAJYQj-MvxpmA

Extraction code: e1wc

Insert image description here

Move the missing files to the corresponding directory and execute the following command to recompile

make clean
make -j4

Other errors you may encounter and their solutions

报错test_features2d.cpp:51:10: fatal error: features2d/test/test_detectors_regression.impl.hpp

Guess you like

Origin blog.csdn.net/qq_19973937/article/details/134989858