opencv4.5.2+cuda11.1+vs2019编译

详细步骤参考Accelerate OpenCV 4.5.0 on Windows – build with CUDA and python bindings

准备

  1. VS2019下载安装
旋转Desktop development with C++ 和 python development
  1. cmake 下载安装 cmake-3.18.4-win64-x64.msi
可以用C:\Program Files\CMake\bin\cmake-gui.exe进行configure和generate
  1. cuda11.1下载安装 cuda_11.1.1_456.81_win10.exe
  2. 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
  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
  1. Intel Media SDK下载安装 MSDK2020R1.exe
  2. Intel MKL and TBB
这次没使用
  1. 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
  1. 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)

问题

  1. 库依赖
#使用VS tools进行查看dll的依赖库
dumpbin /dependents xxx.dll
#查询依赖库的位置
where xxx.dll
  1. 编译过程中文件下载失败
因为wall原因,无法在线下载
可直接从互联网找然后拷贝到.cache目录下对应的文件
其中.cache下为0字节的文件一班就是下载不下来的原因
  1. 路径
#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
  1. 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)
     };
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 205,132评论 6 478
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 87,802评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 151,566评论 0 338
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,858评论 1 277
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,867评论 5 368
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,695评论 1 282
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,064评论 3 399
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,705评论 0 258
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 42,915评论 1 300
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,677评论 2 323
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,796评论 1 333
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,432评论 4 322
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,041评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,992评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,223评论 1 260
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 45,185评论 2 352
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,535评论 2 343

推荐阅读更多精彩内容