• VS+Qt+C++ Yolov8物体识别窗体程序onnx模型


    程序示例精选
    VS+Qt+C++ Yolov8物体识别窗体程序onnx模型
    如需安装运行环境或远程调试,见文章底部个人QQ名片,由专业技术人员远程协助!

    前言

    这篇博客针对《VS+Qt+C++ Yolov8物体识别窗体程序onnx模型》编写代码,代码整洁,规则,易读。 学习与应用推荐首选。


    运行结果

    在这里插入图片描述


    文章目录

    一、所需工具软件
    二、使用步骤
           1. 主要代码
           2. 运行结果
    三、在线协助

    一、所需工具软件

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

    二、使用步骤

    代码如下(示例):
    
    #include
    #include "yolov8_onnx.h"
    #include
    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";
        }
    }
    
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185
    • 186
    • 187
    • 188
    • 189
    • 190
    • 191
    • 192
    • 193
    • 194
    • 195
    • 196
    • 197
    • 198
    • 199
    • 200
    • 201
    • 202
    • 203
    • 204
    • 205
    • 206
    • 207
    • 208
    • 209
    运行结果

    在这里插入图片描述

    三、在线协助:

    如需安装运行环境或远程调试,见文章底部个人 QQ 名片,由专业技术人员远程协助!

    1)远程安装运行环境,代码调试
    2)Visual Studio, Qt, C++, Python编程语言入门指导
    3)界面美化
    4)软件制作
    5)云服务器申请
    6)网站制作

    当前文章连接:https://blog.csdn.net/alicema1111/article/details/132666851
    个人博客主页https://blog.csdn.net/alicema1111?type=blog
    博主所有文章点这里:https://blog.csdn.net/alicema1111?type=blog

    博主推荐:
    Python人脸识别考勤打卡系统:
    https://blog.csdn.net/alicema1111/article/details/133434445
    Python果树水果识别https://blog.csdn.net/alicema1111/article/details/130862842
    Python+Yolov8+Deepsort入口人流量统计:https://blog.csdn.net/alicema1111/article/details/130454430
    Python+Qt人脸识别门禁管理系统:https://blog.csdn.net/alicema1111/article/details/130353433
    Python+Qt指纹录入识别考勤系统:https://blog.csdn.net/alicema1111/article/details/129338432
    Python Yolov5火焰烟雾识别源码分享:https://blog.csdn.net/alicema1111/article/details/128420453
    Python+Yolov8路面桥梁墙体裂缝识别:https://blog.csdn.net/alicema1111/article/details/133434445

  • 相关阅读:
    JS和TS的异同
    el-checkbox-group的全选与反选
    LeetCode 42. 接雨水 - PHP
    JavaScript排他思想小例子之按钮的点击效果
    【卫朋】产品管理:如何管理项目进度?
    猿创征文|Java之static关键字的应用【工具类、代码块和单例】
    01简单的CMakeLists.txt示例
    kong 和konga网关部署及使用
    Linux命令中,Ctrl+z、Ctrl+c和Ctrl+d这三个组合键讲解
    读书笔记之C Primer Plus 1
  • 原文地址:https://blog.csdn.net/alicema1111/article/details/134466397