Opencv C++实现yolov5部署onnx模型完成目标检测

这篇具有很好参考价值的文章主要介绍了Opencv C++实现yolov5部署onnx模型完成目标检测。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

代码分析:

头文件
#include <fstream>  //文件
#include <sstream>  //流
#include <iostream>
#include <opencv2/dnn.hpp>        //深度学习模块-仅提供推理功能
#include <opencv2/imgproc.hpp>    //图像处理模块
#include <opencv2/highgui.hpp>    //媒体的输入输出/视频捕捉/图像和视频的编码解码/图形界面的接口
命名空间
using namespace cv;
using namespace dnn;
using namespace std;
结构体 Net_config
struct Net_config{
	float confThreshold; // 置信度阈值
	float nmsThreshold;  // 非最大抑制阈值
	float objThreshold;  // 对象置信度阈值
	string modelpath;
};

里面存了三个阈值和模型地址,其中置信度,顾名思义,看检测出来的物体的精准度。以测量值为中心,在一定范围内,真值出现在该范围内的几率。

endsWith()函数 判断sub是不是s的子串
int endsWith(string s, string sub) {
	return s.rfind(sub) == (s.length() - sub.length()) ? 1 : 0;
}
anchors_640图像接收数组
const float anchors_640[3][6] = { {10.0,  13.0, 16.0,  30.0,  33.0,  23.0},
				  {30.0,  61.0, 62.0,  45.0,  59.0,  119.0},
				  {116.0, 90.0, 156.0, 198.0, 373.0, 326.0} };

根据图像大小,选择相应长度的二维数组。深度为3,每层6个位置。文章来源地址https://www.toymoban.com/news/detail-635404.html

