I’m hoping someone can guide me resolve this error on my setup. Note that I did not encounter this error on my first try of building the code (used OPENCV_ENABLE_NONFREE). After a while (couple of days), I then tried to include CUDA but this error starts to appear:
Python 3.8.6 (tags/v3.8.6:db45529, Sep 23 2020, 15:52:53) [MSC v.1927 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 "C:\DevHome\Program\OpenCV450\build\python_loader\cv2\__init__.py", line 96, in <module>
bootstrap()
File "C:\DevHome\Program\OpenCV450\build\python_loader\cv2\__init__.py", line 86, in bootstrap
import cv2
ImportError: DLL load failed while importing cv2: The specified module could not be found.
After several days of trying and searching the net, I’m still unable to find a solution. I did try reinstalling Python (still 3.8.6) and changed OpenCV version to 4.5.0 (from 4.5.1) but still no luck. Perhaps a Windows or driver update caused the problem? My recent finding is that when I don’t include OPENCV_ENABLE_NONFREE, the error is gone (but why was I successful before?)
Here are the details of my setup:
– CMake
Selecting Windows SDK version 10.0.18362.0 to target Windows 10.0.19042.
Detected processor: AMD64
libjpeg-turbo: VERSION = 2.0.5, BUILD = opencv-4.5.0-libjpeg-turbo
Could NOT find OpenJPEG (minimal suitable version: 2.0, recommended version >= 2.3.1). OpenJPEG will be built from sources
OpenJPEG: VERSION = 2.3.1, BUILD = opencv-4.5.0-openjp2-2.3.1
OpenJPEG libraries will be built from sources: libopenjp2 (version "2.3.1")
found Intel IPP (ICV version): 2020.0.0 [2020.0.0 Gold]
at: C:/DevHome/Program/OpenCV450/build/3rdparty/ippicv/ippicv_win/icv
found Intel IPP Integration Wrappers sources: 2020.0.0
at: C:/DevHome/Program/OpenCV450/build/3rdparty/ippicv/ippicv_win/iw
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
OpenCV Python: during development append to PYTHONPATH: C:/DevHome/Program/OpenCV450/build/python_loader
Module opencv_alphamat disabled because the following dependencies are not found: Eigen
Caffe: NO
Protobuf: NO
Glog: NO
freetype2: NO
harfbuzz: NO
Julia not found. Not compiling Julia Bindings.
Module opencv_ovis disabled because OGRE3D was not found
No preference for use of exported gflags CMake configuration set, and no hints for include/library directories provided. Defaulting to preferring an installed/exported gflags CMake configuration if available.
Failed to find installed gflags CMake configuration, searching for gflags build directories exported with CMake.
Failed to find gflags - Failed to find an installed/exported CMake configuration for gflags, will perform search for installed gflags components.
Failed to find gflags - Could not find gflags include directory, set GFLAGS_INCLUDE_DIR to directory containing gflags/gflags.h
Failed to find glog - Could not find glog include directory, set GLOG_INCLUDE_DIR to directory containing glog/logging.h
Module opencv_sfm disabled because the following dependencies are not found: Eigen Glog/Gflags
Allocator metrics storage type: 'long long'
Registering hook 'INIT_MODULE_SOURCES_opencv_dnn': C:/DevHome/Program/OpenCV450/source/opencv-4.5.0/modules/dnn/cmake/hooks/INIT_MODULE_SOURCES_opencv_dnn.cmake
opencv_dnn: filter out cuda4dnn source code
General configuration for OpenCV 4.5.0 =====================================
Version control: unknown
Extra modules:
Location (extra): C:/DevHome/Program/OpenCV450/source/opencv_contrib-4.5.0/modules
Version control (extra): unknown
Platform:
Timestamp: 2021-01-14T11:59:53Z
Host: Windows 10.0.19042 AMD64
CMake: 3.19.2
CMake generator: Visual Studio 16 2019
CMake build tool: C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/MSBuild/Current/Bin/MSBuild.exe
MSVC: 1928
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: C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/MSVC/14.28.29333/bin/Hostx64/x64/cl.exe (ver 19.28.29336.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:precise /EHa /wd4127 /wd4251 /wd4324 /wd4275 /wd4512 /wd4589 /MP /MD /O2 /Ob2 /DNDEBUG /Zi
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:precise /EHa /wd4127 /wd4251 /wd4324 /wd4275 /wd4512 /wd4589 /MP /MDd /Zi /Ob0 /Od /RTC1
C Compiler: C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/MSVC/14.28.29333/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:precise /MP /MD /O2 /Ob2 /DNDEBUG /Zi
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:precise /MP /MDd /Zi /Ob0 /Od /RTC1
Linker flags (Release): /machine:x64 /INCREMENTAL:NO /debug
Linker flags (Debug): /machine:x64 /debug /INCREMENTAL
ccache: NO
Precompiled headers: YES
Extra dependencies:
3rdparty dependencies:
OpenCV modules:
To be built: aruco bgsegm bioinspired calib3d ccalib core 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 stereo stitching structured_light superres surface_matching text tracking ts video videoio videostab xfeatures2d ximgproc xobjdetect xphoto
Disabled: world
Disabled by dependency: -
Unavailable: alphamat cnn_3dobj cudaarithm cudabgsegm cudacodec cudafeatures2d cudafilters cudaimgproc cudalegacy cudaobjdetect cudaoptflow cudastereo cudawarping cudev cvv freetype java js julia matlab ovis python2 sfm viz
Applications: tests perf_tests apps
Documentation: NO
Non-free algorithms: YES
Windows RT support: NO
GUI:
Win32 UI: YES
VTK support: NO
Media I/O:
ZLib: build (ver 1.2.11)
JPEG: build-libjpeg-turbo (ver 2.0.5-62)
WEBP: build (ver encoder: 0x020f)
PNG: build (ver 1.6.37)
TIFF: build (ver 42 - 4.0.10)
JPEG 2000: build (ver 2.3.1)
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.91.100)
avformat: YES (58.45.100)
avutil: YES (56.51.100)
swscale: YES (5.7.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:/DevHome/Program/OpenCV450/build/3rdparty/ippicv/ippicv_win/icv
Intel IPP IW: sources (2020.0.0)
at: C:/DevHome/Program/OpenCV450/build/3rdparty/ippicv/ippicv_win/iw
Lapack: NO
Eigen: NO
Custom HAL: NO
Protobuf: build (3.5.1)
OpenCL: YES (NVD3D11)
Include path: C:/DevHome/Program/OpenCV450/source/opencv-4.5.0/3rdparty/include/opencl/1.2
Link libraries: Dynamic load
Python 3:
Interpreter: C:/DevHome/Program/Python/Python38/python.exe (ver 3.8.6)
Libraries: optimized C:/DevHome/Program/Python/Python38/libs/python38.lib debug C:/DevHome/Program/Python/Python38/libs/python38_d.lib (ver 3.8.6)
numpy: C:/DevHome/Program/Python/Python38/lib/site-packages/numpy/core/include (ver 1.19.3)
install path: C:/DevHome/Program/Python/Python38/Lib/site-packages/cv2/python-3.8
Python (for build): C:/DevHome/Program/Python/Python38/python.exe
Java:
ant: NO
JNI: C:/Program Files/Java/jdk-11.0.9/include C:/Program Files/Java/jdk-11.0.9/include/win32 C:/Program Files/Java/jdk-11.0.9/include
Java wrappers: NO
Java tests: NO
Install to: C:/DevHome/Program/OpenCV450/build/install
-----------------------------------------------------------------
Configuring done
Are you 100% that building without OPENCV_ENABLE_NONFREE always works or did it just work the first time?
If its that then I would assume one of the extra modules has a dependent dll.
Can you run the OpenCv test programs without issue, e.g. opencv_test_core.exe? As you are not building world you may need to make sure all the test programs work to see if you need a dll which isn’t on your path.
Which directory containing the opencv dll’s did you add to your path bin or install\x64\vc16\bin?
If the error occurred when you included CUDA, check you have the CUDA bin directory of the CUDA version you built against on your system/user path, e.g.
import os
os.add_dll_directory('C:\\DevHome\\Program\\OpenCV450\\build\\lib\\Release')
if that’s the directory containing the OpenCV dll and then add the CUDA directory and any other directories containing required dll’s until it starts to work. This sometimes fixes python 3.8 issues.
Are you sure the .pyd in C:/DevHome/Program/Python/Python38/Lib/site-packages/cv2/python-3.8 corresponds to the .dll in
C:\DevHome\Program\OpenCV450\build\bin\Release and not one you previously build?
I would also try deleting the cv2 dir and copying the pyd directly into the
C:/DevHome/Program/Python/Python38/Lib/site-packages/ directory.
You may also want to try
opencv_test_cudaarithm.exe
Additionally you don’t need OPENCV_ENABLE_NONFREE for CUDA so you could try building without it, if you are 100% this is causing the problem.
Failing that try anaconda and dload the binaries from
I tried this, same result (no error on init.py now):
Python 3.8.6 (tags/v3.8.6:db45529, Sep 23 2020, 15:52:53) [MSC v.1927 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>
ImportError: DLL load failed while importing cv2: The specified module could not be found.
I would now give the below a second shot in the python terminal before importing cv2.
You could also try
import os
os.add_dll_directory(‘C:\DevHome\Program\OpenCV450\build\lib\Release’)
if that’s the directory containing the OpenCV dll and then add the CUDA directory and any other directories containing required dll’s until it starts to work. This sometimes fixes python 3.8 issues.
Ah, I actually need the OPENCV_ENABLE_NONFREE. I believe I read James Bowley’s blog and tried the steps of code rebuild there as well (via CMD, though I prefer CMake GUI). It was really weird that my first build before was ok. Looking at the apps I installed after that, maybe one of them is the culprit?
Python 3.8.6 (tags/v3.8.6:db45529, Sep 23 2020, 15:52:53) [MSC v.1927 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> os.add_dll_directory('C:\\DevHome\\Program\\OpenCV450\\build\\lib\\Release')
<AddedDllDirectory('C:\\DevHome\\Program\\OpenCV450\\build\\lib\\Release')>
>>> import cv2
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: DLL load failed while importing cv2: The specified module could not be found.
>>> os.add_dll_directory('C:\\DevHome\\Program\\OpenCV450\\build\\bin\\Release')
<AddedDllDirectory('C:\\DevHome\\Program\\OpenCV450\\build\\bin\\Release')>
>>> import cv2
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: DLL load failed while importing cv2: The specified module could not be found.
Is there a way to change the code in OpenCV to tell me which specific DLL is being loaded?
@cudawarped I didn’t notice that you are James Bowley. Thanks for helping out and great blog site by the way!
I guess I should try your binaries now. Though when I first downloaded it, I didn’t know how to use or install it. Maybe you can help me with that. I still consider myself as a newbie in building source code in Windows using CMake and Visual Studio (though I have lots of Linux experience).
perhaps investigate what DLL it’s missing. it might be something opencv picked up in the build process somewhere, but that isn’t in PATH yet…
also be aware that setting PATH in the system settings does not propagate into running processes. that’s transitive, so you will probably need to log out and back in again to make it work easily. starting processes from Win+R should work as well because that explorer process gets those environment variable updates.
Thanks for helping out @crackwitz . Ah yes, I’m aware about the Path behavior. I always create a new CMD everytime I change Path environment value.
I think one improvement in OpenCV code is to provide a more specific error message on what DLL is being loaded. Is there a way to do that? I tried adding debug info to my Python, still not showing. I heard about Python’s flag -O and -OO but also not helping.
that, unfortunately, is an error generated by python. OpenCV can’t affect that.
it would be nice indeed if python could look into what dll failed to load… they probably would, if windows provided that information trivially.
the existence of Dependency Walker tells me that this problem won’t be a quick fix. likely microsoft would have to get off their asses and sit down to stomp a new API out of the ground.
I’ve had to fight with gstreamer… it has its own pile of dll somewhere, AND THEY NAME-CLASH with other DLLs on my system that are system/runtime-related.