发布时间:2023-03-03 19:30
从OpenCV4.2.0 版本开始允许使用 Nvidia GPU 来加速推理。本文介绍最近使用windows11系统编译带CUDA的OpenCV4.5.0的过程。
GPU | CPU |
---|---|
4ms | 311ms |
https://opencv.org/releases/
https://github.com/opencv/opencv/releases/tag/4.5.0
https://sourceforge.net/projects/opencvlibrary/files/4.5.0/opencv-4.5.0-vc14_vc15.exe/download
https://github.com/opencv/opencv_contrib/tags
https://www.raoyunsoft.com/opencv/opencv_contrib/opencv_contrib-4.5.0.zip
https://download.csdn.net/download/iracer/85695997
https://docs.nvidia.com/deeplearning/cudnn/install-guide/index.html#install-windows
www.winimage.com/zLibDll/zlib123dllx64.zip
下载完成后解压zip文件,并将zlibwapi.dll所在目录添加到系统环境变量path中
C:\Users\irace>nvcc --version
nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2021 NVIDIA Corporation
Built on Mon_May__3_19:41:42_Pacific_Daylight_Time_2021
Cuda compilation tools, release 11.3, V11.3.109
Build cuda_11.3.r11.3/compiler.29920130_0
https://github.com/Kitware/CMake/releases/download/v3.22.5/cmake-3.22.5-windows-x86_64.msi
双击解压OpenCV4.5.0.exe,解压后的source目录即OpenCV4.5.0的源码
解压opencv_contrib-4.5.0
新建一个编译目录
D:\opencv\oepncv4.5.0cuda\build
打开CMake
配置source code:
D:/opencv/opencv4.5.0/sources
配置目标目录:
D:\opencv\oepncv4.5.0cuda\build
第1次点击[Configure]
弹出窗口选择vs2019作为编译器:
Visual Studio 16 2019
generate平台选择x64
第2次点击[Configure]后,搜cuda带cuda的都勾选,
- BUILD_CUDA_STUBS
- BUILD_opencv_cudaarithm
- BUILD_opencv_cudabgsegm
- BUILD_opencv_cudacodec
- BUILD_opencv_cudafeatures2d
- BUILD_opencv_cudafilters
- BUILD_opencv_cudaimgproc
- BUILD_opencv_cudalegacy
- BUILD_opencv_cudaobjdetect
- BUILD_opencv_cudaoptflow
- BUILD_opencv_cudastereo
- BUILD_opencv_cudawarping
D:\opencv\oepncv4.5.0cuda\opencv_contrib-4.5.0\modules
第3次点击[Configure],完成后,再次搜索cuda,配置CUDA_ARCH_BIN中将显卡的算力内容改成自己显卡的算力,t如下网址查询自己显卡的算力,我保留了7.5;8.0;8.6
GeForce 笔记本
GPU Compute Capability GeForce RTX 3080 8.6 GeForce RTX 3070 8.6 GeForce RTX 3060 8.6 GeForce RTX 3050 Ti 8.6 GeForce RTX 3050 8.6 Geforce RTX 2080 7.5 Geforce RTX 2070 7.5 Geforce RTX 2060 7.5 GeForce GTX 1080 6.1 GeForce GTX 1070 6.1 GeForce GTX 1060 6.1 GeForce and TITAN 台式机
GPU Compute Capability Geforce RTX 3060 Ti 8.6 Geforce RTX 3060 8.6 GeForce RTX 3090 8.6 GeForce RTX 3080 8.6 GeForce RTX 3070 8.6 GeForce GTX 1650 Ti 7.5 NVIDIA TITAN RTX 7.5 Geforce RTX 2080 Ti 7.5 Geforce RTX 2080 7.5 Geforce RTX 2070 7.5 Geforce RTX 2060 7.5 NVIDIA TITAN V 7.0
搜索并勾选
BUILD_opencv_world,可生成一个整的dll方便使用
OPENCV_ENABLE_NONFREF
解压.cache.rar,将.cache目录拷贝到source目录:
D:\opencv\oepncv4.5.0\source\.cache >![将下载的.cache目录与source中的.cache目录合并](https://img-blog.csdnimg.cn/
第4次点击[Configure]
完成后,点击[Generate],完美生成:
配置完成后CMake log中显示找到了CUDA和cuDNN:NVIDIA CUDA: YES (ver 11.3, CUFFT CUBLAS FAST_MATH)
NVIDIA GPU arch: 75 80 86
NVIDIA PTX archs:
cuDNN: YES (ver 8.4.0)
点击[Open Project],会自动打开vs2019,开始编译
使用VS2019打开刚刚编译工程后,等待左下角显示的项全部加载完毕才可以继续操作
选择Release x64版本
找到解决方案资源管理器中的“CmakeTargets”下的“ALL_BUILD”,右键→“生成”,然后开始漫长的等待……
解决方案资源管理器—>CMakeTargets—>INSTALL—>生成”然后又是等,好在这次时间很短。完成后你的build文件夹中会出现一个install文件夹,这就是完成了。
生成完成后会出现如下文件夹
D:\opencv\oepncv4.5.0cuda\build\install
build/install目录的目录结构(部分)
├─etc
├─include
│ └─opencv2
│ ├─core
│ ├─cudalegacy
│ ├─cudev
│ ├─datasets
│ ├─dnn
│ ├─face
│ ├─features2d
└─x64
└─vc16
├─bin
└─lib
tips:在当前目录中运行cmd,使用tree命令,即可生成目录树
D:\opencv\oepncv4.5.0cuda\build\install>tree
#include
#include
#include
#include
using namespace std;
// 检测结果后处理
void postProcess(
cv::dnn::Net& net,
cv::Mat& frame,
const vector& outs,
vector& boxes,
vector& classIds,
vector& indices,
double confThreshold,
double nmsThreshold
);
// 检测绘制结果
void drawPred(
cv::Mat& frame,
vector& boxes,
vector& classIds,
vector& indices,
vector& classNamesVec);
// 程序设置
bool USE_IMAGE = false; // true: 测试图像; false: 测试视频
bool USE_YOLOv4 = true; // true: YOLOv4; false: YOLOv4_tiny
bool USE_CUDA = true; // true: GPU, false: CPU
float confidenceThreshold = 0.3; // 置信度设置
float nmsThreshold = 0.2; // 置信MNS门限
int main()
{
// [1]模型文件路径设置
cv::String model, config;
if (USE_YOLOv4)
{
model = "D:/models/yolov4/yolov4.weights"; // 模型权重文件
config = "D:/models/yolov4/yolov4.cfg"; // 模型配置文件
}
else // use yolov4-tiny
{
model = "D:/models/yolov4/yolov4-tiny.weights";
config = "D:/models/yolov4/yolov4-tiny.cfg";
}
cv::String framework = "Darknet"; // 深度学习框架
cv::String label_file = "D:/models/yolov4/coco.names";// 类别标签文件
//[2] 加载类别
ifstream classNamesFile(label_file);
vector classNamesVec;
if (classNamesFile.is_open())
{
string className = "";
while (std::getline(classNamesFile, className))
{
classNamesVec.push_back(className);
}
}
// [3]载入模型
cv::dnn::Net net = cv::dnn::readNet(config, model, framework);
if (USE_CUDA)
{
net.setPreferableTarget(cv::dnn::DNN_TARGET_CUDA);
net.setPreferableBackend(cv::dnn::DNN_BACKEND_CUDA);
}
else
{
net.setPreferableTarget(cv::dnn::DNN_TARGET_OPENCL);
net.setPreferableBackend(cv::dnn::DNN_BACKEND_OPENCV);
}
// ...
}