YOLO V5 ONNX模型在C# 中部署

关于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()
{

}
}

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


}


}


}
}

测试效果:
image.png

  • 6
    点赞
  • 64
    收藏
    觉得还不错? 一键收藏
  • 15
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 15
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值