Skip to content

Instantly share code, notes, and snippets.

@raulqf
Last active April 8, 2024 02:21
Show Gist options
  • Save raulqf/f42c718a658cddc16f9df07ecc627be7 to your computer and use it in GitHub Desktop.
Save raulqf/f42c718a658cddc16f9df07ecc627be7 to your computer and use it in GitHub Desktop.
How to install OpenCV 4.5 with CUDA 11.2 in Ubuntu 22.04

How to install OpenCV 4.5.2 with CUDA 11.2 and CUDNN 8.2 in Ubuntu 22.04

First of all install update and upgrade your system:

    $ sudo apt update
    $ sudo apt upgrade

Then, install required libraries:

  • Generic tools:

      $ sudo apt install build-essential cmake pkg-config unzip yasm git checkinstall
    
  • Image I/O libs

    $ sudo apt install libjpeg-dev libpng-dev libtiff-dev
    
  • Video/Audio Libs - FFMPEG, GSTREAMER, x264 and so on.

    $ sudo apt install libavcodec-dev libavformat-dev libswscale-dev libavresample-dev
    $ sudo apt install libgstreamer1.0-dev libgstreamer-plugins-base1.0-dev
    $ sudo apt install libxvidcore-dev x264 libx264-dev libfaac-dev libmp3lame-dev libtheora-dev 
    $ sudo apt install libfaac-dev libmp3lame-dev libvorbis-dev
    
  • OpenCore - Adaptive Multi Rate Narrow Band (AMRNB) and Wide Band (AMRWB) speech codec

    $ sudo apt install libopencore-amrnb-dev libopencore-amrwb-dev
    
  • Cameras programming interface libs

    $ sudo apt-get install libdc1394-22 libdc1394-22-dev libxine2-dev libv4l-dev v4l-utils
    $ cd /usr/include/linux
    $ sudo ln -s -f ../libv4l1-videodev.h videodev.h
    $ cd ~
    
  • GTK lib for the graphical user functionalites coming from OpenCV highghui module

    $ sudo apt-get install libgtk-3-dev
    
  • Python libraries for python3:

    $ sudo apt-get install python3-dev python3-pip
    $ sudo -H pip3 install -U pip numpy
    $ sudo apt install python3-testresources
    
  • Parallelism library C++ for CPU

    $ sudo apt-get install libtbb-dev
    
  • Optimization libraries for OpenCV

    $ sudo apt-get install libatlas-base-dev gfortran
    
  • Optional libraries:

    $ sudo apt-get install libprotobuf-dev protobuf-compiler
    $ sudo apt-get install libgoogle-glog-dev libgflags-dev
    $ sudo apt-get install libgphoto2-dev libeigen3-dev libhdf5-dev doxygen
    

We will now proceed with the installation (see the Qt flag that is disabled to do not have conflicts with Qt5.0).

$ cd ~/Downloads
$ wget -O opencv.zip https://github.com/opencv/opencv/archive/refs/tags/4.5.2.zip
$ wget -O opencv_contrib.zip https://github.com/opencv/opencv_contrib/archive/refs/tags/4.5.2.zip
$ unzip opencv.zip
$ unzip opencv_contrib.zip

$ echo "Create a virtual environtment for the python binding module (OPTIONAL)"
$ sudo pip install virtualenv virtualenvwrapper
$ sudo rm -rf ~/.cache/pip
$ echo "Edit ~/.bashrc"
$ export WORKON_HOME=$HOME/.virtualenvs
$ export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3
$ source /usr/local/bin/virtualenvwrapper.sh
$ mkvirtualenv cv -p python3
$ pip install numpy

$ echo "Procced with the installation"
$ cd opencv-4.5.2
$ mkdir build
$ cd build

cmake -D CMAKE_BUILD_TYPE=RELEASE \
-D CMAKE_INSTALL_PREFIX=/usr/local \
-D WITH_TBB=ON \
-D ENABLE_FAST_MATH=1 \
-D CUDA_FAST_MATH=1 \
-D WITH_CUBLAS=1 \
-D WITH_CUDA=ON \
-D BUILD_opencv_cudacodec=OFF \
-D WITH_CUDNN=ON \
-D OPENCV_DNN_CUDA=ON \
-D CUDA_ARCH_BIN=7.5 \
-D WITH_V4L=ON \
-D WITH_QT=OFF \
-D WITH_OPENGL=ON \
-D WITH_GSTREAMER=ON \
-D OPENCV_GENERATE_PKGCONFIG=ON \
-D OPENCV_PC_FILE_NAME=opencv.pc \
-D OPENCV_ENABLE_NONFREE=ON \
-D OPENCV_PYTHON3_INSTALL_PATH=~/.virtualenvs/cv/lib/python3.8/site-packages \
-D PYTHON_EXECUTABLE=~/.virtualenvs/cv/bin/python \
-D OPENCV_EXTRA_MODULES_PATH=~/Downloads/opencv_contrib-4.5.2/modules \
-D INSTALL_PYTHON_EXAMPLES=OFF \
-D INSTALL_C_EXAMPLES=OFF \
-D BUILD_EXAMPLES=OFF ..

If you want to build the libraries statically you only have to include the -D BUILD_SHARED_LIBS=OFF

$ cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_C_COMPILER=/usr/bin/gcc-6 -D CMAKE_INSTALL_PREFIX=/usr/local -D INSTALL_PYTHON_EXAMPLES=ON -D INSTALL_C_EXAMPLES=OFF -D WITH_TBB=ON -D WITH_CUDA=ON -D BUILD_opencv_cudacodec=OFF -D ENABLE_FAST_MATH=1 -D CUDA_FAST_MATH=1 -D WITH_CUBLAS=1 -D WITH_V4L=ON -D WITH_QT=OFF -D WITH_OPENGL=ON -D WITH_GSTREAMER=ON -D OPENCV_GENERATE_PKGCONFIG=ON -D OPENCV_PC_FILE_NAME=opencv.pc -D OPENCV_ENABLE_NONFREE=ON -D OPENCV_PYTHON3_INSTALL_PATH=~/.virtualenvs/cv/lib/python3.8/site-packages -D OPENCV_EXTRA_MODULES_PATH=~/downloads/opencv/opencv_contrib-4.5.2/modules -D PYTHON_EXECUTABLE=~/.virtualenvs/cv/bin/python -D BUILD_EXAMPLES=ON -D BUILD_SHARED_LIBS=OFF ..

In case you do not want to include include CUDA set -D WITH_CUDA=OFF

$ cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_C_COMPILER=/usr/bin/gcc-6 -D CMAKE_INSTALL_PREFIX=/usr/local -D INSTALL_PYTHON_EXAMPLES=ON -D INSTALL_C_EXAMPLES=OFF -D WITH_TBB=ON -D WITH_CUDA=OFF -D BUILD_opencv_cudacodec=OFF -D ENABLE_FAST_MATH=1 -D CUDA_FAST_MATH=1 -D WITH_CUBLAS=1 -D WITH_V4L=ON -D WITH_QT=OFF -D WITH_OPENGL=ON -D WITH_GSTREAMER=ON -D OPENCV_GENERATE_PKGCONFIG=ON -D OPENCV_PC_FILE_NAME=opencv.pc -D OPENCV_ENABLE_NONFREE=ON -D OPENCV_PYTHON3_INSTALL_PATH=~/.virtualenvs/cv/lib/python3.8/site-packages -D OPENCV_EXTRA_MODULES_PATH=~/downloads/opencv/opencv_contrib-4.5.2/modules -D PYTHON_EXECUTABLE=~/.virtualenvs/cv/bin/python -D BUILD_EXAMPLES=ON ..

