VS+Qt+C++ Yolov8 object recognition form program onnx model

Selected program examples : VS+Qt+C++ Yolov8 object recognition form program onnx model. If you need to install the running environment or remote debugging, please see your personal QQ business card at the bottom of the article for remote assistance from professional technicians!

Preface

This blog writes code for "VS+Qt+C++ Yolov8 object recognition form program onnx model". The code is clean, regular and easy to read. Recommended for learning and application.


operation result

Insert image description here


Article directory

1. Required tools and software
2. Usage steps
       1. Main code
       2. Operation results
3. Online assistance

1. Required tools and software

       1. VS2019, Qt
       2. C++,Yolov8

2. Usage steps

The code is as follows (example):

#include<math.h>
#include "yolov8_onnx.h"
#include<time.h>
using namespace dnn;

template<typename _Tp>

int yolov8_onnx(_Tp& cls, Mat& img, string& model_path)
{
    
    
    //cout << "test00" << endl;

    if (cls.ReadModel(model_path, false)) {
    
    
        cout << "read net ok!" << endl;
    }
    else {
    
    
        return -1;
    }

    vector<Scalar> color;
    srand(time(0));
    for (int i = 0; i < 80; i++) {
    
    
        int b = rand() % 256;
        int g = rand() % 256;
        int r = rand() % 256;
        //color.push_back(Scalar(b, g, r));
        color.push_back(Scalar(255, 0, 255));
    }
    if (cls.OnnxDetect(img, result)) {
    
    
        //cout << "test5" << endl;
        DrawPred(img,cls._className, color);
    }
    else {
    
    
        cout << "Detect Failed!" << endl;
    }
    //system("pause");
    //return 0;
}

