YOLOV5 使用 ML.NET ONNX 在C#部署
前言
参考[https://github.com/mentalstack/yolov5-net](https://github.com/mentalstack/yolov5-net)的实现一、生成dll调用库
1.安装.NET core SDK
.NET Core 3.1 SDK (v3.1.416) - Windows x64
2.Nuget安装 ML ONNX Running Time
3.生成Dll库文件
二、winform 程序调用dll
1.新建winform 程序
2.安装Nuget 包
3.下载官网的onnx模型文件
4.添加引用
5. 程序修改
5.1 模型初始化
scorer = new YoloScorer<YoloCocoP5Model>("Assets/Weights/yolov5s.onnx");
5.2 模型推断
private void button1_Click(object sender, EventArgs e)
{
var image = Image.FromFile(@"Assets/test3.jpg");
DateTime T_start = DateTime.Now;
List<YoloPrediction> predictions = scorer.Predict(image);
var graphics = Graphics.FromImage(image);
foreach (var prediction in predictions) // iterate predictions to draw results
{
double score = Math.Round(prediction.Score, 2);
graphics.DrawRectangles(new Pen(prediction.Label.Color, 1),
new[] { prediction.Rectangle });
var (x, y) = (prediction.Rectangle.X - 3, prediction.Rectangle.Y - 23);
graphics.DrawString($"{prediction.Label.Name} ({score})",
new Font("Arial", 16, GraphicsUnit.Pixel), new SolidBrush(prediction.Label.Color),
new PointF(x, y));
}
image.Save("Assets/result.jpg");
this.pictureBox1.Image = image;
DateTime T_end = DateTime.Now;
TimeSpan T_esp = T_end - T_start;
this.label1.Text = T_esp.TotalMilliseconds.ToString();
}