Qt + OpenCV 部署yolov5

Posted SongpingWang

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Qt + OpenCV 部署yolov5相关的知识,希望对你有一定的参考价值。

文章目录


分别使用了openvino,opencv_cuda进行加速。

关于演示视频及代码讲解请查看:
https://www.bilibili.com/video/BV13S4y1c7ea/
https://www.bilibili.com/video/BV1Dq4y1x7r6/
https://www.bilibili.com/video/BV1kT4y1S7hz/

一、新建项目 UI设计

二、代码部分

mainwindow 类

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QFileDialog>
#include <QFile>
#include <opencv2/opencv.hpp>
#include <opencv2/dnn.hpp>
#include <QMainWindow>
#include <QTimer>
#include <QImage>
#include <QPixmap>
#include <QDateTime>
#include <QMutex>
#include <QMutexLocker>
#include <QMimeDatabase>
#include <iostream>
#include <yolov5.h>
#include <chrono>

#pragma comment(lib,"C:\\\\Program Files (x86)\\\\Intel\\\\openvino_2021\\\\opencv\\\\lib\\\\opencv_core453.lib")
#pragma comment(lib,"C:\\\\Program Files (x86)\\\\Intel\\\\openvino_2021\\\\opencv\\\\lib\\\\opencv_imgcodecs453.lib")
#pragma comment(lib,"C:\\\\Program Files (x86)\\\\Intel\\\\openvino_2021\\\\opencv\\\\lib\\\\opencv_imgproc453.lib")
#pragma comment(lib,"C:\\\\Program Files (x86)\\\\Intel\\\\openvino_2021\\\\opencv\\\\lib\\\\opencv_videoio453.lib")
#pragma comment(lib,"C:\\\\Program Files (x86)\\\\Intel\\\\openvino_2021\\\\opencv\\\\lib\\\\opencv_objdetect453.lib")
#pragma comment(lib,"C:\\\\Program Files (x86)\\\\Intel\\\\openvino_2021\\\\opencv\\\\lib\\\\opencv_dnn453.lib")

#pragma comment(lib,"C:\\\\Program Files (x86)\\\\Intel\\\\openvino_2021\\\\deployment_tools\\\\inference_engine\\\\lib\\\\intel64\\\\Release\\\\inference_engine.lib")
#pragma comment(lib,"C:\\\\Program Files (x86)\\\\Intel\\\\openvino_2021\\\\deployment_tools\\\\inference_engine\\\\lib\\\\intel64\\\\Release\\\\inference_engine_c_api.lib")
#pragma comment(lib,"C:\\\\Program Files (x86)\\\\Intel\\\\openvino_2021\\\\deployment_tools\\\\inference_engine\\\\lib\\\\intel64\\\\Release\\\\inference_engine_transformations.lib")

//LIBS+= -L "C:\\Program Files (x86)\\Intel\\openvino_2021\\opencv\\lib\\*.lib"
//LIBS+= -L "C:\\Program Files (x86)\\Intel\\openvino_2021\\deployment_tools\\inference_engine\\lib\\intel64\\Release\\*.lib"

//#ifdef QT_NO_DEBUG
//#pragma comment(lib,"C:\\Program Files (x86)\\Intel\\openvino_2021\\opencv\\lib\\opencv_core452.lib")
//#pragma comment(lib,"E:/opencv_build/install/x64/vc16/lib/opencv_imgcodecs452.lib")
//#pragma comment(lib,"E:/opencv_build/install/x64/vc16/lib/opencv_imgproc452.lib")
//#pragma comment(lib,"E:/opencv_build/install/x64/vc16/lib/opencv_imgcodecs452.lib")

//#pragma comment(lib,"E:/opencv_build/install/x64/vc16/lib/opencv_video452.lib")
//#pragma comment(lib,"E:/opencv_build/install/x64/vc16/lib/opencv_videoio452.lib")
//#pragma comment(lib,"E:/opencv_build/install/x64/vc16/lib/opencv_objdetect452.lib")
//#pragma comment(lib,"E:/opencv_build/install/x64/vc16/lib/opencv_shape452.lib")
//#pragma comment(lib,"E:/opencv_build/install/x64/vc16/lib/opencv_dnn452.lib")
//#pragma comment(lib,"E:/opencv_build/install/x64/vc16/lib/opencv_dnn_objdetect452.lib")
//#else
//#pragma comment(lib,"E:/opencv_build/install/x64/vc16/lib/opencv_core452d.lib")
//#pragma comment(lib,"E:/opencv_build/install/x64/vc16/lib/opencv_imgcodecs452d.lib")
//#pragma comment(lib,"E:/opencv_build/install/x64/vc16/lib/opencv_imgproc452d.lib")
//#pragma comment(lib,"E:/opencv_build/install/x64/vc16/lib/opencv_imgcodecs452d.lib")

