当前位置:   article > 正文

YOLO V5 ONNX模型在C# 中部署_c#调用yolo 自定义训练集

c#调用yolo 自定义训练集

关于Yolo5训练自定义数据集,请参考此教程:YOLO5训练自定义数据集

完整项目(VS2022)
链接:https://pan.baidu.com/s/17jQUx-Dp0YYC4YWzYLs0AA?pwd=yzj9
提取码:yzj9

一 YOLO 导出ONNX模型

在export.py 中修改参数(如下图),运行导出ONNX
image.pngimage.png

二 Yolov5Net.Scorer.dll文件设置及编译

C# ONNX模型推理dll库:
https://github.com/mentalstack/yolov5-net
根据模型修改model文件后,再编译dll,在项目中引用此dll
image.png

public class YoloCocoP5Model : YoloModel
{
//输入图像大小
public override int Width { get; set; } = 640;
public override int Height { get; set; } = 640;
public override int Depth { get; set; } = 3;

/// <summary>
/// 对象种类数 注意:此值=对象实际种类数+5
/// </summary>
public override int Dimensions { get; set; } = 6;

public override int[] Strides { get; set; } = new int[] { 8, 16, 32 };

public override int[][][] Anchors { get; set; } = new int[][][]
{
new int[][] { new int[] { 010, 13 }, new int[] { 016, 030 }, new int[] { 033, 023 } },
new int[][] { new int[] { 030, 61 }, new int[] { 062, 045 }, new int[] { 059, 119 } },
new int[][] { new int[] { 116, 90 }, new int[] { 156, 198 }, new int[] { 373, 326 } }
};

public override int[] Shapes { get; set; } = new int[] { 80, 40, 20 };

//阈值设定,大于此阈值才识别
public override float Confidence { get; set; } = 0.40f;
public override float MulConfidence { get; set; } = 0.25f;
public override float Overlap { get; set; } = 0.45f;


//ONNX模型的输出变量名称,可在https://netron.app/中查看
public override string[] Outputs { get; set; } = new[] { "output0" };

public override List<YoloLabel> Labels { get; set; } = new List<YoloLabel>()
{

//模型中的对象的种类信息
new YoloLabel { Id = 0, Name = "Stop-Sign" },



};

public override bool UseDetect { get; set; } = true;

public YoloCocoP5Model()
{

}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49

三 C# 中模型进行推理

C# 项目编译环境为>=.NET5,本案例使用.NET6
需要安装以下库:
Microsoft.ML.OnnxRuntime (ONNX运行时)
OpenCvSharp4.Extensions (输出图像框定对象使用)
OpenCvSharp4.Windows (输出图像框定对象使用)
同时需要引用上一步编译的:Yolov5Net.Scorer.dll
image.png

测试代码:

using Yolov5Net.Scorer.Models;
using Yolov5Net.Scorer;
using OpenCvSharp;
using System.Windows.Forms;
using Point = OpenCvSharp.Point;

namespace YOLO_WinformDemo
{
    /// <summary>
    /// 此项目使用.NET6编译
    /// </summary>


    public partial class Form1 : Form
{
YoloScorer<YoloCocoP5Model> scorer;

string filePath = "";
public Form1()
{
InitializeComponent();

//加载模型地址
scorer = new YoloScorer<YoloCocoP5Model>(Application.StartupPath+ "\\onnx\\best.onnx");

}

private void btn_selectFile_Click(object sender, EventArgs e)
{
OpenFileDialog openFile=new OpenFileDialog();
if(openFile.ShowDialog() == DialogResult.OK)
{
filePath = openFile.FileName;
using var image = Image.FromFile(openFile.FileName);

picbox_Display.BackgroundImage = AddInfoToImage(image);


}


}



/// <summary>
/// 使用Opensharp4给识别对象加框和标注
/// </summary>
/// <param name="inputImage"></param>
/// <returns></returns>
public Image AddInfoToImage(Image inputImage)
{


List<YoloPrediction> predictions = scorer.Predict(inputImage);

if (predictions.Count == 0) {

return inputImage;

}
else
{

Mat inputMat = Cv2.ImRead(filePath, ImreadModes.Color);

foreach (YoloPrediction prediction in predictions)
{


Point p1 = new Point(prediction.Rectangle.X, prediction.Rectangle.Y);//矩形左上顶点

Point p2 = new Point(prediction.Rectangle.X+ prediction.Rectangle.Width, prediction.Rectangle.Y + prediction.Rectangle.Height);//矩形右下顶点

Point p3 = new Point(prediction.Rectangle.X, prediction.Rectangle.Y - 60);
Scalar scalar = new Scalar(0, 0, 255);
Cv2.Rectangle(inputMat, p1, p2, scalar, 7);

Cv2.PutText(inputMat, prediction.Label.Name+" "+ Math.Round(prediction.Score,2), p3, HersheyFonts.HersheyDuplex, 2, scalar, 4);

}


return OpenCvSharp.Extensions.BitmapConverter.ToBitmap(inputMat);


}


}


}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94

测试效果:
image.png

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Cpp五条/article/detail/191411
推荐阅读
相关标签
  

闽ICP备14008679号