Skip to content

Instantly share code, notes, and snippets.

@rsippl
Last active April 15, 2021 18:20
Show Gist options
  • Save rsippl/12958200d6e2b138334aec4df5003fd1 to your computer and use it in GitHub Desktop.
Save rsippl/12958200d6e2b138334aec4df5003fd1 to your computer and use it in GitHub Desktop.
Build OpenCV 4 with CUDA support on Ubuntu 18.04 with Conda

Install dependencies:

sudo apt-get update
sudo apt-get upgrade
sudo apt-get install build-essential cmake unzip pkg-config
sudo apt-get install libjpeg-dev libpng-dev libtiff-dev
sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev libv4l-dev
sudo apt-get install libxvidcore-dev libx264-dev
sudo apt-get install libgtk-3-dev
sudo apt-get install libatlas-base-dev gfortran
sudo apt-get install python3-dev

Create Conda environment, create directories, clone OpenCV repos:

conda create -n opencv python=3.6 numpy
source activate opencv

cd Development
mkdir -p opencv/install
cd opencv/install
wget https://github.com/opencv/opencv/archive/4.0.0.tar.gz
tar xvzf 4.0.0.tar.gz
rm 4.0.0.tar.gz
wget https://github.com/opencv/opencv_contrib/archive/4.0.0.tar.gz
tar xvzf 4.0.0.tar.gz
rm 4.0.0.tar.gz

Install CUDA toolkit 9.0 at /usr/local/cuda-9.0 with --override, including all patches. Don't install accelerated driver. Stay away from CUDA toolkit versions newer than 9.0 for now.

Use the workaround for GCC 7 described here: https://carterturn.com/h/Guides/NVIDIA%20CUDA%209.1%20with%20GCC%207.2 i.e. modify host_config.h and floatn.h.

Set LD_LIBRARY on env activation:

mkdir -p $CONDA_PREFIX/etc/conda/activate.d
vi $CONDA_PREFIX/etc/conda/activate.d/activate.sh
#!/bin/sh

ORIGINAL_LD_LIBRARY_PATH=$LD_LIBRARY_PATH
export LD_LIBRARY_PATH=/usr/local/cuda-9.0/lib64:/usr/local/cuda-9.0/extras/CUPTI/lib64:/lib/nccl/cuda-9:$LD_LIBRARY_PATH
mkdir -p $CONDA_PREFIX/etc/conda/deactivate.d
vi $CONDA_PREFIX/etc/conda/deactivate.d/deactivate.sh
#!/bin/sh

export LD_LIBRARY_PATH=$ORIGINAL_LD_LIBRARY_PATH
unset ORIGINAL_LD_LIBRARY_PATH
source deactivate
source activate opencv

vi xcmake.sh
#!/bin/sh

WHERE_OPENCV=../opencv-4.0.0
WHERE_OPENCV_CONTRIB=../opencv_contrib-4.0.0

cmake -D CMAKE_BUILD_TYPE=RELEASE \
      -D CMAKE_INSTALL_PREFIX=$CONDA_PREFIX \
      -D WITH_CUDA=ON \
      -D CUDA_TOOLKIT_ROOT_DIR=/usr/local/cuda-9.0 \
      -D PYTHON3_EXECUTABLE=$CONDA_PREFIX/bin/python \
      -D INSTALL_C_EXAMPLES=ON \
      -D INSTALL_PYTHON_EXAMPLES=ON \
      -D OPENCV_EXTRA_MODULES_PATH=$WHERE_OPENCV_CONTRIB/modules \
      -D BUILD_EXAMPLES=ON $WHERE_OPENCV
chmod 755 xcmake.sh
mkdir build && cd build
../xcmake.sh && make -j4
make install

Verify using:

pkg-config --modversion opencv

ln -s $CONDA_PREFIX/python/cv2/python-3.6/cv2.cpython-36m-x86_64-linux-gnu.so $CONDA_PREFIX/lib/python3.6/site-packages/cv2.so

Verify that Python can use cv2:

python
>>> import cv2
>>> cv2.__version__
'4.0.0'

References:

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