If you want also to use CUDNN you must include those flags (to set the correct value of CUDA_ARCH_BIN you must visit https://developer.nvidia.com/cuda-gpus and find the Compute Capability CC of your graphic card). If you have problems with the setting up of CUDDN check the List of documented problems:

-D WITH_CUDNN=ON \
-D OPENCV_DNN_CUDA=ON \
-D CUDA_ARCH_BIN=7.5 \

Before the compilation you must check that CUDA has been enabled in the configuration summary printed on the screen. (If you have problems with the CUDA Architecture go to the end of the document).

--   NVIDIA CUDA:                   YES (ver 11.2, CUFFT CUBLAS FAST_MATH)
--     NVIDIA GPU arch:             75
--     NVIDIA PTX archs:
-- 
--   cuDNN:                         YES (ver 8.2.0)

I've included below the output of my configuration.

If it is fine proceed with the compilation (Use nproc to know the number of cpu cores):

$ nproc
$ make -j8
$ sudo make install

Include the libs in your environment

$ sudo /bin/bash -c 'echo "/usr/local/lib" >> /etc/ld.so.conf.d/opencv.conf'
$ sudo ldconfig

If you want to have available opencv python bindings in the system environment you should copy the created folder during the installation of OpenCV (* -D OPENCV_PYTHON3_INSTALL_PATH=~/.virtualenvs/cv/lib/python3.8/site-packages *) into the dist-packages folder of the target python interpreter:

$ sudo cp -r ~/.virtualenvs/cv/lib/python3.8/site-packages/cv2 /usr/local/lib/python3.8/dist-packages

$ echo "Modify config-3.8.py to point to the target directory" 
$ sudo nano /usr/local/lib/python3.8/dist-packages/cv2/config-3.8.py 

``` 
    PYTHON_EXTENSIONS_PATHS = [
    os.path.join('/usr/local/lib/python3.8/dist-packages/cv2', 'python-3.8')
    ] + PYTHON_EXTENSIONS_PATHS
``` 

Additional Support

@keaneflynn has created a repository that contains a bash script with all the steps to build and install the libraries and a python script to test it over a mp4 video, already attached. Main peculiarity of his installation is the explicitly definition of the gcc and g++ versions. Other folks have also reported incompatibility problems with g++, as @keaneflynn, so I've found interesting to include his repo as an additional support.

EXAMPLE TO TEST OPENCV 4.5.2 with GPU in C++

Verify the installation by compiling and executing the following example:

#include <iostream>
#include <ctime>
#include <cmath>
#include "bits/time.h"

#include <opencv2/core.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/imgcodecs.hpp>

#include <opencv2/core/cuda.hpp>
#include <opencv2/cudaarithm.hpp>
#include <opencv2/cudaimgproc.hpp>

#define TestCUDA true

int main() {
    std::clock_t begin = std::clock();

        try {
            cv::String filename = "/home/raul/Pictures/Screenshot_20170317_105454.png";
            cv::Mat srcHost = cv::imread(filename, cv::IMREAD_GRAYSCALE);

            for(int i=0; i<1000; i++) {
                if(TestCUDA) {
                    cv::cuda::GpuMat dst, src;
                    src.upload(srcHost);

                    //cv::cuda::threshold(src,dst,128.0,255.0, CV_THRESH_BINARY);
                    cv::cuda::bilateralFilter(src,dst,3,1,1);

                    cv::Mat resultHost;
                    dst.download(resultHost);
                } else {
                    cv::Mat dst;
                    cv::bilateralFilter(srcHost,dst,3,1,1);
                }
            }

            //cv::imshow("Result",resultHost);
            //cv::waitKey();

        } catch(const cv::Exception& ex) {
            std::cout << "Error: " << ex.what() << std::endl;
        }

    std::clock_t end = std::clock();
    std::cout << double(end-begin) / CLOCKS_PER_SEC  << std::endl;
}

Compile and execute:

$ g++ test.cpp `pkg-config opencv --cflags --libs` -o test
$ ./test

Configuration information

This configuration has been defined without using the virtualenvironment. So, opencv python bindings has been directly installed in the system.

Configuration arguments:

cmake -D CMAKE_BUILD_TYPE=RELEASE \
-D CMAKE_INSTALL_PREFIX=/usr/local \
-D WITH_TBB=ON \
-D ENABLE_FAST_MATH=1 \
-D CUDA_FAST_MATH=1 \
-D WITH_CUBLAS=1 \
-D WITH_CUDA=ON \
-D BUILD_opencv_cudacodec=OFF \
-D WITH_CUDNN=ON \
-D OPENCV_DNN_CUDA=ON \
-D CUDA_ARCH_BIN=7.5 \
-D WITH_V4L=ON \
-D WITH_QT=OFF \
-D WITH_OPENGL=ON \
-D WITH_GSTREAMER=ON \
-D OPENCV_GENERATE_PKGCONFIG=ON \
-D OPENCV_PC_FILE_NAME=opencv.pc \
-D OPENCV_ENABLE_NONFREE=ON \
-D OPENCV_PYTHON3_INSTALL_PATH=/usr/lib/python3/dist-packages \
-D PYTHON_EXECUTABLE=/usr/bin/python3 \
-D OPENCV_EXTRA_MODULES_PATH=~/Downloads/opencv_contrib-4.5.2/modules \
-D INSTALL_PYTHON_EXAMPLES=OFF \
-D INSTALL_C_EXAMPLES=OFF \
-D BUILD_EXAMPLES=OFF ..

General Configuration output:

-- General configuration for OpenCV 4.5.2 =====================================
--   Version control:               unknown
-- 
--   Extra modules:
--     Location (extra):            /home/raul/Downloads/opencv_contrib-4.5.2/modules
--     Version control (extra):     unknown
-- 
--   Platform:
--     Timestamp:                   2021-06-25T09:31:43Z
--     Host:                        Linux 5.4.0-77-generic x86_64
--     CMake:                       3.16.3
--     CMake generator:             Unix Makefiles
--     CMake build tool:            /usr/bin/make
--     Configuration:               RELEASE
-- 
--   CPU/HW features:
--     Baseline:                    SSE SSE2 SSE3
--       requested:                 SSE3
--     Dispatched code generation:  SSE4_1 SSE4_2 FP16 AVX AVX2 AVX512_SKX
--       requested:                 SSE4_1 SSE4_2 AVX FP16 AVX2 AVX512_SKX
--       SSE4_1 (17 files):         + SSSE3 SSE4_1
--       SSE4_2 (2 files):          + SSSE3 SSE4_1 POPCNT SSE4_2
--       FP16 (1 files):            + SSSE3 SSE4_1 POPCNT SSE4_2 FP16 AVX
--       AVX (5 files):             + SSSE3 SSE4_1 POPCNT SSE4_2 AVX
--       AVX2 (31 files):           + SSSE3 SSE4_1 POPCNT SSE4_2 FP16 FMA3 AVX AVX2
--       AVX512_SKX (7 files):      + SSSE3 SSE4_1 POPCNT SSE4_2 FP16 FMA3 AVX AVX2 AVX_512F AVX512_COMMON AVX512_SKX
-- 
--   C/C++:
--     Built as dynamic libs?:      YES
--     C++ standard:                11
--     C++ Compiler:                /usr/bin/c++  (ver 9.3.0)
--     C++ flags (Release):         -fsigned-char -ffast-math -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wuninitialized -Wsuggest-override -Wno-delete-non-virtual-dtor -Wno-comment -Wimplicit-fallthrough=3 -Wno-strict-overflow -fdiagnostics-show-option -Wno-long-long -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections  -msse -msse2 -msse3 -fvisibility=hidden -fvisibility-inlines-hidden -O3 -DNDEBUG  -DNDEBUG
--     C++ flags (Debug):           -fsigned-char -ffast-math -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wuninitialized -Wsuggest-override -Wno-delete-non-virtual-dtor -Wno-comment -Wimplicit-fallthrough=3 -Wno-strict-overflow -fdiagnostics-show-option -Wno-long-long -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections  -msse -msse2 -msse3 -fvisibility=hidden -fvisibility-inlines-hidden -g  -O0 -DDEBUG -D_DEBUG
--     C Compiler:                  /usr/bin/cc
--     C flags (Release):           -fsigned-char -ffast-math -W -Wall -Werror=return-type -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wuninitialized -Wno-comment -Wimplicit-fallthrough=3 -Wno-strict-overflow -fdiagnostics-show-option -Wno-long-long -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections  -msse -msse2 -msse3 -fvisibility=hidden -O3 -DNDEBUG  -DNDEBUG
--     C flags (Debug):             -fsigned-char -ffast-math -W -Wall -Werror=return-type -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wuninitialized -Wno-comment -Wimplicit-fallthrough=3 -Wno-strict-overflow -fdiagnostics-show-option -Wno-long-long -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections  -msse -msse2 -msse3 -fvisibility=hidden -g  -O0 -DDEBUG -D_DEBUG
--     Linker flags (Release):      -Wl,--exclude-libs,libippicv.a -Wl,--exclude-libs,libippiw.a   -Wl,--gc-sections -Wl,--as-needed  
--     Linker flags (Debug):        -Wl,--exclude-libs,libippicv.a -Wl,--exclude-libs,libippiw.a   -Wl,--gc-sections -Wl,--as-needed  
--     ccache:                      NO
--     Precompiled headers:         NO
--     Extra dependencies:          m pthread cudart_static dl rt nppc nppial nppicc nppidei nppif nppig nppim nppist nppisu nppitc npps cublas cudnn cufft -L/usr/local/cuda/lib64 -L/usr/lib/x86_64-linux-gnu
--     3rdparty dependencies:
-- 
--   OpenCV modules:
--     To be built:                 alphamat aruco bgsegm bioinspired calib3d ccalib core cudaarithm cudabgsegm cudafeatures2d cudafilters cudaimgproc cudalegacy cudaobjdetect cudaoptflow cudastereo cudawarping cudev datasets dnn dnn_objdetect dnn_superres dpm face features2d flann freetype fuzzy gapi hdf hfs highgui img_hash imgcodecs imgproc intensity_transform line_descriptor mcc ml objdetect optflow phase_unwrapping photo plot python3 quality rapid reg rgbd saliency sfm shape stereo stitching structured_light superres surface_matching text tracking ts video videoio videostab wechat_qrcode xfeatures2d ximgproc xobjdetect xphoto
--     Disabled:                    cudacodec world
--     Disabled by dependency:      -
--     Unavailable:                 cnn_3dobj cvv java julia matlab ovis python2 viz
--     Applications:                tests perf_tests apps
--     Documentation:               NO
--     Non-free algorithms:         YES
-- 
--   GUI: 
--     GTK+:                        YES (ver 3.24.20)
--       GThread :                  YES (ver 2.64.6)
--       GtkGlExt:                  NO
--     OpenGL support:              NO
--     VTK support:                 NO
-- 
--   Media I/O: 
--     ZLib:                        /usr/lib/x86_64-linux-gnu/libz.so (ver 1.2.11)
--     JPEG:                        /usr/lib/x86_64-linux-gnu/libjpeg.so (ver 80)
--     WEBP:                        build (ver encoder: 0x020f)
--     PNG:                         /usr/lib/x86_64-linux-gnu/libpng.so (ver 1.6.37)
--     TIFF:                        /usr/lib/x86_64-linux-gnu/libtiff.so (ver 42 / 4.1.0)
--     JPEG 2000:                   build (ver 2.4.0)
--     OpenEXR:                     build (ver 2.3.0)
--     HDR:                         YES
--     SUNRASTER:                   YES
--     PXM:                         YES
--     PFM:                         YES
-- 
--   Video I/O:
--     DC1394:                      YES (2.2.5)
--     FFMPEG:                      YES
--       avcodec:                   YES (58.54.100)
--       avformat:                  YES (58.29.100)
--       avutil:                    YES (56.31.100)
--       swscale:                   YES (5.5.100)
--       avresample:                YES (4.0.0)
--     GStreamer:                   YES (1.16.2)
--     v4l/v4l2:                    YES (linux/videodev2.h)
-- 
--   Parallel framework:            TBB (ver 2020.1 interface 11101)
-- 
--   Trace:                         YES (with Intel ITT)
-- 
--   Other third-party libraries:
--     Intel IPP:                   2020.0.0 Gold [2020.0.0]
--            at:                   /home/raul/Downloads/opencv-4.5.2/build/3rdparty/ippicv/ippicv_lnx/icv
--     Intel IPP IW:                sources (2020.0.0)
--               at:                /home/raul/Downloads/opencv-4.5.2/build/3rdparty/ippicv/ippicv_lnx/iw
--     VA:                          YES
--     Lapack:                      NO
--     Eigen:                       YES (ver 3.3.7)
--     Custom HAL:                  NO
--     Protobuf:                    build (3.5.1)
-- 
--   NVIDIA CUDA:                   YES (ver 11.2, CUFFT CUBLAS FAST_MATH)
--     NVIDIA GPU arch:             75
--     NVIDIA PTX archs:
-- 
--   cuDNN:                         YES (ver 8.2.0)
-- 
--   OpenCL:                        YES (INTELVA)
--     Include path:                /home/raul/Downloads/opencv-4.5.2/3rdparty/include/opencl/1.2
--     Link libraries:              Dynamic load
-- 
--   Python 3:
--     Interpreter:                 /usr/bin/python3 (ver 3.8.5)
--     Libraries:                   /usr/lib/x86_64-linux-gnu/libpython3.8.so (ver 3.8.5)
--     numpy:                       /usr/local/lib/python3.8/dist-packages/numpy/core/include (ver 1.21.0)
--     install path:                /usr/lib/python3/dist-packages/cv2/python-3.8
-- 
--   Python (for build):            /usr/bin/python3
-- 
--   Java:                          
--     ant:                         NO
--     JNI:                         NO
--     Java wrappers:               NO
--     Java tests:                  NO
-- 
--   Install to:                    /usr/local
-- -----------------------------------------------------------------

List of documented problems

If you have problems with unsupported architectures of your graphic card with the minimum requirements from Opencv, you will get the following error:

CUDA backend for DNN module requires CC 5.3 or higher.  Please remove unsupported architectures from CUDA_ARCH_BIN option.

It means that the DNN module needs that your graphic card supports the 5.3 Compute Capability (CC) version; in this link you can fint the CC of your card. Some opencv versions have fixed the minimum version to 3.0 but there is a clear move to filter above 5.3 since the half-precision precision operations are available from 5.3 version. To fix this problem you can modify the CMakeList.txt file located in opencv > modules > dnn > CMakeList.txt and set the minimum version to the one you have, but bear in mind that the correct functioning of this module will be compromised. However, if you only want GPU for the rest of modules, it could work.

You can also select the target CUDA_ARCH_BIN option in the command to generate the makefile for your current target or modify the list of supported architectures:

$ grep -r 'CUDA_ARCH_BIN' .  //That prompts ./CMakeCache.txt

The restriction is to have a higher version than 5.3, so you can modify the file by removing all the inferior arch to 5.3

CUDA_ARCH_BIN:STRING=6.0 6.1 7.0 7.5

Now, the makefile was created succesfully. Before the compilation you must check that CUDA has been enabled in the configuration summary printed on the screen.

--   NVIDIA CUDA:                   YES (ver 10.0, CUFFT CUBLAS NVCUVID FAST_MATH)
--     NVIDIA GPU arch:             60 61 70 75
--     NVIDIA PTX archs:

Some users as TAF2 had problems when configuring CUDNN libraries but it was solved and here is the TAF2's proposal, you can also find it in the comments:

sudo apt install libcudnn7-dev  libcudnn7-doc  libcudnn7 nvidia-container-csv-cudnn
 -D CUDNN_INCLUDE_DIR=/usr/include \
-D CUDNN_LIBRARY=/usr/lib64/libcudnn_static_v7.a \
-D CUDNN_VERSION=7.6.3

If you have any other problem try updating the nvidia drivers.

Source

@raulqf
Copy link
Author

raulqf commented Sep 11, 2021

Hello everyone!
Thank you for this gist. I followed this guide. I didn't forget to run the "sudo make install"command. But the cv2 directory was not created. Perhaps someone will be helped by a solution that eventually worked for me. So, the initial data:

CUDA 11.4
cuDNN 8.2.2 
Ubuntu 20.04
OpenCV 4.5.3-dev

After I failed with this guide, I executed the following commands:

git clone https://github.com/opencv/opencv.git
git clone https://github.com/opencv/opencv_contrib.git
cd ~/opencv_build/opencv
mkdir -p build && cd build
cmake -D CMAKE_BUILD_TYPE=RELEASE \
    -D CMAKE_INSTALL_PREFIX=/usr/local \
    -D INSTALL_C_EXAMPLES=ON \
    -D INSTALL_PYTHON_EXAMPLES=ON \
    -D OPENCV_GENERATE_PKGCONFIG=ON \
    -D OPENCV_EXTRA_MODULES_PATH=~/opencv_build/opencv_contrib/modules \
    -D ENABLE_FAST_MATH=1 \
    -D CUDA_FAST_MATH=1 \
    -D WITH_CUBLAS=1 \
    -D WITH_CUDA=ON \
    -D BUILD_opencv_cudacodec=OFF \
    -D WITH_CUDNN=ON \
    -D OPENCV_DNN_CUDA=ON \
    -D CUDA_ARCH_BIN=7.0 \
    -D BUILD_EXAMPLES=ON ..
make -j8
sudo make install
make -j8
sudo make install

After that, the cv2 directory was finally created! The path to it is as follows:
/usr/local/lib/python3.8/dist-packages/cv2
Good luck to you!

Hi Anastasija-T,

Have you checked the path defined during the configuration? In the proposed example, it is defined as virtual environment just created to contain cv2: OPENCV_PYTHON3_INSTALL_PATH=~/.virtualenvs/cv/lib/python3.8/site-packages . If you do not define this directive, then it should be installed in your dist-package of yout system.

Regards,
Raul

@Anastasija-T
Copy link

Hello everyone!
Thank you for this gist. I followed this guide. I didn't forget to run the "sudo make install"command. But the cv2 directory was not created. Perhaps someone will be helped by a solution that eventually worked for me. So, the initial data:

CUDA 11.4
cuDNN 8.2.2 
Ubuntu 20.04
OpenCV 4.5.3-dev

After I failed with this guide, I executed the following commands:

git clone https://github.com/opencv/opencv.git
git clone https://github.com/opencv/opencv_contrib.git
cd ~/opencv_build/opencv
mkdir -p build && cd build
cmake -D CMAKE_BUILD_TYPE=RELEASE \
    -D CMAKE_INSTALL_PREFIX=/usr/local \
    -D INSTALL_C_EXAMPLES=ON \
    -D INSTALL_PYTHON_EXAMPLES=ON \
    -D OPENCV_GENERATE_PKGCONFIG=ON \
    -D OPENCV_EXTRA_MODULES_PATH=~/opencv_build/opencv_contrib/modules \
    -D ENABLE_FAST_MATH=1 \
    -D CUDA_FAST_MATH=1 \
    -D WITH_CUBLAS=1 \
    -D WITH_CUDA=ON \
    -D BUILD_opencv_cudacodec=OFF \
    -D WITH_CUDNN=ON \
    -D OPENCV_DNN_CUDA=ON \
    -D CUDA_ARCH_BIN=7.0 \
    -D BUILD_EXAMPLES=ON ..
make -j8
sudo make install
make -j8
sudo make install

After that, the cv2 directory was finally created! The path to it is as follows:
/usr/local/lib/python3.8/dist-packages/cv2
Good luck to you!

Hi Anastasija-T,

Have you checked the path defined during the configuration? In the proposed example, it is defined as virtual environment just created to contain cv2: OPENCV_PYTHON3_INSTALL_PATH=~/.virtualenvs/cv/lib/python3.8/site-packages . If you do not define this directive, then it should be installed in your dist-package of yout system.

Regards,
Raul

Hello, Raul!

This is indeed the case. For me, a system installation is more convenient. As I said above, the cv2 directory in my case has the following path:
/usr/local/lib/python3.8/dist-packages/cv2
With OpenCV 4.5.2, I was unable to make an installation in the dist-packages of the system, the cv2 directory was also not created. Perhaps the problem is in the OpenCV version. I hope this idea will be useful to someone.

With best wishes, Anastasia

@raulqf
Copy link
Author

raulqf commented Sep 12, 2021

Hello everyone!
Thank you for this gist. I followed this guide. I didn't forget to run the "sudo make install"command. But the cv2 directory was not created. Perhaps someone will be helped by a solution that eventually worked for me. So, the initial data:

CUDA 11.4
cuDNN 8.2.2 
Ubuntu 20.04
OpenCV 4.5.3-dev

After I failed with this guide, I executed the following commands:

git clone https://github.com/opencv/opencv.git
git clone https://github.com/opencv/opencv_contrib.git
cd ~/opencv_build/opencv
mkdir -p build && cd build
cmake -D CMAKE_BUILD_TYPE=RELEASE \
    -D CMAKE_INSTALL_PREFIX=/usr/local \
    -D INSTALL_C_EXAMPLES=ON \
    -D INSTALL_PYTHON_EXAMPLES=ON \
    -D OPENCV_GENERATE_PKGCONFIG=ON \
    -D OPENCV_EXTRA_MODULES_PATH=~/opencv_build/opencv_contrib/modules \
    -D ENABLE_FAST_MATH=1 \
    -D CUDA_FAST_MATH=1 \
    -D WITH_CUBLAS=1 \
    -D WITH_CUDA=ON \
    -D BUILD_opencv_cudacodec=OFF \
    -D WITH_CUDNN=ON \
    -D OPENCV_DNN_CUDA=ON \
    -D CUDA_ARCH_BIN=7.0 \
    -D BUILD_EXAMPLES=ON ..
make -j8
sudo make install
make -j8
sudo make install

After that, the cv2 directory was finally created! The path to it is as follows:
/usr/local/lib/python3.8/dist-packages/cv2
Good luck to you!

Hi Anastasija-T,
Have you checked the path defined during the configuration? In the proposed example, it is defined as virtual environment just created to contain cv2: OPENCV_PYTHON3_INSTALL_PATH=~/.virtualenvs/cv/lib/python3.8/site-packages . If you do not define this directive, then it should be installed in your dist-package of yout system.
Regards,
Raul

Hello, Raul!

This is indeed the case. For me, a system installation is more convenient. As I said above, the cv2 directory in my case has the following path:
/usr/local/lib/python3.8/dist-packages/cv2
With OpenCV 4.5.2, I was unable to make an installation in the dist-packages of the system, the cv2 directory was also not created. Perhaps the problem is in the OpenCV version. I hope this idea will be useful to someone.

With best wishes, Anastasia

OpenCV versions should not be the problem. Just a reminder, cv2 could be located in dist-packages or site-packages depending how you install it. So, in case of a virtual environment it is located in site-packages, I think.

Regards,
Raul

@smn06
Copy link

smn06 commented Sep 15, 2021

Why can't i delete opencv-4.5.2 and opencv-contrib-4.5.2 folders from Download after installation? It seems, If i delete one of this, my program doesn't work. But the installation is done properly.

@raulqf
Copy link
Author

raulqf commented Sep 16, 2021

Why can't i delete opencv-4.5.2 and opencv-contrib-4.5.2 folders from Download after installation? It seems, If i delete one of this, my program doesn't work. But the installation is done properly.

No, you can delete both. Once you do "sudo make install", respective libraries (*.so or *.a) have been installed in your system, as well as the header files. Try to move the folders to other directory for a fast check.

@smn06
Copy link

smn06 commented Sep 16, 2021

Why can't i delete opencv-4.5.2 and opencv-contrib-4.5.2 folders from Download after installation? It seems, If i delete one of this, my program doesn't work. But the installation is done properly.

No, you can delete both. Once you do "sudo make install", respective libraries (*.so or *.a) have been installed in your system, as well as the header files. Try to move the folders to other directory for a fast check.

Thanks for the reply,
I moved the folders from my Download folder and after that opencv program stopped working.And the programs started working later If i put back those into the Download folder again. Though the header and libraries are stored in /usr/local/include/ and /usr/lib/python3/dist-packages/ path.

@puppinoo
Copy link

Hi,
Can anyone help, I've been struggling for 2 days now. I followed the tutorial. Just modified the cmake command in the one below otherwise compilation failed. I'm 100% sure it worked jsut after installation cause I could compile and execute test.cpp and many other examples. I just turned off or rebooted and now I nothing works anymore. IMPORTANT. My driver version is Driver Version: 495.44, my Ubuntu is 20.04 and my CUDA version is 11.4 (maybe there's some sort of incompatibility?) On the test example I receive this error:

`~/src/machine_learning/testcuda$ ./test
Error: OpenCV(4.5.2) /home/pinuccio/src/machine_learning/opencv/opencv_contrib-4.5.2/modules/cudaimgproc/src/cuda/bilateral_filter.cu:140: error: (-217:Gpu API call) invalid configuration argument in function 'bilateral_caller'

0.07816
I tried many things. For example I added these in .bashrc# virtualenv and virtualenvwrapper
export WORKON_HOME=$HOME/.virtualenvs
export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3
source /usr/local/bin/virtualenvwrapper.sh

export PATH=/usr/lib/x86_64-linux-gnu:$PATH
export LD_LIBRARY_PATH=/usr/lib/x86_64-linux-gnu:$LD_LIBRARY_PATH
export CPATH=/usr/local/cuda-11.4/targets/x86_64-linux/include${CPATH:+:${CPATH}}
`
but I'm losing a bit confidence and lucidity so probably making things worse.
Any help is appreciated.

This is my CMAKE:

cmake -D CMAKE_BUILD_TYPE=RELEASE \ -D CMAKE_INSTALL_PREFIX=/usr/local \ -D WITH_TBB=ON \ -D ENABLE_FAST_MATH=1 \ -D CUDA_FAST_MATH=1 \ -D WITH_CUBLAS=1 \ -D WITH_CUDA=ON \ -D BUILD_opencv_cudacodec=OFF \ -D WITH_CUDNN=ON \ -D OPENCV_DNN_CUDA=ON \ -D CUDA_ARCH_BIN=7.5 \ -D WITH_V4L=ON \ -D WITH_QT=OFF \ -D WITH_OPENGL=ON \ -D WITH_GSTREAMER=ON \ -D OPENCV_GENERATE_PKGCONFIG=ON \ -D OPENCV_PC_FILE_NAME=opencv.pc \ -D OPENCV_ENABLE_NONFREE=ON \ -D OPENCV_PYTHON3_INSTALL_PATH=~/.virtualenvs/cv/lib/python3.8/site-packages \ -D PYTHON_EXECUTABLE=~/.virtualenvs/cv/bin/python \ -D OPENCV_EXTRA_MODULES_PATH=~/src/machine_learning/opencv/opencv_contrib-4.5.2/modules \ -D INSTALL_PYTHON_EXAMPLES=OFF \ -D INSTALL_C_EXAMPLES=OFF \ -D CUDNN_INCLUDE_DIR="/usr/lib/cuda/include/" \ -D CUDNN_LIBRARY="~/src/machine_learning/opencv/cudnn/cuda/lib64/libcudnn.so.8.2.4" \ -D BUILD_EXAMPLES=OFF ..

@raulqf
Copy link
Author

raulqf commented Nov 18, 2021

Hi, Can anyone help, I've been struggling for 2 days now. I followed the tutorial. Just modified the cmake command in the one below otherwise compilation failed. I'm 100% sure it worked jsut after installation cause I could compile and execute test.cpp and many other examples. I just turned off or rebooted and now I nothing works anymore. IMPORTANT. My driver version is Driver Version: 495.44, my Ubuntu is 20.04 and my CUDA version is 11.4 (maybe there's some sort of incompatibility?) On the test example I receive this error:

`~/src/machine_learning/testcuda$ ./test Error: OpenCV(4.5.2) /home/pinuccio/src/machine_learning/opencv/opencv_contrib-4.5.2/modules/cudaimgproc/src/cuda/bilateral_filter.cu:140: error: (-217:Gpu API call) invalid configuration argument in function 'bilateral_caller'

0.07816 I tried many things. For example I added these in .bashrc# virtualenv and virtualenvwrapper export WORKON_HOME=$HOME/.virtualenvs export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3 source /usr/local/bin/virtualenvwrapper.sh

export PATH=/usr/lib/x86_64-linux-gnu:$PATH export LD_LIBRARY_PATH=/usr/lib/x86_64-linux-gnu:$LD_LIBRARY_PATH export CPATH=/usr/local/cuda-11.4/targets/x86_64-linux/include${CPATH:+:${CPATH}} ` but I'm losing a bit confidence and lucidity so probably making things worse. Any help is appreciated.

This is my CMAKE:

cmake -D CMAKE_BUILD_TYPE=RELEASE \ -D CMAKE_INSTALL_PREFIX=/usr/local \ -D WITH_TBB=ON \ -D ENABLE_FAST_MATH=1 \ -D CUDA_FAST_MATH=1 \ -D WITH_CUBLAS=1 \ -D WITH_CUDA=ON \ -D BUILD_opencv_cudacodec=OFF \ -D WITH_CUDNN=ON \ -D OPENCV_DNN_CUDA=ON \ -D CUDA_ARCH_BIN=7.5 \ -D WITH_V4L=ON \ -D WITH_QT=OFF \ -D WITH_OPENGL=ON \ -D WITH_GSTREAMER=ON \ -D OPENCV_GENERATE_PKGCONFIG=ON \ -D OPENCV_PC_FILE_NAME=opencv.pc \ -D OPENCV_ENABLE_NONFREE=ON \ -D OPENCV_PYTHON3_INSTALL_PATH=~/.virtualenvs/cv/lib/python3.8/site-packages \ -D PYTHON_EXECUTABLE=~/.virtualenvs/cv/bin/python \ -D OPENCV_EXTRA_MODULES_PATH=~/src/machine_learning/opencv/opencv_contrib-4.5.2/modules \ -D INSTALL_PYTHON_EXAMPLES=OFF \ -D INSTALL_C_EXAMPLES=OFF \ -D CUDNN_INCLUDE_DIR="/usr/lib/cuda/include/" \ -D CUDNN_LIBRARY="~/src/machine_learning/opencv/cudnn/cuda/lib64/libcudnn.so.8.2.4" \ -D BUILD_EXAMPLES=OFF ..

Hi Puppinoo,

I would delete the environment variable that you have defined. The problem seems to be related with the opencv library. Could you try another example with opencv for CUDA?

Best Regards,
Raul

@puppinoo
Copy link

puppinoo commented Nov 18, 2021

Hi, Can anyone help, I've been struggling for 2 days now. I followed the tutorial. Just modified the cmake command in the one below otherwise compilation failed. I'm 100% sure it worked jsut after installation cause I could compile and execute test.cpp and many other examples. I just turned off or rebooted and now I nothing works anymore. IMPORTANT. My driver version is Driver Version: 495.44, my Ubuntu is 20.04 and my CUDA version is 11.4 (maybe there's some sort of incompatibility?) On the test example I receive this error:
~/src/machine_learning/testcuda$ ./test Error: OpenCV(4.5.2) /home/pinuccio/src/machine_learning/opencv/opencv_contrib-4.5.2/modules/cudaimgproc/src/cuda/bilateral_filter.cu:140: error: (-217:Gpu API call) invalid configuration argument in function 'bilateral_caller' 0.07816 I tried many things. For example I added these in .bashrc# virtualenv and virtualenvwrapper export WORKON_HOME=$HOME/.virtualenvs export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3 source /usr/local/bin/virtualenvwrapper.sh export PATH=/usr/lib/x86_64-linux-gnu:$PATH export LD_LIBRARY_PATH=/usr/lib/x86_64-linux-gnu:$LD_LIBRARY_PATH export CPATH=/usr/local/cuda-11.4/targets/x86_64-linux/include${CPATH:+:${CPATH}} but I'm losing a bit confidence and lucidity so probably making things worse. Any help is appreciated.
This is my CMAKE:
cmake -D CMAKE_BUILD_TYPE=RELEASE \ -D CMAKE_INSTALL_PREFIX=/usr/local \ -D WITH_TBB=ON \ -D ENABLE_FAST_MATH=1 \ -D CUDA_FAST_MATH=1 \ -D WITH_CUBLAS=1 \ -D WITH_CUDA=ON \ -D BUILD_opencv_cudacodec=OFF \ -D WITH_CUDNN=ON \ -D OPENCV_DNN_CUDA=ON \ -D CUDA_ARCH_BIN=7.5 \ -D WITH_V4L=ON \ -D WITH_QT=OFF \ -D WITH_OPENGL=ON \ -D WITH_GSTREAMER=ON \ -D OPENCV_GENERATE_PKGCONFIG=ON \ -D OPENCV_PC_FILE_NAME=opencv.pc \ -D OPENCV_ENABLE_NONFREE=ON \ -D OPENCV_PYTHON3_INSTALL_PATH=~/.virtualenvs/cv/lib/python3.8/site-packages \ -D PYTHON_EXECUTABLE=~/.virtualenvs/cv/bin/python \ -D OPENCV_EXTRA_MODULES_PATH=~/src/machine_learning/opencv/opencv_contrib-4.5.2/modules \ -D INSTALL_PYTHON_EXAMPLES=OFF \ -D INSTALL_C_EXAMPLES=OFF \ -D CUDNN_INCLUDE_DIR="/usr/lib/cuda/include/" \ -D CUDNN_LIBRARY="~/src/machine_learning/opencv/cudnn/cuda/lib64/libcudnn.so.8.2.4" \ -D BUILD_EXAMPLES=OFF ..

Hi Puppinoo,

I would delete the environment variable that you have defined. The problem seems to be related with the opencv library. Could you try another example with opencv for CUDA?

Best Regards, Raul

Hi,
It seems I fixed after so many attempts.
I'm not sure what fixed but I write here what I probably did different when it started working:

  1. added python2 executable path into cmake string (not sure it did any different but I dit do that)
    -D PYTHON2_EXECUTABLE=/usr/bin/python2.7 \
  2. I noticed that I had 2 different ldconfig binaries in path. The default was /usr/sbin/ldconfig but as soon as I used the other one which is "sudo /sbin/ldconfig" the app started workign correctly.
    Not sure this is the cause of the issue but maybe it can help.
  3. I used correctly the virtualenv environment launching workon opencv_cuda (my created virtualenv) before installing.
    Basically I did
    a) workon openv_cuda
    b) pip install numpy
    c) followed the rest of the tutorial
    but this is well explained in the tutorial as well. It was my fault if I did something wrong.

P.s.: Also, installing darknet repo I noticed my cudnnn installation was not correct cause I had multible libraries installed and the wrong ones pointed to alternatives so I used the dpkg to install the right one for cura 11.4 which is 8.2.4 (if im not wrong) and after that darknet compiled successfully. BTW this issues happened after I fixed the issue with cpp import.

Thanks and regards.

@keaneflynn
Copy link

Hi all,
Just finished my build for OpenCV and thought I would leave a github link to the repository with a bash file and that worked for me with a python script to test if the build worked. Like a few other people in this thread I encountered an issue with the version of gcc/++ used to compile OpenCV and ultimately had to downgrade from gcc/g++ to gcc/++ 10 for the build to process successfully. From what I found on Nvidia's forums it sounds like gcc/++ 11 is not compatible with CUDA 11.4.x but will work with CUDA 11.5.x (I have yet to test this) but gcc/++ 10 will work with CUDA 11.4.x.
This build is not for a virtual environment but can easily be configured to do so. I was able to crank out between 100-150 fps inference using yolov4-tiny object detection and seems to handle most of everything I need to throw at it. Cheers.

Hardware/software specs:
Laptop: Razer Blade 14
CPU: AMD 5900hx
GPU: Nvidia 3080 8gb vram
NVIDIA DRIVERS: 470.86
CUDA: 11.4
CUDNN: 8.4.2
UBUNTU: 21.10
KERNEL: 5.13
OPENCV: 4.5.3

@raulqf
Copy link
Author

raulqf commented Jan 10, 2022

Hi keaneflynn,

Nice bash script. May I include a reference to your repo in the example section with a brief introduction?

CUDA points to the default versions of gcc/g++ regarding the OS in their installation guides, but you are working with latest versions even for Ubuntu distribution. Anyway, you find a compatible version 👌 .

@keaneflynn
Copy link

@raulqf Go ahead! Happy to help anyone through this process if they have a similar system to me and this is compatible

@e101sg
Copy link

e101sg commented Jan 12, 2022

Dear All, I am trying to install Darknet / YOLOv4 on Jetson Xavier. Using Jetpack 4.5.1 /Anaconda base environment
I have installed CUDA
My installation looks like this (Jetson utilities screen shot ) - the CUDNN: NOT_INSTALLED
Default GCC installed version is 7.5.0/ I have installed in GCC 10.1. Can see usr/bin/gcc-10. It has short to aarch64-linux-gnu-gcc-10.
https://1drv.ms/u/s!AgQa8i37A0sihYZsAZ1EnOBAJdeyoA?e=YmRNpK
Then when i run the above script, i get the following errors (screen shot)

https://1drv.ms/u/s!AgQa8i37A0sihYZtPxmyTHy3XxuGsQ
https://1drv.ms/u/s!AgQa8i37A0sihYZuOibIYRgxWTY7Gg

Last 2 days, i am trying...any thoughts highly useful. Thanks.

Cheers!
Chandra

@pinduzera
Copy link

pinduzera commented Jan 12, 2022

@e101sg
Looks like your Cudnn is not installed or is installed in the wrong path. Try looking for cudnn solutions specifically. Maybe it is just a matter of a symbolic link or may require a installation.
https://docs.nvidia.com/jetson/jetpack/install-jetpack/index.html

@e101sg
Copy link

e101sg commented Jan 12, 2022

Hi Pinduzera,
Thanks a lot. hmm...i too think some issue with cuDNN. I have seen the link you mentioned above. After cuDNN, i have to OpenCV /Darknet/ Yolo as well. (this needs GCC 10). I wondering how to upgrade the Jetpack from 4.5 to 4.6 (seems it is not very straight forward)

@keaneflynn
Copy link

@e101sg
The default jetpack installation of Jetpack should have everything in the correct place as far as CUDA & CUDNN are concerned. You should be able to build OpenCV with CUDA dependencies rather easily and then migrate onto the Darknet build from there (if you are building Darknet from source you should have opencv built with CUDA first).

I haven't personally tried to build darknet on a Jetson Xavier, but I have built OpenCV with CUDA & CUDNN and have a script that will work with an Xavier out of the box with the current Jetpack version. Hope this helps.

@e101sg
Copy link

e101sg commented Jan 13, 2022

After some efforts, I have installed CUDNN 8.0 installed. I have trying to build the OpenCV 4.5.3 but getting error
CMake Error at modules/dnn/CMakeLists.txt:39 (message):
DNN: CUDA backend requires cuDNN. Please resolve dependency or disable
OPENCV_DNN_CUDA=OFF

NVIDIA Jetson AGX Xavier [16GB]
L4T 32.5.2 [ JetPack UNKNOWN ]
Ubuntu 18.04.5 LTS
Kernel Version: 4.9.201-tegra
CUDA 10.2.89
CUDA Architecture: 7.2
OpenCV version: 3.2.0
OpenCV Cuda: NO
CUDNN: 8.0.0.180
TensorRT: 7.1.3.0
Vision Works: 1.6.0.501
VPI: ii libnvvpi1 1.0.15 arm64 NVIDIA Vision Programming Interface library
Vulcan: 1.2.70
+++++++++++++++++++
cmake -D CMAKE_BUILD_TYPE=RELEASE
-D CMAKE_INSTALL_PREFIX=/usr
-D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib/modules
-D EIGEN_INCLUDE_PATH=/usr/include/eigen3
-D WITH_OPENCL=OFF
-D WITH_CUDA=ON
-D CUDA_ARCH_BIN=8.0
-D CUDA_ARCH_PTX=""
-D WITH_CUDNN=ON
-D OPENCV_DNN_CUDA=ON
-D WITH_CUBLAS=ON
-D ENABLE_FAST_MATH=ON
-D CUDA_FAST_MATH=ON
-D OPENCV_DNN_CUDA=ON
-D ENABLE_NEON=ON
-D WITH_QT=OFF
-D WITH_OPENMP=ON
-D BUILD_TIFF=ON
-D WITH_FFMPEG=ON
-D WITH_GSTREAMER=ON
-D WITH_TBB=ON
-D BUILD_TBB=ON
-D BUILD_TESTS=OFF
-D WITH_EIGEN=ON
-D WITH_V4L=ON
-D WITH_LIBV4L=ON
-D OPENCV_ENABLE_NONFREE=ON
-D INSTALL_C_EXAMPLES=OFF
-D INSTALL_PYTHON_EXAMPLES=OFF
-D BUILD_opencv_python3=TRUE
-D OPENCV_GENERATE_PKGCONFIG=ON
-D CUDNN_VERSION='8.0'
-D BUILD_opencv_python3=ON
-D CMAKE_BUILD_TYPE=RELEASE
-D BUILD_EXAMPLES=OFF ..

Not sure what am i missing :)

