ImportError: DLL load failed while importing cv2. When compiling OpenCV from source with cmake

Hello all.

I’m building OpenCV with CUDA and DNN support locally from source using CMake with Anaconda Python.

I am using the latest version of OpenCV and OpenCV Contrib (4.9.x) along with the latest versions of Anaconda and DNN.

It gets generated in CMake with no issues and builds fine.

However once the completes I’m faced with this error when attempting to import cv2

(base) C:\Users\usr1>python
Python 3.11.7 | packaged by Anaconda, Inc. | (main, Dec 15 2023, 18:05:47) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "E:\Users\usr1\anaconda3\Lib\site-packages\cv2\__init__.py", line 181, in <module>
    bootstrap()
  File "E:\Users\usr1\anaconda3\Lib\site-packages\cv2\__init__.py", line 153, in bootstrap
    native_module = importlib.import_module("cv2")
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "E:\Users\usr1\anaconda3\Lib\importlib\__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
ImportError: DLL load failed while importing cv2: The specified module could not be found.

I’m brand new to all of this, and honestly have no idea how to start troubleshooting this. I haven’t been very successful with my search online as while many have this issue they tend to use OpenCV imported from cli or are outdated.

Any help would be greatly appreciated.

EDIT: After some testing using process monitor I found that I was missing opencv_world.dll and opencv_img_hash490.dll which was causing the cv2 module to break I guess.

image

I’m looking into acquiring these and/or finding them. If anyone would like to jump in an explain how this happens and/or how to locate them I would greatly appreciate that. Otherwise I’ll probably figure it out eventually and close the post.

Are you sure? You might find result SUCCESS for those .dll’s lower down in process monitor. It is more likely you don’t have the Nvidia CUDA dependencies.

I would check the contents of your E:\Users\usr1\anaconda3\Lib\site-packages\cv2\config.py which should add the paths to the folder containing opencv_world.dll and opencv_img_hash490.dll and the path to CUDA, see below for an example

import os

BINARIES_PATHS = [
    os.path.join('<PATH_TO_BUILD_DIR>/install', 'x64/vc17/bin')
    os.path.join(os.getenv('CUDA_PATH', 'C:/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v12.3'), 'bin')
] + BINARIES_PATHS

If your version has both entries then I would confirm that CUDA_PATH is set to the version of CUDA you built against and not something else as can be the case when using anaconda , i.e.

echo %CUDA_PATH%

Thanks for the reply.

I went through your steps and the CUDA_PATH and config.py is consistent with what you have. Note in my directory for <PATH_TO_BUILD_DIR>/install', 'x64/vc17/bin the directory is called win-install while the path config.py was looking for was install. I’m not sure if this is causing problems as I tried renaming it and nothing changed.

I’m leaning towards the .dlls missing as the searched both drives on my entire system for them top to bottom and didn’t find them. However as I said I’m a total newbie to this so I may be barking up the wrong tree.

Can you post your config.py contents as that is the directory the dll’s should be in?

How did you build and install OpenCV? If you built the INSTALL target for OpenCV and it installed the python bindings for you the .dll’s would have to have been successfully built first.

Sure thing.

Here is config.py in E:\Users\amade\anaconda3\Lib\site-packages\cv2

import os

BINARIES_PATHS = [
    os.path.join('E:/Users/amade/opencvGPU/build/install', 'x64/vc17/bin'),
    os.path.join(os.getenv('CUDA_PATH', 'C:/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v12.3'), 'bin')
] + BINARIES_PATHS

To build I am configuring and generating the build in cmake-gui.

There is the configuration/generation log in case it is useful.