YOLO类
class YOLO{
public:
	YOLO(Net_config config); //构造函数
	void detect(Mat& frame); //通过图像参数,进行目标检测
private:
	float* anchors;
	int num_stride;
	int inpWidth;
	int inpHeight;
	vector<string> class_names;
	int num_class;
	float confThreshold;
	float nmsThreshold;
	float objThreshold;
	const bool keep_ratio = true;
	Net net;
	void drawPred(float conf, int left, int top, int right, int bottom, Mat& frame, int classid);
	Mat resize_image(Mat srcimg, int* newh, int* neww, int* top, int* left);
};
YOLO类构造函数的重载
YOLO::YOLO(Net_config config){
	this->confThreshold = config.confThreshold;
	this->nmsThreshold = config.nmsThreshold;
	this->objThreshold = config.objThreshold;
	this->net = readNet(config.modelpath);
	ifstream ifs("class.names"); //class.name中写入标签内容,当前只有'person',位置与当前.cpp文件同级
	string line;
	while (getline(ifs, line)) this->class_names.push_back(line);
	this->num_class = class_names.size();
	if (endsWith(config.modelpath, "6.onnx")){ //根据onnx的输入图像格式 选择分辨率 当前为1280x1280 可灵活调整
		anchors = (float*)anchors_1280;
		this->num_stride = 4;      //深度
		this->inpHeight = 1280;    //高
		this->inpWidth = 1280;     //宽
	}
	else{                                       //当前为640x640 可以resize满足onnx需求 也可以调整数组或if中的onnx判断
		anchors = (float*)anchors_640;
		this->num_stride = 3;
		this->inpHeight = 640;
		this->inpWidth = 640;
	}
}
重新规定图像大小函数 resize_image()
Mat YOLO::resize_image(Mat srcimg, int* newh, int* neww, int* top, int* left){//传入图像以及需要改变的参数
	int srch = srcimg.rows, srcw = srcimg.cols;
	*newh = this->inpHeight;
	*neww = this->inpWidth;
	Mat dstimg;
	if (this->keep_ratio && srch != srcw) {
		float hw_scale = (float)srch / srcw;
		if (hw_scale > 1) {
			*newh = this->inpHeight;
			*neww = int(this->inpWidth / hw_scale);
			resize(srcimg, dstimg, Size(*neww, *newh), INTER_AREA);
			*left = int((this->inpWidth - *neww) * 0.5);
			copyMakeBorder(dstimg, dstimg, 0, 0, *left, this->inpWidth - *neww - *left, BORDER_CONSTANT, 114);
		}else {
			*newh = (int)this->inpHeight * hw_scale;
			*neww = this->inpWidth;
			resize(srcimg, dstimg, Size(*neww, *newh), INTER_AREA);
			*top = (int)(this->inpHeight - *newh) * 0.5;
			copyMakeBorder(dstimg, dstimg, *top, this->inpHeight - *newh - *top, 0, 0, BORDER_CONSTANT, 114);
		}
	}else {
		resize(srcimg, dstimg, Size(*neww, *newh), INTER_AREA);
	}
	return dstimg;
}
绘制预测的边界框函数 drawPred()
void YOLO::drawPred(float conf, int left, int top, int right, int bottom, Mat& frame, int classid){
	//绘制一个显示边界框的矩形
	rectangle(frame, Point(left, top), Point(right, bottom), Scalar(0, 0, 255), 2);

	//获取类名的标签及其置信度
	string label = format("%.2f", conf);
	label = this->class_names[classid] + ":" + label;

	//在边界框顶部显示标签
	int baseLine;
	Size labelSize = getTextSize(label, FONT_HERSHEY_SIMPLEX, 0.5, 1, &baseLine);
	top = max(top, labelSize.height);
	//rectangle(frame, Point(left, top - int(1.5 * labelSize.height)), Point(left + int(1.5 * labelSize.width), top + baseLine), Scalar(0, 255, 0), FILLED);
	putText(frame, label, Point(left, top), FONT_HERSHEY_SIMPLEX, 0.75, Scalar(0, 255, 0), 1);
}
【核心代码】检测函数 detect()
void YOLO::detect(Mat& frame){
	int newh = 0, neww = 0, padh = 0, padw = 0;
	Mat dstimg = this->resize_image(frame, &newh, &neww, &padh, &padw);
	Mat blob = blobFromImage(dstimg, 1 / 255.0, Size(this->inpWidth, this->inpHeight), Scalar(0, 0, 0), true, false);
	this->net.setInput(blob);
	vector<Mat> outs;
	this->net.forward(outs, this->net.getUnconnectedOutLayersNames());
	int num_proposal = outs[0].size[1];
	int nout = outs[0].size[2];
	if (outs[0].dims > 2){
		outs[0] = outs[0].reshape(0, num_proposal);
	}
	
	vector<float> confidences;
	vector<Rect> boxes;
	vector<int> classIds;
	float ratioh = (float)frame.rows / newh, ratiow = (float)frame.cols / neww;
	int n = 0, q = 0, i = 0, j = 0, row_ind = 0;  //xmin,ymin,xamx,ymax,box_score,class_score
	float* pdata = (float*)outs[0].data;
	for (n = 0; n < this->num_stride; n++){ //特征图尺度
		const float stride = pow(2, n + 3);
		int num_grid_x = (int)ceil((this->inpWidth / stride));
		int num_grid_y = (int)ceil((this->inpHeight / stride));
		for (q = 0; q < 3; q++){
			const float anchor_w = this->anchors[n * 6 + q * 2];
			const float anchor_h = this->anchors[n * 6 + q * 2 + 1];
			for (i = 0; i < num_grid_y; i++){
				for (j = 0; j < num_grid_x; j++){
					float box_score = pdata[4];
					if (box_score > this->objThreshold){
						Mat scores = outs[0].row(row_ind).colRange(5, nout);
						Point classIdPoint;
						double max_class_socre;
						//获取最高分的值和位置
						minMaxLoc(scores, 0, &max_class_socre, 0, &classIdPoint);
						max_class_socre *= box_score;
						if (max_class_socre > this->confThreshold){
							const int class_idx = classIdPoint.x;
							float cx = pdata[0];  //cx
							float cy = pdata[1];  //cy
							float w = pdata[2];   //w
							float h = pdata[3];   //h
							int left = int((cx - padw - 0.5 * w) * ratiow);
							int top = int((cy - padh - 0.5 * h) * ratioh);
							confidences.push_back((float)max_class_socre);
							boxes.push_back(Rect(left, top, (int)(w * ratiow), (int)(h * ratioh)));
							classIds.push_back(class_idx);
						}
					}
					row_ind++;
					pdata += nout;
				}
			}
		}
	}
	// 执行非最大抑制以消除冗余重叠框
	// 置信度较低
	vector<int> indices;
	dnn::NMSBoxes(boxes, confidences, this->confThreshold, this->nmsThreshold, indices);
	for (size_t i = 0; i < indices.size(); ++i){
		int idx = indices[i];
		Rect box = boxes[idx];
		this->drawPred(confidences[idx], box.x, box.y,box.x + box.width, box.y + box.height, frame, classIds[idx]);
	}
}

