yolov5部署之dll封装并调用的方法

96 篇文章 19 订阅
43 篇文章 6 订阅


*****************************************************************************************************************8

下述cpp和.h文件下载链接如下:
封装的cpp文件

前言

前面讲过yolov5的环境配置,源码测试,以及使用TensorRT进行推理加速,但是有时候为了让模型方便部署在移动端,需要将模型封装成一个动态链接库dll,从而使得模型能够在任意的移动端无需配置环境即可运行,只需通过读取dll即可在其他模块进行调用。

假设此时,您已经熟悉了pytorch的环境配置,yolov5源码的测试,CUDA以及TensorRT的安装,完成推理过程并成功生成了engine模型
如果以上的步骤还没有完成请参考以下博文:

1. VS2015新建一个dll项目


如果不了解新建dll项目的基本方法,请参考以下博文

2. 将yolov5封装成一个导出类

由于在前面我们已经生成了engine模型,所以将yolov5封装成一个类我们只需两个成员函数即可,一个是初始化函数inital(),一个是推理检测函数detectFunction(),当然封装的过程可以根据自己喜好任意设定。下面贴出我封装yolov5类的成员函数及成员变量。

#pragma once

#include <iostream>
#include <chrono>
#include "cuda_runtime_api.h"
#include "logging.h"
#include "common.hpp"
#include "utils.h"
#include "calibrator.h"

#define USE_FP16  // set USE_INT8 or USE_FP16 or USE_FP32
#define DEVICE 0  // GPU id
#define NMS_THRESH 0.4
#define CONF_THRESH 0.5
#define BATCH_SIZE 1

#define NET s  // s m l x
#define NETSTRUCT(str) createEngine_##str
#define CREATENET(net) NETSTRUCT(net)
#define STR1(x) #x
#define STR2(x) STR1(x)

using namespace std;
using namespace cv;


class YoloV5
{
public:
	YoloV5();
	~YoloV5();

	bool inital(const string& enginePath);

	void detect(const Mat& inputImg, vector<Rect>& vRect);


private:
	char* trtModelStream;
	size_t size;
	Logger gLogger;

	int INPUT_H;
	int INPUT_W;
	int CLASS_NUM;
	int OUTPUT_SIZE;
	char* INPUT_BLOB_NAME;
	char* OUTPUT_BLOB_NAME;
	void* buffers[2];
	float* data;
	float* prob;

	IExecutionContext* context;
	cudaStream_t stream;
private:
	void doInference(IExecutionContext& context, cudaStream_t& stream, void **buffers, float* input, float* output, int batchSize);



	cv::Rect get_rect(cv::Mat& img, float bbox[4]);
	float iou(float lbox[4], float rbox[4]);

	//bool cmp(const Yolo::Detection& a, const Yolo::Detection& b);

	void nms(std::vector<Yolo::Detection>& res, float *output, float conf_thresh, float nms_thresh = 0.5);



};

inline bool cmp(const Yolo::Detection& a, const Yolo::Detection& b) {
	return a.conf > b.conf;
}

对于每个函数的具体实现,请参考wang-xinyu的工作,源码链接点这里

注:

  • 封装时如果编译报错,请检查包含的头文件是否正确
  • 项目属性设置是否正确
  • 配置环境路径是否设置正确

如果编译不报错Release文件夹下会生成一个yolov5.dll文件和yolov5.lib文件,即表示封装成功

3. 新建一个Test项目,调用yolov5类

// Test.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include<iostream>
#include "..//yolov5/yolov5.h"
#include"..//yolov5/include/dirent.h"


int main()
{
	yolov5 yolov5_1;

	string eiginePath = "C:\\Users\\Administrator\\Desktop\\Python\\model\\broken\\yolov5s.engine";
	yolov5_1.inital(eiginePath);

	vector<cv::String> vImgPath;
	string filter = "C:\\Users\\Administrator\\Desktop\\Python\\img\\samples\\*.jpg";
	glob(filter, vImgPath);
	for (auto i = 0; i < vImgPath.size(); i++)
	{
		//cout << "vImgPath.size() : " << vImgPath.size() << endl;
		Mat img = imread(vImgPath[i]);
		if (img.channels() == 1)
		{
			cvtColor(img, img, cv::COLOR_GRAY2BGR);
		}
		vector<Rect> res1;
		yolov5_1.detectFunction(img, ref(res1));

	}
	return 0;
}

4. 编译运行

在这里插入图片描述

  • 编译运行在Release文件夹下会生成Test.exe文件,yolov5.dll文件以及它们对应的lib文件

在这里插入图片描述
双击Test.exe即可调用yolov5模型进行目标检测
在这里插入图片描述
GIF图动态演示如下

在这里插入图片描述

总结

封装的过程主要的工作是解决各种报错问题,分析报错的原因,如头文件和库的依赖等,从图中可以看出,yolov5通过TensorRT推理加速使得模型运行的效率非常的高效,每张图片的检测时间只有2ms左右,这使得该模型部署在移动端具有很大的优势。


视觉与控制前沿公众号,第一时间获取最有价值的前沿视觉与控制文章。

在这里插入图片描述

公众号链接视觉与控制公众号
评论 55
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Christo3

你的鼓励将是我创作的最大动力

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

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

打赏作者

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

抵扣说明:

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

余额充值