Qt工具集成模型部署(ONNXRunTime+openvino)之UI设计篇

文章描述了一个界面,其中包含模型选择、图像选择以及部署选项(ONNX+OpenVINO),用户可以使用YOLOV5模型处理图像或视频,并展示了相关代码片段,展示了如何在界面中进行模型操作和展示推理结果。
摘要由CSDN通过智能技术生成

Demo示范

  • 最左一列为模型选择列,会将常见模型名称列在此处,后续还需添加其他模型
  • 左侧第二列为图像选择和模型部署选择(onnx+openvino)
  • 左侧第三列为选择图像按钮和运行模型按钮
  • 最后一列为模型推理结果

运行结果显示,这里选择运行YOLOV5模型,选一张图像运行,结果如下

原图像为

还可以选择MP4格式的视频作为素材,来运行模型推理。

界面代码演示

uideploy.h 界面设计头文件

#ifndef DEPLOY_H
#define DEPLOY_H

#include <QPushButton>
#include <QListWidget>
#include <QStackedWidget>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QTabWidget>
#include <QGroupBox>
#include <QFormLayout>
#include <QLabel>
#include <QLineEdit>
#include <QRadioButton>

#include <QWidget>

#include "common_api.h"

class Deploy : public QWidget, public Show
{
    Q_OBJECT

public:
    Deploy(QWidget *parent = 0);
    ~Deploy();
    void initSetting();

public:
    virtual void imageshow(cv::Mat &image);


public:
    void uiInit();
    QWidget& uileftModelInit();
    QWidget& uiStackWidgetInit();
    QLayout& uiButtonInit();
    QLayout& uiShowInit();

public: // button
    QPushButton* openfileButton;
    QPushButton* RunButton;

    QLineEdit *pathLineEdit;
    QRadioButton *onnxruntimeRadioBtn;
    QRadioButton *opvinoRadioBtn;

    modelTypeInfo_ modelTypeInfo;

public slots:
    void onPushButtonClick();
public:
    QListWidget leftModeListWidget;

public:
    QStackedWidget stackWidget;
    QHBoxLayout stackWidgetLayout;
    QGroupBox stackWidgetGroup;

    QWidget settingShowQwidget;


public:
    QVBoxLayout buttonLayout;

public:
    QTabWidget showTabWidget;
    QLabel QshowLabel;

public:
    QHBoxLayout uilayout;

public:
    ImageProcessor *imageProcess;
    void setImageProcesser(ImageProcessor *process){imageProcess = process;}

};

#endif // DEPLOY_H

uideploy.cpp 界面设计实现文件

#include "uideploy.h"
#include <QImage>
#include <QDebug>
#include <QString>
#include <QFileDialog>
#include <QMessageBox>
#include <QSettings>

const char *modetye[] = {"resnet18", "YOLOv5", "YOLOv8", "RetinaNet", "FasterRcnn", "MaskRcnn", "Unet"};

Deploy::Deploy(QWidget *parent)
    : QWidget(parent)
{
    uiInit();
}

void Deploy::uiInit()
{
      uilayout.addWidget(&uileftModelInit());
      uilayout.addWidget(&uiStackWidgetInit());
      uilayout.addLayout(&uiButtonInit());
      uilayout.addStretch(1);
      uilayout.addLayout(&uiShowInit());
//      uilayout.addStretch(1);
      setLayout(&uilayout);

      initSetting();
}

void Deploy::imageshow(cv::Mat &image)
{
    QImage dst(image.data, image.cols, image.rows, static_cast<int>(image.step), QImage::Format::Format_RGB888);
    QshowLabel.setPixmap(QPixmap::fromImage(dst.rgbSwapped()));
}

QWidget& Deploy::uileftModelInit()
{

    for(int i = 0; i < sizeof(modetye)/sizeof(modetye[0]); i++)
    {
        leftModeListWidget.insertItem(i, modetye[i]);
    }

    return leftModeListWidget;
}

