Yolov5Qt工程
main.cpp
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
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()));
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