Selecting Windows SDK version 10.0.19041.0 to target Windows 10.0.19045.
Detected processor: AMD64
libjpeg-turbo: VERSION = 2.1.3, BUILD = opencv-4.9.0-dev-libjpeg-turbo
libjpeg-turbo(SIMD): SIMD extensions disabled: could not find NASM compiler.  Performance will suffer.
Could NOT find OpenJPEG (minimal suitable version: 2.0, recommended version >= 2.3.1). OpenJPEG will be built from sources
OpenJPEG: VERSION = 2.5.0, BUILD = opencv-4.9.0-dev-openjp2-2.5.0
OpenJPEG libraries will be built from sources: libopenjp2 (version "2.5.0")
found Intel IPP (ICV version): 2021.10.1 [2021.10.1]
at: E:/Users/amade/opencvGPU/build/3rdparty/ippicv/ippicv_win/icv
found Intel IPP Integration Wrappers sources: 2021.10.1
at: E:/Users/amade/opencvGPU/build/3rdparty/ippicv/ippicv_win/iw
CUDA detected: 12.3
CUDA: Using CUDA_ARCH_BIN=6.1
CUDA: NVCC target flags -gencode;arch=compute_61,code=sm_61;-D_FORCE_INLINES;-gencode;arch=compute_90,code=compute_90
CUDA: MSVS generator is detected. Disabling CMake re-run checks (CMAKE_SUPPRESS_REGENERATION=ON). You need to run CMake manually if updates are required.
Could not find OpenBLAS include. Turning OpenBLAS_FOUND off
Could not find OpenBLAS lib. Turning OpenBLAS_FOUND off
Could NOT find BLAS (missing: BLAS_LIBRARIES) 
Could NOT find LAPACK (missing: LAPACK_LIBRARIES) 
    Reason given by package: LAPACK could not be found because dependency BLAS could not be found.

VTK is not found. Please set -DVTK_DIR in CMake to VTK build directory, or to VTK install subdirectory with VTKConfig.cmake file
Module opencv_alphamat disabled because the following dependencies are not found: Eigen
freetype2:   NO
harfbuzz:    NO
Julia not found. Not compiling Julia Bindings. 
Module opencv_ovis disabled because OGRE3D was not found
Module opencv_sfm disabled because the following dependencies are not found: Eigen
Tesseract:   NO
Processing WORLD modules...
    module opencv_cudev...
    module opencv_core...
Allocator metrics storage type: 'long long'
Excluding from source files list: <BUILD>/modules/world/test/test_intrin256.lasx.cpp
    module opencv_cudaarithm...
    module opencv_flann...
    module opencv_imgproc...
    module opencv_intensity_transform...
    module opencv_ml...
    module opencv_phase_unwrapping...
    module opencv_plot...
    module opencv_quality...
    module opencv_reg...
    module opencv_signal...
    module opencv_surface_matching...
    module opencv_cudafilters...
    module opencv_cudaimgproc...
    module opencv_cudawarping...
    module opencv_dnn...
Registering hook 'INIT_MODULE_SOURCES_opencv_dnn': E:/Users/amade/opencvGPU/opencv/modules/dnn/cmake/hooks/INIT_MODULE_SOURCES_opencv_dnn.cmake
    module opencv_dnn_superres...
    module opencv_features2d...
    module opencv_fuzzy...
    module opencv_hfs...
    module opencv_imgcodecs...
imgcodecs: OpenEXR codec is disabled in runtime. Details: https://github.com/opencv/opencv/issues/21326
    module opencv_line_descriptor...
    module opencv_photo...
    module opencv_saliency...
    module opencv_text...
    module opencv_videoio...
    module opencv_xphoto...
    module opencv_calib3d...
    module opencv_cudacodec...
    module opencv_cudafeatures2d...
    module opencv_cudastereo...
    module opencv_datasets...
    module opencv_highgui...
highgui: using builtin backend: WIN32UI
    module opencv_mcc...
    module opencv_objdetect...
    module opencv_rapid...
    module opencv_rgbd...
rgbd: Eigen support is disabled. Eigen is Required for Posegraph optimization
    module opencv_shape...
    module opencv_structured_light...
    module opencv_video...
    module opencv_wechat_qrcode...
    module opencv_xfeatures2d...
    module opencv_ximgproc...
    module opencv_xobjdetect...
    module opencv_aruco...
    module opencv_bgsegm...
    module opencv_bioinspired...
    module opencv_ccalib...
    module opencv_cudabgsegm...
    module opencv_cudalegacy...
    module opencv_cudaobjdetect...
    module opencv_dnn_objdetect...
    module opencv_dpm...
    module opencv_face...
    module opencv_gapi...
    module opencv_optflow...
    module opencv_stitching...
    module opencv_tracking...
    module opencv_cudaoptflow...