//#pragma comment(lib,"E:/opencv_build/install/x64/vc16/lib/opencv_video452d.lib")
//#pragma comment(lib,"E:/opencv_build/install/x64/vc16/lib/opencv_videoio452d.lib")
//#pragma comment(lib,"E:/opencv_build/install/x64/vc16/lib/opencv_objdetect452d.lib")
//#pragma comment(lib,"E:/opencv_build/install/x64/vc16/lib/opencv_shape452d.lib")
//#pragma comment(lib,"E:/opencv_build/install/x64/vc16/lib/opencv_dnn452d.lib")
//#pragma comment(lib,"E:/opencv_build/install/x64/vc16/lib/opencv_dnn_objdetect452d.lib")
//#endif


//#ifdef QT_NO_DEBUG
//#pragma comment(lib,"E:/opencv452_cuda/install/x64/vc16/lib/opencv_core452.lib")
//#pragma comment(lib,"E:/opencv452_cuda/install/x64/vc16/lib/opencv_imgcodecs452.lib")
//#pragma comment(lib,"E:/opencv452_cuda/install/x64/vc16/lib/opencv_imgproc452.lib")
//#pragma comment(lib,"E:/opencv452_cuda/install/x64/vc16/lib/opencv_imgcodecs452.lib")

//#pragma comment(lib,"E:/opencv452_cuda/install/x64/vc16/lib/opencv_video452.lib")
//#pragma comment(lib,"E:/opencv452_cuda/install/x64/vc16/lib/opencv_videoio452.lib")
//#pragma comment(lib,"E:/opencv452_cuda/install/x64/vc16/lib/opencv_objdetect452.lib")
//#pragma comment(lib,"E:/opencv452_cuda/install/x64/vc16/lib/opencv_shape452.lib")
//#pragma comment(lib,"E:/opencv452_cuda/install/x64/vc16/lib/opencv_dnn452.lib")
//#pragma comment(lib,"E:/opencv452_cuda/install/x64/vc16/lib/opencv_dnn_objdetect452.lib")
//#endif



QPixmap Mat2Image(cv::Mat src);

QT_BEGIN_NAMESPACE
namespace Ui  class MainWindow; 
QT_END_NAMESPACE

class MainWindow : public QMainWindow

    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);
    void Init();
    ~MainWindow();

private slots:
    void readFrame(); //自定义信号处理函数


    void on_openfile_clicked();

    void on_loadfile_clicked();

    void on_startdetect_clicked();

    void on_stopdetect_clicked();

    void on_comboBox_activated(const QString &arg1);

private:
    Ui::MainWindow *ui;
    QTimer *timer;
    cv::VideoCapture *capture;

    YOLOV5 *yolov5;
    NetConfig conf;
    NetConfig *yolo_nets;
    std::vector<cv::Rect> bboxes;
    int IsDetect_ok = 0;
;
#endif // MAINWINDOW_H

mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"



MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)

    ui->setupUi(this);
    setWindowTitle(QStringLiteral("YoloV5目标检测软件"));

    timer = new QTimer(this);
    timer->setInterval(33);
    connect(timer,SIGNAL(timeout()),this,SLOT(readFrame()));
    ui->startdetect->setEnabled(false);
    ui->stopdetect->setEnabled(false);
    Init();


MainWindow::~MainWindow()


    capture->release();
    delete capture;
    delete [] yolo_nets;
    delete yolov5;
    delete ui;


void MainWindow::Init()

    capture = new cv::VideoCapture();
    yolo_nets = new NetConfig[4]
                                0.5, 0.5, 0.5, "yolov5s",
                                0.6, 0.6, 0.6, "yolov5m",
                                0.65, 0.65, 0.65, "yolov5l",
                                0.75, 0.75, 0.75, "yolov5x"
                            ;
    conf = yolo_nets[0];
    yolov5 = new YOLOV5();
    yolov5->Initialization(conf);
            ui->textEditlog->append(QStringLiteral("默认模型类别:yolov5s args: %1 %2 %3")
                                    .arg(conf.nmsThreshold)
                                    .arg(conf.objThreshold)
                                    .arg(conf.confThreshold));


void MainWindow::readFrame()

    cv::Mat frame;
    capture->read(frame);
    if (frame.empty()) return;

    auto start = std::chrono::steady_clock::now();
    yolov5->detect(frame);
    auto end = std::chrono::steady_clock::now();
    std::chrono::duration<double, std::milli> elapsed = end - start;
    ui->textEditlog->append(QString("cost_time: %1 ms").arg(elapsed.count()));

//    double t0 = static_cast<double>(cv::getTickCount());
//    yolov5->detect(frame);
//    double t1 = static_cast<double>(cv::getTickCount());
//    ui->textEditlog->append(QStringLiteral("cost_time: %1 ").arg((t1 - t0) / cv::getTickFrequency()));

    cv::cvtColor(frame, frame, cv::COLOR_BGR2RGB);
    QImage rawImage = QImage((uchar*)(frame.data),frame.cols,frame.rows,frame.step,QImage::Format_RGB888);
    ui->label->setPixmap(QPixmap::fromImage(rawImage));


