Skip to content

Instantly share code, notes, and snippets.

@fengyuentau
Last active June 5, 2024 13:19
Show Gist options
  • Save fengyuentau/28b72e4b83ee192434d66059a1ef00af to your computer and use it in GitHub Desktop.
Save fengyuentau/28b72e4b83ee192434d66059a1ef00af to your computer and use it in GitHub Desktop.
Compiling and installing OpenCV DNN with CUDA (With Python Interface)
cmake -D CMAKE_BUILD_TYPE=RELEASE \
-D CMAKE_INSTALL_PREFIX=/home/tau/software/opencv-4.5.1 \
-D BUILD_opencv_python2=OFF \
-D BUILD_opencv_python3=ON \
-D PYTHON3_LIBRARY=/home/tau/anaconda3/envs/opencv_dnn_cuda/lib \
-D PYTHON3_INCLUDE_DIR=/home/tau/anaconda3/envs/opencv_dnn_cuda/include/python3.8 \
-D PYTHON3_EXECUTABLE=/home/tau/anaconda3/envs/opencv_dnn_cuda/bin/python3.8 \
-D INSTALL_PYTHON_EXAMPLES=OFF \
-D INSTALL_C_EXAMPLES=OFF \
-D BUILD_EXAMPLES=OFF \
-D WITH_CUDA=ON \
-D WITH_CUDNN=ON \
-D OPENCV_DNN_CUDA=ON \
-D ENABLE_FAST_MATH=1 \
-D CUDA_ARCH_BIN=7.5 \
-D WITH_CUBLAS=1 \
-D CUDA_TOOLKIT_ROOT_DIR=/usr/local/cuda-10.2 \
-D OPENCV_EXTRA_MODULES_PATH=/home/tau/software/src/opencv_contrib-4.5.1/modules \
-D OPENCV_GENERATE_PKGCONFIG=YES ..

Compiling and installing OpenCV DNN with CUDA

In this tutorial, we are compiling and install OpenCV 4.5.1 and OpenCV Contrib 4.5.1 with CUDA 10.2 and cuDNN 7.6.5. Other versions should aslo work.

Preparation

  • Download and extract the source code of OpenCV 4.5.1 and OpenCV Contrib 4.5.1. Assume that the root directory for each is $opencv-4.5.1 and $opencv_contrib-4.5.1.
  • Install CUDA 10.2 and cuDNN 7.6.5 following NVIDIA's guide. Assume that the root directory for CUDA is /usr/local/cuda-10.2.

Configuring using CMake

See build_with_cuda.sh and modify accordingly.

Compiling and Installing

make -j
make install

Installing Python Interface

After compiling and installing, there should be a file named cv2.cpython-38-x86_64-linux-gnu.so in $CMAKE_INSTALL_PREFIX/lib/python3.8/site-packages/cv2/python-3.8. Create a symlink to this file from your Python environment:

cd $PYTHON3_LIBRARY/python3.8/site-packages
ln -s $CMAKE_INSTALL_PREFIX/lib/python3.8/site-packages/cv2/python-3.8/cv2.cpython-38-x86_64-linux-gnu.so cv2.so

Test if Python Can Load OpenCV

Switch to the Python env you just installed OpenCV, then:

python
>>> import cv2
>>> cv2.__version__
`4.5.1`
@fengyuentau
Copy link
Author

fengyuentau commented Aug 10, 2023

@SixtyTrees I am still using the same script (but with a little modifications) to build the latest OpenCV with CUDA on my Jetson Nano board:

TIMESTAMP="release-4.8.0"

cmake -B build/${TIMESTAMP} \
    -D CMAKE_BUILD_TYPE=RELEASE \
    -D CMAKE_INSTALL_PREFIX=build/${TIMESTAMP}/install \
    -D BUILD_opencv_python2=OFF \
    -D BUILD_opencv_python3=ON \
    -D PYTHON3_LIBRARY=/usr/lib/aarch64-linux-gnu/libpython3.6m.so \
    -D PYTHON3_INCLUDE_DIR=/usr/include/python3.6m \
    -D PYTHON3_EXECUTABLE=/usr/bin/python3.6 \
    -D INSTALL_PYTHON_EXAMPLES=OFF \
    -D INSTALL_C_EXAMPLES=OFF \
    -D BUILD_DOCS=OFF \
    -D BUILD_PERF_TESTS=OFF \
    -D BUILD_TESTS=OFF \
    -D BUILD_EXAMPLES=OFF \
    -D WITH_CUDA=ON \
    -D WITH_CUDNN=ON \
    -D OPENCV_DNN_CUDA=ON \
    -D CUDA_FAST_MATH=ON \
    -D ENABLE_NEON=ON \
    -D OPENCV_DNN_CUDA=ON \
    -D ENABLE_FAST_MATH=1 \
    -D CUDA_ARCH_BIN=5.3 \
    -D WITH_CUBLAS=1 \
    -D CUDA_TOOLKIT_ROOT_DIR=/usr/local/cuda-10.2 \
    -D OPENCV_EXTRA_MODULES_PATH=/home/opencv-cn/Workspace/opencv/opencv_contrib/modules opencv

@madtunebk
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment