OpenCV Python - unable to access CUDA modules

My procedure was as follows:

In Windows Environment Variables, I have a variable named CMAKE_ARGS with the following value:

-DCMAKE_BUILD_TYPE=RELEASE -DWITH_CUDA=ON -DWITH_CUBLAS=ON -DINSTALL_PYTHON_EXAMPLES=OFF -DINSTALL_C_EXAMPLES=OFF -DWITH_CUDNN=ON -DOPENCV_DNN_CUDA=ON -DENABLE_FAST_MATH=ON -DCUDA_FAST_MATH=ON -DOPENCV_ENABLE_NONFREE=ON -D CUDA_ARCH_BIN=6.1 -DPYTHON3_NUMPY_INCLUDE_DIRS:PATH="C:\Users\fdsahfjksda\AppData\Local\Programs\Python\Python310\Lib\site-packages\numpy\core\include"

Notice the last argument :

-DPYTHON3_NUMPY_INCLUDE_DIRS:PATH=“C:\Users\fdsahfjksda\AppData\Local\Programs\Python\Python310\Lib\site-packages\numpy\core\include”

This is because of the Numpy version which OpenCV-Python tries to look for, if I don’t specify this argument, it will look for Numpy in some temp folder and end with fatal error C1083: Cannot open include file: 'numpy/ndarrayobject.h'near the end of the build process

In Windows Command Line in C:\ directory, I ran

git clone --recursive https://github.com/opencv/opencv-python.git

This created the C:\opencv-python directory (it includes the opencv-contrib subdirectory)

Then, I edited the pyproject.toml file, from
"numpy==1.21.2; python_version>='3.10'"
to
"numpy==1.23.3; python_version>='3.10'"
So that it uses the installed Numpy 1.23.3 version, instead of trying to install Numpy 1.21.2 which fails with TypeError: CCompiler_spawn() got an unexpected keyword argument 'env'

Next, I executed the following commands in Windows Command Line:

C:\opencv-python\ set ENABLE_CONTRIB=1

C:\opencv-python\ pip wheel . --verbose


I was observing the build process and saw that the text output had many CUDA references like ‘Building NVCC (Device)’ and filenames with the .cu extension, and this is the CMake report:

  -- General configuration for OpenCV 4.6.0 =====================================
  --   Version control:               4.6.0
  --
  --   Extra modules:
  --     Location (extra):            C:/opencv-python/opencv_contrib/modules
  --     Version control (extra):     4.6.0
  --
  --   Platform:
  --     Timestamp:                   2022-09-09T19:51:33Z
  --     Host:                        Windows 10.0.22000 AMD64
  --     CMake:                       3.24.1
  --     CMake generator:             Visual Studio 17 2022
  --     CMake build tool:            C:/Program Files/Microsoft Visual Studio/2022/Community/MSBuild/Current/Bin/amd64/MSBuild.exe
  --     MSVC:                        1932
  --     Configuration:               Debug 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 (4 files):             + SSSE3 SSE4_1 POPCNT SSE4_2 AVX
  --       AVX2 (31 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:                C:/Program Files/Microsoft Visual Studio/2022/Community/VC/Tools/MSVC/14.32.31326/bin/Hostx64/x64/cl.exe  (ver 19.32.31332.0)
  --     C++ flags (Release):         /DWIN32 /D_WINDOWS /W4 /GR  /D _CRT_SECURE_NO_DEPRECATE /D _CRT_NONSTDC_NO_DEPRECATE /D _SCL_SECURE_NO_WARNINGS /Gy /bigobj /Oi  /fp:fast     /EHa /wd4127 /wd4251 /wd4324 /wd4275 /wd4512 /wd4589 /MP  /MT /O2 /Ob2 /DNDEBUG
  --     C++ flags (Debug):           /DWIN32 /D_WINDOWS /W4 /GR  /D _CRT_SECURE_NO_DEPRECATE /D _CRT_NONSTDC_NO_DEPRECATE /D _SCL_SECURE_NO_WARNINGS /Gy /bigobj /Oi  /fp:fast     /EHa /wd4127 /wd4251 /wd4324 /wd4275 /wd4512 /wd4589 /MP  /MTd /Zi /Ob0 /Od /RTC1
  --     C Compiler:                  C:/Program Files/Microsoft Visual Studio/2022/Community/VC/Tools/MSVC/14.32.31326/bin/Hostx64/x64/cl.exe
  --     C flags (Release):           /DWIN32 /D_WINDOWS /W3  /D _CRT_SECURE_NO_DEPRECATE /D _CRT_NONSTDC_NO_DEPRECATE /D _SCL_SECURE_NO_WARNINGS /Gy /bigobj /Oi  /fp:fast     /MP   /MT /O2 /Ob2 /DNDEBUG
  --     C flags (Debug):             /DWIN32 /D_WINDOWS /W3  /D _CRT_SECURE_NO_DEPRECATE /D _CRT_NONSTDC_NO_DEPRECATE /D _SCL_SECURE_NO_WARNINGS /Gy /bigobj /Oi  /fp:fast     /MP /MTd /Zi /Ob0 /Od /RTC1
  --     Linker flags (Release):      /machine:x64  /NODEFAULTLIB:atlthunk.lib /INCREMENTAL:NO  /NODEFAULTLIB:libcmtd.lib /NODEFAULTLIB:libcpmtd.lib /NODEFAULTLIB:msvcrtd.lib
  --     Linker flags (Debug):        /machine:x64  /NODEFAULTLIB:atlthunk.lib /debug /INCREMENTAL  /NODEFAULTLIB:libcmt.lib /NODEFAULTLIB:libcpmt.lib /NODEFAULTLIB:msvcrt.lib
  --     ccache:                      NO
  --     Precompiled headers:         YES
  --     Extra dependencies:          wsock32 comctl32 gdi32 ole32 setupapi ws2_32 cudart_static.lib nppc.lib nppial.lib nppicc.lib nppidei.lib nppif.lib nppig.lib nppim.lib nppist.lib nppisu.lib nppitc.lib npps.lib cublas.lib cudnn.lib cufft.lib -LIBPATH:C:/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v11.7/lib/x64 -LIBPATH:C:/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v11.7/lib
  --     3rdparty dependencies:       libprotobuf ade ittnotify libjpeg-turbo libwebp libpng libtiff libopenjp2 IlmImf zlib quirc ippiw ippicv
  --
  --   OpenCV modules:
  --     To be built:                 aruco barcode 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 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 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 cvv freetype hdf java julia matlab ovis python2 sfm ts viz
  --     Applications:                -
  --     Documentation:               NO
  --     Non-free algorithms:         YES
  --
  --   Windows RT support:            NO
  --
  --   GUI:                           WIN32UI
  --     Win32 UI:                    YES
  --     VTK support:                 NO
  --
  --   Media I/O:
  --     ZLib:                        build (ver 1.2.12)
  --     JPEG:                        build-libjpeg-turbo (ver 2.1.2-62)
  --     WEBP:                        build (ver encoder: 0x020f)
  --     PNG:                         build (ver 1.6.37)
  --     TIFF:                        build (ver 42 - 4.2.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:                      NO
  --     FFMPEG:                      YES (prebuilt binaries)
  --       avcodec:                   YES (58.134.100)
  --       avformat:                  YES (58.76.100)
  --       avutil:                    YES (56.70.100)
  --       swscale:                   YES (5.9.100)
  --       avresample:                YES (4.0.0)
  --     GStreamer:                   NO
  --     DirectShow:                  YES
  --     Media Foundation:            YES
  --       DXVA:                      YES
  --
  --   Parallel framework:            Concurrency
  --
  --   Trace:                         YES (with Intel ITT)
  --
  --   Other third-party libraries:
  --     Intel IPP:                   2020.0.0 Gold [2020.0.0]
  --            at:                   C:/opencv-python/_skbuild/win-amd64-3.10/cmake-build/3rdparty/ippicv/ippicv_win/icv
  --     Intel IPP IW:                sources (2020.0.0)
  --               at:                C:/opencv-python/_skbuild/win-amd64-3.10/cmake-build/3rdparty/ippicv/ippicv_win/iw
  --     Lapack:                      NO
  --     Eigen:                       NO
  --     Custom HAL:                  NO
  --     Protobuf:                    build (3.19.1)
  --
  --   NVIDIA CUDA:                   YES (ver 11.7, CUFFT CUBLAS FAST_MATH)
  --     NVIDIA GPU arch:             61
  --     NVIDIA PTX archs:
  --
  --   cuDNN:                         YES (ver 8.4.1)
  --
  --   OpenCL:                        YES (NVD3D11)
  --     Include path:                C:/opencv-python/opencv/3rdparty/include/opencl/1.2
  --     Link libraries:              Dynamic load
  --
  --   Python 3:
  --     Interpreter:                 C:\Users\fdsahfjksda\AppData\Local\Programs\Python\Python310\python.exe (ver 3.10.7)
  --     Libraries:                   C:/Users/fdsahfjksda/AppData/Local/Programs/Python/Python310/libs/python310.lib (ver 3.10.7)
  --     numpy:                       C:/Users/fdsahfjksda/AppData/Local/Programs/Python/Python310/Lib/site-packages/numpy/core/include (ver 1.23.3)
  --     install path:                python/cv2/python-3
  --
  --   Python (for build):            C:\Users\fdsahfjksda\AppData\Local\Programs\Python\Python310\python.exe
  --
  --   Java:
  --     ant:                         NO
  --     JNI:                         NO
  --     Java wrappers:               NO
  --     Java tests:                  NO
  --
  --   Install to:                    C:/opencv-python/_skbuild/win-amd64-3.10/cmake-install
  -- -----------------------------------------------------------------
  --
  -- Configuring done

At the end it produced opencv_contrib_python-4.6.0.3725898-cp310-cp310-win_amd64.whlin the C:\opencv-python folder

Next, I installed the wheel:

C:\opencv-python\ pip install opencv_contrib_python-4.6.0.3725898-cp310-cp310-win_amd64.whl

And that results in opencv-contrib-python version 4.6.0.3725898 being installed into my Python