前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >源码 | OpenCV DNN + YOLOv7目标检测

源码 | OpenCV DNN + YOLOv7目标检测

作者头像
OpenCV学堂
发布2022-07-19 19:10:25
3.8K1
发布2022-07-19 19:10:25
举报

点击上方蓝字关注我们

作者:王博,极视角科技算法研究员 微信公众号:OpenCV学堂 关注获取更多计算机视觉与深度学习知识

简单说明

分别使用OpenCV、ONNXRuntime部署YOLOV7目标检测,一共包含12个onnx模型,依然是包含C++和Python两个版本的程序。

编写这套YOLOV7的程序,跟此前编写的YOLOV6的程序,大部分源码是相同的,区别仅仅在于图片预处理的过程不一样。YOLOV7的图片预处理是BGR2RGB+不保持高宽比的resize+除以255

由于onnx文件太多,无法直接上传到仓库里,需要从百度云盘下载, 

代码语言:javascript
复制
链接: https://pan.baidu.com/s/1FoC0n7qMz4Fz0RtDGpI6xQ 密码: 7mhs

下载完成后把models目录放在主程序文件的目录内,编译运行

使用opencv部署的程序,有一个待优化的问题。onnxruntime读取.onnx文件可以获得输入张量的形状信息, 但是opencv的dnn模块读取.onnx文件无法获得输入张量的形状信息,目前是根据.onnx文件的名称来解析字符串获得输入张量的高度和宽度的。

YOLOV7的训练源码是:

代码语言:javascript
复制
 https://github.com/WongKinYiu/yolov7

跟YOLOR是同一个作者的。

OpenCV+YOLOv7

推理过程跟之前的YOLO系列部署代码可以大部分重用!这里就不在赘述了,详细看源码如下:输出部分直接解析最后一个输出层就好啦!

详细实现代码如下:

代码语言:javascript
复制
#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
    string modelpath;
};

class YOLOV7
{
public:
    YOLOV7(Net_config config);
    void detect(Mat& frame);
private:
    int inpWidth;
    int inpHeight;
    vector<string> 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<Mat> 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<float> confidences;
    vector<Rect> boxes;
    vector<int> 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 < num_proposal; n++)   ///ÌØÕ÷ͼ³ß¶È
    {
        float box_score = pdata[4];
        if (box_score > 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<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 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();
}

运行测试如下:

本文作者github主页:

代码语言:javascript
复制
https://github.com/hpc203/yolov7-opencv-onnxrun-cpp-py

读书欲精不欲博

用心欲专不欲杂

扫码查看OpenCV+OpenVIO+Pytorch系统化学习路线图

 推荐阅读 

CV全栈开发者说 - 从传统算法到深度学习怎么修炼

2022入坑深度学习,我选择Pytorch框架!

Pytorch轻松实现经典视觉任务

教程推荐 | Pytorch框架CV开发-从入门到实战

OpenCV4 C++学习 必备基础语法知识三

OpenCV4 C++学习 必备基础语法知识二

OpenCV4.5.4 人脸检测+五点landmark新功能测试

OpenCV4.5.4人脸识别详解与代码演示

OpenCV二值图象分析之Blob分析找圆

OpenCV4.5.x DNN + YOLOv5 C++推理

OpenCV4.5.4 直接支持YOLOv5 6.1版本模型推理

OpenVINO2021.4+YOLOX目标检测模型部署测试

比YOLOv5还厉害的YOLOX来了,官方支持OpenVINO推理

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2022-07-15,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 OpenCV学堂 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
图像识别
腾讯云图像识别基于深度学习等人工智能技术,提供车辆,物体及场景等检测和识别服务, 已上线产品子功能包含车辆识别,商品识别,宠物识别,文件封识别等,更多功能接口敬请期待。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档