Building with NVIDIA Optical Flow API 2.0
    module opencv_stereo...
    module opencv_superres...
    module opencv_videostab...
Processing WORLD modules... DONE
Excluding from source files list: modules/imgproc/src/imgwarp.lasx.cpp
Excluding from source files list: modules/imgproc/src/resize.lasx.cpp
Excluding from source files list: modules/dnn/src/layers/cpu_kernels/conv_winograd_f63.neon.cpp
Excluding from source files list: <BUILD>/modules/world/layers/layers_common.rvv.cpp
Excluding from source files list: <BUILD>/modules/world/layers/layers_common.lasx.cpp
Excluding from source files list: <BUILD>/modules/world/int8layers/layers_common.lasx.cpp
Excluding from source files list: <BUILD>/modules/world/layers/cpu_kernels/conv_block.neon.cpp
Excluding from source files list: <BUILD>/modules/world/layers/cpu_kernels/conv_block.neon_fp16.cpp
Excluding from source files list: <BUILD>/modules/world/layers/cpu_kernels/conv_depthwise.rvv.cpp
Excluding from source files list: <BUILD>/modules/world/layers/cpu_kernels/conv_depthwise.lasx.cpp
Excluding from source files list: <BUILD>/modules/world/layers/cpu_kernels/conv_winograd_f63.neon_fp16.cpp
Excluding from source files list: <BUILD>/modules/world/layers/cpu_kernels/fast_gemm_kernels.neon.cpp
Excluding from source files list: <BUILD>/modules/world/layers/cpu_kernels/fast_gemm_kernels.lasx.cpp
Found 'misc' Python modules from E:/Users/amade/opencvGPU/opencv/modules/python/package/extra_modules
Found 'mat_wrapper;utils' Python modules from E:/Users/amade/opencvGPU/opencv/modules/core/misc/python/package
Found 'gapi' Python modules from E:/Users/amade/opencvGPU/opencv/modules/gapi/misc/python/package
Found 'misc' Python modules from E:/Users/amade/opencvGPU/opencv/modules/python/package/extra_modules
Found 'mat_wrapper;utils' Python modules from E:/Users/amade/opencvGPU/opencv/modules/core/misc/python/package
Found 'gapi' Python modules from E:/Users/amade/opencvGPU/opencv/modules/gapi/misc/python/package
CMake Warning at cmake/OpenCVGenSetupVars.cmake:54 (message):
  CONFIGURATION IS NOT SUPPORTED: validate setupvars script in install
  directory
Call Stack (most recent call first):
  CMakeLists.txt:1138 (include)



