美文网首页
opencv4.5.2+cuda11.1+vs2019编译

opencv4.5.2+cuda11.1+vs2019编译

作者: 流水瀑布 | 来源:发表于2022-03-17 11:08 被阅读0次

详细步骤参考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)
     };

相关文章

  • opencv4.5.2+cuda11.1+vs2019编译

    详细步骤参考Accelerate OpenCV 4.5.0 on Windows – build with CUD...

  • dtb文件编译

    反编译 编译 批量反编译

  • 编译过程与静态库&动态库

    一、编译过程 1、预编译(Preprocessing) 预编译即预处理,编译器不能直接对预编译命令进行编译,必须在...

  • 宏、const、static、extern使用详解

    宏与const的区别 编译时刻:宏是预编译(编译之前处理),const是编译阶段。编译检查:宏不做检查,不会报编译...

  • iOS const 与宏

    编译时刻:宏是预编译(编译之前处理),const是编译阶段。 编译检查:宏不做检查,不会报编译错误,只是替换,co...

  • 2020-09-09 RK系统编译

    编译uboot: 编译kernel: 环境配置: 编译系统:

  • 程序编译过程

    本文编译的文件cpu架构为X86_64。 编译型语言编译过程主要有预编译、编译、汇编、链接。 预编译This st...

  • 自己动手编译OpenJDK

    Linux下 编译 Centos 下编译OpenJDK Mac下编译 MacOS 下编译OpenJDK Windo...

  • 宏 const static extern的区别与使用

    一. const 与 宏的区别 编译时刻: 宏是预编译(编译之前处理), const是编译阶段处理 编译检查: 宏...

  • 晚期(运行期)优化

    HotSpot的即时编译器 解释器与编译器 编译对象与触发条件 编译过程 编译优化技术 如果还对其他的经典编译优化...

网友评论

      本文标题:opencv4.5.2+cuda11.1+vs2019编译

      本文链接:https://www.haomeiwen.com/subject/puxurrtx.html