2026/5/21 14:23:47
网站建设
项目流程
自己做网站推广在那个网站,广州公司注册官网,外贸行业建站,营销型网站建设品牌如果只需要opencv去部署yolov11分类模型可以参考我其他博文#xff0c;本文和 opencv去部署yolov11分类模型区别是#xff1a;opencv部署推理核心使用opencv自带api#xff0c;而本文推理核心用的onnxruntime#xff0c;opencv只是辅助作用即读取处理图片【算法介绍】ONNX …如果只需要opencv去部署yolov11分类模型可以参考我其他博文本文和 opencv去部署yolov11分类模型区别是opencv部署推理核心使用opencv自带api而本文推理核心用的onnxruntimeopencv只是辅助作用即读取处理图片【算法介绍】ONNX Runtime是微软推出的一款高性能的机器学习推理引擎框架专注于加速机器学习模型的预测阶段。它支持多种运行后端包括CPU、GPU等使得开发者可以灵活选择最适合其应用场景的硬件平台。使用C和ONNX Runtime部署YOLOv11-CLS图像分类ONNX模型涉及到以下几个关键步骤环境配置首先需要安装ONNX Runtime库可以通过从ONNX Runtime的GitHub存储库中下载预编译的二进制文件来安装或者通过源代码进行构建。同时还需要安装OpenCV等图像处理库以便对输入图像进行预处理。模型加载加载YOLOv11-CLS的ONNX模型文件通常涉及到指定模型的路径并创建一个InferenceSession对象该对象将用于后续的推理。数据预处理使用OpenCV等库对输入图像进行预处理包括调整图像大小、归一化像素值等以满足模型输入的要求。模型推理将预处理后的数据传递给InferenceSession对象并调用其Run方法来执行推理。这将返回模型的输出通常是一个包含分类结果的张量。结果处理解析模型的输出提取有用的信息如分类标签和置信度并根据需要进行进一步的处理或可视化。通过以上步骤可以在C中使用ONNX Runtime成功部署YOLO26CLS图像分类模型实现高效的图像分类任务。【效果展示】【调用代码】#pragma once #include iostream #include opencv2/core.hpp #include fstream #include inference.h #include chrono using namespace std; int main(int argc, char *argv[]) { if (argc 1) { std::cout Usage: main.exe image_path std::endl; return 0; } DL_INIT_PARAM params; params.labelPath class_names.txt; params.modelPath yolo26n-cls.onnx; params.modelType YOLO_CLS_26; params.imgSize {224, 224}; params.rectConfidenceThreshold 0.4; params.iouThreshold 0.0001; params.cudaEnable false; auto starttime_1 std::chrono::high_resolution_clock::now(); std::unique_ptrYOLO_26 yolo(new YOLO_26); yolo-CreateSession(params); auto starttime_3 std::chrono::high_resolution_clock::now(); auto duration_ms4 std::chrono::duration_caststd::chrono::milliseconds(starttime_3 - starttime_1).count(); std::cout [YOLO_26]: warm up: duration_ms4 ms std::endl; std::string imagepath argv[1]; cv::Mat image cv::imread(imagepath); auto starttime_2 std::chrono::high_resolution_clock::now(); auto results yolo-Inference(image); auto starttime_4 std::chrono::high_resolution_clock::now(); auto duration_ms3 std::chrono::duration_caststd::chrono::milliseconds(starttime_4 - starttime_2).count(); std::cout [YOLO_26]: inference time: duration_ms3 ms std::endl; for (const auto result : results) { std::cout [YOLO_26]: label is: result.className , confidence is: result.confidence std::endl; std::string text result.className std::to_string(result.confidence).substr(0, 4); cv::putText(image, text, cv::Point(10, 30), cv::FONT_HERSHEY_SIMPLEX, 1, cv::Scalar(0, 255, 0), 2); } return 0; }【测试环境】vs2019cmake3.30.1opencv4.8.0onnxruntime1.16.3【运行步骤】通过cmake编译出exe后执行yolo26-cls.exe 【图片路径】即可