简单说明
分别使用opencv、onnxruntime部署yolov7目标检测,一共包含12个onnx模型,依然是包含c++和python两个版本的程序。 编写这套yolov7的程序,跟此前编写的yolov6的程序,大部分源码是相同的,区别仅仅在于图片预处理的过程不一样。yolov7的图片预处理是bgr2rgb+不保持高宽比的resize+除以255 由于onnx文件太多,无法直接上传到仓库里,需要从百度云盘下载,
下载完成后把models目录放在主程序文件的目录内,编译运行 使用opencv部署的程序,有一个待优化的问题。onnxruntime读取.onnx文件可以获得输入张量的形状信息, 但是opencv的dnn模块读取.onnx文件无法获得输入张量的形状信息,目前是根据.onnx文件的名称来解析字符串获得输入张量的高度和宽度的。
yolov7的训练源码是:
跟yolor是同一个作者的。
opencv+yolov7
推理过程跟之前的yolo系列部署代码可以大部分重用!这里就不在赘述了,详细看源码如下:输出部分直接解析最后一个输出层就好啦!
详细实现代码如下:
#include #include #include #include #include #include using namespace cv;using namespace dnn;using namespace std;struct net_config{ float confthreshold; // confidence threshold float nmsthreshold; // non-maximum suppression threshold string modelpath;};class yolov7{public: yolov7(net_config config); void detect(mat& frame);private: int inpwidth; int inpheight; vector class_names; int num_class; float confthreshold; float nmsthreshold; net net; void drawpred(float conf, int left, int top, int right, int bottom, mat& frame, int classid);};yolov7::yolov7(net_config config){ this->confthreshold = config.confthreshold; this->nmsthreshold = config.nmsthreshold; this->net = readnet(config.modelpath); ifstream ifs(coco.names); string line; while (getline(ifs, line)) this->class_names.push_back(line); this->num_class = class_names.size(); size_t pos = config.modelpath.find(_); int len = config.modelpath.length() - 6 - pos; string hxw = config.modelpath.substr(pos + 1, len); pos = hxw.find(x); string h = hxw.substr(0, pos); len = hxw.length() - pos; string w = hxw.substr(pos + 1, len); this->inpheight = stoi(h); this->inpwidth = stoi(w);}void yolov7::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 yolov7::detect(mat& frame){ mat blob = blobfromimage(frame, 1 / 255.0, size(this->inpwidth, this->inpheight), scalar(0, 0, 0), true, false); this->net.setinput(blob); vector outs; this->net.forward(outs, this->net.getunconnectedoutlayersnames()); int num_proposal = outs[0].size[0]; int nout = outs[0].size[1]; if (outs[0].dims > 2) { num_proposal = outs[0].size[1]; nout = outs[0].size[2]; outs[0] = outs[0].reshape(0, num_proposal); } /////generate proposals vector confidences; vector boxes; vector classids; float ratioh = (float)frame.rows / this->inpheight, ratiow = (float)frame.cols / this->inpwidth; int n = 0, row_ind = 0; ///cx,cy,w,h,box_score,class_score float* pdata = (float*)outs[0].data; for (n = 0; n this->confthreshold) { 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] * ratiow; ///cx float cy = pdata[1] * ratioh; ///cy float w = pdata[2] * ratiow; ///w float h = pdata[3] * ratioh; ///h int left = int(cx - 0.5 * w); int top = int(cy - 0.5 * h); confidences.push_back((float)max_class_socre); boxes.push_back(rect(left, top, (int)(w), (int)(h))); classids.push_back(class_idx); } } row_ind++; pdata += nout; } // perform non maximum suppression to eliminate redundant overlapping boxes with // lower confidences vector indices; dnn::nmsboxes(boxes, confidences, this->confthreshold, this->nmsthreshold, indices); for (size_t i = 0; i drawpred(confidences[idx], box.x, box.y, box.x + box.width, box.y + box.height, frame, classids[idx]); }}int main(){ net_config yolov7_nets = { 0.3, 0.5, models/yolov7_736x1280.onnx }; ////choices=[models/yolov7_736x1280.onnx, models/yolov7-tiny_384x640.onnx, models/yolov7_480x640.onnx, models/yolov7_384x640.onnx, models/yolov7-tiny_256x480.onnx, models/yolov7-tiny_256x320.onnx, models/yolov7_256x320.onnx, models/yolov7-tiny_256x640.onnx, models/yolov7_256x640.onnx, models/yolov7-tiny_480x640.onnx, models/yolov7-tiny_736x1280.onnx, models/yolov7_256x480.onnx] yolov7 net(yolov7_nets); string imgpath = images/dog.jpg; mat srcimg = imread(imgpath); net.detect(srcimg); static const string kwinname = deep learning object detection in opencv; namedwindow(kwinname, window_normal); imshow(kwinname, srcimg); waitkey(0); destroyallwindows();}
运行测试如下:
消息称苹果已预订台积电3nm产能
视频监控APP开发公司
红米K40系列开售被秒光
***如何做车规认证?
使用ADS用微带线代替传输线而导致仿真结果发生变化?
使用OpenCV+ONNXRuntime部署YOLOV7目标检测
如何对单片机的定时器进行赋初值
区块链正在改变游戏行业的发展理念
一文彻底搞懂内存屏障与volatile
微型光纤压力传感器助力医疗设备
百度智能音箱可替换家电遥控器
这款手机的发布或意味着HTC的落幕
智慧水务能效管理平台在污水处理厂的具体应用
虹科与Overland-Tandberg正式建立合作伙伴关系
苹果“降速门”调查 三星也被牵扯其中
选择网线型号小技巧
多数智能手机厂商一直在积极开发IoT(物联网)细分市场
媒体聚焦 | 从今年慕展,看汽车电子的技术与市场变化
国内AI+教育玩家众多,产业泡沫究竟几何?一块屏幕解决的问题是什么?
固态光源如何助力新型红外照明发展