当前位置: 首页 > news >正文

在C#上运行YOLOv11模型---CPU版

一. 模型导出

二. 环境搭建

三. 代码程序

参考链接:https://blog.csdn.net/qq_41375318/article/details/142747415


1. 模型导出

参考链接:https://docs.ultralytics.com/zh/modes/export/#cli

将训练完成的YOLO模型导出成ONNX格式,代码如下:

from ultralytics import YOLO # Load a model model = YOLO("yolo11n.pt") # load an official model model = YOLO("path/to/best.pt") # load a custom-trained model # Export the model model.export(format="onnx")

成功导出的onnx模型储存在yolo模型的同级目录下


2. 环境搭建

主要包括C#中的Nuget包下载,其中需要的DLL包括4个:

  • Microsoft.ML.OnnxRuntime
  • Microsoft.ML.OnnxRuntime.Managed
  • OpenCvSharp4
  • OpenCvSharp4.runtime.win

3. 代码程序

3.1 检测结果类

public class DetectionResult { public DetectionResult(int ClassId, string Class, Rect Rect, float Confidence) { this.ClassId = ClassId; this.Confidence = Confidence; this.Rect = Rect; this.Class = Class; } public string Class { get; set; } public int ClassId { get; set; } public float Confidence { get; set; } public Rect Rect { get; set; } }

3.2 变量和Tanspose函数

string fileFilter = "*.*|*.bmp;*.jpg;*.jpeg;*.tiff;*.tiff;*.png"; string image_path = ""; string model_path; string classer_path; public string[] class_names; public int class_num; DateTime dt1 = DateTime.Now; DateTime dt2 = DateTime.Now; int input_height; int input_width; float ratio_height; float ratio_width; InferenceSession onnx_session; int box_num; float conf_threshold; float nms_threshold; public unsafe float[] Transpose(float[] tensorData, int rows, int cols) { float[] transposedTensorData = new float[tensorData.Length]; fixed (float* pTensorData = tensorData) { fixed (float* pTransposedData = transposedTensorData) { for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { int index = i * cols + j; int transposedIndex = j * rows + i; pTransposedData[transposedIndex] = pTensorData[index]; } } } } return transposedTensorData; }

3.2 加载模型与label.txt

private void Form1_Load(object sender, EventArgs e) { model_path =@"...\...\model\yolo11n.onnx"; //创建输出会话,用于输出模型读取信息 SessionOptions options = new SessionOptions(); options.LogSeverityLevel = OrtLoggingLevel.ORT_LOGGING_LEVEL_INFO; options.AppendExecutionProvider_CPU(0);// 设置为CPU上运行 // 创建推理模型类,读取模型文件 onnx_session = new InferenceSession(model_path, options);//model_path 为onnx模型文件的路径 input_height = 640; input_width = 640; box_num = 8400; conf_threshold = 0.25f; nms_threshold = 0.5f; classer_path = @"...\...\model\label.txt"; class_names = File.ReadAllLines(classer_path, Encoding.UTF8); class_num = class_names.Length; // 图片路径 image_path = @"...\...\image1.jpg"; pictureBox1.Image = new Bitmap(image_path); }

3.3 开始检测推理

private void button2_Click(object sender, EventArgs e) { if (image_path == "") { return; } button2.Enabled = false; pictureBox2.Image = null; textBox1.Text = ""; Application.DoEvents(); Mat image = new Mat(image_path); //图片缩放 int height = image.Rows; int width = image.Cols; Mat temp_image = image.Clone(); if (height > input_height || width > input_width) { float scale = Math.Min((float)input_height / height, (float)input_width / width); OpenCvSharp.Size new_size = new OpenCvSharp.Size((int)(width * scale), (int)(height * scale)); Cv2.Resize(image, temp_image, new_size); } ratio_height = (float)height / temp_image.Rows; ratio_width = (float)width / temp_image.Cols; Mat input_img = new Mat(); Cv2.CopyMakeBorder(temp_image, input_img, 0, input_height - temp_image.Rows, 0, input_width - temp_image.Cols, BorderTypes.Constant, 0); //Cv2.ImShow("input_img", input_img); //输入Tensor Tensor<float> input_tensor = new DenseTensor<float>(new[] { 1, 3, 640, 640 }); for (int y = 0; y < input_img.Height; y++) { for (int x = 0; x < input_img.Width; x++) { input_tensor[0, 0, y, x] = input_img.At<Vec3b>(y, x)[0] / 255f; input_tensor[0, 1, y, x] = input_img.At<Vec3b>(y, x)[1] / 255f; input_tensor[0, 2, y, x] = input_img.At<Vec3b>(y, x)[2] / 255f; } } List<NamedOnnxValue> input_container = new List<NamedOnnxValue> { NamedOnnxValue.CreateFromTensor("images", input_tensor) }; //推理 dt1 = DateTime.Now; var ort_outputs = onnx_session.Run(input_container).ToArray(); dt2 = DateTime.Now; float[] data = Transpose(ort_outputs[0].AsTensor<float>().ToArray(), 4 + class_num, box_num); float[] confidenceInfo = new float[class_num]; float[] rectData = new float[4]; List<DetectionResult> detResults = new List<DetectionResult>(); for (int i = 0; i < box_num; i++) { Array.Copy(data, i * (class_num + 4), rectData, 0, 4); Array.Copy(data, i * (class_num + 4) + 4, confidenceInfo, 0, class_num); float score = confidenceInfo.Max(); // 获取最大值 int maxIndex = Array.IndexOf(confidenceInfo, score); // 获取最大值的位置 int _centerX = (int)(rectData[0] * ratio_width); int _centerY = (int)(rectData[1] * ratio_height); int _width = (int)(rectData[2] * ratio_width); int _height = (int)(rectData[3] * ratio_height); detResults.Add(new DetectionResult( maxIndex, class_names[maxIndex], new Rect(_centerX - _width / 2, _centerY - _height / 2, _width, _height), score)); } //NMS CvDnn.NMSBoxes(detResults.Select(x => x.Rect), detResults.Select(x => x.Confidence), conf_threshold, nms_threshold, out int[] indices); detResults = detResults.Where((x, index) => indices.Contains(index)).ToList(); //绘制结果 Mat result_image = image.Clone(); foreach (DetectionResult r in detResults) { Cv2.PutText(result_image, $"{r.Class}:{r.Confidence:P0}", new OpenCvSharp.Point(r.Rect.TopLeft.X, r.Rect.TopLeft.Y - 10), HersheyFonts.HersheySimplex, 1, Scalar.Red, 2); Cv2.Rectangle(result_image, r.Rect, Scalar.Red, thickness: 2); } pictureBox2.Image = new Bitmap(result_image.ToMemoryStream()); textBox1.Text = "推理耗时:" + (dt2 - dt1).TotalMilliseconds + "ms"; button2.Enabled = true; }

注意:设置picture1picture2SizeMode属性为Zoom

http://www.cnnetsun.cn/news/89941.html

相关文章:

  • 关于uniapp vue2 canvas重绘元素节点时,提示cos of null相关异常警告,导致js线程崩溃,vue响应式丢失的问题
  • 【微服务稳定性提升利器】:基于Dify与Spring AI的异常熔断与恢复策略
  • concurrent hashmap原理,扩容,扩容时怎么保证线程安全?
  • 空间转录组降维必杀技:5步用R语言完成PCA、t-SNE与UMAP优化
  • 【R语言与量子计算加速新突破】:GPU如何将量子模拟效率提升10倍?
  • AWS专家Greg Coquillo提出的 6种LLM ORCHESTRATION PATTERNS解析
  • “.商标”不等同于商标权:企业做知识产权保护,别把“后缀名”当“确权证”
  • 面向削峰填谷的电动汽车多目标优化调度策略
  • 如何在30分钟内完成Dify与Spring AI的无缝部署?资深架构师亲授秘诀
  • 【Vue知识点总结】Vue中的namespaced命名空间详解
  • 告别单一生态限制,构建R-Python一体化可视化工作流
  • 论基于REST服务的Web应用系统设计
  • R语言在气象数据分析中的应用(相关性建模全攻略)
  • 揭秘Docker Compose中的Agent健康检测机制:如何避免服务假死?
  • Python期末复习:30个核心知识点完全详解
  • 大模型训练数据全攻略:从数据处理到高质量数据集构建(建议收藏)
  • 企业级容器安全迫在眉睫,Docker Scout如何实现小时级响应?
  • 12th Live2D Creative Awards(2025)获奖名单公布!
  • 【稀缺资料】:Dify重排序系统调优的3个黄金法则与实测数据验证
  • 【混合检索的Dify查询优化秘籍】:揭秘提升查询效率5倍的核心策略
  • 告别 “自动化孤岛”,解锁实验室真正智能
  • Dify版本历史管理的秘密武器:实现安全、可控、可追溯的回滚体系
  • 13.长视频和短视频的目标追踪(yolo_insightface模型)
  • 前端开发必备:JavaScript 核心事件详解与实战
  • 为什么你的服务总崩溃?:Docker MCP 网关负载均衡未正确配置的3大隐患
  • 专利检索漏查1个参数,千万研发卡壳量产线
  • 自动化测试团队效率提升指南
  • LobeChat能否通过等保测评?国内合规性达标
  • paperzz 降重 / 降 AIGC:从重复率超标到学术合规,高校生论文 “隐形风险” 的解决逻辑
  • paperzz AI 期刊论文功能实测:从 “标题输入” 到 “期刊适配提纲”,学术写作如何少走格式与逻辑的弯路?