简体   繁体   English

OpenCV Python 与 CUDA 在 Windows 上:DLL 加载失败

[英]OpenCV Python with CUDA on Windows: DLL Load Failed

I am trying to get OpenCV python bindings to work with CUDA, but when I run我正在尝试让 OpenCV python 绑定与 CUDA 一起使用,但是当我运行

import cv2

I get the following error:我收到以下错误:

OpenCV loader: os.name="nt"  platform.system()="Windows"
OpenCV loader: loading config: C:\opencv\build\python_loader\cv2\config.py
OpenCV loader: loading config: C:\opencv\build\python_loader\cv2\config-3.8.py
OpenCV loader: PYTHON_EXTENSIONS_PATHS=['C:/opencv/build/lib/python3/Release']
OpenCV loader: BINARIES_PATHS=['C:/opencv/build/bin/Release']
OpenCV loader: PATH=C:/opencv/build/bin/Release;C:\Program Files\Python38\Scripts\;C:\Program Files\Python38\;C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v10.2\bin;C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v10.2\libnvvp;C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\WINDOWS\CCM;C:\WINDOWS\CCM;C:\Perl64\c\bin;C:\Perl64\perl\site\bin;C:\Perl64\perl\bin;C:\WINDOWS\CCM;C:\WINDOWS\CCM;C:\WINDOWS\CCM;C:\Program Files\PuTTY\;C:\Program Files\Microsoft SQL Server\Client SDK\ODBC\110\Tools\Binn\;C:\Program Files (x86)\Microsoft SQL Server\120\Tools\Binn\;C:\Program Files\Microsoft SQL Server\120\Tools\Binn\;C:\Program Files\Microsoft SQL Server\120\DTS\Binn\;C:\Program Files (x86)\Windows Kits\8.1\Windows Performance Toolkit\;C:\Program Files\Git\cmd;C:\Program Files\Git\mingw64\bin;C:\Program Files\Git\usr\bin;C:\Program Files\nodejs\;C:\Program Files (x86)\Yarn\bin\;C:\Program Files\NVIDIA Corporation\Nsight Compute 2019.5.0\;C:\opencv\build\install\x64\vc16\bin\;C:\Users\md249\AppData\Local\Microsoft\WindowsApps;
OpenCV loader: replacing cv2 module
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\opencv\build\python_loader\cv2\__init__.py", line 96, in <module>
    bootstrap()
  File "C:\opencv\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.

I have the following environment variables set:我设置了以下环境变量:

OPENCV_DIR: C:\opencv\build\
PATH: $PATH;C:\opencv\build\install\x64\vc16\bin\;C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v10.2\bin

It seems that OpenCV builds binaries to multiple different directories for reasons I do not understand, so I am unsure where to point PATH and OPENCV_DIR.似乎 OpenCV 出于我不理解的原因将二进制文件构建到多个不同的目录,所以我不确定指向 PATH 和 OPENCV_DIR 的位置。

Running Dependency Walker against cv2.cp38-win_amd64.pyd does not indicate any missing dependencies from OpenCV or CUDA.针对cv2.cp38-win_amd64.pyd运行 Dependency Walker 并不表示 OpenCV 或 CUDA 中缺少任何依赖项。

Does the.pyd dynamically load any other dependencies that Dependency Walker might not catch? .pyd 是否动态加载 Dependency Walker 可能无法捕获的任何其他依赖项? C++ applications load OpenCV just fine so I imagine the problem has something to do with where the.pyd extension is looking for dependencies. C++ 应用程序加载 OpenCV 就好了所以我想这个问题与 .pyd 扩展正在寻找依赖项的位置有关。

Hopefully I have included all the relevant information.希望我已经包含了所有相关信息。

It is probably because you didnt installed OpenCV, you donwloaded it, if you don't want to spend time configuring extra path to make sure python finds the right opencv package you just need to install it using 'pip':可能是因为你没有安装OpenCV,你下载了它,如果你不想花时间配置额外的路径来确保python找到正确的opencv包,你只需要使用'pip'安装它:

pip install opencv-contrib-python

It will probably solve you problem, and the C++ software can still use the downloaded version without conflict.它可能会解决您的问题,并且C++软件仍然可以使用下载的版本而不会发生冲突。

Install Reference安装参考

I have got the same error message when importing cv2 , but I compiled OpenCV from source including the extra modules from opencv_contrib , because I wanted a Win64 build including cuda* and python3 modules.导入cv2时我收到了相同的错误消息,但我从源代码编译了OpenCV ,包括来自opencv_contrib的额外模块,因为我想要一个包含cuda*python3模块的Win64版本。

Workaround解决方法