主函数

int main(){
        //加载onnx模型
	Net_config yolo_nets = { 0.3, 0.5, 0.3, "yolov5n_person_320.onnx" };
	YOLO yolo_model(yolo_nets);
        //加载单张图片
	string imgpath = "112.png";
	Mat srcimg = imread(imgpath);
	//开始检测
        yolo_model.detect(srcimg);
	static const string kWinName = "Deep learning object detection in OpenCV";
	namedWindow(kWinName, WINDOW_NORMAL);
	imshow(kWinName, srcimg); //显示图片
	waitKey(0);               //保持停留
	destroyAllWindows();      //关闭窗口并取消分配任何相关的内存使用
}

完整代码

#include <fstream>
#include <sstream>
#include <iostream>
#include <opencv2/dnn.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/highgui.hpp>

using namespace cv;
using namespace dnn;
using namespace std;

struct Net_config
{
	float confThreshold; // Confidence threshold
	float nmsThreshold;  // Non-maximum suppression threshold
	float objThreshold;  //Object Confidence threshold
	string modelpath;
};

int endsWith(string s, string sub) {
	return s.rfind(sub) == (s.length() - sub.length()) ? 1 : 0;
}

const float anchors_640[3][6] = { {10.0,  13.0, 16.0,  30.0,  33.0,  23.0},
				  {30.0,  61.0, 62.0,  45.0,  59.0,  119.0},
				  {116.0, 90.0, 156.0, 198.0, 373.0, 326.0} };

const float anchors_1280[4][6] = { {19, 27, 44, 40, 38, 94},
                                   {96, 68, 86, 152, 180, 137},
                                   {140, 301, 303, 264, 238, 542},
				   {436, 615, 739, 380, 925, 792} };

class YOLO
{
public:
	YOLO(Net_config config);
	void detect(Mat& frame);
private:
	float* anchors;
	int num_stride;
	int inpWidth;
	int inpHeight;
	vector<string> class_names;
	int num_class;

	float confThreshold;
	float nmsThreshold;
	float objThreshold;
	const bool keep_ratio = true;
	Net net;
	void drawPred(float conf, int left, int top, int right, int bottom, Mat& frame, int classid);
	Mat resize_image(Mat srcimg, int* newh, int* neww, int* top, int* left);
};

YOLO::YOLO(Net_config config)
{
	this->confThreshold = config.confThreshold;
	this->nmsThreshold = config.nmsThreshold;
	this->objThreshold = config.objThreshold;

	this->net = readNet(config.modelpath);
	ifstream ifs("class.names");
	string line;
	while (getline(ifs, line)) this->class_names.push_back(line);
	this->num_class = class_names.size();

	if (endsWith(config.modelpath, "6.onnx"))
	{
		anchors = (float*)anchors_1280;
		this->num_stride = 4;
		this->inpHeight = 1280;
		this->inpWidth = 1280;
	}
	else
	{
		anchors = (float*)anchors_640;
		this->num_stride = 3;
		this->inpHeight = 640;
		this->inpWidth = 640;
	}
}

