I've encountered an issue where importing cv2 in Python triggers a DLL load failed error. Here's a detailed overview of my setup:
Installation and Configuration: OpenCV Installation: I built OpenCV from source using CMake and Visual Studio 2019. The build was configured for CUDA support with the following command (executed from the build directory):
cmake -G "NMake Makefiles" -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX="C:\DevelopmentTools\OpenCV\install" -DOPENCV_EXTRA_MODULES_PATH="C:\DevelopmentTools\OpenCV\opencv_contrib-4.7.0\modules" -DBUILD_opencv_world=ON -DBUILD_opencv_python3=ON -DWITH_CUDA=ON -DCUDA_TOOLKIT_ROOT_DIR="C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.8" -DCUDA_ARCH_BIN=8.6 -DWITH_CUDNN=ON -DCUDNN_INCLUDE_DIR="C:\Program Files\NVIDIA\CUDNN\v8.9.7\include" -DCUDNN_LIBRARY="C:\Program Files\NVIDIA\CUDNN\v8.9.7\lib\x64\cudnn.lib" -DOpenCV_DNN_CUDA=ON "C:\DevelopmentTools\OpenCV\opencv-4.7.0"
File Paths:
OpenCV Installation Path: C:\DevelopmentTools\OpenCV\install
DLL Directory: C:\DevelopmentTools\OpenCV\install\x64\vc16\bin
This directory contains opencv_world470.dll and opencv_videoio_ffmpeg470_64.dll among others.
Environment Variables:
PATH: Included C:\DevelopmentTools\OpenCV\install\x64\vc16\bin
LIB: Included C:\DevelopmentTools\OpenCV\install\x64\vc16\lib
INCLUDE: Included C:\DevelopmentTools\OpenCV\install\include
The Issue: When I try to import cv2 in Python, I receive the following error:
Traceback (most recent call last):
File "C:\Users\Edwar\Downloads\pyTest\pyTest.py", line 3, in <module>
import cv2
File "C:\Users\Edwar\AppData\Local\Programs\Python\Python310\lib\site-packages\cv2\__init__.py", line 181, in <module>
bootstrap()
File "C:\Users\Edwar\AppData\Local\Programs\Python\Python310\lib\site-packages\cv2\__init__.py", line 153, in bootstrap
native_module = importlib.import_module("cv2")
File "C:\Users\Edwar\AppData\Local\Programs\Python\Python310\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.
What I've Tried: Adding DLL Directory at Runtime: I've tried explicitly adding the directory containing the OpenCV DLLs to Python's DLL search path:
import os
os.add_dll_directory("C:\\DevelopmentTools\\OpenCV\\install\\x64\\vc16\\bin")
import cv2
Path Verification: I've checked and confirmed that the PATH environment variable includes the correct directory for OpenCV DLLs.
DLL Presence: Verified that opencv_world470.dll and other required DLLs are present in the installation's bin directory.
Request for Help:
What could be causing this error despite having the correct paths set and the DLLs in place? How can I resolve this DLL load failed issue to successfully import and use OpenCV in Python? Any insights into additional dependencies or configuration settings I might have missed would be appreciated.
If code goes well, version should be printed out
PATH) either some microsoft runtime DLLs and/or some CUDA DLLs (more likely).