Import not possible after build OpenCV with CUDA in Windows 10 (Python3.9)

Hello all,

I have been looking for a solution for several days and could not find one for the following error:

Blockquote
Traceback (most recent call last):
File “<pyshell#0>”, line 1, in
import cv2
File “C:\Python\lib\site-packages\cv2_init_.py”, line 181, in
bootstrap()
File “C:\Python\lib\site-packages\cv2_init_.py”, line 153, in bootstrap
native_module = importlib.import_module(“cv2”)
File “C:\Python\lib\importlib_init_.py”, line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
ImportError: DLL load failed while importing cv2: Das angegebene Modul wurde nicht gefunden. <

I can’t figure out what is causing this.
If I install this via PIP, opencv works without problems, only then without CUDA. What I would like to change.
When creating everything went through without problems, even with cmake.
Therefore, I am now at the end where I should still look for the error.
Does anyone have tips on what you could check everything?
Or even a solution for?

THANKS FOR ANY HELP!

I think you have to give path to cuda dll. Something like this in init.py :

'''
OpenCV Python binary extension loader
'''
import os
import sys
os.add_dll_directory(r'G:\Lib\install\opencv\x64\vc15\bin')
os.add_dll_directory(r'G:\Lib\install\vtk\bin')
os.add_dll_directory(r'G:\Lib\install\ceres-solver\bin')
os.add_dll_directory(r'G:\Lib\install\glog\bin')
os.add_dll_directory(r'F:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.2\bin')
os.add_dll_directory(r'F:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.2')

try:
    import numpy
    import numpy.core.multiarray
except ImportError:
    print('OpenCV bindings requires "numpy" package.')
    print('Install it via command:')
    print('    pip install numpy')
    raise

Hello,
thanks for the tip, unfortunately did not help to solve the problem.

Can you see somewhere more precisely which DLL is missing?

Peter

Did you modify the code from @laurent.berger to point to your Nvidia SDK installation and your intsall location. e.g. for me the below works

os.add_dll_directory(‘D:/build/opencv/cuda_build/bin’)
os.add_dll_directory(‘C:/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v11.7/bin’)

If that doesn’t work then you can track down the dll using process monitor by filtering for

Process Name = python.exe
Operation = Create File

and looking for the dll which eventually fails. But if you are not familiar with the tool its better to verify the paths above first in case that works.

Alternatively if you are using CUDA 11.7 you could try installing this pre-built wheel after first making sure you have uninstalled all existing OpenCV wheels and deleted all files copied accross to Lib/site-packages (cv2 folder) when you built/installed OpenCv from source.

1 Like

OMG!!! THANKS!!!

With the Process Monitor I was able to find the missing dll. It was the CUDNN file that was missing, so nothing worked for me. Copied all DLL’s where there was a problem into the CV2 folder under site-packgase and the last DLL then brought the solution.

THANK YOU VERY MUCH!!!

1 Like