Skip to content

Instantly share code, notes, and snippets.

@Ayke
Last active July 2, 2020 05:52
Show Gist options
  • Save Ayke/db70375a8637a4492b0be1ba322bc00e to your computer and use it in GitHub Desktop.
Save Ayke/db70375a8637a4492b0be1ba322bc00e to your computer and use it in GitHub Desktop.
OpenCV 4 Python Installation Notes for CUDA 10

Environment: Ubuntu 16.04, CUDA 10

The trickiest part is, OpenCV 4 only supports CUDA 10, not CUDA 10.1. So if you have installed CUDA 10.1, you have to get 10.0 instead.

Installation

Install required packages

sudo apt-get install gcc gfortran python-dev libopenblas-dev liblapack-dev cython

sudo apt-get install build-essential
sudo apt-get install cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev
sudo apt-get install python-dev python-numpy libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libjasper-dev libdc1394-22-dev qt5-default

sudo apt-get install -y \
    cmake \
    libavcodec-dev \
    libavformat-dev \
    libavutil-dev \
    libeigen3-dev \
    libglew-dev \
    libgtk2.0-dev \
    libgtk-3-dev \
    libjasper-dev \
    libjpeg-dev \
    libpng12-dev \
    libpostproc-dev \
    libswscale-dev \
    libtbb-dev \
    libtiff5-dev \
    libv4l-dev \
    libxvidcore-dev \
    libx264-dev \
    qt5-default \
    zlib1g-dev \
    libopenblas-base \
    libopenblas-dev \
    pkg-config

sudo apt-get install gtk2-devel
sudo apt-get install libv4l-devel
sudo apt-get install ffmpeg-devel
sudo apt-get install gstreamer-plugins-base-devel

Download OpenCV file

sudo apt-get install git
git clone https://github.com/opencv/opencv.git
git clone https://github.com/opencv/opencv_contrib.git

The first is OpenCV main modules, the second is extra modules.

After going into the OpenCV folder

mkdir build
cd build
cmake -D CMAKE_BUILD_TYPE=Release \
      -D WITH_TBB=ON \
      -D WITH_CUDA=ON \
      -D ENABLE_FAST_MATH=ON \
      -D CUDA_FAST_MATH=ON \
      -D WITH_NVCUVID=ON \
      -D WITH_CUBLAS=ON \
      -D WITH_V4L=ON \
      -D WITH_OPENGL=ON \
      -D WITH_QT=ON \
      -D WITH_GSTREAMER=ON \
      -D BUILD_opencv_cudacodec=OFF \
      -D OPENCV_GENERATE_PKGCONFIG=ON \
      -D OPENCV_EXTRA_MODULES_PATH=../opencv_contrib/modules/ \
      ..
sudo make -j7
sudo make install

Possible Errors

"libcudart.so.8.0: cannot open shared object file: No such file or directory" when you try to "import cv2 as cv"

This happends because you have install old version OpenCV which requires CUDA 8.0. Install OpenCV4 will solve the problem. Please be ware that OpenCV 4 only supports CUDA 10.0, not CUDA 10.1.

If you have installed CUDA 8.0 and do want to use old version OpenCV, though it's out of the topic, a good suggestion is to take good care of LD_LIBRARY_PATH and PATH. My example:

export LD_LIBRARY_PATH="$LD_LIBRARY_PATH:/usr/local/cuda/lib64:/usr/local/cuda/extras/CUPTI/lib64:/usr/local/cuda-10.0/lib64"
export PATH=$PATH:"/usr/local/cuda-10.0/bin"

Add them into ~/.bashrc to make it permanent

Also, make sure you've done with shared linker config:

vim /etc/ld.so.conf

check if CUDA 8 is correctly configured, then

sudo ldconfig

CMake Error at /usr/local/lib/cmake/gflags/gflags-targets.cmake:82 (message): The imported target "gflags_static" references the file "/usr/local/lib/libgflags.a"

The error is that /usr/local/lib/libgflags.a is not found. This is a file for gflags, so the solution is to reinstall "gflags". The readme file in gflags project doesn't contain specific instruction on installation, we'll install by cmake, and use the solution given by BVLC/caffe#2171.

git clone https://github.com/gflags/gflags
cd gflags
mkdir build
cmake ..
vim CMakeCache.txt

Find CMAKE_CXX_FLAGS:STRING and change it to CMAKE_CXX_FLAGS:STRING=-fPIC.

cmake ..
make
make install

Then you may resume the installation of opencv.

After installing opencv, failed to import cv2 in python: ImportError: numpy.core.multiarray failed to import

Try import numpy then numpy to check where the numpy is. For me, I happened to installed two numpy at the same time. One is at /usr/local/lib/python3.5/dist-packages, the other is /home/xxx/.local/lib/python3.5/. After I ran pip uninstall numpy, the latter was removed and everything goes well.

When trying to use cv::cuda::CascadeClassifier, an error on running: "error: (-216:No CUDA support) The library is compiled without CUDA support in function 'throw_no_cuda'"

This appears because the opencv was installed without "-D WITH_CUDA=ON" or "-D WITH_NVCUVID=ON". Reinstall opencv these options.

If you have installed opencv with "WITH_CUDA=ON", and this message pops up when you try to use "cv::cuda::CascadeClassifier" while everything else in "cv::cuda" works, then I have a very bad news for you: cuda::CascadeClassifier is no longer supported in OpenCV 4.1. You have to install OpenCV 3.4 in order to use this feature.

Build fail when "-D WITH_CUDA=ON", "nvcuvid.h: No such file or directory"

Add option "-D BUILD_opencv_cudacodec=OFF" in cmake (see my script above)

If you want to install cudacodec: https://developer.nvidia.com/nvidia-video-codec-sdk/download

CMake Warning at cmake/OpenCVFindLAPACK.cmake:29 (message): LAPACK(OpenBLAS): CBLAS/LAPACK headers are not found in '/usr/include'

sudo apt-get install liblapacke-dev
sudo cp /usr/include/lapacke*.h /usr/include/openblas
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment