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<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";
    }
}



运行结果

在这里插入图片描述

三、在线协助:

如需安装运行环境或远程调试,见文章底部个人 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

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
PyCharm是一种很受欢迎的Python集成开发环境(IDE),它提供了许多强大的功能,方便开发者编写和调试Python代码。通过PyCharm,我们可以创建、编辑和运行Python项目,还可以使用其强大的调试工具帮助我们查找和修复代码错误。 而Qt5是一个流行的跨平台应用程序开发框架,它用于开发图形用户界面(GUI)和嵌入式系统。Qt提供了丰富的GUI控件和功能,使得开发者能够创建漂亮且功能丰富的应用程序Qt的主要语言C++,但它也提供了Python绑定,可以在Python中使用Qt开发应用程序YOLOv8是一个有名的实时目标检测算法,它是YOLO(You Only Look Once)系列算法的最新版本。YOLOv8使用深度学习模型,可以在图像或视频中实时检测多个目标,并输出它们的位置和类别。这使得它在很多应用领域都有广泛的应用,如智能安防、自动驾驶等。 结合起来,如果你想用PyCharm和Qt5来开发一个基于YOLOv8的应用程序,你可以使用PyCharm作为开发环境,来创建和编辑你的Python代码。你可以使用Qt5来创建应用程序的界面,并集成YOLOv8模型来实现目标检测功能。PyCharm的调试工具可以帮助你调试和优化你的代码,而Qt的GUI控件和功能可以让你的应用程序具有更好的用户界面和交互性。 使用PyCharm和Qt5开发YOLOv8应用程序可以帮助你更高效地进行开发,并且能够快速迭代和测试你的代码。这样你就可以更好地利用YOLOv8算法,并将其应用于实际生产环境中。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

荷塘月色2

您的鼓励将是我创作最大的动力

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

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

打赏作者

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

抵扣说明:

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

余额充值