-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
176 lines (135 loc) · 7.25 KB
/
Program.cs
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
using System;
using System.IO;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using Microsoft.ML;
using ObjectDetection.YoloParser;
using ObjectDetection.DataStructures;
using Microsoft.ML.Data;
using System.Numerics;
namespace ObjectDetection
{
class Program
{
public static void Main()
{
var assetsRelativePath = @"../../../assets";
string assetsPath = GetAbsolutePath(assetsRelativePath);
//var modelFilePath = Path.Combine(assetsPath, "Model", "TinyYolo2_model.onnx");
var modelFilePath = Path.Combine(assetsPath, "Model", "yolov4_-1_3_416_416_dynamic.onnx");
var imagesFolder = Path.Combine(assetsPath, "images");
var outputFolder = Path.Combine(assetsPath, "images", "output");
// Initialize MLContext
MLContext mlContext = new MLContext();
try
{
// Load Data
IEnumerable<ImageNetData> images = ImageNetData.ReadFromFile(imagesFolder);
IDataView imageDataView = mlContext.Data.LoadFromEnumerable(images);
// Create instance of model scorer
var modelScorer = new OnnxModelScorer(imagesFolder, modelFilePath, mlContext);
// Use model to score data
var modelOutput = modelScorer.Score(imageDataView);
// Post-process model output
YoloOutputParser parser = new YoloOutputParser();
var boxes = modelOutput.GetColumn<float[]>("boxes").ToList();
var confs = modelOutput.GetColumn<float[]>("confs").ToList();
if (boxes.Count != confs.Count)
{
throw new InvalidOperationException("Errrorrrr");
}
List<IList<YoloBoundingBox>> outputBoxes = new List<IList<YoloBoundingBox>>();
//Boxes.Count == images.count because the model can handle multiple images at once
for (int i = 0; i < boxes.Count; i++)
{
var boxesHere = boxes[i];
var confsHere = confs[i];
var parsedBoxes = parser.ParseOutputs(boxesHere, confsHere);
var filteredBoxes = parser.FilterBoundingBoxes(parsedBoxes, 5, 0.5f);
outputBoxes.Add(filteredBoxes);
}
// Draw bounding boxes for detected objects in each of the images
for (var i = 0; i < images.Count(); i++)
{
string imageFileName = images.ElementAt(i).Label;
IList<YoloBoundingBox> detectedObjects = outputBoxes.ElementAt(i);
DrawBoundingBox(imagesFolder, outputFolder, imageFileName, detectedObjects);
LogDetectedObjects(imageFileName, detectedObjects);
}
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
Console.WriteLine("========= End of Process..Hit any Key ========");
Console.ReadLine();
}
public 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;
}
private static void DrawBoundingBox(string inputImageLocation, string outputImageLocation, string imageName, IList<YoloBoundingBox> filteredBoundingBoxes)
{
Image image = Image.FromFile(Path.Combine(inputImageLocation, imageName));
var originalImageHeight = image.Height;
var originalImageWidth = image.Width;
foreach (var box in filteredBoundingBoxes)
{
//// Get Bounding Box Dimensions
//var x = (uint)Math.Max(box.Dimensions.X * OnnxModelScorer.ImageNetSettings.imageWidth, 0);
//var y = (uint)Math.Max(box.Dimensions.Y * OnnxModelScorer.ImageNetSettings.imageWidth, 0);
//var width = (uint)Math.Min(originalImageWidth - x, box.Dimensions.Width * OnnxModelScorer.ImageNetSettings.imageWidth);
//var height = (uint)Math.Min(originalImageHeight - y, box.Dimensions.Height * OnnxModelScorer.ImageNetSettings.imageWidth);
//// Resize To Image
//x = (uint)originalImageWidth * x / OnnxModelScorer.ImageNetSettings.imageWidth;
//y = (uint)originalImageHeight * y / OnnxModelScorer.ImageNetSettings.imageHeight;
//width = (uint)originalImageWidth * width / OnnxModelScorer.ImageNetSettings.imageWidth;
//height = (uint)originalImageHeight * height / OnnxModelScorer.ImageNetSettings.imageHeight;
var x = (uint)(originalImageWidth * box.Dimensions.X);
var y = (uint)(originalImageHeight * box.Dimensions.Y);
var width = (uint)(originalImageWidth * box.Dimensions.Width);
var height = (uint)(originalImageHeight * box.Dimensions.Height);
// Bounding Box Text
string text = $"{box.Label} ({(box.Confidence * 100).ToString("0")}%)";
using (Graphics thumbnailGraphic = Graphics.FromImage(image))
{
thumbnailGraphic.CompositingQuality = CompositingQuality.HighQuality;
thumbnailGraphic.SmoothingMode = SmoothingMode.HighQuality;
thumbnailGraphic.InterpolationMode = InterpolationMode.HighQualityBicubic;
// Define Text Options
Font drawFont = new Font("Arial", 12, FontStyle.Bold);
SizeF size = thumbnailGraphic.MeasureString(text, drawFont);
SolidBrush fontBrush = new SolidBrush(Color.Black);
Point atPoint = new Point((int)x, (int)y - (int)size.Height - 1);
// Define BoundingBox options
Pen pen = new Pen(box.BoxColor, 3.2f);
SolidBrush colorBrush = new SolidBrush(box.BoxColor);
// Draw text on image
thumbnailGraphic.FillRectangle(colorBrush, (int)x, (int)(y - size.Height - 1), (int)size.Width, (int)size.Height);
thumbnailGraphic.DrawString(text, drawFont, fontBrush, atPoint);
// Draw bounding box on image
thumbnailGraphic.DrawRectangle(pen, x, y, width, height);
}
}
if (!Directory.Exists(outputImageLocation))
{
Directory.CreateDirectory(outputImageLocation);
}
image.Save(Path.Combine(outputImageLocation, imageName));
}
private static void LogDetectedObjects(string imageName, IList<YoloBoundingBox> boundingBoxes)
{
Console.WriteLine($".....The objects in the image {imageName} are detected as below....");
foreach (var box in boundingBoxes)
{
Console.WriteLine($"{box.Label} and its Confidence score: {box.Confidence}");
}
Console.WriteLine("");
}
}
}