agx orin nx 安裝 opencv4.9 cuda 版本

cold_moon發表於2024-03-27

1. 解除安裝 原來的CPU版 opencv

sudo apt purge libopencv*
sudo apt update

2. 找一個路徑,存放下載 opencv 安裝包

sudo wget -O opencv-4.9.0.zip wget https://github.com/opencv/opencv/archive/4.9.0.zip
sudo wget -O opencv_contrib-4.9.0.zip wget https://github.com/opencv/opencv_contrib/archive/4.9.0.zip

解壓、放到一個資料夾中

unzip opencv.zip && unzip opencv_contrib.zip

組織成這種層次:

├── 4.9.0
│   ├── opencv-4.9.0
│   └── opencv_contrib-4.9.0

3. 編譯支援GPU加速的 OpenCV

cd opencv-4.9.0
# 3. Create build directory and switch into it
mkdir build && cd build

├── 4.9.0
│   ├── opencv-4.9.0
│	├── build
│   └── opencv_contrib-4.9.0

檢視 agx orin nx 的 CUDA_ARCH_BIN 為 8.7。 jetson 除了網址可以去查:https://developer.nvidia.com/cuda-gpus#compute ,也可以使用 jtop 查詢 CUDA_ARCH_BIN。

Configure, 在 build 目錄內
sudo cmake -D CMAKE_BUILD_TYPE=Release \
	  -D CMAKE_INSTALL_PREFIX=/usr/local \
	  -D OPENCV_EXTRA_MODULES_PATH=../../opencv_contrib-4.9.0/modules \
	  -D CUDA_TOOLKIT_ROOT_DIR=/usr/local/cuda \
	  -D CUDA_ARCH_BIN=8.7 \
	  -D CUDA_ARCH_PTX=8.7 \
	  -D WITH_CUDA=ON \
	  -D OPENCV_DNN_CUDA=ON  \
	  -D WITH_CUDNN=ON  \
	  -D ENABLE_FAST_MATH=ON \
	  -D CUDA_FAST_MATH=ON \
	  -D WITH_CUBLAS=ON \
	  -D OPENCV_GENERATE_PKGCONFIG=ON  \
	  -D BUILD_EXAMPLES=ON \
	  -D BUILD_NEW_PYTHON_SUPPORT=ON \
	  -D BUILD_opencv_python3=ON \
	  -D HAVE_opencv_python3=ON  \
	  -D OPENCV_ENABLE_NONFREE=ON  \
	  -D INSTALL_PYTHON_EXAMPLES=ON \
	  -D OPENCV_PYTHON3_INSTALL_PATH=/usr/local/lib/python3.8/dist-packages  \
	  -D PYTHON_EXECUTABLE=/usr/bin/python3.8 \
	  ..

注意:

  • CMAKE_INSTALL_PREFIX=/usr/local : opencv 的安裝路徑
  • OPENCV_EXTRA_MODULES_PATH=../../opencv_contrib-4.9.0/modules
  • ll /usr/local/cuda 可以檢視 cuda 是誰的軟連結
  • CUDA_ARCH_BIN CUDA_ARCH_PTX 都是 8.7
  • python3 支援的話,請使用 which python3ll /usr/bin/python3 找到是哪個 python3, 從而 獲得 OPENCV_PYTHON3_INSTALL_PATH 路徑。

當前操作主要是網路問題可能會報錯,請使用梯子

https://www.cnblogs.com/odesey/p/17960079

export http_proxy=http:你的IP:7890
export https_proxy=http:你的IP:7890

失敗的話: 清除 rm CMakeCache.txt

報錯:
-- Checking for module 'gtk+-2.0'
--   No package 'gtk+-2.0' found
-- Allocator metrics storage type: 'int'
CMake Error at modules/core/CMakeLists.txt:158 (message):
  CUDA: OpenCV requires enabled 'cudev' module from 'opencv_contrib'
  repository: https://github.com/opencv/opencv_contrib

解決:opencv_contrib-4.9.0 的路徑沒填對,已更正。

 sudo apt install -y libgtk2.0-dev

成功的配置:

