Skip to content

Instantly share code, notes, and snippets.

@vicente-gonzalez-ruiz
Last active October 10, 2023 08:59
Show Gist options
  • Save vicente-gonzalez-ruiz/b53ab07b44c89f3f0b35ded4e1b72a8c to your computer and use it in GitHub Desktop.
Save vicente-gonzalez-ruiz/b53ab07b44c89f3f0b35ded4e1b72a8c to your computer and use it in GitHub Desktop.
  1. This recipe install OpenCV with CUDA suport in a virtual environment. See https://docs.opencv.org/4.x/d7/d9f/tutorial_linux_install.html and specifically https://docs.opencv.org/4.x/db/d05/tutorial_config_reference.html. This also helps: https://rodosingh.medium.com/using-cmake-to-build-and-install-opencv-for-python-and-c-in-ubuntu-20-04-6c5881eebd9a

  2. Install CUDA Toolkit from https://developer.nvidia.com/cuda-download. Use Download Installer for Linux Ubuntu 22.04 x86_64.

wget https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2204/x86_64/cuda-keyring_1.0-1_all.deb
sudo dpkg -i cuda-keyring_1.0-1_all.deb
sudo apt-get update
sudo apt-get -y install cuda
  1. Reboot and check that nvidia-smi is working.
  2. Create a Python environment, activate and install NumPy:
sudo apt install python3-venv # Currently, I'm using python 3.10
python3 -m venv opencv-cuda
source opencv-cuda/bin/activate
pip install numpy
  1. Install required packages for compiling OpenCV (see https://github.com/spmallick/learnopencv/tree/master/Getting-Started-OpenCV-CUDA-Module):
sudo apt-get update
sudo apt-get -y install build-essential cmake unzip pkg-config
sudo apt-get -y install libjpeg-dev libpng-dev libtiff-dev
sudo apt-get -y install libavcodec-dev libavformat-dev libswscale-dev libv4l-dev
sudo apt-get -y install libxvidcore-dev libx264-dev
sudo apt-get -y install libgtk-3-dev
sudo apt-get -y install libatlas-base-dev gfortran
sudo apt-get -y install python3-dev
  1. Download OpenCV:
mkdir ~/opencv_build && cd ~/opencv_build
wget https://github.com/opencv/opencv/archive/refs/heads/4.x.zip -O opencv.zip
wget https://github.com/opencv/opencv_contrib/archive/refs/heads/4.x.zip -O opencv_contrib.zip
  1. Unzip:
unzip opencv.zip
unzip opencv_contrib.zip
mv opencv-4.x opencv
mv opencv_contrib-4.x opencv_contrib
mkdir -p build && cd build
  1. Configure:
cmake \
-D WITH_CUDA=ON \
-D BUILD_TIFF=ON \
-D BUILD_opencv_java=OFF \
-D WITH_OPENGL=ON \
-D WITH_OPENCL=ON \
-D WITH_IPP=ON \
-D WITH_TBB=ON \
-D WITH_EIGEN=ON \
-D WITH_V4L=ON \
-D WITH_VTK=OFF \
-D BUILD_TESTS=OFF \
-D BUILD_PERF_TESTS=OFF \
-D CMAKE_BUILD_TYPE=RELEASE \
-D BUILD_opencv_python2=OFF \
-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 OPENCV_ENABLE_NONFREE=ON -D PYTHON3_EXECUTABLE=$(which python3) \
-D PYTHON_DEFAULT_EXECUTABLE=$(which python3) \
-D OPENCV_EXTRA_MODULES_PATH=~/opencv_build/opencv_contrib/modules \
-D PYTHON2_EXECUTABLE=NO \
-D CMAKE_INSTALL_PREFIX=$(python3 -c "import site; print(site.getsitepackages()[0])") \
..

You should see something similar to:

:
-- CUDA detected: 12.2
-- CUDA: Using CUDA_ARCH_BIN=5.0;5.2;6.0;6.1;7.0;7.5;8.0;8.6;8.9;9.0
:
--   NVIDIA CUDA:                   YES (ver 12.2, CUFFT CUBLAS)
--     NVIDIA GPU arch:             50 52 60 61 70 75 80 86 89 90
--     NVIDIA PTX archs:            90
-- 
--   cuDNN:                         NO
-- 
--   OpenCL:                        YES (no extra features)
--     Include path:                /home/vruiz/opencv_build/opencv/3rdparty/include/opencl/1.2
--     Link libraries:              Dynamic load
-- 
--   Python 3:
--     Interpreter:                 /shared/PEs/opencv-cuda/bin/python3 (ver 3.11.5)
--     Libraries:                   /usr/lib/x86_64-linux-gnu/libpython3.10.so (ver 3.11.5)
--     numpy:                       /shared/PEs/opencv-cuda/lib/python3.10/site-packages/numpy/core/include (ver 1.26.0)
--     install path:                /shared/PEs/opencv-cuda/lib/python3.10/site-packages/cv2/python-3.10
-- 
--   Python (for build):            /shared/PEs/opencv-cuda/bin/python3
:
  1. Compile:
make -j 20

The output should finish with something similar to:

:
[100%] Linking CXX shared module ../../lib/python3/cv2.cpython-310-x86_64-linux-gnu.so
[100%] Built target opencv_python3
  1. Install:
make install

Everything should be at:

/shared/PEs/opencv-cuda/lib/python3.10/site-packages/cv2   # The Python wraper for OpenCV
/shared/PEs/opencv-cuda/lib/python3.10/site-packages/bin   # OpenCV executables
/shared/PEs/opencv-cuda/lib/python3.10/site-packages/lib   # OpenCV libraries
/shared/PEs/opencv-cuda/lib/python3.10/site-packages/share # OpenCV aux files

See also:

install_manifest.txt

Notice that if you don't use "make clean" (or delete the "build" folder), you can keep compiled OpenCV and install it in a different virtual environment repeating the installation from the new envinroment.

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