Windwos 10(64位)实现C++的YOLOv5目标检测推理?

本文介绍了如何在C++环境中使用YOLOv5的ONNXRuntime进行CPU和GPU推理,包括环境准备(VisualStudio,Cmake,OpenCV,ONNXRuntime)、CUDA/cuDNN安装以及针对CPU和GPU的编译与部署步骤。
摘要由CSDN通过智能技术生成

前言:YOLO系列的目标检测算法已经进展到YOLOv8,但大多是Pytorch框架下实现的Python代码,而我经常会遇到一些C++推理、C++部署的需求,这里以Github上找到的C++代码为例,讲解一下,如何进行YOLOv5的C++推理(CPU或GPU)。需要尝试的同学,请全部按照这个教程来,尤其是软件或压缩包的版本。

1 代码下载

 itsnine/yolov5-onnxruntime: YOLOv5 ONNX Runtime C++ inference code. (github.com)

2 环境搭建 

    无论是CPU环境还是GPU环境,都需要下载并安装VisualStudio2017或者2019CmakeOpenCV。此外,还需要下载“onnxruntime”,这个有GPU版本和CPU版本

1、VS2019安装:安装时,注意要选择“使用C++的桌面开发”以及将安装位置选到系统盘之外。

2、OpenCV安装:OpenCV安装教程_安装opencv-CSDN博客  

3、Cmake安装:Windows 10环境下如何安装CMake?(更新时间:2021.9.13)_win10安装cmake_liO_Oil的博客-CSDN博客

2.1 CPU

    如果你想用CPU推理,那么请在如下地址下载onnxruntime:Release ONNX Runtime v1.7.0 · microsoft/onnxruntime (github.com)

2.2 GPU

如果你想用GPU推理,那么请在如下地址下载onnxruntime: 

Release ONNX Runtime v1.10.0 · microsoft/onnxruntime (github.com)

 因为GPU推理需要使用NVIDIA的显卡,所以还需要安装CUDA和cuDNN,安装步骤如下:

【精选】目标检测第3步:如何在Windows 10系统下安装CUDA(更新时间2022.03.22)_cuda path windows-CSDN博客

目标检测第4步:显卡、GPU、CUDA、cuDNN的介绍及如何在Windows 10下安装cuDNN?_nvidia cudnn sample mnist 怎么用-CSDN博客

3 编译

3.1 CPU编译 

 第一步:将代码压缩包和onnxruntime包放在同一文件夹下,使用7-zip解压到当前目录下

第二步:打开“yolov5-onnxruntime-master”文件夹,并在文件路径处出入“cmd”启动命令行。

第三步:输入命令,具体命令为:

cmake -DONNXRUNTIME_DIR=你的onnxruntime地址 -DCMAKE_BUILD_TYPE=Release
# 例如
# cmake -DONNXRUNTIME_DIR=C:\Users\Cat\Desktop\CPU\onnxruntime-win-x64-1.7.0 -DCMAKE_BUILD_TYPE=Release

第四步:紧接着输入命令为

cmake --build .

第五步:

a、将yolov5-onnxruntime-master\images文件夹中的bus.jpg复制到yolov5-onnxruntime-master/debug文件夹中。

b、将yolov5-onnxruntime-master\models文件夹中的yolov5s.onnx和coco.names也复制到yolov5-onnxruntime-master/debug文件夹中。

c、将onnxruntime-win-x64-1.7.0/lib文件夹中的“onnxruntime.dll”和“onnxruntime.lib”复制到yolov5-onnxruntime-master/debug     如下图所示

在Debug文件夹目录下打开cmd,进行推理。推理命令为:

yolo_ort.exe --model_path yolov5s.onnx --image bus.jpg --class_names coco.names

以上是CPU的部署和推理过程。

3.2 GPU

    和CPU的类似,我们先创建一个文件夹放置下载的代码和onnxruntimeGPU版,如下图所示。

第一步:编译。在yolov5-onnxruntime-master文件目录下打开CMD,然后使用如下命令编译:

cmake -DONNXRUNTIME_DIR=你的onnxruntime-gpu的地址 -DCMAKE_BUILD_TYPE=Release
例如:
cmake -DONNXRUNTIME_DIR=C:\Users\Cat\Desktop\GPU\yolov5-onnxruntime-master -DCMAKE_BUILD_TYPE=Release

cmake --build .

第二步:

a、将yolov5-onnxruntime-master\images文件夹中的bus.jpg复制到yolov5-onnxruntime-master/debug文件夹中。

b、将yolov5-onnxruntime-master\models文件夹中的yolov5s.onnx和coco.names也复制到yolov5-onnxruntime-master/debug文件夹中。

c、将onnxruntime-win-x64-gpu-1.10.0/lib文件夹中的“onnxruntime.dll”、“onnxruntime.lib”和“onnxruntime_providers_cuda.dll”和“onnxruntime_providers_shared.dll”复制到yolov5-onnxruntime-master/debug     如下图所示

第三步:

在Debug文件夹目录下打开cmd,进行推理。推理命令为:

yolo_ort.exe --model_path yolov5s.onnx --image bus.jpg --class_names coco.names --gpu

要在 C++ 中使用 YOLOv5 进行目标检测,需要执行以下步骤: 1. 下载并编译 YOLOv5:可以在 https://github.com/ultralytics/yolov5 下载 YOLOv5 的源代码,并按照 README.md 文件中的说明进行编译。生成的可执行文件可以用于在命令行中运行 YOLOv5。 2. 准备输入数据:YOLOv5 接收的输入数据是图片或视频,需要将它们加载到内存中或从文件中读取。可以使用 OpenCV 库来处理图像和视频文件。 3. 运行 YOLOv5:将准备好的输入数据传递给 YOLOv5 可执行文件,并解析输出,即可得到检测到的目标的位置和类别。可以使用 C++ 的子进程库来运行可执行文件并获取输出。 一个简单的示例代码如下所示: ``` #include <iostream> #include <opencv2/opencv.hpp> #include <cstdlib> #include <cstdio> #include <string> #include <vector> using namespace std; using namespace cv; int main(int argc, char* argv[]) { // Load the input image Mat input_image = imread("test.jpg"); // Prepare the command to run YOLOv5 string command = "./yolov5 detect --source -"; // Start the child process and run YOLOv5 FILE* pipe = popen(command.c_str(), "w"); if (!pipe) { cerr << "Failed to run command." << endl; return 1; } fwrite(input_image.data, 1, input_image.total() * input_image.elemSize(), pipe); pclose(pipe); // Parse the output of YOLOv5 and get the detected objects // ... return 0; } ``` 注意,这只是一个简单的示例代码,实际操作可能需要更多的代码和细节处理。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

liO_Oil

打赏我,开启隐藏模式。

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值