Mat YOLO::resize_image(Mat srcimg, int* newh, int* neww, int* top, int* left)
{
	int srch = srcimg.rows, srcw = srcimg.cols;
	*newh = this->inpHeight;
	*neww = this->inpWidth;
	Mat dstimg;
	if (this->keep_ratio && srch != srcw) {
		float hw_scale = (float)srch / srcw;
		if (hw_scale > 1) {
			*newh = this->inpHeight;
			*neww = int(this->inpWidth / hw_scale);
			resize(srcimg, dstimg, Size(*neww, *newh), INTER_AREA);
			*left = int((this->inpWidth - *neww) * 0.5);
			copyMakeBorder(dstimg, dstimg, 0, 0, *left, this->inpWidth - *neww - *left, BORDER_CONSTANT, 114);
		}
		else {
			*newh = (int)this->inpHeight * hw_scale;
			*neww = this->inpWidth;
			resize(srcimg, dstimg, Size(*neww, *newh), INTER_AREA);
			*top = (int)(this->inpHeight - *newh) * 0.5;
			copyMakeBorder(dstimg, dstimg, *top, this->inpHeight - *newh - *top, 0, 0, BORDER_CONSTANT, 114);
		}
	}
	else {
		resize(srcimg, dstimg, Size(*neww, *newh), INTER_AREA);
	}
	return dstimg;
}

void YOLO::drawPred(float conf, int left, int top, int right, int bottom, Mat& frame, int classid)   // Draw the predicted bounding box
{
	//Draw a rectangle displaying the bounding box
	rectangle(frame, Point(left, top), Point(right, bottom), Scalar(0, 0, 255), 2);

	//Get the label for the class name and its confidence
	string label = format("%.2f", conf);
	label = this->class_names[classid] + ":" + label;

	//Display the label at the top of the bounding box
	int baseLine;
	Size labelSize = getTextSize(label, FONT_HERSHEY_SIMPLEX, 0.5, 1, &baseLine);
	top = max(top, labelSize.height);
	//rectangle(frame, Point(left, top - int(1.5 * labelSize.height)), Point(left + int(1.5 * labelSize.width), top + baseLine), Scalar(0, 255, 0), FILLED);
	putText(frame, label, Point(left, top), FONT_HERSHEY_SIMPLEX, 0.75, Scalar(0, 255, 0), 1);
}