@pinduzera
Copy link

@e101sg Xavier's compute capability is 7.2 not 8.0. (https://developer.nvidia.com/cuda-gpus) (Cuda compute =/= CUDNN)
This may be source of the conflict for cudnn.
Try CUDA_ARCH_BIN=7.2

@e101sg
Copy link

e101sg commented Jan 20, 2022

@pinduzera:yes. corrected mistake. I have updated the Jetson Xavier AGX memory with 512GB SSD. ( following Jetsonhacks YT channel) and installed JetPack 4.6 on new L4T /Ubuntu 18.04 image. Will try again. Will CUDA 10.2 instead of latest 11.X. Any thoughts! Cheers !! - Chandra

@paulheisterkamp97
Copy link

I got a general question:

I compiled opencv according to the instructions and with a virtualenv. Everything works fine but pip does not show the package.
Is there any way to tell pip that cv2 is installed?
I fear that pip might overwrite my compiled version of cv2 with the standart package (and i got to compile it again, it takes forever).

@daggarwa
Copy link

@asandberg17 @puppinoo I encountered the same error today :

Error: OpenCV(4.5.2) /opencv/opencv_contrib-4.5.2/modules/cudaimgproc/src/cuda/bilateral_filter.cu:140: error: (-217:Gpu API call) invalid configuration argument in function 'bilateral_caller'