void MainWindow::on_openfile_clicked()

    QString filename = QFileDialog::getOpenFileName(this,QStringLiteral("打开文件"),".","*.mp4 *.avi;;*.png *.jpg *.jpeg *.bmp");
    if(!QFile::exists(filename))
        return;
    
    ui->statusbar->showMessage(filename);

    QMimeDatabase db;
    QMimeType mime = db.mimeTypeForFile(filename);
    if (mime.name().startsWith("image/")) 
        cv::Mat src = cv::imread(filename.toLatin1().data());
        if(src.empty())
            ui->statusbar->showMessage("图像不存在!");
            return;
        
        cv::Mat temp;
        if(src.channels()==4)
            cv::cvtColor(src,temp,cv::COLOR_BGRA2RGB);
        else if (src.channels()==3)
            cv::cvtColor(src,temp,cv::COLOR_BGR2RGB);
        else
            cv::cvtColor(src,temp,cv::COLOR_GRAY2RGB);

        auto start = std::chrono::steady_clock::now();
        yolov5->detect(temp);
        auto end = std::chrono::steady_clock::now();
        std::chrono::duration<double, std::milli> elapsed = end - start;
        ui->textEditlog->append(QString("cost_time: %1 ms").arg(elapsed.count()));
        QImage img = QImage((uchar*)(temp.data),temp.cols,temp.rows,temp.step,QImage::Format_RGB888);
        ui->label->setPixmap(QPixmap::fromImage(img));
        ui->label->resize(ui->label->pixmap()->size());
        filename.clear();
    else if (mime.name().startsWith("video/")) 
        capture->open(filename.toLatin1().data());
        if (!capture->isOpened())
            ui->textEditlog->append("fail to open MP4!");
            return;
        
        IsDetect_ok +=1;
        if (IsDetect_ok ==2)
            ui->startdetect->setEnabled(true);
        ui->textEditlog->append(QString::fromUtf8("Open video: %1 succesfully!").arg(filename));

        //获取整个帧数QStringLiteral
        long totalFrame = capture->get(cv::CAP_PROP_FRAME_COUNT);
        ui->textEditlog->append(QStringLiteral("整个视频共 %1 帧").arg(totalFrame));
        ui->label->resize(QSize(capture->get(cv::CAP_PROP_FRAME_WIDTH), capture->get(cv::CAP_PROP_FRAME_HEIGHT)));

        //设置开始帧()
        long frameToStart = 0;
        capture->set(cv::CAP_PROP_POS_FRAMES, frameToStart);
        ui->textEditlog->append(QStringLiteral("从第 %1 帧开始读").arg(frameToStart));

        //获取帧率
        double rate = capture->get(cv::CAP_PROP_FPS);
        ui->textEditlog->append(QStringLiteral("帧率为: %1 ").arg(rate));
    


void MainWindow::on_loadfile_clicked()

    QString onnxFile = QFileDialog::getOpenFileName(this,QStringLiteral("选择模型"),".","*.onnx");
    if(!QFile::exists(onnxFile))
        return;
    
    ui->statusbar->showMessage(onnxFile);
    if (!yolov5->loadModel(onnxFile.toLatin1().data()))
        ui->textEditlog->append(QStringLiteral("加载模型失败!"));
        return;
    
    IsDetect_ok +=1;
    ui->textEditlog->append(QString::fromUtf8("Open onnxFile: %1 succesfully!").arg(onnxFile));
    if (IsDetect_ok ==2)
        ui->startdetect->setEnabled(true);


void MainWindow::on_startdetect_clicked()

    timer->start();
    ui->startdetect->setEnabled(false);
    ui->stopdetect->setEnabled(true);
    ui->openfile->setEnabled(false);
    ui->loadfile->setEnabled(false);
    ui->comboBox->setEnabled(false);
    ui->textEditlog->append(QStringLiteral("================\\n"
                                           "    开始检测\\n"
                                           "================\\n"));


void MainWindow::on_stopdetect_clicked()

    ui->startdetect->setEnabled(true);
    ui->stopdetect->setEnabled(false);
    ui->openfile->setEnabled(true);
    ui->loadfile->setEnabled(true);
    ui->comboBox->setEnabled(true);
    timer->stop();
    ui->textEditlog->append(QStringLiteral("================\\n"
                                           "    停止检测\\n"
                                           "================\\n"));


void MainWindow::on_comboBox_activated(const QString &arg1)

    if (arg1.contains("s"))
        conf = yolo_nets[0];
    else if (arg1.contains("m")) 
        conf = yolo_nets[1];
    else if (arg1.contains("l")) 
        conf = yolo_nets[2];
    else if (arg1.contains("x")) 
        conf = yolo_nets[3];
    yolov5->Initialization(conf);
    ui->textEditlog->append(QStringLiteral("使用模型类别:%1 args: %2 %3 %4")
                            .arg(arg1)
                            .arg(conf.nmsThreshold)
                            .arg(conf.objThreshold)
                            .arg(confyolov3和yolov5 android端实时部署(ncnn)

QT Widget使用OpenCV错误部署到Android [重复]

深度学习目标检测(YoloV5)项目——从0开始到项目落地部署

YOLOv5 在最新 OpenVINO 2021R02 版本的部署与代码演示详解

QT Creator 中的部署和静态打包将 QT 和 OpenCV 链接到单个可执行文件中

Yolov5 OpenCV 错误:(-215:断言失败)同时使用 .onnx