void YOLO::detect(Mat& frame)
{
	int newh = 0, neww = 0, padh = 0, padw = 0;
	Mat dstimg = this->resize_image(frame, &newh, &neww, &padh, &padw);
	Mat blob = blobFromImage(dstimg, 1 / 255.0, Size(this->inpWidth, this->inpHeight), Scalar(0, 0, 0), true, false);
	this->net.setInput(blob);
	vector<Mat> outs;
	this->net.forward(outs, this->net.getUnconnectedOutLayersNames());

	int num_proposal = outs[0].size[1];
	int nout = outs[0].size[2];
	if (outs[0].dims > 2)
	{
		outs[0] = outs[0].reshape(0, num_proposal);
	}
	/generate proposals
	vector<float> confidences;
	vector<Rect> boxes;
	vector<int> classIds;
	float ratioh = (float)frame.rows / newh, ratiow = (float)frame.cols / neww;
	int n = 0, q = 0, i = 0, j = 0, row_ind = 0; ///xmin,ymin,xamx,ymax,box_score,class_score
	float* pdata = (float*)outs[0].data;
	for (n = 0; n < this->num_stride; n++)   ///特征图尺度
	{
		const float stride = pow(2, n + 3);
		int num_grid_x = (int)ceil((this->inpWidth / stride));
		int num_grid_y = (int)ceil((this->inpHeight / stride));
		for (q = 0; q < 3; q++)    ///anchor
		{
			const float anchor_w = this->anchors[n * 6 + q * 2];
			const float anchor_h = this->anchors[n * 6 + q * 2 + 1];
			for (i = 0; i < num_grid_y; i++)
			{
				for (j = 0; j < num_grid_x; j++)
				{
					float box_score = pdata[4];
					if (box_score > this->objThreshold)
					{
						Mat scores = outs[0].row(row_ind).colRange(5, nout);
						Point classIdPoint;
						double max_class_socre;
						// Get the value and location of the maximum score
						minMaxLoc(scores, 0, &max_class_socre, 0, &classIdPoint);
						max_class_socre *= box_score;
						if (max_class_socre > this->confThreshold)
						{
							const int class_idx = classIdPoint.x;
							//float cx = (pdata[0] * 2.f - 0.5f + j) * stride;  ///cx
							//float cy = (pdata[1] * 2.f - 0.5f + i) * stride;   ///cy
							//float w = powf(pdata[2] * 2.f, 2.f) * anchor_w;   ///w
							//float h = powf(pdata[3] * 2.f, 2.f) * anchor_h;  ///h

							float cx = pdata[0];  ///cx
							float cy = pdata[1];   ///cy
							float w = pdata[2];   ///w
							float h = pdata[3];  ///h

							int left = int((cx - padw - 0.5 * w) * ratiow);
							int top = int((cy - padh - 0.5 * h) * ratioh);

							confidences.push_back((float)max_class_socre);
							boxes.push_back(Rect(left, top, (int)(w * ratiow), (int)(h * ratioh)));
							classIds.push_back(class_idx);
						}
					}
					row_ind++;
					pdata += nout;
				}
			}
		}
	}

	// Perform non maximum suppression to eliminate redundant overlapping boxes with
	// lower confidences
	vector<int> indices;
	dnn::NMSBoxes(boxes, confidences, this->confThreshold, this->nmsThreshold, indices);
	for (size_t i = 0; i < indices.size(); ++i)
	{
		int idx = indices[i];
		Rect box = boxes[idx];
		this->drawPred(confidences[idx], box.x, box.y,
			box.x + box.width, box.y + box.height, frame, classIds[idx]);
	}
}

int main()
{
	Net_config yolo_nets = { 0.3, 0.5, 0.3, "yolov5n_person_320.onnx" };
	YOLO yolo_model(yolo_nets);

	//string imgpath = "112.png";
	//Mat srcimg = imread(imgpath);
	//yolo_model.detect(srcimg);
        
        int n = 588;
	for (int i = 1; i <= n; i++) {
		string s = to_string(i) + ".png";
		string imgpath = "F://test//p1//yanfa2//bh//cc//" + s;
		cout << imgpath << endl;

		Mat srcimg = imread(imgpath);
		yolo_model.detect(srcimg);
		imwrite("F://test//p2//yanfa2//bh//cc//" + s, srcimg);
	}

	//static const string kWinName = "Deep learning object detection in OpenCV";
	//namedWindow(kWinName, WINDOW_NORMAL);
	//imshow(kWinName, srcimg);
	//waitKey(0);
	//destroyAllWindows();
}