0.094137

while running the test example. But I found that I was using the default path for imread given in example instead of using my own image. It was resolved after changing it to my image path.

@aktiver
Copy link

aktiver commented Apr 7, 2022

SIDE QUES: What does -j8 do? What is the point of make j8?

@keaneflynn
Copy link

SIDE QUES: What does -j8 do? What is the point of make j8?

the -j flag indicates how many 'jobs' you are allocating to this of your computer's processor (i.e. how many physical threads from your CPU core do you want to allocate). My computer has 8 cores and 16 threads, so I can allocate a maximum of 16 thread or -j16. If you want to know how many you can allocate, type 'nproc' into your terminal. Cheers

@uunnxx
Copy link

uunnxx commented Sep 21, 2022

Hello everyone! Thank you for this gist. I followed this guide. I didn't forget to run the "sudo make install"command. But the cv2 directory was not created. Perhaps someone will be helped by a solution that eventually worked for me. So, the initial data:

CUDA 11.4
cuDNN 8.2.2 
Ubuntu 20.04
OpenCV 4.5.3-dev

After I failed with this guide, I executed the following commands:

git clone https://github.com/opencv/opencv.git
git clone https://github.com/opencv/opencv_contrib.git
cd ~/opencv_build/opencv
mkdir -p build && cd build
cmake -D CMAKE_BUILD_TYPE=RELEASE \
    -D CMAKE_INSTALL_PREFIX=/usr/local \
    -D INSTALL_C_EXAMPLES=ON \
    -D INSTALL_PYTHON_EXAMPLES=ON \
    -D OPENCV_GENERATE_PKGCONFIG=ON \
    -D OPENCV_EXTRA_MODULES_PATH=~/opencv_build/opencv_contrib/modules \
    -D ENABLE_FAST_MATH=1 \
    -D CUDA_FAST_MATH=1 \
    -D WITH_CUBLAS=1 \
    -D WITH_CUDA=ON \
    -D BUILD_opencv_cudacodec=OFF \
    -D WITH_CUDNN=ON \
    -D OPENCV_DNN_CUDA=ON \
    -D CUDA_ARCH_BIN=7.0 \
    -D BUILD_EXAMPLES=ON ..
