-
-
Notifications
You must be signed in to change notification settings - Fork 140
Expand file tree
/
Copy pathProgram.cs
More file actions
252 lines (206 loc) · 8.26 KB
/
Program.cs
File metadata and controls
252 lines (206 loc) · 8.26 KB
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
using System;
using System.IO;
using System.Linq;
using System.Diagnostics;
using System.Collections.Generic;
using YoloDotNet;
using YoloDotNet.Enums;
using YoloDotNet.Models;
using ConsoleDemo.Config;
using YoloDotNet.Extensions;
using YoloDotNet.Test.Common;
using YoloDotNet.Test.Common.Enums;
using SkiaSharp;
Console.CursorVisible = false;
CreateOutputFolder();
Action<ModelType, ModelVersion, ImageType, bool, bool> runDemoAction = RunDemo;
runDemoAction(ModelType.Classification, ModelVersion.V8, ImageType.Hummingbird, false, false);
runDemoAction(ModelType.Classification, ModelVersion.V11, ImageType.Hummingbird, false, false);
runDemoAction(ModelType.ObjectDetection, ModelVersion.V8, ImageType.Street, false, false);
runDemoAction(ModelType.ObjectDetection, ModelVersion.V9, ImageType.Street, false, false);
runDemoAction(ModelType.ObjectDetection, ModelVersion.V10, ImageType.Street, false, false);
runDemoAction(ModelType.ObjectDetection, ModelVersion.V11, ImageType.Street, false, false);
runDemoAction(ModelType.ObbDetection, ModelVersion.V8, ImageType.Island, false, false);
runDemoAction(ModelType.ObbDetection, ModelVersion.V11, ImageType.Island, false, false);
runDemoAction(ModelType.Segmentation, ModelVersion.V8, ImageType.People, false, false);
runDemoAction(ModelType.Segmentation, ModelVersion.V11, ImageType.People, false, false);
runDemoAction(ModelType.PoseEstimation, ModelVersion.V8, ImageType.Crosswalk, false, false);
runDemoAction(ModelType.PoseEstimation, ModelVersion.V11, ImageType.Crosswalk, false, false);
ObjectDetectionOnVideo();
DisplayOutputFolder();
DisplayOnnxMetaDataExample();
#region Helper methods
static void CreateOutputFolder()
{
var outputFolder = DemoSettings.OUTPUT_FOLDER;
if (Directory.Exists(outputFolder) is false)
Directory.CreateDirectory(outputFolder);
}
static void RunDemo(ModelType modelType, ModelVersion modelVersion, ImageType imageType, bool cuda = false, bool primeGpu = false)
{
var modelPath = modelVersion switch
{
ModelVersion.V8 => SharedConfig.GetTestModelV8(modelType),
ModelVersion.V9 => SharedConfig.GetTestModelV9(modelType),
ModelVersion.V10 => SharedConfig.GetTestModelV10(modelType),
ModelVersion.V11 => SharedConfig.GetTestModelV11(modelType),
_ => throw new ArgumentException("Unkown yolo version")
};
var imagePath = SharedConfig.GetTestImage(imageType);
using var yolo = new Yolo(new YoloOptions()
{
OnnxModel = modelPath,
HwAccelerator = cuda ? HwAcceleratorType.Cuda : HwAcceleratorType.None,
PrimeGpu = primeGpu,
ModelType = modelType,
});
using var image = SKImage.FromEncodedData(imagePath);
var device = cuda ? "GPU" : "CPU";
device += device == "CPU" ? "" : primeGpu ? ", primed: yes" : ", primed: no";
Console.Write($"{yolo.OnnxModel.ModelType,-16} {modelVersion, -5}device: {device}");
Console.WriteLine();
SKImage resultImage = SKImage.Create(new SKImageInfo());
List<LabelModel> labels = new();
switch (modelType)
{
case ModelType.Classification:
{
var result = yolo.RunClassification(image, 1);
labels = result.Select(x => new LabelModel { Name = x.Label }).ToList();
resultImage = image.Draw(result);
break;
}
case ModelType.ObjectDetection:
{
var result = yolo.RunObjectDetection(image, 0.23, 0.7);
labels = result.Select(x => x.Label).ToList();
resultImage = image.Draw(result);
break;
}
case ModelType.ObbDetection:
{
var result = yolo.RunObbDetection(image, 0.23, 0.7);
labels = result.Select(x => x.Label).ToList();
resultImage = image.Draw(result);
break;
}
case ModelType.Segmentation:
{
var result = yolo.RunSegmentation(image, 0.23, 0.65, 0.7);
labels = result.Select(x => x.Label).ToList();
resultImage = image.Draw(result);
break;
}
case ModelType.PoseEstimation:
{
var result = yolo.RunPoseEstimation(image, 0.23, 0.7);
labels = result.Select(x => x.Label).ToList();
resultImage = image.Draw(result, CustomKeyPointColorMap.KeyPointOptions);
break;
}
}
DisplayDetectedLabels(labels);
resultImage.Save(Path.Combine(DemoSettings.OUTPUT_FOLDER, $"{modelType}_{modelVersion}.jpg"), SKEncodedImageFormat.Jpeg);
}
static void ObjectDetectionOnVideo()
{
var videoOptions = new VideoOptions
{
VideoFile = SharedConfig.GetTestImage("walking.mp4"),
OutputDir = DemoSettings.OUTPUT_FOLDER,
//GenerateVideo = false,
//DrawLabels = false,
//FPS = 30,
//Width = 640, // Resize video...
//Height = -2, // -2 = automatically calculate dimensions to keep proportions
//Quality = 28,
//DrawConfidence = true,
//KeepAudio = true,
//KeepFrames = false,
DrawSegment = DrawSegment.Default,
KeyPointOptions = CustomKeyPointColorMap.KeyPointOptions
};
Console.WriteLine();
Console.WriteLine("Running Object Detection on video with Yolo v8...");
using var yolo = new Yolo(new YoloOptions
{
OnnxModel = SharedConfig.GetTestModelV8(ModelType.ObjectDetection),
ModelType = ModelType.ObjectDetection,
HwAccelerator = HwAcceleratorType.Cuda
});
int currentLineCursor = 0;
// Listen to events...
yolo.VideoStatusEvent += (sender, e) =>
{
Console.WriteLine();
Console.Write((string)sender!);
currentLineCursor = Console.CursorTop;
};
yolo.VideoProgressEvent += (object sender, EventArgs e) =>
{
Console.SetCursorPosition(20, currentLineCursor);
Console.Write(new string(' ', 4));
Console.SetCursorPosition(20, currentLineCursor);
Console.Write("{0}%", (int)sender!);
};
yolo.VideoCompleteEvent += (object sender, EventArgs e) =>
{
Console.WriteLine();
Console.WriteLine();
Console.WriteLine("Complete!");
};
Dictionary<int, List<ObjectDetection>> detections = yolo.RunObjectDetection(videoOptions, 0.25);
Console.WriteLine();
}
static void DisplayDetectedLabels(IEnumerable<LabelModel> labels)
{
if (!labels.Any())
return;
var ls = labels.GroupBy(x => x.Name)
.ToDictionary(x => x.Key, x => x.Count());
Console.WriteLine(new string('-', 33));
Console.ForegroundColor = ConsoleColor.Blue;
foreach (var label in ls)
Console.WriteLine($"{label.Key,16} ({label.Value})");
Console.ForegroundColor = ConsoleColor.Gray;
Console.WriteLine();
}
static void DisplayOnnxMetaDataExample()
{
Console.WriteLine();
Console.WriteLine("Internal ONNX properties");
Console.WriteLine(new string('-', 58));
using var yolo = new Yolo(new YoloOptions
{
OnnxModel = SharedConfig.GetTestModelV8(ModelType.ObjectDetection),
ModelType = ModelType.ObjectDetection
});
// Display internal ONNX properties...
foreach (var property in yolo.OnnxModel.GetType().GetProperties())
{
var value = property.GetValue(yolo.OnnxModel);
Console.WriteLine($"{property.Name,-20}{value!}");
if (property.Name == nameof(yolo.OnnxModel.CustomMetaData))
{
var customMetaData = (Dictionary<string, string>)value!;
foreach (var data in customMetaData)
Console.WriteLine($"{"",-20}{data.Key,-20}{data.Value}");
}
}
var labels = yolo.OnnxModel.Labels;
Console.WriteLine();
Console.WriteLine($"Labels ({labels.Length}):");
Console.WriteLine(new string('-', 58));
// Display labels and its corresponding color
for (var i = 0; i < 3; i++)
{
// Capitalize first letter in label
var label = char.ToUpper(labels[i].Name[0]) + labels[i].Name[1..];
Console.WriteLine($"index: {i,-8} label: {label,-20} color: {labels[i].Color}");
}
Console.WriteLine("...");
Console.WriteLine();
}
static void DisplayOutputFolder()
=> Process.Start("explorer.exe", DemoSettings.OUTPUT_FOLDER);
#endregion