Qt + OpenCV 部署yolov5(多线程)


项目基于 Qt + OpenCV 部署yolov5 添加了检测线程(主线程负责主窗口UI显示)避免拖动窗口时卡顿

开始
主线程 mainwindow UI
检测线程 detector
yolov5
结束

一、新建项目 UI设计

在这里插入图片描述

二、代码部分

mainwindow 类

mainwindow.hpp

#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QFileDialog>
#include <QFile>
#include <QMainWindow>
#include <QTimer>
#include <QImage>
#include <QPixmap>
#include <QDateTime>
#include <QMutex>
#include <QMutexLocker>
#include <QMimeDatabase>
#include <QProgressBar>
#include <iostream>
#include <chrono>
#include "detector.h"

QT_BEGIN_NAMESPACE
namespace Ui {
    class MainWindow; }
QT_END_NAMESPACE

class MainWindow : public QMainWindow
{
   
    Q_OBJECT

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

private slots:
    void recv_frame_all(long frame_all);
    void recv_frame_num(long frame_num);
    void on_openfile_clicked();

    void on_loadfile_clicked();

    void on_startdetect_clicked();

    void on_stopdetect_clicked();

    void on_comboBox_activated(const QString &arg1);

    void on_pausedetect_clicked();

    void on_photodetect_clicked();

signals:
    void send_imagefile_info(QString filename);
    void send_videofile_info(QString filename);
    void send_onnxfile_info(QString filename);
    void send_comboBox_info(QString arg);


private:
    Ui::MainWindow *ui;
    detector* detector_;
    QProgressBar * pProgressBar;
    QLabel *  pLabel;
    
    QString filename;
    bool IsDetect_ok = false;
    long m_frame_all = 0;
    long m_frame_num = 0;
};
#endif // MAINWINDOW_H

mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QTextEdit>



MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
   
    ui->setupUi(this);
    setWindowTitle(QStringLiteral("YoloV5目标检测软件"));

    ui->startdetect->setEnabled(false);
    ui->stopdetect->setEnabled(false);
    ui->pausedetect->setEnabled(false);
    ui->photodetect->setEnabled(false);
    ui->textEditlog->document()->setMaximumBlockCount(60);
    detector_ = new detector();

    pProgressBar = new QProgressBar();
    pLabel= new QLabel();
    ui->statusbar->addPermanentWidget(pLabel);
    ui->statusbar->addPermanentWidget(pProgressBar);

    connect(this, &MainWindow::send_imagefile_info, detector_, &detector::recv_imagefile);
    connect(this, &MainWindow::send_videofile_info, detector_, &detector::recv_videofile);
    connect(this, &MainWindow::send_onnxfile_info, detector_, &detector::recv_onnxfile);

    connect(detector_, &detector::send_init_info, this, [=](QString init_info){
   
        ui->textEditlog->append(init_info);
        });

    connect(detector_, &detector::send_loadModel_info, this, [=](QString model_info){
   
        ui->textEditlog->append(model_info);
        });

    connect(detector_, &detector::send_detect_info, this, [=](QString detect_info){
   
        ui->textEditlog->append(detect_info);
        });

    connect(detector_, &detector::send_video_info, this, [=](QString video_info)
  • 3
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
以下是使用 PyQT5 实现 Qt 界面链接 YOLOv5 目标检测的 Python 代码示例: ```python import sys from PyQt5.QtWidgets import QApplication, QWidget, QLabel, QPushButton, QFileDialog from PyQt5.QtGui import QPixmap import cv2 from yolov5.detect import YOLOv5Detector class App(QWidget): def __init__(self): super().__init__() self.title = 'YOLOv5 Object Detection' self.left = 50 self.top = 50 self.width = 640 self.height = 480 self.image_path = '' self.initUI() def initUI(self): self.setWindowTitle(self.title) self.setGeometry(self.left, self.top, self.width, self.height) # 选择图像文件按钮 self.select_button = QPushButton('Select Image', self) self.select_button.move(20, 20) self.select_button.clicked.connect(self.select_image) # 显示图像 self.image_label = QLabel(self) self.image_label.move(20, 60) self.image_label.resize(600, 400) # 检测目标按钮 self.detect_button = QPushButton('Detect Objects', self) self.detect_button.move(20, 470) self.detect_button.clicked.connect(self.detect_objects) self.show() def select_image(self): options = QFileDialog.Options() options |= QFileDialog.DontUseNativeDialog file_name, _ = QFileDialog.getOpenFileName(self, 'Select Image', '', 'Images (*.png *.xpm *.jpg *.bmp *.gif)', options=options) if file_name: self.image_path = file_name pixmap = QPixmap(file_name) self.image_label.setPixmap(pixmap) self.image_label.setScaledContents(True) def detect_objects(self): if not self.image_path: return detector = YOLOv5Detector() image = cv2.imread(self.image_path) result_image, _ = detector.detect(image) cv2.imwrite('result.jpg', result_image) pixmap = QPixmap('result.jpg') self.image_label.setPixmap(pixmap) self.image_label.setScaledContents(True) if __name__ == '__main__': app = QApplication(sys.argv) ex = App() sys.exit(app.exec_()) ``` 其中 `yolov5.detect` 模块的代码可以参考 YOLOv5 的官方实现。在 `detect` 函数中,需要对输入的图像进行目标检测,并返回检测结果。在本例中,我们将检测结果保存为 `result.jpg`,并在界面中显示出来。 注意,需要将模型文件和权重文件放在适当的位置,并在 `YOLOv5Detector` 类中设置正确的路径。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

SongpingWang

你的鼓励是我创作的最大动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值