详细步骤参考Accelerate OpenCV 4.5.0 on Windows – build with CUDA and python bindings
准备
- VS2019下载安装
旋转Desktop development with C++ 和 python development
- cmake 下载安装 cmake-3.18.4-win64-x64.msi
可以用C:\Program Files\CMake\bin\cmake-gui.exe进行configure和generate
- cuda11.1下载安装 cuda_11.1.1_456.81_win10.exe
- cudnn下载 cudnn-11.1-windows-x64-v8.0.4.30
Extract and copy the bin, include and lib directories to your CUDA installation
//C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.1
- Nvidia Video Codec SDK下载 Video_Codec_SDK_11.1.5.zip
Extract and copy the contents of the Interface to the include and the Lib to the lib directory inside your CUDA installation
//C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.1
- Intel Media SDK下载安装 MSDK2020R1.exe
- Intel MKL and TBB
这次没使用
- Anaconda3(包括opencv-python、opencv_contrib_python)
Anaconda3-2021.05-Windows-x86_64.exe
opencv_contrib_python-4.5.2.52-cp38-cp38-win_amd64.whl
opencv_python-4.5.2.52-cp38-cp38-win_amd64.whl
- OpenCV源码 opencv-4.5.2.zip 和 opencv_contrib-4.5.2.zip
Python bindings修改
//pyconfig.h
pragma comment(lib,"python37_d.lib")
to
pragma comment(lib,"python37.lib")
and
# define Py_DEBUG
to
//# define Py_DEBUG
wechat_qrcode 支持cuda
//opencv_contrib-4.5.2\modules\wechat_qrcode\src\detector\ssd_detector.cpp
int SSDDetector::init(const string& proto_path, const string& model_path) {
net_ = dnn::readNetFromCaffe(proto_path, model_path);
#支持GPU
net_.setPreferableBackend(dnn::DNN_BACKEND_CUDA);
net_.setPreferableTarget(dnn::DNN_TARGET_CUDA);
return 0;
}
编译
#opencv4源码路径
set "openCvSource=D:/opencv4/opencv-4.5.2"
#opencv_contrib源码路径,到modules
set "openCVExtraModules=D:/opencv4/opencv_contrib-4.5.2/modules"
#anaconda 环境路径
set "pathToAnaconda=C:/Users/xxx/.conda/envs/py38"
set "pyVer=38"
set "openCvBuild=%openCvSource%\build"
set "buildType=Release"
set "generator=Visual Studio 16 2019"
"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="C:/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v11.1" -DCUDA_FAST_MATH=ON -DWITH_CUBLAS=ON -DCUDA_ARCH_PTX=8.6 -DWITH_NVCUVID=ON ^
-DWITH_OPENGL=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
#以上步骤建议用cmake-gui.exe操作比较直观
"C:\Program Files\CMake\bin\cmake.exe" --build %openCvBuild% --config Release --target install
测试
import cv2
import os
import time
import queue
import time
import threading
import json
import socket
import urllib
import requests
import platform
import base64
import ctypes
from ctypes import *
#C:\ProgramData\Anaconda3\Lib\site-packages\cv2
#GPU. cv.cuda.imread ()
print(cv2.getBuildInformation())
#返回cuda设备数量
print(cv2.cuda.getCudaEnabledDeviceCount())
cap = cv2.VideoCapture(0) #
cap.set(cv2.CAP_PROP_FRAME_WIDTH,1280)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT,720)
#CAP_PROP_FOURCC要在之后CAP_PROP_FRAME之后设置,否则无效
cap.set(cv2.CAP_PROP_FOURCC, cv2.VideoWriter.fourcc('M','J','P','G'))
print(cap.isOpened())
gpu_frame=cv2.cuda_GpuMat()
img=cv2.imread('./qrcode.jpg')
# 加载图像(CPU)
screenshot = cv2.imread('./qrcode.jpg')
# 上传到GPU
gpu_frame.upload(screenshot)
# 转换颜色到opencv (numpy) ndarray→cv2.cuda_GpuMat
screenshot = cv2.cuda.cvtColor(gpu_frame, cv2.COLOR_RGB2BGR)
# 反向阈值@ 100
screenshot = cv2.cuda.threshold(screenshot, 105, 255, cv2.THRESH_BINARY_INV)
# 调整图像
screenshot = cv2.cuda.resize(screenshot[1], (200, 200))
# 从GPU下载图像(cv2) cuda_GpuMat→numpy.ndarray
screenshot = screenshot.download()
cv2.imshow('frame1',screenshot)
cv2.waitKey(5000)
问题
- 库依赖
#使用VS tools进行查看dll的依赖库
dumpbin /dependents xxx.dll
#查询依赖库的位置
where xxx.dll
- 编译过程中文件下载失败
因为wall原因,无法在线下载
可直接从互联网找然后拷贝到.cache目录下对应的文件
其中.cache下为0字节的文件一班就是下载不下来的原因
- 路径
#opencv-4.5.2\build\lib\python3\Release
cv2.cp38-win_amd64.pyd
#opencv-4.5.2\build\bin\Release
opencv_world452.dll
opencv_videoio_ffmpeg452_64.dll
opencv_img_hash452.dll
#Anaconda3\Lib\site-packages\cv2
cv2.cp38-win_amd64.pyd
opencv_world452.dll
opencv_videoio_ffmpeg452_64.dll
opencv_img_hash452.dll
- DSHOW和MSMF
针对摄像头cv2.VideoCapture(1, cv2.CAP_MSMF)和cv2.VideoCapture(1, cv2.CAP_DSHOW)调用方式是有区别的,具体区别是啥?
- 资源利用?
- 是否利用显卡?
- MSMF打开慢,因为要等待FrameServer启动
把【服务】里的【Windows Camera Frame Server】服务禁用掉后,CAP_MSMF则无法使用
enum VideoCaptureAPIs {
CAP_ANY = 0, //!< Auto detect == 0
CAP_VFW = 200, //!< Video For Windows (obsolete, removed)
CAP_V4L = 200, //!< V4L/V4L2 capturing support
CAP_V4L2 = CAP_V4L, //!< Same as CAP_V4L
CAP_FIREWIRE = 300, //!< IEEE 1394 drivers
CAP_FIREWARE = CAP_FIREWIRE, //!< Same value as CAP_FIREWIRE
CAP_IEEE1394 = CAP_FIREWIRE, //!< Same value as CAP_FIREWIRE
CAP_DC1394 = CAP_FIREWIRE, //!< Same value as CAP_FIREWIRE
CAP_CMU1394 = CAP_FIREWIRE, //!< Same value as CAP_FIREWIRE
CAP_QT = 500, //!< QuickTime (obsolete, removed)
CAP_UNICAP = 600, //!< Unicap drivers (obsolete, removed)
CAP_DSHOW = 700, //!< DirectShow (via videoInput)
CAP_PVAPI = 800, //!< PvAPI, Prosilica GigE SDK
CAP_OPENNI = 900, //!< OpenNI (for Kinect)
CAP_OPENNI_ASUS = 910, //!< OpenNI (for Asus Xtion)
CAP_ANDROID = 1000, //!< Android - not used
CAP_XIAPI = 1100, //!< XIMEA Camera API
CAP_AVFOUNDATION = 1200, //!< AVFoundation framework for iOS (OS X Lion will have the same API)
CAP_GIGANETIX = 1300, //!< Smartek Giganetix GigEVisionSDK
CAP_MSMF = 1400, //!< Microsoft Media Foundation (via videoInput)
CAP_WINRT = 1410, //!< Microsoft Windows Runtime using Media Foundation
CAP_INTELPERC = 1500, //!< RealSense (former Intel Perceptual Computing SDK)
CAP_REALSENSE = 1500, //!< Synonym for CAP_INTELPERC
CAP_OPENNI2 = 1600, //!< OpenNI2 (for Kinect)
CAP_OPENNI2_ASUS = 1610, //!< OpenNI2 (for Asus Xtion and Occipital Structure sensors)
CAP_GPHOTO2 = 1700, //!< gPhoto2 connection
CAP_GSTREAMER = 1800, //!< GStreamer
CAP_FFMPEG = 1900, //!< Open and record video file or stream using the FFMPEG library
CAP_IMAGES = 2000, //!< OpenCV Image Sequence (e.g. img_%02d.jpg)
CAP_ARAVIS = 2100, //!< Aravis SDK
CAP_OPENCV_MJPEG = 2200, //!< Built-in OpenCV MotionJPEG codec
CAP_INTEL_MFX = 2300, //!< Intel MediaSDK
CAP_XINE = 2400, //!< XINE engine (Linux)
};