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

        }

三、实际的效果

在这里插入图片描述

评论 58
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值