到了这里,关于Opencv C++实现yolov5部署onnx模型完成目标检测的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • C++调用yolov5 onnx模型的初步探索

    yolov5-dnn-cpp-python https://github.com/hpc203/yolov5-dnn-cpp-python 转onnx: 用opencv的dnn模块做yolov5目标检测的程序,包含两个步骤:(1).把pytorch的训练模型.pth文件转换到.onnx文件。(2).opencv的dnn模块读取.onnx文件做前向计算。 SiLU其实就是swish激活函数,而在onnx模型里是不直接支持swish算子的

    2024年02月12日
    浏览(33)
  • [C++]使用yolov8的onnx模型仅用opencv和bytetrack实现目标追踪

    【官方框架地址】 yolov8: https://github.com/ultralytics/ultralytics bytetrack: https://github.com/ifzhang/ByteTrack 【算法介绍】 随着人工智能技术的不断发展,目标追踪已成为计算机视觉领域的重要研究方向。Yolov8和ByTetrack作为当前先进的算法,当它们结合使用时,能够显著提升目标追踪的准

    2024年01月24日
    浏览(40)
  • 详细介绍 Yolov5 转 ONNX模型 + 使用ONNX Runtime 的 Python 部署(包含官方文档的介绍)

    对ONNX的介绍强烈建议看,本文做了很多参考:模型部署入门教程(一):模型部署简介 模型部署入门教程(三):PyTorch 转 ONNX 详解 以及Pytorch的官方介绍:(OPTIONAL) EXPORTING A MODEL FROM PYTORCH TO ONNX AND RUNNING IT USING ONNX RUNTIME C++的部署:详细介绍 Yolov5 转 ONNX模型 + 使用 ONNX Runti

    2024年02月01日
    浏览(43)
  • 使用OpenCV DNN推理YOLOv5-CLS转换后的ONNX分类模型

    YOLOv5是一种先进的目标检测算法,而YOLOv5-CLS则是YOLOv5的一个变种,专门用于图像分类任务。为了在实际应用中使用YOLOv5-CLS模型,我们需要将其转换为Open Neural Network Exchange (ONNX) 格式,并使用OpenCV DNN库来进行推理。 步骤1: 安装OpenCV和ONNX 首先,你需要确保已经安装了OpenCV和

    2024年02月16日
    浏览(52)
  • 36、RK3399Pro 环境搭建和Yolov5 c++调用opencv进行RKNN模型部署和使用

    基本思想:记录rk3399 pro配置环境和c++ npu开发记录,主要想搞一份c++代码和其它图像算法结合一下,好进行部署,淘宝链接见附录  需要的python3.7对应的aarch64的whl包:包含opencv-whl 、h5py-whl包: 链接: https://pan.baidu.com/s/1cvCAmHBa_4KgEjrcFIYnig 提取码: 5ui4 链接: https://pan.baidu.com/s/1hrc

    2024年02月07日
    浏览(32)
  • yolov5 opencv dnn部署自己的模型

    github开源代码地址 yolov5官网还提供的dnn、tensorrt推理链接 本人使用的opencv c++ github代码,代码作者非本人,也是上面作者推荐的链接之一 如果想要尝试直接运行源码中的yolo.cpp文件和yolov5s.pt推理sample.mp4,请参考这个链接的介绍 使用github源码结合自己导出的onnx模型推理自己的

    2024年01月23日
    浏览(40)
  • 用C++部署yolov5模型

    要在C语言中部署YoloV5模型,可以使用以下步骤: 安装C语言的深度学习库,例如Darknet或者ncnn。 下载训练好的YoloV5模型权重文件(.pt文件)和模型配置文件(.yaml文件)。 将下载的权重文件和配置文件移动到C语言深度学习库中指定的目录下。 在C语言中编写代码,使用深度学习库加

    2024年02月15日
    浏览(33)
  • yolov5 pt 模型 导出 onnx

    在训练好的yolov5 pt 模型 可以 通过 export.py 进行导出 onnx 导出流程 在 export.py 设置模型和数据源的yaml 在官方的文档中 说明了可以导出的具体的类型。 在 --include 添加导出的类型, 不同的 类型的 环境要求不一样,建议虚拟环境,比如onnx 和 openvino 的numpy 版本要求不一只,一

    2024年02月11日
    浏览(37)
  • YOLOv5 实例分割 用 OPenCV DNN C++ 部署

    如果之前从没接触过实例分割,建议先了解一下实例分割的输出是什么。 实例分割两个关键输出是:mask系数、mask原型 本文参考自该项目(这么优秀的代码当然要给star!):GitHub - UNeedCryDear/yolov5-seg-opencv-onnxruntime-cpp: yolov5 segmentation with onnxruntime and opencv 目录 Pre: 一、代码总结

    2024年02月12日
    浏览(29)
  • Yolov5 ONNX Runtime 的 Python 部署

    这里使用的yolov5 6.2,使用export.py很方便地得到onnx格式的模型。然后用onnxruntime推理框架在Python上进行部署。主要是为了测试模型的准确,模型部署的最终是用 C++ 部署,从而部署在嵌入式设备等。 整个代码分为四个部分:1、对输入进行预处理; 2、onnxruntime推理得到输出;

    2024年02月13日
    浏览(37)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包