C#实现控制台传参调用YoloV5模型进行人体识别
1.详细Anaconda+Pycharm配置YoloV5环境笔记2.Pytorch学习笔记-(二)搭建Pytorch环境(Pytorch2.1+CUDA12.1+Anaconda3_2023+Pycharm2023)3.yolov5-OSError: [WinError 1455] 页面文件太小,无法完成操作。 Error loading "C:\Anaconda3\lib\site-packages\torch\lib\caffe2_detectron_ops_gpu.dll"4.YoloV5_RuntimeError: CUDA out of memory. Tried to allocate 100.00 MiB (GPU 0; 2.00 GiB total capacity; 1.15 GiB already allocated; 0 bytes free; 1.19 GiB reserved in total by PyTorch)5.C#调用YoloV5与YoloV8示例
6.C#实现控制台传参调用YoloV5模型进行人体识别
7.ML.NET-API(一)_常用代码片段8.ML.NET-模型生成器工具(二)-对象检测教程一、项目地址
https://gitee.com/qq28069933146_admin/yoloconsole_body
二、代码解析
1、获取输入参数
//static void Main(string[] args)方法中运行如下代码:
string toBeTestedImgPathStr = AppContext.BaseDirectory + "\\test.jpg";
if (args.Length > 0)
{
toBeTestedImgPathStr = args[0];
}
2、C#加载YoloV5的onnx模型
YoloScorer<YoloCocoP5Model> scorer = new YoloScorer<YoloCocoP5Model>(modelPath); // 选择模型
3、使用YoloV5模型进行预测
List<YoloPrediction> predictions = scorer.Predict(image); // 预测
4、自定义人体信息类
该类用于在控制台打印人体位置信息
/// <summary>
/// 人体信息
/// </summary>
public class BodyInfo
{
public int Left { get; set; } = 0;
public int Top { get; set; } = 0;
public int Right { get; set; } = 0;
public int Bottom { get; set; } = 0;
/// <summary>
/// 概率
/// </summary>
public double Score { get; set; } = 0;
public BodyInfo(int left, int top, int right, int bottom, double score)
{
Left = left;
Top = top;
Right = right;
Bottom = bottom;
Score = score;
}
}
5、用BodyInfo保存人体信息并在图片上标注人体位置
// 保存人体位置信息
List<BodyInfo> bodyInfos = new List<BodyInfo>();
// 标注图片
foreach (YoloPrediction prediction in predictions)
{
if (prediction.Label.Name == "person") // prediction.Label.Id == "1"
{
double score = Math.Round(prediction.Score, 2); // 概率(保留两位小数)
var (x, y) = (prediction.Rectangle.Left - 3, prediction.Rectangle.Top - 23); // 标注信息的位置信息
// 标说明
image.Mutate(a => a.DrawText($"{prediction.Label.Name} ({score})",
font, prediction.Label.Color, new PointF(x, y)));
// 框位置
image.Mutate(a => a.DrawPolygon(prediction.Label.Color, 1,
new PointF(prediction.Rectangle.Left, prediction.Rectangle.Top),
new PointF(prediction.Rectangle.Right, prediction.Rectangle.Top),
new PointF(prediction.Rectangle.Right, prediction.Rectangle.Bottom),
new PointF(prediction.Rectangle.Left, prediction.Rectangle.Bottom)
));
bodyInfos.Add(new BodyInfo((int)prediction.Rectangle.Left, (int)prediction.Rectangle.Top, (int)prediction.Rectangle.Right, (int)prediction.Rectangle.Bottom, score));
}
}
6、保存图片
image.SaveAsync(toBeTestedImgNewNameStr);
7、控制台输出结果(json字符串格式)
string resultStr = JsonConvert.SerializeObject(bodyInfos);
Console.WriteLine(resultStr);
8、运行效果
控制台调用,输入参数即可(如:YoloConsole.exe -E:);效果如下:
本文来自博客园,作者:꧁执笔小白꧂,转载请注明原文链接:https://www.cnblogs.com/qq2806933146xiaobai/p/18289303
合集:
机器视觉-Yolo
分类:
C#+深度学习Yolo
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 记一次.NET内存居高不下排查解决与启示