当前位置:   article > 正文

YOLOV5 使用 ML.NET ONNX 在C#部署_.net yolov5

.net yolov5


前言

参考[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");
  • 1

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();

        }
  • 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

三、实际的效果

在这里插入图片描述

本文内容由网友自发贡献,转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/311706?site
推荐阅读
相关标签
  

闽ICP备14008679号