首页 > 编程语言 >【opencv c++】实现yolov5部署onnx模型完成目标检测

【opencv c++】实现yolov5部署onnx模型完成目标检测

时间:2023-02-14 14:37:01浏览次数:51  
标签:box yolov5 int onnx top float newh c++ class

总代码

#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);

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

标签:box,yolov5,int,onnx,top,float,newh,c++,class
From: https://www.cnblogs.com/iuk11/p/17119462.html

相关文章

  • c++函数与结构
    当结构比较小时,按值传递结构最合理。传递2个值结构体,返回一个结构体,返回的结构体中的成员是参数各成员的和。#include<cstring>usingnamespacestd;structthings{i......
  • C/C++读入含有空格的字符串
    好久之前遇到gets()不准用的情况,所以稍稍参考了一下网上的方法,整理一下。 charst[maxn];strings;1、gets(st);2、scanf("%[^\n]",st);3、getline(cin,s);......
  • C++PrimerPlus中文第六版第11章编程练习答案
    1、//vector.h#ifndefVECTOR_H_#defineVECTOR_H_#include<iostream>namespaceVECTOR{classVector{public:enumMode{RECT,POL};......
  • C/C++产品销售统计系统[2023-02-14]
    C/C++产品销售统计系统[2023-02-14]题目15: 产品销售统计一家公司生产五种产品,每种产品在一个月内每周的生产数量和销售价格都要记录下来。下面是一个二维的表格,表格的每......
  • C/C++图书入库管理系统[2023-02-14]
    C/C++图书入库管理系统[2023-02-14]题目21图书入库管理系统【说明及要求】实现图书信息(书号、书名、作者、定价、数量)的新增、修改、删除和查询功能;实现入库信息(书......
  • C/C++便条管理系统[2023-02-14]
    C/C++便条管理系统[2023-02-14]便条管理系统某公司有四个销售员(编号:1-4),负责销售五种产品(编号:1-5)。每个销售员都将当天出售的每种产品各写一张便条交上来。每张便......
  • <简易>通讯录管理系统(C++)
    #include<iostream>usingnamespacestd;#include<string>#defineMAX1000//*封装函数显示该界面如`voidshowMenu()`//*在main函数中调用封装好的函数//......
  • C++奥赛一本通递推题解
    title:C++奥赛一本通刷题记录(递推)date:2017-11-08tags:一本通openjudegecategories:OIC++奥赛一本通刷题记录(递推)2017.11.8Bygwj1139177410斐波那契数列​​op......
  • C++奥赛一本通排序题解
    title:C++奥赛一本通刷题记录(排序)date:2017-11-16tags:一本通openjudegecategories:OIC++奥赛一本通刷题记录(排序)2017.11.16Bygwj1139177410都是拿STL水的…别......
  • C++奥赛一本通刷题高精度题解
    title:C++奥赛一本通刷题记录(高精度)date:2017-11-15tags:一本通openjudegecategories:OIC++奥赛一本通刷题记录(高精度)2017.11.15Bygwj1139177410大整数加法​......