-
Notifications
You must be signed in to change notification settings - Fork 216
Expand file tree
/
Copy pathObjectDetection.xaml.cs
More file actions
217 lines (183 loc) · 7.43 KB
/
ObjectDetection.xaml.cs
File metadata and controls
217 lines (183 loc) · 7.43 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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using AIDevGallery.Models;
using AIDevGallery.Samples.Attributes;
using AIDevGallery.Samples.SharedCode;
using Microsoft.ML.OnnxRuntime;
using Microsoft.ML.OnnxRuntime.Tensors;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Media.Imaging;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Windows.Storage.Pickers;
namespace AIDevGallery.Samples.OpenSourceModels.ObjectDetection.FasterRCNN;
[GallerySample(
Model1Types = [ModelType.FasterRCNN],
Scenario = ScenarioType.ImageDetectObjects,
SharedCode = [
SharedCodeEnum.Prediction,
SharedCodeEnum.BitmapFunctions,
SharedCodeEnum.RCNNLabelMap
],
NugetPackageReferences = [
"System.Drawing.Common",
"Microsoft.ML.OnnxRuntime.Extensions",
"System.Numerics.Tensors"
],
AssetFilenames = [
"pose_default.png"
],
Name = "Faster RCNN Object Detection",
Id = "9b74ccc0-f5f7-430f-bed0-758ffc063508",
Icon = "\uE8B3")]
internal sealed partial class ObjectDetection : BaseSamplePage
{
private InferenceSession? _inferenceSession;
public ObjectDetection()
{
this.Unloaded += (s, e) => _inferenceSession?.Dispose();
this.Loaded += (s, e) => Page_Loaded(); // <exclude-line>
this.InitializeComponent();
}
protected override async Task LoadModelAsync(SampleNavigationParameters sampleParams)
{
try
{
await InitModel(sampleParams.ModelPath, sampleParams.WinMlSampleOptions.Policy, sampleParams.WinMlSampleOptions.EpName, sampleParams.WinMlSampleOptions.CompileModel, sampleParams.WinMlSampleOptions.DeviceType);
sampleParams.NotifyCompletion();
}
catch (Exception ex)
{
ShowException(ex, "Failed to load model.");
return;
}
// Loads inference on default image
await DetectObjects(Path.Join(Windows.ApplicationModel.Package.Current.InstalledLocation.Path, "Assets", "pose_default.png"));
}
// <exclude>
private void Page_Loaded()
{
UploadButton.Focus(FocusState.Programmatic);
}
// </exclude>
private Task InitModel(string modelPath, ExecutionProviderDevicePolicy? policy, string? epName, bool compileModel, string? deviceType)
{
return Task.Run(async () =>
{
if (_inferenceSession != null)
{
return;
}
var catalog = Microsoft.Windows.AI.MachineLearning.ExecutionProviderCatalog.GetDefault();
try
{
var registeredProviders = await catalog.EnsureAndRegisterCertifiedAsync();
}
catch (Exception ex)
{
Debug.WriteLine($"WARNING: Failed to install packages: {ex.Message}");
}
SessionOptions sessionOptions = new();
sessionOptions.RegisterOrtExtensions();
if (policy != null)
{
sessionOptions.SetEpSelectionPolicy(policy.Value);
}
else if (epName != null)
{
sessionOptions.AppendExecutionProviderFromEpName(epName, deviceType);
if (compileModel)
{
modelPath = sessionOptions.GetCompiledModel(modelPath, epName) ?? modelPath;
}
}
_inferenceSession = new InferenceSession(modelPath, sessionOptions);
});
}
private async void UploadButton_Click(object sender, RoutedEventArgs e)
{
var window = new Window();
var hwnd = WinRT.Interop.WindowNative.GetWindowHandle(window);
var picker = new FileOpenPicker();
WinRT.Interop.InitializeWithWindow.Initialize(picker, hwnd);
picker.FileTypeFilter.Add(".png");
picker.FileTypeFilter.Add(".jpeg");
picker.FileTypeFilter.Add(".jpg");
picker.ViewMode = PickerViewMode.Thumbnail;
var file = await picker.PickSingleFileAsync();
UploadButton.Focus(FocusState.Programmatic);
if (file != null)
{
SendSampleInteractedEvent("FileSelected"); // <exclude-line>
await DetectObjects(file.Path);
}
}
private async Task DetectObjects(string filePath)
{
Loader.IsActive = true;
Loader.Visibility = Visibility.Visible;
UploadButton.Visibility = Visibility.Collapsed;
DefaultImage.Source = new BitmapImage(new Uri(filePath));
NarratorHelper.AnnounceImageChanged(DefaultImage, "Image changed: new upload."); // <exclude-line>
Bitmap image = new(filePath);
var predictions = await Task.Run(() =>
{
// Resizing image ==> Suggested that height and width are in range of [800, 1333].
float ratio = 800f / Math.Max(image.Width, image.Height);
int width = (int)(ratio * image.Width);
int height = (int)(ratio * image.Height);
var paddedHeight = (int)(Math.Ceiling(image.Height / 32f) * 32f);
var paddedWidth = (int)(Math.Ceiling(image.Width / 32f) * 32f);
var resizedImage = BitmapFunctions.ResizeBitmap(image, paddedWidth, paddedHeight);
image.Dispose();
image = resizedImage;
// Preprocessing
Tensor<float> input = new DenseTensor<float>([3, paddedHeight, paddedWidth]);
input = BitmapFunctions.PreprocessBitmapForObjectDetection(image, paddedHeight, paddedWidth);
// Setup inputs and outputs
var inputMetadataName = _inferenceSession!.InputNames[0];
var inputs = new List<NamedOnnxValue>
{
NamedOnnxValue.CreateFromTensor(inputMetadataName, input)
};
// Run inference
using IDisposableReadOnlyCollection<DisposableNamedOnnxValue> results = _inferenceSession!.Run(inputs);
// Postprocess to get predictions
var resultsArray = results.ToArray();
float[] boxes = resultsArray[0].AsEnumerable<float>().ToArray();
long[] labels = resultsArray[1].AsEnumerable<long>().ToArray();
float[] confidences = resultsArray[2].AsEnumerable<float>().ToArray();
var predictions = new List<Prediction>();
var minConfidence = 0.7f;
for (int i = 0; i < boxes.Length - 4; i += 4)
{
var index = i / 4;
if (confidences[index] >= minConfidence)
{
predictions.Add(new Prediction
{
Box = new Box(boxes[i], boxes[i + 1], boxes[i + 2], boxes[i + 3]),
Label = RCNNLabelMap.Labels[labels[index]],
Confidence = confidences[index]
});
}
}
return predictions;
});
BitmapImage outputImage = BitmapFunctions.RenderPredictions(image, predictions);
DispatcherQueue.TryEnqueue(() =>
{
DefaultImage.Source = outputImage;
Loader.IsActive = false;
Loader.Visibility = Visibility.Collapsed;
UploadButton.Visibility = Visibility.Visible;
});
NarratorHelper.AnnounceImageChanged(DefaultImage, "Image changed: objects detected."); // <exclude-line>
image.Dispose();
}
}