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

目录

首先你要定义v8的模型特征文件

下方是完整的模型编写函数

然后你要在控件窗体中定义应用这些方法以及函数

一、定义你的标签

二、下方是定义模块和坐标的方法

三、画框

完整推理函数代码块

效果展示

完整源码


训练其实和yolov10差不多 因为v10就是在v8的基础上做了优化

YoloV10 训练自己的数据集(推理,转化,C#部署)_c# yolo-CSDN博客

首先你要定义v8的模型特征文件

下方是完整的模型编写函数

复制代码
protected List<Prediction> ParseOutput(DenseTensor<float> output, Image image)
{
    DenseTensor<float> output2 = output;
    ConcurrentBag<Prediction> result = new ConcurrentBag<Prediction>();
    int width = image.Width;
    int height = image.Height;
    int w = width;
    int h = height;
    float num = (float)base.ModelInputWidth / (float)w;
    float num2 = (float)base.ModelInputHeight / (float)h;
    float val = num;
    float val2 = num2;
    float gain = Math.Min(val, val2);
    num = ((float)base.ModelInputWidth - (float)w * gain) / 2f;
    float num3 = ((float)base.ModelInputHeight - (float)h * gain) / 2f;
    float xPad = num;
    float yPad = num3;
    Parallel.For(0, output2.Dimensions[0], delegate (int i)
    {
        Parallel.For(0, (int)(output2.Length / output2.Dimensions[1]), delegate (int j)
        {
            float xMin = (output2[new int[3] { i, 0, j }] - output2[new int[3] { i, 2, j }] / 2f - xPad) / gain;
            float yMin = (output2[new int[3] { i, 1, j }] - output2[new int[3] { i, 3, j }] / 2f - yPad) / gain;
            float xMax = (output2[new int[3] { i, 0, j }] + output2[new int[3] { i, 2, j }] / 2f - xPad) / gain;
            float yMax = (output2[new int[3] { i, 1, j }] + output2[new int[3] { i, 3, j }] / 2f - yPad) / gain;
            xMin = Utils.Clamp(xMin, 0f, w);
            yMin = Utils.Clamp(yMin, 0f, h);
            xMax = Utils.Clamp(xMax, 0f, w - 1);
            yMax = Utils.Clamp(yMax, 0f, h - 1);
            Parallel.For(0, base.ModelOutputDimensions - 4, delegate (int l)
            {
                float num4 = output2[new int[3]
                {
                    i,
                    4 + l,
                    j
                }];
                if (!(num4 < base.Confidence))
                {
                    result.Add(new Prediction
                    {
                        Label = base.Labels[l],
                        Score = num4,
                        Rectangle = new RectangleF(xMin, yMin, xMax - xMin, yMax - yMin)
                    });
                }
            });
        });
    });
    return result.ToList();
} 

然后你要在控件窗体中定义应用这些方法以及函数

一、定义你的标签

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

二、下方是定义模块和坐标的方法

三、画框

完整推理函数代码块
复制代码
private void showimg(OpenFileDialog dialog)
{
    string imgpath = dialog.FileName;

    Image image = System.Drawing.Image.FromFile(imgpath);
    Form form = new Form();
    form.Text = dialog.SafeFileName + " " + image.Width + "x" + image.Height;

    PictureBox pictureBox = new PictureBox();
    pictureBox.Parent = form;
    //图像预测
    var predictions = yolov8.Predict(image);
    // Draw your boxes
    //using var graphics = Graphics.FromImage(image);
    foreach (var pred in predictions)
    {
        var originalImageHeight = image.Height;
        var originalImageWidth = image.Width;

        var x = Math.Max(pred.Rectangle.X, 0);
        var y = Math.Max(pred.Rectangle.Y, 0);
        Console.WriteLine(x);
        Console.WriteLine(y);
        var width = Math.Min(originalImageWidth - x, pred.Rectangle.Width);
        var height = Math.Min(originalImageHeight - y, pred.Rectangle.Height);

        // 框定义
        string text = $"{pred.Label.Name} [{pred.Score}]";

        using (Graphics graphics = Graphics.FromImage(image))
        {
            graphics.CompositingQuality = CompositingQuality.HighQuality;
            graphics.SmoothingMode = SmoothingMode.HighQuality;
            graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;

            // 
            Font drawFont = new Font("consolas", 11, FontStyle.Regular);
            SizeF size = graphics.MeasureString(text, drawFont);
            SolidBrush fontBrush = new SolidBrush(Color.Black);
            Point atPoint = new Point((int)x, (int)y - (int)size.Height - 1);

            // 
            Pen pen = new Pen(Color.Yellow, 2.0f);
            SolidBrush colorBrush = new SolidBrush(Color.Yellow);

            // 画框
            graphics.FillRectangle(colorBrush, (int)x, (int)(y - size.Height - 1), (int)size.Width, (int)size.Height);
            graphics.DrawString(text, drawFont, fontBrush, atPoint);

            // Draw bounding box on image
            graphics.DrawRectangle(pen, x, y, width, height);
        }
    }
    
    pictureBox.Width = image.Width;
    pictureBox.Height = image.Height;
    pictureBox1.Image = (Bitmap)image.Clone();


}
效果展示
完整源码

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

相关推荐
B站_计算机毕业设计之家1 小时前
python手写数字识别计分系统+CNN模型+YOLOv5模型 深度学习 计算机毕业设计(建议收藏)✅
python·深度学习·yolo·计算机视觉·数据分析·cnn
xuehaikj2 小时前
基于YOLOv8的汽车目标检测系统实现与优化_含多种车型识别与自动驾驶应用场景
yolo·目标检测·汽车
xuehaikj2 小时前
【水下目标检测】Yolov8-GDFPN实现水下气泡智能识别系统
yolo·目标检测·目标跟踪
带土115 小时前
2. YOLOv5 搭建一个完整的目标检测系统核心步骤
人工智能·yolo·目标检测
放羊郎19 小时前
人工智能算法优化YOLO的目标检测能力
人工智能·算法·yolo·视觉slam·建图
xuehaikj19 小时前
基于YOLOv5-AUX的棕熊目标检测与识别系统实现
人工智能·yolo·目标检测
一勺汤1 天前
YOLO12 改进、魔改|秩增强线性注意力RALA,通过增强 KV 缓冲与输出特征的矩阵秩,增强 YOLO 对小目标、复杂场景目标的识别能力
线性代数·yolo·矩阵·yolov12·yolo12·yolo12改进·小目标
深蓝海拓1 天前
YOLO v11的学习记录(五) 使用自定义数据从头训练一个实例分割的模型
学习·yolo
深度学习lover2 天前
<数据集>yolo航拍斑马线识别数据集<目标检测>
人工智能·深度学习·yolo·目标检测·计算机视觉·数据集·航拍斑马线识别
Sunhen_Qiletian2 天前
YOLOv2算法详解(上篇):从经典到进化的目标检测之路
算法·yolo·目标检测