網頁

2020年8月25日 星期二

Build OpenCV on Jetson NANO with CUDA CUDNN

參考 How to configure your NVIDIA Jetson Nano for Computer Vision and Deep Learning

make 時出現下列錯誤
error: 'CUDNN_CONVOLUTION_FWD_PREFER_FASTEST' was not declared in this scope
error: 'CUDNN_CONVOLUTION_BWD_PREFER_FASTEST' was not declared in this scope
原因為 OpenCV 4.4 以前,並不支援 CUDNN8.0

CUDNN 8.0 只能在 OpenCV 4.4 才能 build 成功
$ git clone https://github.com/opencv/opencv.git -b 4.4.0 --depth 1
$ git clone https://github.com/opencv/opencv_contrib.git -b 4.4.0 --depth 1


刪除舊的安裝
$ sudo apt purge libopencv-dev libopencv-python libopencv-samples libopencv*
$ sudo apt update

參考 Any OCR model run in Jetson Nano
參考 Installing tesseract 4.0 on Ubuntu 16.04
$ sudo apt install tesseract-ocr libtesseract-dev


$ sudo add-apt-repository ppa:alex-p/tesseract-ocr
$ sudo apt-get update
$ sudo apt-get install libleptonica-dev
$ sudo apt-get install qt5-default
$ sudo apt-get install qtcreator

cmake \
 -D CMAKE_BUILD_TYPE=RELEASE \
 -D CMAKE_INSTALL_PREFIX=/usr/local \
 -D WITH_CUDA=ON \
 -D WITH_CUDNN=ON \
 -D WITH_CUBLAS=ON \
 -D WITH_V4L=ON \
 -D CUDNN_VERSION='8.0' \
 -D CUDNN_INCLUDE_DIR='/usr/include' \
 -D OPENCV_DNN_CUDA=ON \
 -D WITH_GTK=ON \
 -D WITH_QT=ON \
 -D INSTALL_C_EXAMPLES=OFF \
 -D WITH_GSTREAMER=ON \
 -D OPENCV_EXTRA_MODULES_PATH=../../opencv_contrib-4.4.0/modules/ \
 -D OPENCV_ENABLE_NONFREE=ON \
 -D BUILD_TESTS=OFF \
 -D BUILD_PERF_TESTS=OFF \
 -D BUILD_EXAMPLES=OFF \
 -D INSTALL_PYTHON_EXAMPLES=ON \
 -D PYTHON_EXECUTABLE=$(which python3) \
 -D BUILD_opencv_python2=OFF \
 -D BUILD_opencv_python3=ON \
 -D BUILD_opencv_java=OFF \
 -D PYTHON3_EXECUTABLE=$(which python3) \
 -D PYTHON3_INCLUDE_DIR=$(python3 -c "from distutils.sysconfig import get_python_inc; print(get_python_inc())") \
 -D PYTHON3_PACKAGES_PATH=$(python3 -c "from distutils.sysconfig import get_python_lib; print(get_python_lib())") \
 -D Tesseract_INCLUDE_DIR=/usr/include/tesseract \
 -D Tesseract_LIBRARY=/usr/lib/aarch64-linux-gnu/libtesseract.so \
 -D Lept_LIBRARY=/usr/lib/aarch64-linux-gun/liblept.so \
 -D CUDA_ARCH_PTX="" \
 -D CUDA_ARCH_BIN="5.3,6.2,7.2" \
 -D OPENCV_GENERATE_PKGCONFIG=YES ..

$ make -j4
$ sudo make install
$ sudo ldconfig


$ opencv_version -v

安裝於 python 的 virtualenv 中
nano@nano-desktop:~/envs/py3cv4/lib/python3.6/site-packages$ ln -s /usr/lib/python3/dist-packages/cv2/python-3.6/cv2.cpython-36m-aarch64-linux-gnu.so cv2.so


在 mainwindow.cpp 檔內增加
#include <opencv2/opencv.hpp>
#include <opencv2/cudaimgproc.hpp>


SSD 測試
(py3cv4) nano@nano-desktop:~/Data/opencv/opencv-dnn-gpu-examples$ python opencv-ssd-cuda/ssd_object_detection.py --prototxt opencv-ssd-cuda/MobileNetSSD_deploy.prototxt --model opencv-ssd-cuda/MobileNetSSD_deploy.caffemodel --input example_videos/guitar.mp4 --display 0 --use-gpu=1
[INFO] setting preferable backend and target to CUDA...
[INFO] accessing video stream...
[INFO] elasped time: 34.94
[INFO] approx. FPS: 7.07
(py3cv4) nano@nano-desktop:~/Data/opencv/opencv-dnn-gpu-examples$ python opencv-ssd-cuda/ssd_object_detection.py --prototxt opencv-ssd-cuda/MobileNetSSD_deploy.prototxt --model opencv-ssd-cuda/MobileNetSSD_deploy.caffemodel --input example_videos/guitar.mp4 --display 0
[INFO] accessing video stream...
[INFO] elasped time: 81.08
[INFO] approx. FPS: 3.05

Yolo 測試
(py3cv4) nano@nano-desktop:~/Data/opencv/opencv-dnn-gpu-examples$ python3 opencv-yolo-cuda/yolo_object_detection.py -y opencv-yolo-cuda/yolo-coco -i example_videos/guitar.mp4 --display=0 --use-gpu=0
[INFO] loading YOLO from disk...
[INFO] setting preferable backend and target to CUDA...
[INFO] accessing video stream...
[INFO] elasped time: 212.71
[INFO] approx. FPS: 1.16
(py3cv4) nano@nano-desktop:~/Data/opencv/opencv-dnn-gpu-examples$ python3 opencv-yolo-cuda/yolo_object_detection.py -y opencv-yolo-cuda/yolo-coco -i example_videos/guitar.mp4 --display=0
[INFO] loading YOLO from disk...
[INFO] accessing video stream...
[INFO] elasped time: 1275.95
[INFO] approx. FPS: 0.19

Mask R-CNN 測試
(py3cv4) nano@nano-desktop:~/Data/opencv/opencv-dnn-gpu-examples$ python3 opencv-mask-rcnn-cuda/mask_rcnn_segmentation.py --mask-rcnn opencv-mask-rcnn-cuda/mask-rcnn-coco/ --input example_videos/guitar.mp4 --display=0 --use-gpu=1
[INFO] loading Mask R-CNN from disk...
[INFO] setting preferable backend and target to CUDA...
[INFO] accessing video stream...
[INFO] elasped time: 445.18
[INFO] approx. FPS: 0.55
(py3cv4) nano@nano-desktop:~/Data/opencv/opencv-dnn-gpu-examples$ python3 opencv-mask-rcnn-cuda/mask_rcnn_segmentation.py --mask-rcnn opencv-mask-rcnn-cuda/mask-rcnn-coco/ --input example_videos/guitar.mp4 --display=0
[INFO] loading Mask R-CNN from disk...
[INFO] accessing video stream...
[INFO] elasped time: 4512.29
[INFO] approx. FPS: 0.05

沒有留言:

張貼留言