QWidget& Deploy::uiStackWidgetInit()
{
    stackWidgetGroup.setTitle("Type");

    QLabel *pathLabel = new QLabel(tr("path:"));
    pathLineEdit = new QLineEdit();

    QHBoxLayout *pathLayout = new QHBoxLayout;
    pathLayout->addWidget(pathLabel);
    pathLayout->addWidget(pathLineEdit);

    QGroupBox *DeployModelTypeGroupBox = new QGroupBox(tr("Deploy ModeType"));
    onnxruntimeRadioBtn = new QRadioButton(tr("&onnxruntime"));
    opvinoRadioBtn      = new QRadioButton(tr("&openvino"));

    QVBoxLayout *vbox = new QVBoxLayout;
    vbox->addWidget(onnxruntimeRadioBtn);
    vbox->addWidget(opvinoRadioBtn);
    vbox->addStretch(1);
    DeployModelTypeGroupBox->setLayout(vbox);

    QVBoxLayout *configLayout = new QVBoxLayout;
    configLayout->addLayout(pathLayout);
    configLayout->addWidget(DeployModelTypeGroupBox);
    configLayout->addStretch(1);
    stackWidgetGroup.setLayout(configLayout);


    onnxruntimeRadioBtn->setChecked(true);

    return stackWidgetGroup;

}


QLayout& Deploy::uiButtonInit()
{
    openfileButton = new QPushButton("Openfile");
    RunButton = new QPushButton("Run");

    buttonLayout.addWidget(openfileButton);
    buttonLayout.addWidget(RunButton);
    buttonLayout.addStretch(1);

    connect(openfileButton, SIGNAL(clicked(bool)), this, SLOT(onPushButtonClick()));
    connect(RunButton, SIGNAL(clicked(bool)), this, SLOT(onPushButtonClick()));

    return buttonLayout;
}

void Deploy::onPushButtonClick()
{
    QPushButton* btn =  (QPushButton*)sender();
    QString text = btn->text();
//    qDebug() << text;
    if( text == "Openfile")
    {
        QString fileName = QFileDialog::getOpenFileName(this, tr("Open File"),
                                                                  "/home",
                                                                  tr("Images (*.png *.jpg *.png *.mp4)"));
//        qDebug() << fileName;
        pathLineEdit->setText(fileName);
        QSettings initSetting("config.ini", QSettings::IniFormat);
        initSetting.setValue("/init/path", fileName);
    }else if( text == "Run" )
    {
        modelTypeInfo.filePath = pathLineEdit->text();
        modelTypeInfo.modelType = modetye[leftModeListWidget.currentRow()];
        modelTypeInfo.deploymode = onnxruntimeRadioBtn->isChecked() ? OnnxRunTime : Openvino;

//        qDebug() << modelTypeInfo.modelType;
//        qDebug() << modelTypeInfo.deploymode;
//        qDebug() << modelTypeInfo.filePath;

        if( modelTypeInfo.filePath != NULL)
        {
            imageProcess->processor(modelTypeInfo);
        }else
        {
            QMessageBox::warning(this, "No ImagePath","Please input the imagePath", QMessageBox::Ok);
        }


    }


}

void Deploy::initSetting()
{
    QSettings initSetting("config.ini", QSettings::IniFormat);
    QString imagePath = initSetting.value("/init/path").toString();
    if( !imagePath.isEmpty())
    {
        pathLineEdit->setText(imagePath);
    }
}

QLayout& Deploy::uiShowInit()
{
    QHBoxLayout *showLayout = new QHBoxLayout;
    QshowLabel.setMinimumSize(480,400);
    QshowLabel.setStyleSheet("QLabel{background-color:rgb(0,0,0);}");
    QshowLabel.setScaledContents(true);
    showLayout->addWidget(&QshowLabel);

    return *showLayout;

}

Deploy::~Deploy()
{
    delete openfileButton;
    delete RunButton;
    delete pathLineEdit;
    delete onnxruntimeRadioBtn;
    delete opvinoRadioBtn;

}

接口文件 common_api.h 界面类继承于该文件中的Show类,并实现对应的方法,其他类需要显示图像,只需注册该类即可

#ifndef COMMON_API_H
#define COMMON_API_H

#include <iostream>
#include <vector>
#include <opencv2/opencv.hpp>


class Show
{
public:
    virtual void imageshow(cv::Mat &image) = 0;
};



#endif // COMMON_API_H

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值