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.
- Download and extract the source code of
OpenCV 4.5.1
andOpenCV 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
andcuDNN 7.6.5
following NVIDIA's guide. Assume that the root directory for CUDA is/usr/local/cuda-10.2
.
See build_with_cuda.sh
and modify accordingly.
make -j
make install
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
Switch to the Python env you just installed OpenCV, then:
python
>>> import cv2
>>> cv2.__version__
`4.5.1`
(1) Flag -D BUILD_opencv_python2=OFF is ignored:
-- Found PythonInterp: /usr/bin/python2.7 (found suitable version "2.7.17", minimum required is "2.7")
-- Found PythonLibs: /usr/lib/x86_64-linux-gnu/libpython2.7.so (found suitable exact version "2.7.17")
(2) -- Found PythonLibs: /home/stepan/anaconda3/envs/acne/lib (found suitable exact version "3.8.13") # is required, but
ImportError: No module named numpy.distutils # To fix, downgrade to Python 3.7.
So, what am I supposed to do?
(3)
-- Performing Test HAVE_C_WNON_VIRTUAL_DTOR
-- Performing Test HAVE_C_WNON_VIRTUAL_DTOR - Failed
-- Performing Test HAVE_CXX_WMISSING_PROTOTYPES
-- Performing Test HAVE_CXX_WMISSING_PROTOTYPES - Failed
-- Performing Test HAVE_CXX_WSTRICT_PROTOTYPES
-- Performing Test HAVE_CXX_WSTRICT_PROTOTYPES - Failed
etc
Eventually make -j fails to build with compiler version must not be greater than 6
Fixed it by adding a flag
cmake -DCMAKE_C_COMPILER=/usr/bin/gcc-6 \
Now it fails at 22% with
error: calling a device function("operator float") from a host function("clipped_relu") is not allowed
detected during instantiation of "void cv::dnn::cuda4dnn::kernels::clipped_relu(const cv::dnn::cuda4dnn::csl::Stream &, cv::dnn::cuda4dnn::csl::Span, cv::dnn::cuda4dnn::csl::View, T, T) [with T=__half]"
(295): here
The instructions provided above are NOT correct.