make -j8
sudo make install
make -j8
sudo make install

After that, the cv2 directory was finally created! The path to it is as follows: /usr/local/lib/python3.8/dist-packages/cv2 Good luck to you!

Thank you! <3

@karishmathumu
Copy link

karishmathumu commented Nov 21, 2022

Hello everyone, thank you for this great tutorial on OPENCV with CUDA backend. just what I needed.

I am working on a NVIDIA Jetson Orin to enable OpenCV with CUDA - DNN support

$ python3
Python 3.8.10 (default, Jun 22 2022, [20:18:18]
[GCC 9.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.

import cv2
cv2.version
'4.5.4'
exit()

I then, used a sample.py . It worked. But it works, when run only inside the opencv/build folder.

My results are

  1. lenovo: ~ /workspace/opencv-4.5.4$ cd build/ workon opencv_dnn_cuda
    (opencv_dnn_cuda) lenovo:~ /workspace/opencv-4.5.4/build$ python3 test.py
    CUDA using GPU --- 0.76786208152771 seconds ---
    CPU --- 1.6167230606079102 seconds ---

without virtual environment
lenovo:$ cd workspace/opencv-4.5.4/build
lenovo:
/workspace/opencv-4.5.4/build$ python3 test.py
CUDA using GPU --- 1.4443564414978027 seconds ---
CPU --- 3.2225732803344727 seconds ---

Can i know, how can it be done, without the virtual environment each time ???

But, when I use the test.cpp code you have shared for the testing process. I think it fails.

(opencv_dnn_cuda) .........:$ cd workspace/opencv-4.5.4
(opencv_dnn_cuda) ..........:
/workspace/opencv-4.5.4$ g++ test.cpp pkg-config opencv4 --cflags --libs -o test
(opencv_dnn_cuda) ...........:~/workspace/opencv-4.5.4$ ./test
Error: OpenCV(4.5.4) /home/ubuntu/build_opencv/opencv/modules/core/include/opencv2/core/private.cuda.hpp:106: error: (-216:No CUDA support) The library is compiled without CUDA support in function 'throw_no_cuda'

0.000296

Here is the weird part.
I do not have any folder by the name "ubuntu" in my home, nor the "build_opencv" folder and nor the path /home/ubuntu/build_opencv/opencv/modules/core/include/opencv2/core/private.cuda.hpp

Instead the actual exiting path in my PC is as follows:
/home/....../workspace/opencv-4.5.4/modules/core/include/opencv2/core/private.cuda.hpp

Could you please help me rectifying the issue.
Thanks and regards

@raulqf
Copy link
Author

raulqf commented Nov 21, 2022

I don't understand when you say when you run inside 'opencv/build' folder related to a question about the virtual environment.

Why do you not want to use the virtualenvironment? You could activate it in your bashrc if required, so whenever your log in it would be automatically activated.

@karishmathumu
Copy link

I observed, after linking the python bindings.

  1. when I run test the "import cv2" in the home, or workspace or opencv folder, it fails. It works only if run inside the build folder of opencv.

  2. Because for my YOLO inference with OPENCV (DNN) - CUDA, I am running as a root on the linux Jetson Orin or remotely from Visual studio. Which results in switching to CPU. And showing the error in a path that is not even present.

I do get the inference results of object detection. But the problem is, it should use the GPU and CUDA. But it is switching back to CPU for inferencing.

It would be of huge help if you could help me.

@raulqf
Copy link
Author

raulqf commented Nov 22, 2022

If you are working with a virtualenvironmet, when you "import cv2" it doesn't depend of the folder in which you are working... If the installation has been done correctly.

So, can you check the correct installation by listing the files in your virtualenvironment ('-D OPENCV_PYTHON3_INSTALL_PATH=~/.virtualenvs/cv/lib/python3.8/site-packages')?

@karishmathumu
Copy link

This was the path I have used.
OPENCV_PYTHON3_INSTALL_PATH=~/.virtualenvs/opencv_dnn_cuda/lib/python3.8/dist-packages

@karishmathumu
Copy link

karishmathumu commented Nov 22, 2022

The same error is still repeating

> : $ g++ test.cpp `pkg-config opencv4 --cflags --libs` -o test
> : $ ./test

Error: OpenCV(4.5.4) /home/ubuntu/build_opencv/opencv/modules/core/include/opencv2/core/private.cuda.hpp:106: error: (-216:No CUDA support)
The library is compiled without CUDA support in function 'throw_no_cuda'

:~$ pkg-config --modversion opencv4
4.5.4

@nelson-hc
Copy link

Hi,

I followed the guide but receive this error. Can someone help me?

Build output check failed:
Regex: 'argument .* is not valid'
Output line: 'cc1: warning: ‘-Werror=’ argument ‘-Werror=non-virtual-dtor’ is not valid for C'
Compilation failed:
source file: '/home/n/Downloads/opencv-4.5.2/build/CMakeFiles/CMakeTmp/src.c'
check option: ' -fsigned-char -ffast-math -W -Wall -Werror=return-type -Werror=non-virtual-dtor'

@hipforth
Copy link

@asandberg17 @puppinoo I encountered the same error today :

Error: OpenCV(4.5.2) /opencv/opencv_contrib-4.5.2/modules/cudaimgproc/src/cuda/bilateral_filter.cu:140: error: (-217:Gpu API call) invalid configuration argument in function 'bilateral_caller'

0.094137

while running the test example. But I found that I was using the default path for imread given in example instead of using my own image. It was resolved after changing it to my image path.

image is empty, check image path

@baheytharwat
Copy link

@keaneflynn @raulqf
This may seem unrelated but I faced a problem after installing cuda, cudnn, ooencv with gpu support. I followed the steps mentioned here and opencv works well. Thank you
The problem I have is that I use argparse to get arguments from the terminal when running a python script. when I run python3 train.py --name cifar10 I get this error message train.py: error: argument --name is required. This is so strange because I passed the name argument!
Before installing opencv, I had no problem running the same script. so I am wondering if anything happened to argparse. Does anyone have any suggestions?

@raulqf
Copy link
Author

raulqf commented Apr 8, 2023

Review how you are invoking the script, because OpenCV installation has nothing to do with argparse.

@Potagashev
Copy link

Did you this problem? This problem has been bothering me for a long time(((

I got a general question:

I compiled opencv according to the instructions and with a virtualenv. Everything works fine but pip does not show the package. Is there any way to tell pip that cv2 is installed? I fear that pip might overwrite my compiled version of cv2 with the standart package (and i got to compile it again, it takes forever).

@paulheisterkamp97
Copy link

Did you this problem?

no i dont

@Prashant528
Copy link

Hi,

I followed the guide but receive this error. Can someone help me?

Build output check failed: Regex: 'argument .* is not valid' Output line: 'cc1: warning: ‘-Werror=’ argument ‘-Werror=non-virtual-dtor’ is not valid for C' Compilation failed: source file: '/home/n/Downloads/opencv-4.5.2/build/CMakeFiles/CMakeTmp/src.c' check option: ' -fsigned-char -ffast-math -W -Wall -Werror=return-type -Werror=non-virtual-dtor'

Hi, I am getting this error too. Did you find any solution? Can anyone help please?

@SixtyTrees
Copy link

I am following these instructions https://techzizou.com/setup-opencv-dnn-cuda-module-for-linux/
My build command is

cmake -D CMAKE_BUILD_TYPE=RELEASE \
    -D CMAKE_INSTALL_PREFIX=/home/stepan/opencv \
    -D BUILD_opencv_python2=OFF \
    -D BUILD_opencv_python3=ON \
    -D PYTHON3_LIBRARY=/home/stepan/anaconda3/envs/acne/lib \
    -D PYTHON3_INCLUDE_DIR=/home/stepan/anaconda3/envs/acne/include/python3.8 \
    -D PYTHON3_EXECUTABLE=/home/stepan/anaconda3/envs/acne/bin/python3.8 \
    -D INSTALL_PYTHON_EXAMPLES=OFF \
    -D INSTALL_C_EXAMPLES=OFF \
    -D BUILD_EXAMPLES=OFF \
    -D WITH_CUDA=ON \
    -D WITH_CUDNN=ON \
    -D OPENCV_DNN_CUDA=ON \
    -D ENABLE_FAST_MATH=1 \
    -D CUDA_ARCH_BIN=6.1 \
    -D WITH_CUBLAS=1 \
    -D CUDA_TOOLKIT_ROOT_DIR=/usr/local/cuda-10.2 \
    -D OPENCV_EXTRA_MODULES_PATH=/home/stepan/opencv_contrib/modules \
	-DCMAKE_C_COMPILER=/usr/bin/gcc-6 \
    -D OPENCV_GENERATE_PKGCONFIG=YES ..


The build sais nothing about Python 3. Previously it gave error messages about Python 3.8.14 is not suitable: version 3.8.14 is required. I've fixed this issue by installing a new Anaconda environment.

Now the build seem to work for Python3. It does report errors for for Python2.7, even though I have a flag -D BUILD_opencv_python2=OFF \

make -j command and make -j8 command fail with

/home/sam/opencv/modules/dnn/src/cuda/activation_eltwise.cu(74): error: calling a __device__ function("operator float") from a __host__ function("clipped_relu_eltwise_sum_2_inplace") is not allowed
          detected during instantiation of "void cv::dnn::cuda4dnn::kernels::clipped_relu_eltwise_sum_2_inplace(const cv::dnn::cuda4dnn::csl::Stream &, cv::dnn::cuda4dnn::csl::Span<T>, cv::dnn::cuda4dnn::csl::View<T>, T, T) [with T=__half]"
(105): here

/home/sam/opencv/modules/dnn/src/cuda/activation_eltwise.cu(74): error: calling a __device__ function("operator float") from a __host__ function("clipped_relu_eltwise_sum_2_inplace") is not allowed
          detected during instantiation of "void cv::dnn::cuda4dnn::kernels::clipped_relu_eltwise_sum_2_inplace(const cv::dnn::cuda4dnn::csl::Stream &, cv::dnn::cuda4dnn::csl::Span<T>, cv::dnn::cuda4dnn::csl::View<T>, T, T) [with T=__half]"
(105): here

2 errors detected in the compilation of "/tmp/tmpxft_00003924_00000000-4_activation_eltwise.cpp4.ii".
CMake Error at cuda_compile_1_generated_activation_eltwise.cu.o.RELEASE.cmake:281 (message):
Error generating file
/home/sam/opencv/build/modules/dnn/CMakeFiles/cuda_compile_1.dir/src/cuda/./cuda_compile_1_generated_activation_eltwise.cu.o

I am attaching the log of the build and of the make.

How to fix it?
build log: https://pastebin.com/DanW9YFS
make -j log: https://pastebin.com/Zgf2K64r
make -j8 log: https://pastebin.com/37TzJCxe

@keaneflynn
Copy link

Something is off on your build directories. You seem to have two different home directories for me named Stepan and another named Sam. Opencv doesn’t know where your object files are. I’d recommend starting clean and figuring out your anaconda environment issues first before trying to proceed.

@Ngocmai001
Copy link

Dear @raulqf and everyone
Please help me to install openCV to compatible with my CUDA 11.8 and CuDNN 8.6
I used Pytorch 2.0.1 and need to install openCV for computer vision and i worried about conflict between openCV and another part like pytorch, cuda, cudnn, gcc ...
and specifically is:
if i used (base) virtualenvs created by anaconda, can I deny this step:
$ echo "Create a virtual environtment for the python binding module (OPTIONAL)"
$ sudo pip install virtualenv virtualenvwrapper
$ sudo rm -rf /.cache/pip
$ echo "Edit /.bashrc"
$ export WORKON_HOME=$HOME/.virtualenvs
$ export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3
$ source /usr/local/bin/virtualenvwrapper.sh
$ mkvirtualenv cv -p python3
$ pip install numpy
and how to fixed the value of this:
-D OPENCV_PYTHON3_INSTALL_PATH=
/.virtualenvs/cv/lib/python3.8/site-packages
-D PYTHON_EXECUTABLE=
/.virtualenvs/cv/bin/python
to compatible with my (base) environment?
And can you share some tips in my case to install success?
Sorry for my bad english and thank you so much for your help

@CisMine
Copy link

CisMine commented Sep 14, 2023

I followed the instructions provided to set up OpenCV (without CUDA) from the following GitHub repository: https://github.com/lynux0906/OpenCv-pp-Ubuntu. In Step 4, which involves building and installing OpenCV, there is a command with the flags -D BUILD_TIFF=ON -D WITH_CUDA=OFF. So what should i do to install opencv ( with cuda)

@kleberbueno
Copy link

kleberbueno commented Dec 18, 2023

Just a quick comment, I spent almost a day trying to identify the problem while compiling it. The CMAKE threw an error every single time at 75% of building process. As I am not a Linux user, took some time and search to identify my problem was python2.7 originally installed with Ubuntu 20.04. For some reason, few libs were conflicting with libs installed with Anaconda. After removing python2.7 I got it done. I can say it was painful.

@mjorrico
Copy link

I got a general question:

I compiled opencv according to the instructions and with a virtualenv. Everything works fine but pip does not show the package. Is there any way to tell pip that cv2 is installed? I fear that pip might overwrite my compiled version of cv2 with the standart package (and i got to compile it again, it takes forever).

Same here. I still can't make Pip aware of cv2. The good thing is, we can import from within the environment.

@kleberbueno
Copy link

I got a general question:
I compiled opencv according to the instructions and with a virtualenv. Everything works fine but pip does not show the package. Is there any way to tell pip that cv2 is installed? I fear that pip might overwrite my compiled version of cv2 with the standart package (and i got to compile it again, it takes forever).

Same here. I still can't make Pip aware of cv2. The good thing is, we can import from within the environment.

I fixed this by running the following:

The general steps are:

Compile opencv

Move into the opencv python directory with setup.py. Mine was under build/python_loader folder.
Open the setup.py file and check values there. I had to change package_name = 'opencv-python'. Mine was just package_name = 'opencv' originally.

And run python setup.py bdist_wheel

You will get a file name like this opencv_python-4.7.0-py3-none-any.whl created in 'dist' folder.

After that just run pip install opencv_python-4.7.0-py3-none-any.whl .

pip list will now show opencv-python compiled version

@chen579
Copy link

chen579 commented Mar 3, 2024

Hey guys, when I run the command ./test, I met the same question error: (-217:Gpu API call) invalid device function in function 'bilateral_caller'.I have replaced the image path, but the program still cannot run properly. How can I solve this problem?My specific environment configuration is as follows,Thanks a lot.
CUDA:11.2
Cudnn:8.2.0
opencv:4.5.2

@keaneflynn
Copy link

@chen579
Going to need a lot more information than what you provided. The full script you are running and the traceback is a good starting point

@chen579
Copy link

chen579 commented Mar 4, 2024

@chen579 Going to need a lot more information than what you provided. The full script you are running and the traceback is a good starting point

Thanks for your reply,my specific operating program is as follows

#include
#include
#include
#include "bits/time.h"

#include <opencv2/core.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/imgcodecs.hpp>

#include <opencv2/core/cuda.hpp>
#include <opencv2/cudaarithm.hpp>
#include <opencv2/cudaimgproc.hpp>

#define TestCUDA true

int main() {
std::clock_t begin = std::clock();

    try {
        cv::String filename = "/home/chen/traj1.png";
        cv::Mat srcHost = cv::imread(filename, cv::IMREAD_GRAYSCALE);

        for(int i=0; i<1000; i++) {
            if(TestCUDA) {
                cv::cuda::GpuMat dst, src;
                src.upload(srcHost);

                //cv::cuda::threshold(src,dst,128.0,255.0, CV_THRESH_BINARY);
                cv::cuda::bilateralFilter(src,dst,3,1,1);

                cv::Mat resultHost;
                dst.download(resultHost);
            } else {
                cv::Mat dst;
                cv::bilateralFilter(srcHost,dst,3,1,1);
            }
        }

        //cv::imshow("Result",resultHost);
        //cv::waitKey();

    } catch(const cv::Exception& ex) {
        std::cout << "Error: " << ex.what() << std::endl;
    }

std::clock_t end = std::clock();
std::cout << double(end-begin) / CLOCKS_PER_SEC  << std::endl;

}
The specific error information is as follows:

Error: OpenCV(4.5.2) /home/chen/opencv452/opencv_contrib-4.5.2/modules/cudaimgproc/src/cuda/bilateral_filter.cu:138: error: (-217:Gpu API call) invalid device function in function 'bilateral_caller'

@josyulavt
Copy link

Hello everyone! Thank you for this gist. I followed this guide. I didn't forget to run the "sudo make install"command. But the cv2 directory was not created. Perhaps someone will be helped by a solution that eventually worked for me. So, the initial data:

CUDA 11.4
cuDNN 8.2.2 
Ubuntu 20.04
OpenCV 4.5.3-dev

After I failed with this guide, I executed the following commands:

git clone https://github.com/opencv/opencv.git
git clone https://github.com/opencv/opencv_contrib.git
cd ~/opencv_build/opencv
mkdir -p build && cd build
cmake -D CMAKE_BUILD_TYPE=RELEASE \
    -D CMAKE_INSTALL_PREFIX=/usr/local \
    -D INSTALL_C_EXAMPLES=ON \
    -D INSTALL_PYTHON_EXAMPLES=ON \
    -D OPENCV_GENERATE_PKGCONFIG=ON \
    -D OPENCV_EXTRA_MODULES_PATH=~/opencv_build/opencv_contrib/modules \
    -D ENABLE_FAST_MATH=1 \
    -D CUDA_FAST_MATH=1 \
    -D WITH_CUBLAS=1 \
    -D WITH_CUDA=ON \
    -D BUILD_opencv_cudacodec=OFF \
    -D WITH_CUDNN=ON \
    -D OPENCV_DNN_CUDA=ON \
    -D CUDA_ARCH_BIN=7.0 \
    -D BUILD_EXAMPLES=ON ..
make -j8
sudo make install
make -j8
sudo make install

After that, the cv2 directory was finally created! The path to it is as follows: /usr/local/lib/python3.8/dist-packages/cv2 Good luck to you!

Thank you! <3

I have used the exact same flags, cmake detects cudnn and nvidia yet I don't see the file cudaarithm.hpp in include.

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