mainWindow::mainWindow(QWidget *parent)
	: QMainWindow(parent)
{
    
    
	ui.setupUi(this);
    QObject::connect(ui.pushButton, SIGNAL(clicked()), this, SLOT(onCamera()));
    QObject::connect(ui.pushButton_2, SIGNAL(clicked()), this, SLOT(onVideo()));

static void sleep(int msec)
{
    
    
    QTime dieTime = QTime::currentTime().addMSecs(msec);
    while (QTime::currentTime() < dieTime)
        QCoreApplication::processEvents(QEventLoop::AllEvents, 100);
}
void mainWindow::onCamera()
{
    
    
    std::cout << "test_ " << std::endl;
    cout << "aa" << endl;

    VideoCapture capture(0);
    while (1) {
    
    
        Mat frame;
        capture >> frame;
        if (frame.empty()) break;
        ui.label->clear();
        cvtColor(frame, frame, COLOR_RGB2BGR);
        QImage img = QImage((const unsigned char*)(frame.data), 
        ui.label->setPixmap(QPixmap::fromImage(img));
        ui.label->update();
    }
}
void mainWindow::onVideo()
{
    
    
    std::cout << "test_ " << std::endl;
    cout << "aa" << endl;
    QString filePath = QFileDialog::getOpenFileName(this, tr("Open"));
    if (filePath.isEmpty()) {
    
    
        qDebug() << "Select nothing";
        return;
    }
    QTextCodec* codec = QTextCodec::codecForName("UTF-8");
    std::string imagePath = codec->fromUnicode(filePath).data();
    std::cout << imagePath << std::endl;

    VideoCapture capture(imagePath);
    capture.open(imagePath);
    if (!capture.isOpened())
    {
    
    
        cout << "could not load video..." << endl;
    }
    while (1) {
    
    
        Mat frame;
        capture >> frame;
        if (frame.empty()) break;
        ui.label->clear();
        cvtColor(frame, frame, COLOR_RGB2BGR);
        QImage img = QImage((const unsigned char*)(frame.data), 
        ui.label->setPixmap(QPixmap::fromImage(img));
        ui.label->update();
        sleep(30); 
    }
}
void mainWindow::onRecogn()
{
    
    
    //string img_path = "./data/image/aa.png";
    //string detect_model_path = "yolov8n.onnx";
    //Mat img = imread(img_path);

    //Yolov8Onnx task_detect_onnx;
    //yolov8_onnx(task_detect_onnx, img, detect_model_path);  //onnxruntime detect

    //return 0;
    Mat img;
    img = capture.open("aa.mp4");

    QStringList nameList;
    QStringList countList;

    while (capture.read(img))
  
        //cout << "test" << endl;
        Yolov8Onnx task_detect_onnx;
        yolov8_onnx(task_detect_onnx, img, detect_model_path);  //onnxruntime detect

        //imshow("output", img);
        //waitKey(10);

        extern Mat imgT;
        //cout << "imgT" << imgT << endl;
        ui.label->clear();
        cvtColor(imgT, imgT, COLOR_RGB2BGR);
        QImage img = QImage((const unsigned char*)(imgT.data), imgT.cols, imgT.rows, imgT.cols * imgT.channels(), QImage::Format_RGB888);
        img = img.scaled(600, 500, Qt::KeepAspectRatio); 
        ui.label->setPixmap(QPixmap::fromImage(img));
        ui.label->update();
        QSqlDatabase db;
        db = QSqlDatabase::addDatabase("QSQLITE");
        db.setDatabaseName("information.db");
        if (!db.open()) {
    
    
            qDebug() << "";
            //qDebug() << "open error" << db.lastError();
        }

        QSqlQuery query;
        QString sqlSelect = "SELECT name, count FROM informationTable";

        if (query.exec(sqlSelect)) {
    
    
            while (query.next()) {
    
    
                QString name = query.value(0).toString();
                QString count = query.value(1).toString();
                nameList.append(name);
                countList.append(count);
            }

        }
        else {
    
    
            qDebug() << "Query failed";
        }
        // 一次性显示到 UI
        for (int i = 0; i < nameList.size(); i++) {
    
    
            ui.textEdit->append(nameList[i] + ": " + countList[i]);
        }
        db.close();
        sleep(30);  
    //nameList.clear();
    //countList.clear();
    capture.release();
}
void mainWindow::testT() {
    
    

    QString nameEd = QString::fromStdString("person");


    //将信息写入数据库
    QSqlDatabase db;
    db = QSqlDatabase::addDatabase("QSQLITE");
    db.setDatabaseName("information.db");

    // 2. 打开数据库
    if (!db.open()) {
    
    
        qDebug() << "";
        //qDebug() << "open error" << db.lastError();
    }

    QSqlQuery query;

    //QString sqlInert = QString("insert into informationTable(name,count)" "values('%1', '%2')").arg(nameEd).arg(countEd);
    //if (!query.exec(sqlInert))
    //	qDebug() << "insert data error" << db.lastError();
    // 检查数据库中是否存在nameEd
    QString checkExistQuery = QString("SELECT COUNT(*) FROM informationTable WHERE name = '%1'").arg(nameEd);

    QSqlQuery existQuery(checkExistQuery);
    if (existQuery.exec() && existQuery.next()) {
    
    
        int count = existQuery.value(0).toInt();
        if (count > 0) {
    
    
            qDebug() << "count" << count;
            // 如果存在,则执行更新操作
        }
        else {
    
    
            qDebug() << "no exists";
            // 如果不存在,则执行插入操作
        }
    }
    else {
    
    
        qDebug() << "Check existence query failed";
    }
}



operation result

Insert image description here

3. Online assistance:

If you need to install the operating environment or remote debugging, please see your personal QQ business card at the bottom of the article for remote assistance from professional technicians!

1) Remote installation and running environment, code debugging
2) Visual Studio, Qt, C++, Python programming language introductory guide
3) Interface beautification
4) Software production 5
) Cloud server application
6) Website production

Current article link: https://blog.csdn.net/alicema1111/article/details/132666851
Personal blog homepage : https://blog.csdn.net/alicema1111?type=
All articles by blogger click here: https:/ /blog.csdn.net/alicema1111?type=blog

Recommended by bloggers:
Python face recognition attendance punching system:
https://blog.csdn.net/alicema1111/article/details/133434445
Python fruit tree fruit recognition : https://blog.csdn.net/alicema1111/article/details/ 130862842
Python+Yolov8+Deepsort entrance traffic statistics: https://blog.csdn.net/alicema1111/article/details/130454430
Python+Qt face recognition access management system: https://blog.csdn.net/alicema1111/ article/details/130353433
Python+Qt fingerprint entry recognition attendance system: https://blog.csdn.net/alicema1111/article/details/129338432
Python Yolov5 flame smoke recognition source code sharing: https://blog.csdn.net/alicema1111 /article/details/128420453
Python+Yolov8 road bridge wall crack identification: https://blog.csdn.net/alicema1111/article/details/133434445

Supongo que te gusta

Origin blog.csdn.net/alicema1111/article/details/134466397
Recomendado
Clasificación