OpenCV w/CUDA build seems successful, but "import cv2" fails

Hello folks.

I have successfully (?) compiled OpenCV for Python using the Jordan Benge guide (You’ll have to google it. I’m not allowed to post two links in a post).

I am on Windows 10 home, attempting to build with Python3 bindings into a fresh Python 3.8 conda environment.

I have checked my paths several times. No issues. I have set DCUDA_ARCH_BIN to 8.6, which is appropriate for my 3090 RTX. The build seems successful, although there are a few errors in the cmake error log that I will post below. I will also post my cmake build settings and system info.

*** I can conform that the cv2.cp38-win_amd64.pyd has successfully been copied into the conda environment’s site-packages directory. ****

However, when I try and import, I get this error:

In [1]: import cv2
---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
<ipython-input-1-c8ec22b3e787> in <cell line: 1>()
----> 1 import cv2

ImportError: DLL load failed while importing cv2: The specified module could not be found.

Note that this is a failure to load the pyd, and not a missing pyd. For comparison, here is the output when I try to import a fake pyd.

In [2]: import unicorn
---------------------------------------------------------------------------
ModuleNotFoundError                       Traceback (most recent call last)
<ipython-input-2-718a38ac41fc> in <cell line: 1>()
----> 1 import unicorn

ModuleNotFoundError: No module named 'unicorn'

---- What versions Python, OpenCV, OpenCV_contrib, CUDA, and CUDNN?

I have tried exclusively with:
OpenCV 4.6.0
OpenCV_contrib 4.6.0
Cuda 11.8.0
and CudNN 8.7.0.84 for Cuda 11 (cudnn-windows-x86_64-8.7.0.84_cuda11-archive)

I have attempted installs on Python 3.8 and 3.11. My last few attempts have focused on version 3.8 because it is the version used in the Jordan Benge guide i linked to earlier. I’ve

Conda has been installed for the user and with system path variables enabled.

---- Have you tried os.add_dll_directory ?

I have tried, unsuccessfully, to use os.add_dll_directory() for the following directories (and actually, more, but I did not log them all.)