-- General configuration for OpenCV 4.9.0 =====================================
--   Version control:               unknown
-- 
--   Extra modules:
--     Location (extra):            /home/nvidia/Downloads/4.9.0/opencv_contrib-4.9.0/modules
--     Version control (extra):     unknown
-- 
--   Platform:
--     Timestamp:                   2024-03-27T06:50:39Z
--     Host:                        Linux 5.10.104-tegra aarch64
--     CMake:                       3.16.3
--     CMake generator:             Unix Makefiles
--     CMake build tool:            /usr/bin/make
--     Configuration:               Release
-- 
--   CPU/HW features:
--     Baseline:                    NEON FP16
--     Dispatched code generation:  NEON_DOTPROD NEON_FP16
--       requested:                 NEON_FP16 NEON_BF16 NEON_DOTPROD
--       NEON_DOTPROD (1 files):    + NEON_DOTPROD
--       NEON_FP16 (2 files):       + NEON_FP16
-- 
--   C/C++:
--     Built as dynamic libs?:      YES
--     C++ standard:                11
--     C++ Compiler:                /usr/bin/c++  (ver 9.4.0)
--     C++ flags (Release):         -fsigned-char -ffast-math -fno-finite-math-only -W -Wall -Wreturn-type -Wnon-virtual-dtor -Waddress -Wsequence-point -Wformat -Wformat-security -Wmissing-declarations -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wuninitialized -Wsuggest-override -Wno-delete-non-virtual-dtor -Wno-comment -Wimplicit-fallthrough=3 -Wno-strict-overflow -fdiagnostics-show-option -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections    -fvisibility=hidden -fvisibility-inlines-hidden -O3 -DNDEBUG  -DNDEBUG
--     C++ flags (Debug):           -fsigned-char -ffast-math -fno-finite-math-only -W -Wall -Wreturn-type -Wnon-virtual-dtor -Waddress -Wsequence-point -Wformat -Wformat-security -Wmissing-declarations -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wuninitialized -Wsuggest-override -Wno-delete-non-virtual-dtor -Wno-comment -Wimplicit-fallthrough=3 -Wno-strict-overflow -fdiagnostics-show-option -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections    -fvisibility=hidden -fvisibility-inlines-hidden -g  -O0 -DDEBUG -D_DEBUG
--     C Compiler:                  /usr/bin/cc
--     C flags (Release):           -fsigned-char -ffast-math -fno-finite-math-only -W -Wall -Wreturn-type -Waddress -Wsequence-point -Wformat -Wformat-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wuninitialized -Wno-comment -Wimplicit-fallthrough=3 -Wno-strict-overflow -fdiagnostics-show-option -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections    -fvisibility=hidden -O3 -DNDEBUG  -DNDEBUG
--     C flags (Debug):             -fsigned-char -ffast-math -fno-finite-math-only -W -Wall -Wreturn-type -Waddress -Wsequence-point -Wformat -Wformat-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wuninitialized -Wno-comment -Wimplicit-fallthrough=3 -Wno-strict-overflow -fdiagnostics-show-option -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections    -fvisibility=hidden -g  -O0 -DDEBUG -D_DEBUG
--     Linker flags (Release):      -Wl,--gc-sections -Wl,--as-needed -Wl,--no-undefined  
--     Linker flags (Debug):        -Wl,--gc-sections -Wl,--as-needed -Wl,--no-undefined  
--     ccache:                      NO
--     Precompiled headers:         NO
--     Extra dependencies:          m pthread cudart_static dl rt nppc nppial nppicc nppidei nppif nppig nppim nppist nppisu nppitc npps cublas cudnn cufft -L/usr/local/cuda/lib64 -L/usr/lib/aarch64-linux-gnu
--     3rdparty dependencies:
-- 
--   OpenCV modules:
--     To be built:                 aruco bgsegm bioinspired calib3d ccalib core cudaarithm cudabgsegm cudacodec cudafeatures2d cudafilters cudaimgproc cudalegacy cudaobjdetect cudaoptflow cudastereo cudawarping cudev datasets dnn dnn_objdetect dnn_superres dpm face features2d flann freetype fuzzy gapi hfs highgui img_hash imgcodecs imgproc intensity_transform line_descriptor mcc ml objdetect optflow phase_unwrapping photo plot python3 quality rapid reg rgbd saliency shape stereo stitching structured_light superres surface_matching text tracking ts video videoio videostab wechat_qrcode xfeatures2d ximgproc xobjdetect xphoto
--     Disabled:                    world
--     Disabled by dependency:      -
--     Unavailable:                 alphamat cannops cvv hdf java julia matlab ovis python2 sfm viz
--     Applications:                tests perf_tests examples apps
--     Documentation:               NO
--     Non-free algorithms:         YES
-- 
--   GUI:                           GTK3
--     GTK+:                        YES (ver 3.24.20)
--       GThread :                  YES (ver 2.64.6)
--       GtkGlExt:                  NO
--     VTK support:                 NO
-- 
--   Media I/O: 
--     ZLib:                        /usr/lib/aarch64-linux-gnu/libz.so (ver 1.2.11)
--     JPEG:                        /usr/lib/aarch64-linux-gnu/libjpeg.so (ver 80)
--     WEBP:                        build (ver encoder: 0x020f)
--     PNG:                         /usr/lib/aarch64-linux-gnu/libpng.so (ver 1.6.37)
--     TIFF:                        /usr/lib/aarch64-linux-gnu/libtiff.so (ver 42 / 4.1.0)
--     JPEG 2000:                   build (ver 2.5.0)
--     OpenEXR:                     /usr/lib/aarch64-linux-gnu/libImath.so /usr/lib/aarch64-linux-gnu/libIlmImf.so /usr/lib/aarch64-linux-gnu/libIex.so /usr/lib/aarch64-linux-gnu/libHalf.so /usr/lib/aarch64-linux-gnu/libIlmThread.so (ver 2_3)
--     HDR:                         YES
--     SUNRASTER:                   YES
--     PXM:                         YES
--     PFM:                         YES
-- 
--   Video I/O:
--     DC1394:                      YES (2.2.5)
--     FFMPEG:                      YES
--       avcodec:                   YES (58.54.100)
--       avformat:                  YES (58.29.100)
--       avutil:                    YES (56.31.100)
--       swscale:                   YES (5.5.100)
--       avresample:                YES (4.0.0)
--     GStreamer:                   YES (1.16.3)
--     v4l/v4l2:                    YES (linux/videodev2.h)
-- 
--   Parallel framework:            pthreads
-- 
--   Trace:                         YES (with Intel ITT)
-- 
--   Other third-party libraries:
--     Lapack:                      NO
--     Eigen:                       NO
--     Custom HAL:                  YES (carotene (ver 0.0.1, Auto detected))
--     Protobuf:                    build (3.19.1)
--     Flatbuffers:                 builtin/3rdparty (23.5.9)
-- 
--   NVIDIA CUDA:                   YES (ver 11.4, CUFFT CUBLAS FAST_MATH)
--     NVIDIA GPU arch:             87
--     NVIDIA PTX archs:            87
-- 
--   cuDNN:                         YES (ver 8.6.0)
-- 
--   OpenCL:                        YES (no extra features)
--     Include path:                /home/nvidia/Downloads/4.9.0/opencv-4.9.0/3rdparty/include/opencl/1.2
--     Link libraries:              Dynamic load
-- 
--   Python 3:
--     Interpreter:                 /usr/bin/python3.8 (ver 3.8.10)
--     Libraries:                   /usr/lib/aarch64-linux-gnu/libpython3.8.so (ver 3.8.10)
--     numpy:                       /usr/lib/python3/dist-packages/numpy/core/include (ver 1.17.4)
--     install path:                /usr/local/lib/python3.8/dist-packages/cv2/python-3.8
-- 
--   Python (for build):            /usr/bin/python3.8
-- 
--   Java:                          
--     ant:                         NO
--     Java:                        NO
--     JNI:                         NO
--     Java wrappers:               NO
--     Java tests:                  NO
-- 
--   Install to:                    /usr/local
-- -----------------------------------------------------------------
-- 
-- Configuring done
-- Generating done
-- Build files have been written to: /home/nvidia/Downloads/4.9.0/opencv-4.9.0/build

編譯:

sudo make -j$(nproc)  

$(nproc) 可以使用的最大執行緒

4. 安裝 OpenCV

sudo make install -j 12

注意:如果沒有報錯,可能會卡住,你需要等待。
卡住的話或許你也可以使用: sudo make install -j 1, 然後等待。然後一直在安裝的話,你就可以換為:`` sudo make install -j 12`

5. python 測試 和 jtop 確認:

使用 python3 直譯器和 cv2.cuda.printCudaDeviceInfo(0) 來驗證庫是否正常工作:

image

除了上面的 python 來測試。jetson 也可以用 jtop 來檢視。

opencv with CUDA YES

6. 清理

安裝後,安裝檔案和壓縮檔案 我們將不再需要,可以安全刪除:

cd ../..
sudo rm -rf  ./*

其他

我找到一個安裝指令碼:https://github.com/s95huang/Jetson_orin_cv_cuda/tree/main

此外還有 docker 的: https://github.com/JulianAssmann/opencv-cuda-docker

但是我沒有嘗試

相關文章