General configuration for OpenCV 4.9.0-dev =====================================
  Version control:               4.9.0-241-g1eb061f89d

  Extra modules:
    Location (extra):            E:/Users/amade/opencvGPU/opencv_contrib/modules
    Version control (extra):     4.9.0-30-g6b5142ff

  Platform:
    Timestamp:                   2024-03-07T17:35:43Z
    Host:                        Windows 10.0.19045 AMD64
    CMake:                       3.29.0-rc2
    CMake generator:             Visual Studio 17 2022
    CMake build tool:            E:/Program Files/Microsoft Visual Studio/2022/Community/MSBuild/Current/Bin/amd64/MSBuild.exe
    MSVC:                        1930
    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 (18 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 (9 files):             + SSSE3 SSE4_1 POPCNT SSE4_2 AVX
      AVX2 (38 files):           + SSSE3 SSE4_1 POPCNT SSE4_2 FP16 FMA3 AVX AVX2
      AVX512_SKX (8 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:                E:/Program Files/Microsoft Visual Studio/2022/Community/VC/Tools/MSVC/14.30.30705/bin/Hostx64/x64/cl.exe  (ver 19.30.30706.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 /wd4819 /MP  /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 /wd4819 /MP  /Zi /Ob0 /Od /RTC1 
    C Compiler:                  E:/Program Files/Microsoft Visual Studio/2022/Community/VC/Tools/MSVC/14.30.30705/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   /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 /Zi /Ob0 /Od /RTC1 
    Linker flags (Release):      /machine:x64  /INCREMENTAL:NO 
    Linker flags (Debug):        /machine:x64  /debug /INCREMENTAL 
    ccache:                      NO
    Precompiled headers:         NO
    Extra dependencies:          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/v12.3/lib/x64
    3rdparty dependencies:

  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 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 ts video videoio videostab wechat_qrcode world xfeatures2d ximgproc xobjdetect xphoto
    Disabled:                    -
    Disabled by dependency:      -
    Unavailable:                 alphamat cannops cvv freetype hdf java julia matlab ovis python2 python2 sfm viz
    Applications:                tests perf_tests apps
    Documentation:               NO
    Non-free algorithms:         NO

  Windows RT support:            NO

  GUI: 
    Win32 UI:                    YES
    VTK support:                 NO

  Media I/O: 
    ZLib:                        build (ver 1.3.1)
    JPEG:                        build-libjpeg-turbo (ver 2.1.3-62)
      SIMD Support Request:      YES
      SIMD Support:              NO
    WEBP:                        build (ver encoder: 0x020f)
    PNG:                         build (ver 1.6.37)
    TIFF:                        build (ver 42 - 4.2.0)
    JPEG 2000:                   build (ver 2.5.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:                   2021.10.1 [2021.10.1]
           at:                   E:/Users/amade/opencvGPU/build/3rdparty/ippicv/ippicv_win/icv
    Intel IPP IW:                sources (2021.10.1)
              at:                E:/Users/amade/opencvGPU/build/3rdparty/ippicv/ippicv_win/iw
    Lapack:                      NO
    Eigen:                       NO
    Custom HAL:                  NO
    Protobuf:                    build (3.19.1)
    Flatbuffers:                 builtin/3rdparty (23.5.9)

  NVIDIA CUDA:                   YES (ver 12.3, CUFFT CUBLAS FAST_MATH)
    NVIDIA GPU arch:             61
    NVIDIA PTX archs:            90

  cuDNN:                         YES (ver 9.0.0)

  OpenCL:                        YES (NVD3D11)
    Include path:                E:/Users/amade/opencvGPU/opencv/3rdparty/include/opencl/1.2
    Link libraries:              Dynamic load

  Python 3:
    Interpreter:                 E:/Users/amade/anaconda3/python.exe (ver 3.11.7)
    Libraries:                   E:/Users/amade/anaconda3/libs/python311.lib (ver 3.11.7)
    Limited API:                 NO
    numpy:                       E:/Users/amade/anaconda3/Lib/site-packages/numpy/core/include (ver 1.26.4)
    install path:                E:/Users/amade/anaconda3/Lib/site-packages/cv2/python-3.11

  Python (for build):            E:/Users/amade/anaconda3/python.exe

  Java:                          
    ant:                         NO
    Java:                        YES (ver 21.0.2)
    JNI:                         C:/Program Files/Java/jdk-21/include C:/Program Files/Java/jdk-21/include/win32 C:/Program Files/Java/jdk-21/include
    Java wrappers:               NO
    Java tests:                  NO

  Install to:                    E:/Users/amade/opencvGPU/build/install
-----------------------------------------------------------------

Configuring done (19.6s)
Generating done (17.9s)

And then building it in an Anaconda Prompt with
cmake --build "E:\Users\amade\opencvGPU\build" --target INSTALL --config Release

I suspect OpenCV is not actually being built as cuDNN 9.0 is not supported yet

Do you get any build errors? If so try using an earlier version of cuDNN.

I rebuilt everything looked through the build logs in the Anaconda prompt. Except I got this error during build.

E:\Users\amade\opencvGPU\opencv\modules\dnn\src\cuda4dnn\csl\cudnn/recurrent.hpp(122,1): error C3861: 'cudnnSetRNNDescr
iptor_v6': identifier not found (compiling source file E:\Users\amade\opencvGPU\opencv\modules\dnn\src\layers\recurrent
_layers.cpp) [E:\Users\amade\opencvGPU\build\modules\world\opencv_world.vcxproj]

You need to downgrade your version of cuDNN. Try 8.9.7.

2 Likes

That did the trick.

You’re the best!

1 Like