os.add_dll_directory(“D://opencv-gpu//opencv_build//bin”)
os.add_dll_directory(“C://Program Files//NVIDIA GPU Computing Toolkit//CUDA//v11.8//bin”)

---- The CMAKE settings:

ECHO -- Starting OpenCV Configuration --
ECHO ---- Opening Visual Studio builder ----
call "C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Auxiliary/Build/vcvars64.bat"
ECHO ---- Setting up environment variables ----
call set_env_paths.bat
ECHO ---- Running CMake Commands ----
call "C:/Program Files/CMake/bin/cmake.exe" -B"%openCvBuild%/" -H"%openCvSource%/" -G"%generator%" -DCMAKE_BUILD_TYPE=%buildType% -DOPENCV_EXTRA_MODULES_PATH="%openCVExtraModules%/" ^
-DINSTALL_TESTS=ON -DINSTALL_C_EXAMPLES=ON -DBUILD_EXAMPLES=ON ^
-DBUILD_opencv_world=ON ^
-DWITH_CUDA=ON -DCUDA_TOOLKIT_ROOT_DIR="%toolkitRoot%" -DCUDA_FAST_MATH=ON -DWITH_CUBLAS=ON -DCUDA_ARCH_BIN=8.6 -DWITH_NVCUVID=ON ^
-DWITH_OPENGL=ON -DENABLE_FAST_MATH=ON ^
-DWITH_MFX=ON ^
-DBUILD_opencv_python3=ON -DPYTHON3_INCLUDE_DIR=%pathToAnaconda%/include -DPYTHON3_LIBRARY=%pathToAnaconda%/libs/python%pyVer%.lib -DPYTHON3_EXECUTABLE=%pathToAnaconda%/python.exe -DPYTHON3_NUMPY_INCLUDE_DIRS=%pathToAnaconda%/lib/site-packages/numpy/core/include -DPYTHON3_PACKAGES_PATH=%pathToAnaconda%/Lib/site-packages/ -DOPENCV_SKIP_PYTHON_LOADER=ON
ECHO -- OpenCV Configuration has finished, proceeding to build phase --
call "C:/Program Files/CMake/bin/cmake.exe" --build %openCvBuild% --target install
:End
PAUSE

---- Relevant system Info:

Host Name: DESKTOP-O4IGMIE
OS Name: Microsoft Windows 10 Home
OS Version: 10.0.19044 N/A Build 19044
OS Manufacturer: Microsoft Corporation
OS Configuration: Standalone Workstation
OS Build Type: Multiprocessor Free
Registered Owner:
Registered Organization:
Product ID: 00325-81445-19233-AAOEM
Original Install Date: 4/3/2021, 6:17:02 PM
System Boot Time: 12/14/2022, 9:08:47 AM
System Manufacturer: To Be Filled By O.E.M.
System Model: To Be Filled By O.E.M.
System Type: x64-based PC
Processor(s): 1 Processor(s) Installed.
[01]: Intel64 Family 6 Model 165 Stepping 5 GenuineIntel ~3701 Mhz
BIOS Version: American Megatrends Inc. P1.20, 4/15/2020
Windows Directory: C:\Windows
System Directory: C:\Windows\system32
Boot Device: \Device\HarddiskVolume1
System Locale: en-us;English (United States)
Input Locale: en-us;English (United States)
Time Zone: (UTC-05:00) Eastern Time (US & Canada)
Total Physical Memory: 81,810 MB
Available Physical Memory: 57,897 MB
Virtual Memory: Max Size: 93,586 MB
Virtual Memory: Available: 63,168 MB
Virtual Memory: In Use: 30,418 MB
Page File Location(s): C:\pagefile.sys

[Display]

Item Value
Name NVIDIA GeForce RTX 3090
PNP Device ID PCI\VEN_10DE&DEV_2204&SUBSYS_161319DA&REV_A1\4&30760922&0&0008
Adapter Type NVIDIA GeForce RTX 3090, NVIDIA compatible
Adapter Description NVIDIA GeForce RTX 3090
Adapter RAM (1,048,576) bytes
Installed Drivers C:\Windows\System32\DriverStore\FileRepository\nv_dispi.inf_amd64_807bbbd5b556d8f2\nvldumdx.dll,C:\Windows\System32\DriverStore\FileRepository\nv_dispi.inf_amd64_807bbbd5b556d8f2\nvldumdx.dll,C:\Windows\System32\DriverStore\FileRepository\nv_dispi.inf_amd64_807bbbd5b556d8f2\nvldumdx.dll,C:\Windows\System32\DriverStore\FileRepository\nv_dispi.inf_amd64_807bbbd5b556d8f2\nvldumdx.dll
Driver Version 31.0.15.2737
INF File oem62.inf (Section071 section)
Color Planes Not Available
Color Table Entries 4294967296
Resolution 2715 x 1697 x 59 hertz
Bits/Pixel 32
Memory Address 0xB2000000-0xB30FFFFF
Memory Address 0xA0000000-0xB1FFFFFF
Memory Address 0xB0000000-0xB1FFFFFF
I/O Port 0x00006000-0x00006FFF
IRQ Channel IRQ 4294967252
I/O Port 0x000003B0-0x000003BB
I/O Port 0x000003C0-0x000003DF
Memory Address 0xA0000-0xBFFFF
Driver C:\WINDOWS\SYSTEM32\DRIVERSTORE\FILEREPOSITORY\NV_DISPI.INF_AMD64_807BBBD5B556D8F2\NVLDDMKM.SYS (31.0.15.2737, 55.31 MB (58,000,888 bytes), 12/1/2022 5:30 PM)

---- ENV VARIABLES ( a few removed for security/privacy )

ALLUSERSPROFILE=C:\ProgramData
ANDROID_SDK_HOME=C:\Android
APPDATA=C:\Users\gabri\AppData\Roaming
CommonProgramFiles=C:\Program Files\Common Files
CommonProgramFiles(x86)=C:\Program Files (x86)\Common Files
CommonProgramW6432=C:\Program Files\Common Files
COMPUTERNAME=DESKTOP-O4IGMIE
ComSpec=C:\Windows\system32\cmd.exe
CUDA_PATH=C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.8
CUDA_PATH_V11_8=C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.8
DriverData=C:\Windows\System32\Drivers\DriverData
HOMEDRIVE=C:
HOMEPATH=\Users\gabri
LOCALAPPDATA=C:\Users\gabri\AppData\Local
NUMBER_OF_PROCESSORS=20
NVTOOLSEXT_PATH=C:\Program Files\NVIDIA Corporation\NvToolsExt
OneDrive=C:\Users\gabri\OneDrive
OneDriveConsumer=C:\Users\gabri\OneDrive
OS=Windows_NT
Path=C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.8\bin;C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.8\libnvvp;;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0;C:\Windows\System32\OpenSSH;C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;C:\Program Files\NVIDIA Corporation\NVIDIA NvDLISR;C:\Android;C:\Program Files\dotnet;C:\Program Files\Pandoc;C:\Program Files\Git\cmd;C:\Program Files\PuTTY;D:\Github\pupil\pupil_external;C:\Program Files\CMake\bin;C:\Program Files\NVIDIA Corporation\Nsight Compute 2022.3.0;D:\OpenCV-GPU\zlib\dll_x64;C:\Program Files\NVIDIA\CUDNN\v8.7\bin;C:\Users\gabri\anaconda3;C:\Users\gabri\anaconda3\Library\mingw-w64\bin;C:\Users\gabri\anaconda3\Library\usr\bin;C:\Users\gabri\anaconda3\Library\bin;C:\Users\gabri\anaconda3\Scripts;C:\Users\gabri\AppData\Local\Microsoft\WindowsApps;C:\Users\gabri\AppData\Local\GitHubDesktop\bin;C:\Users\gabri\AppData\Local\Programs\Microsoft VS Code\bin;C:\Program Files\Blender Foundation\Blender 2.92;C:\Users\gabri.dotnet\tools
PATHEXT=.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC
PROCESSOR_ARCHITECTURE=AMD64
PROCESSOR_IDENTIFIER=Intel64 Family 6 Model 165 Stepping 5, GenuineIntel
PROCESSOR_LEVEL=6
PROCESSOR_REVISION=a505
ProgramData=C:\ProgramData
ProgramFiles=C:\Program Files
ProgramFiles(x86)=C:\Program Files (x86)
ProgramW6432=C:\Program Files
PROMPT=$P$G
PSModulePath=C:\Program Files\WindowsPowerShell\Modules;C:\Windows\system32\WindowsPowerShell\v1.0\Modules
PUBLIC=C:\Users\Public
SESSIONNAME=Console
shell=C:\Program Files\Git\bin\sh.exe
SystemDrive=C:
SystemRoot=C:\Windows
TEMP=C:\Users\gabri\AppData\Local\Temp
TMP=C:\Users\gabri\AppData\Local\Temp

---- CMAKE ERROR LOG:

Here is a link, so I don’t make this post super long.

That error implies python can’t find the .pyd file, can you post the location of the cv2.cp38-win_amd64.pyd?

1 Like

Thank you for your question. The *.pyd is in the appropriate location:
C:\Users<username>\anaconda3\envs\py38\Lib\site-packages

I realize that I made mistake in that portion of my post. I believe I did not activate the correct conda environment before trying to import. I’ve updated the post. notice that the output is:

ImportError: DLL load failed while importing cv2: The specified module could not be found.

That is distinct from trying to import a module that does not exist. in that case, the output is:

ModuleNotFoundError: No module named 'unicorn'

it is probably looking for the dependant opencv dlls (opencv_core opencv_imgproc, etc)

for now, put the bin folder containing those on the environment PATH, or, better, rebuild with

cmake ... -DBUILD_SHARED_LIBS=OFF ...

so the cv2.pyd does no more depend on those

1 Like

Now the module is loaded you need to add the below when you are in the python interpreter.

If that doesn’t work you can use process monitor to track down the missing dll see

1 Like

Hi folks,

The combination of your suggestions worked, strangely enough.

As my post says, I had already tried to include the statements:

os.add_dll_directory(“D://opencvgpu//opencv_build//bin”)
os.add_dll_directory(“C://Program Files//NVIDIA GPU Computing Toolkit//CUDA//v11.8//bin”)

That did not work.

Rebuilding with the flag -DBUILD_SHARED_LIBS=OFF did not work either.

However, when I tried the combination of those two actions, it worked!

It is possible that the os.add_dll_directory() statements were somehow different last time in a way that prevented them from working, and the CMAKE arg -DBUILD_SHARED_LIBS is unnecessary, but I can’t say without further testing.

In any case, thanks so much for your feedback, folks. I’m now a very happy OpenCVer (now, with CUDA!).

This is likely because you have another dependency other than the ones in

D://opencvgpu//opencv_build//bin

and

C://Program Files//NVIDIA GPU Computing Toolkit//CUDA//v11.8//bin

which was built into the static lib.

If your interested try again with the shared lib and investigate with process monitor.

2 Likes

I’m a bit in the dark on how to use process monitor in this case. Do you have a helpful link for me? What should I filter by, and what are the red flags that indicate a missing dependency?

Of course, nobody would blame you if you let me know that this is above your pay grade (which is, of course, nothing!). Thanks again for your support so far.

I’ve uploaded a quick guide to adding/locating the missing opencv dll’s.

3 Likes

Just saw this reply. This is much more than I had expected. Thank you!