QT开发技术 [opencv加载onnx模型,dnn推理]

一、导出onnx 模型

yolo export model=xx\xx\best.pt format=onnx

二、qt加载onnx模型,推理显示

cpp 复制代码
 std::string fileName = QCoreApplication::applicationDirPath().toStdString() + "/Model/best.onnx";

 cv::dnn::Net net = cv::dnn::readNetFromONNX(fileName);
 if (net.empty()) {
     std::cerr << "Failed to load ONNX model. Check: " << std::endl
         << "1. File path: " << fileName << std::endl
         << "2. OpenCV version (require >= 4.5)" << std::endl
         << "3. ONNX opset compatibility" << std::endl;
     return;
 }

 cv::Mat image = cv::imread(QCoreApplication::applicationDirPath().toStdString() + "/Data/test3.jpg");
 if (image.empty()) {
     std::cerr << "Failed to load image" << std::endl;
     return;
 }

 // 预处理增强
 cv::Mat blob;
 try {
     bool swapRB = true;  // OpenCV默认BGR,YOLO需要RGB
     bool crop = false;
     cv::Scalar mean = cv::Scalar(0, 0, 0);
     double scale = 1.0 / 255.0;

     blob = cv::dnn::blobFromImage(image,
         scale,
         cv::Size(640, 640),
         mean,
         swapRB,
         crop,
         CV_32F);
 }
 catch (...) {
     std::cerr << "Blob creation failed" << std::endl;
     return;
 }

 // 设置计算后端(根据环境配置)
 net.setPreferableBackend(cv::dnn::DNN_BACKEND_CUDA);
 net.setPreferableTarget(cv::dnn::DNN_TARGET_CUDA);

 std::vector<std::string> outLayerNames = net.getUnconnectedOutLayersNames();
 std::vector<cv::Mat> predictions;

 try {
     net.setInput(blob);
     net.forward(predictions, outLayerNames);
 }
 catch (const cv::Exception& e) {
     std::cerr << "Forward pass failed: " << e.what() << std::endl;
     return;
 }


 // 后处理
 std::vector<int> classIds;
 std::vector<float> confidences;
 std::vector<cv::Rect> boxes;
 float x_factor = image.cols / 640.0;
 float y_factor = image.rows / 640.0;
 // YOLO 专用预处理参数
 float confThreshold = 0.25;
 float nmsThreshold = 0.45;

 for (const auto& pred : predictions) {
     for (int i = 0; i < pred.rows; ++i) {
         cv::Mat scores = pred.row(i).colRange(5, pred.cols);
         cv::Point classIdPoint;
         double confidence;
         cv::minMaxLoc(scores, 0, &confidence, 0, &classIdPoint);
         if (confidence > confThreshold) {
             int centerX = static_cast<int>(pred.at<float>(i, 0) * x_factor);
             int centerY = static_cast<int>(pred.at<float>(i, 1) * y_factor);
             int width = static_cast<int>(pred.at<float>(i, 2) * x_factor);
             int height = static_cast<int>(pred.at<float>(i, 3) * y_factor);
             int left = centerX - width / 2;
             int top = centerY - height / 2;

             classIds.push_back(classIdPoint.x);
             confidences.push_back(static_cast<float>(confidence));
             boxes.push_back(cv::Rect(left, top, width, height));
         }
     }
 }

 // 非极大值抑制
 std::vector<int> indices;
 cv::dnn::NMSBoxes(boxes, confidences, confThreshold, nmsThreshold, indices);

 // 在图像上绘制边界框和标签
 for (int idx : indices) {
     cv::Rect box = boxes[idx];
     cv::rectangle(image, box, cv::Scalar(0, 255, 0), 2);
     std::string label = cv::format("%.2f", confidences[idx]);
     cv::putText(image, label, cv::Point(box.x, box.y - 10), cv::FONT_HERSHEY_SIMPLEX, 0.5, cv::Scalar(0, 255, 0), 2);
 }

 // 在 ui->label_Map 上显示图像
 cv::cvtColor(image, image, cv::COLOR_BGR2RGB);
 QPixmap pixmap = QPixmap::fromImage(QImage(image.data, image.cols, image.rows, image.step, QImage::Format_RGB888));
 ui->label_Map->setPixmap(pixmap);
相关推荐
扶尔魔ocy19 小时前
【QT自定义2D控件】QGraphics绘制仪表盘
数据库·qt·microsoft
开心-开心急了20 小时前
关于Flutter与Qt for python 的一些技术、开源、商用等问题
开发语言·python·qt·flutter
友友马20 小时前
『 QT 』按钮类控件属性解析
开发语言·数据库·qt
QT 小鲜肉1 天前
【Git、GitHub、Gitee】按功能分类汇总Git常用命令详解(超详细)
c语言·网络·c++·git·qt·gitee·github
却道天凉_好个秋1 天前
OpenCV(十九):图像的加法运算
opencv·计算机视觉
老歌老听老掉牙1 天前
解决 PyQt5 中 sipPyTypeDict() 弃用警告的完整指南
python·qt
【赫兹威客】浩哥1 天前
基于 YOLO11+PyQt6+OpenCV 的智能水果检测系统设计与实现
人工智能·opencv·计算机视觉
AI technophile2 天前
OpenCV计算机视觉实战(29)——OpenCV DNN模块
opencv·计算机视觉·dnn
共享家95272 天前
Qt窗口教程(上)
开发语言·qt
俊俊谢2 天前
OpenCV环境配置(QT 6.6.1 MSVC2019 64bit + OpenCV – 4.12.0)
qt·opencv·msvc