Build opencv-cuda-dnn in Colab

I am trying oto build opencv, cuda and cudnn in google colab. I have read that I have to clone the latest repository, then use cmake and then build as by default colab does not have the necessary packages.

To do so, i do the following:

#Clone the OpenCV repository
%cd /content
!git clone
!git clone

Then I run the cmake command

!cmake -DOPENCV_EXTRA_MODULES_PATH=/content/opencv_contrib/modules \

And i get the following:

General configuration for OpenCV 4.9.0-dev =====================================
--   Version control:               4.9.0-131-g8850a8219e
--   Extra modules:
--     Location (extra):            /content/opencv_contrib/modules
--     Version control (extra):     4.9.0-16-gc9506561
--   Platform:
--     Timestamp:                   2024-02-06T09:57:23Z
--     Host:                        Linux 6.1.58+ x86_64
--     CMake:                       3.27.9
--     CMake generator:             Unix Makefiles
--     CMake build tool:            /usr/bin/gmake
--     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 (16 files):         + SSSE3 SSE4_1
--       SSE4_2 (1 files):          + SSSE3 SSE4_1 POPCNT SSE4_2
--       FP16 (0 files):            + SSSE3 SSE4_1 POPCNT SSE4_2 FP16 AVX
--       AVX (8 files):             + SSSE3 SSE4_1 POPCNT SSE4_2 AVX
--       AVX2 (36 files):           + SSSE3 SSE4_1 POPCNT SSE4_2 FP16 FMA3 AVX AVX2
--       AVX512_SKX (5 files):      + SSSE3 SSE4_1 POPCNT SSE4_2 FP16 FMA3 AVX AVX2 AVX_512F AVX512_COMMON AVX512_SKX
--   C/C++:
--     Built as dynamic libs?:      NO
--     C++ standard:                11
--     C++ Compiler:                /usr/bin/c++  (ver 11.4.0)
--     C++ flags (Release):         -fsigned-char -ffast-math -fno-finite-math-only -W -Wall -Wreturn-type -Wnon-virtual-dtor -Waddress -Wsequence-point -Wformat -Wformat-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 -fno-finite-math-only -W -Wall -Wreturn-type -Wnon-virtual-dtor -Waddress -Wsequence-point -Wformat -Wformat-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 -fno-finite-math-only -W -Wall -Wreturn-type -Waddress -Wsequence-point -Wformat -Wformat-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 -fno-finite-math-only -W -Wall -Wreturn-type -Waddress -Wsequence-point -Wformat -Wformat-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 -Wl,--no-undefined  
--     Linker flags (Debug):        -Wl,--exclude-libs,libippicv.a -Wl,--exclude-libs,libippiw.a   -Wl,--gc-sections -Wl,--as-needed -Wl,--no-undefined  
--     ccache:                      NO
--     Precompiled headers:         NO
--     Extra dependencies:          /usr/lib/x86_64-linux-gnu/ /usr/lib/x86_64-linux-gnu/ /usr/lib/x86_64-linux-gnu/ /usr/lib/x86_64-linux-gnu/ openjp2 /usr/lib/x86_64-linux-gnu/hdf5/serial/ /usr/lib/x86_64-linux-gnu/ /usr/lib/x86_64-linux-gnu/ /usr/lib/x86_64-linux-gnu/libpthread.a /usr/lib/x86_64-linux-gnu/ /usr/lib/x86_64-linux-gnu/ /usr/lib/x86_64-linux-gnu/libdl.a /usr/lib/x86_64-linux-gnu/ Iconv::Iconv 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:       libprotobuf ade ittnotify ippiw ippicv
--   OpenCV modules:
--     To be built:                 aruco bgsegm bioinspired calib3d ccalib core cudaarithm cudabgsegm cudacodec cudafeatures2d cudafilters cudaimgproc cudalegacy cudaobjdetect cudaoptflow cudastereo cudawarping cudev datasets dnn dnn_objdetect dnn_superres dpm face features2d flann 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 shape signal stereo stitching structured_light superres surface_matching text tracking video videoio videostab wechat_qrcode xfeatures2d ximgproc xobjdetect xphoto
--     Disabled:                    world
--     Disabled by dependency:      -
--     Unavailable:                 alphamat cannops cvv freetype java julia matlab ovis python2 sfm ts viz
--     Applications:                apps
--     Documentation:               NO
--     Non-free algorithms:         NO
--   GUI:                           NONE
--     GTK+:                        NO
--     OpenGL support:              NO
--     VTK support:                 NO
--   Media I/O: 
--     ZLib:                        /usr/lib/x86_64-linux-gnu/ (ver 1.2.11)
--     JPEG:                        /usr/lib/x86_64-linux-gnu/ (ver 80)
--     WEBP:                        /usr/lib/x86_64-linux-gnu/ (ver encoder: 0x020f)
--     PNG:                         /usr/lib/x86_64-linux-gnu/ (ver 1.6.37)
--     TIFF:                        /usr/lib/x86_64-linux-gnu/ (ver 42 / 4.3.0)
--     JPEG 2000:                   OpenJPEG (ver 2.4.0)
--     HDR:                         YES
--     SUNRASTER:                   YES
--     PXM:                         YES
--     PFM:                         YES
--   Video I/O:
--     DC1394:                      YES (2.2.6)
--     FFMPEG:                      YES
--       avcodec:                   YES (58.134.100)
--       avformat:                  YES (58.76.100)
--       avutil:                    YES (56.70.100)
--       swscale:                   YES (5.9.100)
--       avresample:                NO
--     GStreamer:                   NO
--     v4l/v4l2:                    YES (linux/videodev2.h)
--   Parallel framework:            pthreads
--   Trace:                         YES (with Intel ITT)
--   Other third-party libraries:
--     Intel IPP:                   2021.10.0 [2021.10.0]
--            at:                   /content/drive/My Drive/cv2_cuda/3rdparty/ippicv/ippicv_lnx/icv
--     Intel IPP IW:                sources (2021.10.0)
--               at:                /content/drive/My Drive/cv2_cuda/3rdparty/ippicv/ippicv_lnx/iw
--     VA:                          NO
--     Lapack:                      NO
--     Eigen:                       NO
--     Custom HAL:                  NO
--     Protobuf:                    build (3.19.1)
--     Flatbuffers:                 builtin/3rdparty (23.5.9)
--   NVIDIA CUDA:                   YES (ver 12.2, CUFFT CUBLAS FAST_MATH)
--     NVIDIA GPU arch:             75
--     NVIDIA PTX archs:
--   cuDNN:                         YES (ver 8.9.6)
--   OpenCL:                        YES (no extra features)
--     Include path:                /content/opencv/3rdparty/include/opencl/1.2
--     Link libraries:              Dynamic load
--   Python 3:
--     Interpreter:                 /usr/bin/python3 (ver 3.10.12)
--     Libraries:                   /usr/lib/x86_64-linux-gnu/ (ver 3.10.12)
--     Limited API:                 NO
--     numpy:                       /usr/local/lib/python3.10/dist-packages/numpy/core/include (ver 1.23.5)
--     install path:                lib/python3.10/dist-packages/cv2/python-3.10
--   Python (for build):            /usr/bin/python3
--   Java:                          
--     ant:                         NO
--     Java:                        YES (ver 11.0.21)
--     JNI:                         NO
--     Java wrappers:               NO
--     Java tests:                  NO
--   Install to:                    /usr/local
-- -----------------------------------------------------------------
-- Configuring done (4.9s)
-- Generating done (1.1s)```

This generates 2 new folders both located at /content/ :

  1. opencv
  2. opencv_contrib

Then I run:
!make -j8 install

Then I copy the files into mydrive with:

!mkdir -p "/content/drive/My Drive/cv2_cuda/opencv"
!mkdir -p "/content/drive/My Drive/cv2_cuda/opencv_contrib"
!cp  -r /content/opencv/   "/content/drive/My Drive/cv2_cuda/opencv"
!cp  -r /content/opencv_contrib/   "/content/drive/My Drive/cv2_cuda/opencv_contrib"

Then, I restart the session, I mount drive and then:
%cd /content/drive/My Drive/cv2_cuda

But when for example I put:
gray = cv2.cuda.cvtColor(gpu_frame, cv2.COLOR_BGR2GRAY)
It gives me the error:

AttributeError: module ‘cv2.cuda’ has no attribute ‘cvtColor’

I have tried using different configurations of cmake and I cannot find the error.

The reason why I am using colab and not vscode is that I already tried to install it with vscode but it gave me a lot of errors.

I would appreciate your help as I am quite stuck.

unfortunately, this only copies the opencv src code, NOT anything you built locally

i’d change a few things in your setup:

  • add -DBUILD_SHARED_LIBS=OFF to your cmake cmdline.
    (build a statically linked so, so it no more needs all’s at runtime)
  • do not build in /content/ directly, but use some special folder like /content/build
    (then cd into there to run cmake)
  • do not install to /usr/local but some place of your choice, like /content/ocv
    (far easier to retrieve things from there later)
  • reset runtime
  • add the folder with your new to sys.path
    (don’t cd into your install folder to run things, terrible !)