詳細步驟參考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 環(huán)境路徑
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設備數(shù)量
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)
# 調(diào)整圖像
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原因修噪,無法在線下載
可直接從互聯(lián)網(wǎng)找然后拷貝到.cache目錄下對應的文件
其中.cache下為0字節(jié)的文件一班就是下載不下來的原因
- 路徑
#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)調(diào)用方式是有區(qū)別的,具體區(qū)別是啥?
- 資源利用籍凝?
- 是否利用顯卡周瞎?
- MSMF打開慢,因為要等待FrameServer啟動
把【服務】里的【W(wǎng)indows 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)
};