简体   繁体   English

如何在 python (Windows) 上的 openCV dnn 模块上使用 GPU 加速?

[英]How to use GPU-acceleration on openCV dnn module on python (Windows)?

I want to use opencv-python4.4 to do inference for YOLOv3.我想使用opencv-python4.4opencv-python4.4进行推理。 But it uses CPU by default.但它默认使用 CPU。 I have read this instruction here , saying the new version opencv4.2.0 has already supported GPU.我在这里看过这个说明,说新版本opencv4.2.0已经支持GPU了。 But the instruction is only for Ubuntu but mine is Windows10 .但是该说明仅适用于Ubuntu而我的是Windows10

I tried to Could you please walk me through how to pull this off in Wondows10, python?我试过你能不能请教我如何在Wondows10,python中完成这个? As I directly used it, the error occurred:当我直接使用它时,出现错误:

cv::dnn::dnn4_v20191202::Net::Impl::setUpNet DNN module was not built with CUDA backend; switching to CPU

It seems that I have to manually compile opencv but isn't it for Cpp ?似乎我必须手动编译opencv但不是用于Cpp吗? But I only want to implement it on python .但我只想在python上实现它。 Could anyone give me some instructions?谁能给我一些指示?

You will indeed need to build OpenCV yourself.您确实需要自己构建 OpenCV。

While you're using the Python bindings for OpenCV, the OpenCV library itself is written in C++ instead of Python.当您使用 OpenCV 的 Python 绑定时,OpenCV 库本身是用 C++ 而非 Python 编写的。 That also explains how OpenCV can use CUDA, another C++ library to access NVidia GPU's.这也解释了 OpenCV 如何使用 CUDA,另一个 C++ 库来访问 NVidia GPU。

The instructions you linked are from a person not associated with OpenCV, who admits to an anti-Windows bias.您链接的说明来自与 OpenCV 无关的人,他承认有反 Windows 偏见。 That means those instructions are not useful to you.这意味着这些说明对您没有用。

These instructions do cover the Windows build. 这些说明确实涵盖了 Windows 版本。

Summary:概括:

  • Use Visual Studio 2019 (Latest C++ compiler from Microsoft) and CMake使用 Visual Studio 2019(来自 Microsoft 的最新 C++ 编译器)和 CMake
  • Download & install OpenCV下载并安装 OpenCV
  • Download & install CUDA and cuDNN下载并安装 CUDA 和 cuDNN
  • Download & install Anaconda3 and use it as default Python下载并安装 Anaconda3 并将其用作默认 Python
  • Set environment variables so CMake can find your installed libraries设置环境变量,以便 CMake 可以找到您安装的库
  • Set the environment variables and options so CMake knows that you also want the Python bindings for OpenCV设置环境变量和选项,以便 CMake 知道您还需要 OpenCV 的 Python 绑定
  • Use CMake to create .sln file for Visual Studio使用 CMake 为 Visual Studio 创建 .sln 文件
  • Open .sln in Visual Studio and build it.在 Visual Studio 中打开 .sln 并构建它。
  • This generates the cv2.cp37-win_amd64.pyd file you need.这将生成您需要的cv2.cp37-win_amd64.pyd文件。

I have figured it out... both我已经想通了......两者都

-DBUILD_opencv_python3=ON
-DBUILD_opencv_python2=OFF

must be specified, otherwise the to be built will not include any python module at all.必须指定,否则to be built将根本不包含任何 python 模块。

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

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