Even after investing an extended amount of time I cannot offer a satisfying solution.即使在投入大量时间之后,我也无法提供令人满意的解决方案。 But my functioning workaround is to use dumpbin or Dependency walker to find all the cv2.cp39-win_amd64.pyd dependencies recursively and provide all of them including cv2.cp39-win_amd64.pyd from the same flat directory, which you add to PYTHONPATH .但我的有效解决方法是使用dumpbinDependency walker递归查找所有cv2.cp39-win_amd64.pyd依赖项,并从同一平面目录中提供所有依赖项,包括cv2.cp39-win_amd64.pyd ,您将其添加到PYTHONPATH

In my case this meant the following manual steps after compiling OpenCV from source:就我而言,这意味着在从源代码编译OpenCV后执行以下手动步骤:

  • Make a directory like C:\\opencv-py39 .创建一个像C:\\opencv-py39这样的目录。
  • Copy cv2.cp39-win_amd64.pyd to C:\\opencv-py39 .cv2.cp39-win_amd64.pyd复制到C:\\opencv-py39
  • Copy all OpenCV shared libs ( ${EXECUTABLE_OUTPUT_PATH)\\*.dll eg opencv_world455.dll ) to C:\\opencv-py39 .将所有 OpenCV 共享库( ${EXECUTABLE_OUTPUT_PATH)\\*.dll例如opencv_world455.dllopencv_world455.dllC:\\opencv-py39
  • Copy all CUDA-relevant shared libs ( %CUDA_PATH%\\bin\\*.dll ) to C:\\opencv-py39 .将所有与 CUDA 相关的共享库( %CUDA_PATH%\\bin\\*.dll )复制到C:\\opencv-py39
  • Copy all Intel TBB shared libs ( %ONEAPI_ROOT%\\tbb\\latest\\redist\\intel64\\vc14\\*.dll ) to C:\\opencv-py39 .将所有英特尔 TBB共享库( %ONEAPI_ROOT%\\tbb\\latest\\redist\\intel64\\vc14\\*.dll%ONEAPI_ROOT%\\tbb\\latest\\redist\\intel64\\vc14\\*.dllC:\\opencv-py39
  • Set the environment variable PYTHONPATH=C:\\opencv-py39 .设置环境变量PYTHONPATH=C:\\opencv-py39

Depending on the build options you choose this list might be inconclusive.根据您选择的构建选项,此列表可能不确定。

More details更多细节

This workaround bypasses the problems that I experience not having the dependencies of my cv2.cp39-win_amd64.pyd loaded from their specific installation directories even if environment variables are set properly and C++ example progams build and execute as expected.这种解决方法绕过了我遇到的问题,即使环境变量设置正确并且 C++ 示例程序按预期构建和执行,我的cv2.cp39-win_amd64.pyd的依赖项也没有从它们的特定安装目录加载。 I guess this is specific to the shared library loader of CPython .我想这是特定于CPython的共享库加载器的。 I have found evidence that this problem might have been introduced in Python 3.8.我发现有证据表明这个问题可能是在 Python 3.8 中引入的。

Having a closer look using SysInternals procmon utility reveals, that the python interpreter stops using the right module name while probing all the configured DLL search directories.使用SysInternals procmon实用程序仔细查看会发现,python 解释器在探测所有配置的 DLL 搜索目录时停止使用正确的模块名称。 Ie it starts looking for opencv_world455.dll in the current working directory, then in the Python directory etc. but when it starts to look into the PATH's directories it uses a faulty module name like <stdin> or similar corrupted names.即它开始在当前工作目录中寻找opencv_world455.dll ,然后在 Python 目录等中,但是当它开始查看 PATH 的目录时,它使用了错误的模块名称,如<stdin>或类似的损坏名称。 Maybe I should submit a bug report to CPython...也许我应该向 CPython 提交一个错误报告......

Adding to @salchint workaround:添加到@salchint解决方法:

I got the same ImportError problem with my installation and solved it thanks to an Anaconda install I had on the side.我的安装遇到了同样的ImportError问题并解决了它,这要归功于我旁边的 Anaconda 安装。

It turns out that all the missing DLLs could be found in .../Anaconda3/Library/bin .事实证明,所有丢失的 DLL 都可以在.../Anaconda3/Library/bin中找到。 So including the folder to your script with:因此,将文件夹包含到您的脚本中:

import os
os.add_dll_directory('path_to_Anaconda/Anaconda3/Library/bin')

Solved the error for me.为我解决了错误。 Note that this folder is in my Path and should be discoverable but Python has (or had?) some problems doing it.请注意,此文件夹我的路径中,应该可以发现,但 Python 有(或有?)一些问题

A cleaner solution (but still a workaround) is then to add this command to the __init__.py file of the cv2 module in YOUR_PYTHON_PATH/Lib/site-packages一个更干净的解决方案(但仍然是一种解决方法)是将此命令添加到YOUR_PYTHON_PATH/Lib/site-packagescv2模块的__init__.py文件

I wrote about it in more detail here .我在这里写得更详细。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM