您的位置:首页 > 教育 > 锐评 > 大连网站建设制作公司_营业执照怎么年审_微信指数官网_网红推广一般怎么收费

大连网站建设制作公司_营业执照怎么年审_微信指数官网_网红推广一般怎么收费

2025/2/23 3:52:50 来源:https://blog.csdn.net/weixin_45397344/article/details/145772636  浏览:    关键词:大连网站建设制作公司_营业执照怎么年审_微信指数官网_网红推广一般怎么收费
大连网站建设制作公司_营业执照怎么年审_微信指数官网_网红推广一般怎么收费

一、导出onnx 模型

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

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

 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需要RGBbool 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);

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com