2010年4月江苏省03340网站建设与管理答案,微信公众网站怎么做的,纵横天下网站建设,快手小程序入口本文介绍如何在 ML.NET 中使用 YOLOv7 的 ONNX 模型来检测图像中的对象。什么是 YOLOYOLO#xff08;You Only Look Once#xff09;是一种先进的实时目标检测系统。它是一个在COCO数据集上预训练的物体检测架构和模型系列#xff0c;其版本也是在不断优化更新。2022年7月You Only Look Once是一种先进的实时目标检测系统。它是一个在COCO数据集上预训练的物体检测架构和模型系列其版本也是在不断优化更新。2022年7月YOLOv7 来临。官方版的YOLOv7相同体量下比YOLOv5 精度更高速度更快。论文地址https://arxiv.org/abs/2207.02696Yolov7ONNX 模型开放神经网络交换 (ONNX) 是 AI 模型的开放源代码格式。ONNX 支持框架之间的互操作性常见的机器学习框架都支持该模型的使用。YOLOv7 的模型我们可以从一作 Chien-Yao Wang 的仓库获取https://github.com/WongKinYiu/yolov7。在 Releases v0.1 中提供的 onnx 不能直接使用我们需要下载预训练的 yolov7.pt 然后克隆项目使用导出工具自行导出 onnx 模型。python export.py --weightsyolov7.pt --grid --simplify导出完成我们就可以得到 yolov7.onnx 你也可以直接前往 CSDN 下载我分享的文件[1]。执行预测1.首先创建控制台应用程序选择 .NET 6 作为要使用的框架。2.安装 Microsoft.ML.OnnxTransformer NuGet 包3.YOLOv7 整体结构与 YOLOv5 极其相似我们可以直接使用 Yolov5Net NuGet 包里的分析器来处理模型输出。剩下的工作我们只需要编写执行预测的代码即可static readonly string assetsPath GetAbsolutePath(../../../assets);
static readonly string modelFilePath Path.Combine(assetsPath, Model, yolov7.onnx);
static readonly string imagesFolder Path.Combine(assetsPath, images);
static readonly string outputFolder Path.Combine(assetsPath, images, output);private static void Main(string[] args)
{if (!Directory.Exists(outputFolder)){Directory.CreateDirectory(outputFolder);}var imgs Directory.GetFiles(imagesFolder).Where(filePath Path.GetExtension(filePath) .jpg);using var scorer new YoloScorerYoloCocoP5Model(modelFilePath);foreach (var imgsFile in imgs){using var image Image.FromFile(imgsFile);ListYoloPrediction predictions scorer.Predict(image);using var graphics Graphics.FromImage(image);foreach (var prediction in predictions){double score Math.Round(prediction.Score, 2);graphics.DrawRectangles(new Pen(prediction.Label.Color, 3),new[] { prediction.Rectangle });var (x, y) (prediction.Rectangle.X - 3, prediction.Rectangle.Y - 23);graphics.DrawString(${prediction.Label.Name} ({score}),new Font(Consolas, 16, GraphicsUnit.Pixel), new SolidBrush(prediction.Label.Color),new PointF(x, y));}image.Save(Path.Combine(outputFolder, $result{DateTime.Now.Ticks}.jpg));}
}/// summary
/// 获取程序启动目录
/// /summary
/// param namerelativePath之下的某个路径/param
/// returns/returns
private static string GetAbsolutePath(string relativePath)
{FileInfo _dataRoot new FileInfo(typeof(Program).Assembly.Location);string assemblyFolderPath _dataRoot.Directory!.FullName;string fullPath Path.Combine(assemblyFolderPath, relativePath);return fullPath;
}完整的代码样例见https://github.com/sangyuxiaowu/ml_yolov7编写完成执行然后我们就可以在 assets/images/output 目录看到样例图片的预测结果预测结果示例和参考微软官方提供了 在 ML.NET 中使用 ONNX 检测对象[2] 的更详细的教程包含训练和预测感兴趣的同学可前往查阅。References[1] CSDN 下载我分享的文件: https://download.csdn.net/download/marin1993/86912472[2] 在 ML.NET 中使用 ONNX 检测对象: https://learn.microsoft.com/zh-cn/dotnet/machine-learning/tutorials/object-detection-onnx