Yolov8训练方式以及C#中读取yolov8+onnx模子举行目标检测.NET 6.0

[复制链接]
发表于 2025-11-13 00:35:57 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

×
目次
起首你要界说v8的模子特性文件
下方是完备的模子编写函数
 然后你要在控件窗体中界说应用这些方法以及函数
一、界说你的标签
 二、下方是界说模块和坐标的方法
三、画框
完备推理函数代码块
结果展示
 完备源码


训练着实和yolov10差不多 由于v10就是在v8的根本上做了优化
YoloV10 训练自己的数据集(推理,转化,C#摆设)_c# yolo-CSDN博客
起首你要界说v8的模子特性文件


下方是完备的模子编写函数

  1. protected List<Prediction> ParseOutput(DenseTensor<float> output, Image image)
  2. {
  3.     DenseTensor<float> output2 = output;
  4.     ConcurrentBag<Prediction> result = new ConcurrentBag<Prediction>();
  5.     int width = image.Width;
  6.     int height = image.Height;
  7.     int w = width;
  8.     int h = height;
  9.     float num = (float)base.ModelInputWidth / (float)w;
  10.     float num2 = (float)base.ModelInputHeight / (float)h;
  11.     float val = num;
  12.     float val2 = num2;
  13.     float gain = Math.Min(val, val2);
  14.     num = ((float)base.ModelInputWidth - (float)w * gain) / 2f;
  15.     float num3 = ((float)base.ModelInputHeight - (float)h * gain) / 2f;
  16.     float xPad = num;
  17.     float yPad = num3;
  18.     Parallel.For(0, output2.Dimensions[0], delegate (int i)
  19.     {
  20.         Parallel.For(0, (int)(output2.Length / output2.Dimensions[1]), delegate (int j)
  21.         {
  22.             float xMin = (output2[new int[3] { i, 0, j }] - output2[new int[3] { i, 2, j }] / 2f - xPad) / gain;
  23.             float yMin = (output2[new int[3] { i, 1, j }] - output2[new int[3] { i, 3, j }] / 2f - yPad) / gain;
  24.             float xMax = (output2[new int[3] { i, 0, j }] + output2[new int[3] { i, 2, j }] / 2f - xPad) / gain;
  25.             float yMax = (output2[new int[3] { i, 1, j }] + output2[new int[3] { i, 3, j }] / 2f - yPad) / gain;
  26.             xMin = Utils.Clamp(xMin, 0f, w);
  27.             yMin = Utils.Clamp(yMin, 0f, h);
  28.             xMax = Utils.Clamp(xMax, 0f, w - 1);
  29.             yMax = Utils.Clamp(yMax, 0f, h - 1);
  30.             Parallel.For(0, base.ModelOutputDimensions - 4, delegate (int l)
  31.             {
  32.                 float num4 = output2[new int[3]
  33.                 {
  34.                     i,
  35.                     4 + l,
  36.                     j
  37.                 }];
  38.                 if (!(num4 < base.Confidence))
  39.                 {
  40.                     result.Add(new Prediction
  41.                     {
  42.                         Label = base.Labels[l],
  43.                         Score = num4,
  44.                         Rectangle = new RectangleF(xMin, yMin, xMax - xMin, yMax - yMin)
  45.                     });
  46.                 }
  47.             });
  48.         });
  49.     });
  50.     return result.ToList();
  51. }
复制代码
 然后你要在控件窗体中界说应用这些方法以及函数

一、界说你的标签

标签你也可以将其写成txt文件再用ReadAllText方法举行读取也是可以的

 二、下方是界说模块和坐标的方法


三、画框


完备推理函数代码块

  1. private void showimg(OpenFileDialog dialog)
  2. {
  3.     string imgpath = dialog.FileName;
  4.     Image image = System.Drawing.Image.FromFile(imgpath);
  5.     Form form = new Form();
  6.     form.Text = dialog.SafeFileName + " " + image.Width + "x" + image.Height;
  7.     PictureBox pictureBox = new PictureBox();
  8.     pictureBox.Parent = form;
  9.     //图像预测
  10.     var predictions = yolov8.Predict(image);
  11.     // Draw your boxes
  12.     //using var graphics = Graphics.FromImage(image);
  13.     foreach (var pred in predictions)
  14.     {
  15.         var originalImageHeight = image.Height;
  16.         var originalImageWidth = image.Width;
  17.         var x = Math.Max(pred.Rectangle.X, 0);
  18.         var y = Math.Max(pred.Rectangle.Y, 0);
  19.         Console.WriteLine(x);
  20.         Console.WriteLine(y);
  21.         var width = Math.Min(originalImageWidth - x, pred.Rectangle.Width);
  22.         var height = Math.Min(originalImageHeight - y, pred.Rectangle.Height);
  23.         // 框定义
  24.         string text = $"{pred.Label.Name} [{pred.Score}]";
  25.         using (Graphics graphics = Graphics.FromImage(image))
  26.         {
  27.             graphics.CompositingQuality = CompositingQuality.HighQuality;
  28.             graphics.SmoothingMode = SmoothingMode.HighQuality;
  29.             graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
  30.             //
  31.             Font drawFont = new Font("consolas", 11, FontStyle.Regular);
  32.             SizeF size = graphics.MeasureString(text, drawFont);
  33.             SolidBrush fontBrush = new SolidBrush(Color.Black);
  34.             Point atPoint = new Point((int)x, (int)y - (int)size.Height - 1);
  35.             //
  36.             Pen pen = new Pen(Color.Yellow, 2.0f);
  37.             SolidBrush colorBrush = new SolidBrush(Color.Yellow);
  38.             // 画框
  39.             graphics.FillRectangle(colorBrush, (int)x, (int)(y - size.Height - 1), (int)size.Width, (int)size.Height);
  40.             graphics.DrawString(text, drawFont, fontBrush, atPoint);
  41.             // Draw bounding box on image
  42.             graphics.DrawRectangle(pen, x, y, width, height);
  43.         }
  44.     }
  45.    
  46.     pictureBox.Width = image.Width;
  47.     pictureBox.Height = image.Height;
  48.     pictureBox1.Image = (Bitmap)image.Clone();
  49. }
复制代码
结果展示


 完备源码

C#+Yolov8+onnxnet6.0目标检测推理资源-CSDN文库
回复

使用道具 举报

登录后关闭弹窗

登录参与点评抽奖  加入IT实名职场社区
去登录
快速回复 返回顶部 返回列表