Install OpenCV with CUDA for Conda

Everytime I compile OpenCV from source, I hate myself for not writing this up before. Not doing the same mistake again. There are great tutorials on installing OpenCV by PyImage Search (see References), however they work for system-level Python with virtualenv. These are my notes on building OpenCV 3 with CUDA on Ubuntu 16.04 with Anaconda environment in case those tutorials did not work, e.g. you cannot find the cv2.so file.

Setup

This assumes a running Anaconda distribution as the default Python environment (check which python ). If you want to install with CUDA support, CUDA and CuDNN libraries should be installed and enabled (check nvcc --version ).

Install dependencies:

$ sudo apt-get update
$ sudo apt-get upgrade
$ sudo apt-get install build-essential cmake pkg-config
$ sudo apt-get install libjpeg8-dev libtiff5-dev libjasper-dev libpng12-dev
$ sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev libv4l-dev
$ sudo apt-get install libxvidcore-dev libx264-dev libgtk-3-dev
$ sudo apt-get install libatlas-base-dev gfortran

Optional dependencencies (not necessary unless you know why you need them):

$ sudo apt-get install libopenblas-dev liblapack-dev libeigen3-dev libtheora-dev libvorbis-dev sphinx-common
$ sudo apt-get libtbb-dev yasm libopencore-amrnb-dev libopencore-amrwb-dev libopenexr-dev libgstreamer-plugins-base1.0-dev
$ sudo apt-get libavutil-dev libavfilter-dev libavresample-dev ffmpeg

Download and unzip OpenCV and OpenCV's extra modules:

$ cd
$ mkdir opencv
$ cd opencv
$ wget https://github.com/opencv/opencv/archive/3.4.5.zip -O opencv.zip
$ wget https://github.com/opencv/opencv_contrib/archive/3.4.5.zip -O opencv_contrib.zip
$ unzip opencv.zip
$ unzip opencv_contrib.zip
$ rm -f *.zip

Create new Conda environment and install NumPy:

$ conda create -n cv python=3.6
$ source activate cv
(cv) $ conda install numpy

Locate environment-specific Python directories:

(cv) $ export python_exec=`which python`
(cv) $ export include_dir=`python -c "from distutils.sysconfig import get_python_inc; print(get_python_inc())"`
(cv) $ export library=`python -c "import distutils.sysconfig as sysconfig; print(sysconfig.get_config_var('LIBDIR'))"`
(cv) $ export default_exec=`which python3.6`

Configure and install

Configure build with CMake:

(cv) $ cd ~/opencv/opencv-3.4.5
(cv) $ mkdir build
(cv) $ cd build
(cv) $ cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D WITH_CUDA=ON -D ENABLE_FAST_MATH=1 -D CUDA_FAST_MATH=1 -D WITH_CUBLAS=1 -D INSTALL_PYTHON_EXAMPLES=ON -D OPENCV_EXTRA_MODULES_PATH=../../opencv_contrib-3.4.5/modules -D PYTHON_EXECUTABLE=$python_exec -D PYTHON_DEFAULT_EXECUTABLE=$default_exec -D PYTHON_INCLUDE_DIRS=$include_dir -D PYTHON_LIBRARY=$library -D BUILD_EXAMPLES=ON ..

At this point, it is important to double check the output of CMake. Make sure your output contains something along those lines:

--   Python 3:
--     Interpreter:                 /home/daniel/anaconda3/envs/cv/bin/python (ver 3.6)
--     Libraries:                   /home/daniel/anaconda3/envs/cv/lib/libpython3.6m.so (ver 3.6.8)
--     numpy:                       /home/daniel/anaconda3/envs/cv/lib/python3.6/site-packages/numpy/core/include (ver 1.15.4)
--     packages path:               /home/daniel/anaconda3/envs/cv/lib/python3.6/site-packages

Unless you see the appropriate paths to Interpreter and Libraries, do not proceed.

Otherwise, proceed as usual:

(cv) $ make -j6
(cv) $ sudo make install
(cv) $ sudo ldconfig

Finally, OpenCV should be located in /usr/local/lib/python3.6/site-packages and you can symlink the cv2 directory to your Conda environment(s):

(cv) $ ln -s /usr/local/lib/python3.6/site-packages/cv2 ~/anaconda3/envs/cv/lib/python3.6/site-packages/cv2

Test your installation.

References