This repository has been archived by the owner on Feb 10, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 575
/
RealtimeObjectDetection.xaml.cs
366 lines (325 loc) · 15.6 KB
/
RealtimeObjectDetection.xaml.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
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
//
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license.
//
// Microsoft Cognitive Services: http://www.microsoft.com/cognitive
//
// Microsoft Cognitive Services Github:
// https://github.com/Microsoft/Cognitive
//
// Copyright (c) Microsoft Corporation
// All rights reserved.
//
// MIT License:
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED ""AS IS"", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using IntelligentKioskSample.Controls;
using IntelligentKioskSample.Models;
using IntelligentKioskSample.Views.CustomVision;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Threading.Tasks;
using Windows.ApplicationModel;
using Windows.Graphics.Imaging;
using Windows.Media;
using Windows.Storage;
using Windows.UI;
using Windows.UI.Popups;
using Windows.UI.ViewManagement;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
namespace IntelligentKioskSample.Views
{
[KioskExperience(Id = "OnDeviceObjectDetection",
DisplayName = "Realtime Object Detection",
Description = "Detect objects in real-time using Windows ML",
ImagePath = "ms-appx:/Assets/DemoGallery/Realtime Object Detection.jpg",
ExperienceType = ExperienceType.Automated | ExperienceType.Business | ExperienceType.IntelligentEdge,
TechnologiesUsed = TechnologyType.WinML,
TechnologyArea = TechnologyAreaType.Vision,
DateAdded = "2018/03/14")]
public sealed partial class RealtimeObjectDetection : Page, ICameraFrameProcessor
{
private readonly int ObjectDetectionModelInputSize = 416;
private readonly float MinProbabilityValue = 0.6f;
private string[] allModelObjects;
private ObjectDetection objectDetectionModel;
private bool isModelLoadedSuccessfully = false;
public ObservableCollection<CustomVisionModelData> Projects { get; set; } = new ObservableCollection<CustomVisionModelData>();
public RealtimeObjectDetection()
{
this.InitializeComponent();
Window.Current.Activated += CurrentWindowActivationStateChanged;
this.cameraControl.HideCameraControls();
this.cameraControl.CameraFrameProcessor = this;
this.cameraControl.PerformFaceTracking = false;
this.cameraControl.ShowFaceTracking = false;
this.cameraControl.CameraAspectRatioChanged += CameraControl_CameraAspectRatioChanged;
}
private void CameraControl_CameraAspectRatioChanged(object sender, EventArgs e)
{
this.UpdateCameraHostSize();
}
private async void CurrentWindowActivationStateChanged(object sender, Windows.UI.Core.WindowActivatedEventArgs e)
{
if ((e.WindowActivationState == Windows.UI.Core.CoreWindowActivationState.CodeActivated ||
e.WindowActivationState == Windows.UI.Core.CoreWindowActivationState.PointerActivated) &&
this.cameraControl.CameraStreamState == Windows.Media.Devices.CameraStreamState.Shutdown)
{
// When our Window loses focus due to user interaction Windows shuts it down, so we
// detect here when the window regains focus and trigger a restart of the camera.
await this.cameraControl.StartStreamAsync(isForRealTimeProcessing: true);
}
}
protected override async void OnNavigatedTo(NavigationEventArgs e)
{
this.DataContext = this;
EnterKioskMode();
await this.LoadProjectsFromFile(e.Parameter as CustomVisionModelData);
await this.cameraControl.StartStreamAsync(isForRealTimeProcessing: true);
base.OnNavigatedTo(e);
}
private void EnterKioskMode()
{
ApplicationView view = ApplicationView.GetForCurrentView();
if (!view.IsFullScreenMode)
{
view.TryEnterFullScreenMode();
}
}
protected override async void OnNavigatingFrom(NavigatingCancelEventArgs e)
{
Window.Current.Activated -= CurrentWindowActivationStateChanged;
this.cameraControl.CameraAspectRatioChanged -= CameraControl_CameraAspectRatioChanged;
await this.cameraControl.StopStreamAsync();
base.OnNavigatingFrom(e);
}
private async Task LoadProjectsFromFile(CustomVisionModelData preselectedProject = null)
{
try
{
this.Projects.Clear();
List<CustomVisionModelData> prebuiltModelList = CustomVisionDataLoader.GetBuiltInModelData(CustomVisionProjectType.ObjectDetection) ?? new List<CustomVisionModelData>();
foreach (CustomVisionModelData prebuiltModel in prebuiltModelList)
{
this.Projects.Add(prebuiltModel);
}
List<CustomVisionModelData> customVisionModelList = await CustomVisionDataLoader.GetCustomVisionModelDataAsync(CustomVisionProjectType.ObjectDetection) ?? new List<CustomVisionModelData>();
foreach (CustomVisionModelData customModel in customVisionModelList)
{
this.Projects.Add(customModel);
}
CustomVisionModelData defaultProject = preselectedProject != null ? this.Projects.FirstOrDefault(x => x.Id.Equals(preselectedProject.Id)) : null;
if (defaultProject != null)
{
this.projectsComboBox.SelectedValue = defaultProject;
}
else
{
this.projectsComboBox.SelectedIndex = 0;
}
}
catch (Exception ex)
{
await Util.GenericApiCallExceptionHandler(ex, "Failure loading projects");
}
}
private void LoadSupportedClasses(CustomVisionModelData currentProject)
{
string modelLabelsPrefix = "Supported classes: ";
this.modelObjects.Text = string.Empty;
this.moreModelObjects.Text = string.Empty;
this.allModelObjects = currentProject?.ClassLabels?.OrderBy(x => x).ToArray() ?? new string[] { };
if (this.allModelObjects.Length > 2)
{
this.modelObjects.Text = $"{modelLabelsPrefix}{string.Join(", ", this.allModelObjects.Take(2))}";
this.moreModelObjects.Text = $"and {allModelObjects.Length - 2} more"; ;
}
else
{
this.modelObjects.Text = modelLabelsPrefix + string.Join(", ", allModelObjects);
}
}
private void OnShowAllSupportedClassesTapped(object sender, TappedRoutedEventArgs e)
{
this.allSupportedClassesListView.ItemsSource = new ObservableCollection<string>(this.allModelObjects);
supportedClassesBox.ShowAt((TextBlock)sender);
}
private void OnPageSizeChanged(object sender, SizeChangedEventArgs e)
{
this.UpdateCameraHostSize();
}
private void UpdateCameraHostSize()
{
this.cameraHostGrid.Width = this.cameraHostGrid.ActualHeight;
}
private async void OnProjectSelectionChanged(object sender, SelectionChangedEventArgs e)
{
try
{
this.isModelLoadedSuccessfully = false;
if (this.projectsComboBox.SelectedValue is CustomVisionModelData currentProject)
{
await LoadCurrentModelAsync(currentProject);
}
}
finally
{
this.isModelLoadedSuccessfully = true;
}
}
private async Task LoadCurrentModelAsync(CustomVisionModelData currentProject)
{
try
{
this.deleteBtn.Visibility = currentProject.IsPrebuiltModel ? Visibility.Collapsed : Visibility.Visible;
LoadSupportedClasses(currentProject);
StorageFile modelFile = await GetModelFileAsync(currentProject);
this.objectDetectionModel = new ObjectDetection(this.allModelObjects, probabilityThreshold: MinProbabilityValue);
await this.objectDetectionModel.Init(modelFile);
}
catch (Exception ex)
{
await Util.GenericApiCallExceptionHandler(ex, "Failure loading current project");
}
}
private async Task<StorageFile> GetModelFileAsync(CustomVisionModelData customVisionModelData)
{
if (customVisionModelData.IsPrebuiltModel)
{
string modelPath = $"Assets\\{customVisionModelData.FileName}";
return await Package.Current.InstalledLocation.GetFileAsync(modelPath);
}
else
{
StorageFolder onnxProjectDataFolder = await CustomVisionDataLoader.GetOnnxModelStorageFolderAsync(CustomVisionProjectType.ObjectDetection);
return await onnxProjectDataFolder.GetFileAsync(customVisionModelData.FileName);
}
}
private async void OnAddProjectClicked(object sender, RoutedEventArgs e)
{
await new MessageDialog("To add a new project here, please select one of your projects in the Custom Vision Setup page and use the ONNX Export feature.", "New project").ShowAsync();
}
private async void OnDeleteProjectClicked(object sender, RoutedEventArgs e)
{
await Util.ConfirmActionAndExecute("Delete project?", async () => { await DeleteProjectAsync(); });
}
private async Task DeleteProjectAsync()
{
CustomVisionModelData currentProject = this.projectsComboBox.SelectedValue as CustomVisionModelData;
if (currentProject != null && !currentProject.IsPrebuiltModel)
{
// delete ONNX model file
StorageFolder onnxProjectDataFolder = await CustomVisionDataLoader.GetOnnxModelStorageFolderAsync(CustomVisionProjectType.ObjectDetection);
StorageFile modelFile = await onnxProjectDataFolder.GetFileAsync(currentProject.FileName);
if (modelFile != null)
{
await modelFile.DeleteAsync();
}
// update local file with custom models
this.Projects.Remove(currentProject);
List<CustomVisionModelData> updatedCustomModelList = this.Projects.Where(x => !x.IsPrebuiltModel).ToList();
await CustomVisionDataLoader.SaveCustomVisionModelDataAsync(updatedCustomModelList, CustomVisionProjectType.ObjectDetection);
this.projectsComboBox.SelectedIndex = 0;
}
}
public async Task ProcessFrame(VideoFrame videoFrame, Canvas visualizationCanvas)
{
if (!isModelLoadedSuccessfully)
{
return;
}
try
{
using (SoftwareBitmap bitmapBuffer = new SoftwareBitmap(BitmapPixelFormat.Bgra8,
ObjectDetectionModelInputSize, ObjectDetectionModelInputSize, BitmapAlphaMode.Ignore))
{
using (VideoFrame buffer = VideoFrame.CreateWithSoftwareBitmap(bitmapBuffer))
{
await videoFrame.CopyToAsync(buffer);
DateTime start = DateTime.Now;
IList<PredictionModel> predictions = await this.objectDetectionModel.PredictImageAsync(buffer);
double predictionTimeInMilliseconds = (DateTime.Now - start).TotalMilliseconds;
await this.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
{
this.ShowVisualization(visualizationCanvas, predictions);
this.fpsTextBlock.Text = predictionTimeInMilliseconds > 0 ? $"{Math.Round(1000 / predictionTimeInMilliseconds)} fps" : string.Empty;
});
}
}
}
catch (Exception ex)
{
this.isModelLoadedSuccessfully = false;
await this.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, async () =>
{
await Util.GenericApiCallExceptionHandler(ex, "Failure processing frame");
});
}
}
private void ShowVisualization(Canvas visualizationCanvas, IList<PredictionModel> detectedObjects)
{
visualizationCanvas.Children.Clear();
double canvasWidth = visualizationCanvas.ActualWidth;
double canvasHeight = visualizationCanvas.ActualHeight;
foreach (PredictionModel prediction in detectedObjects)
{
visualizationCanvas.Children.Add(
new Border
{
BorderBrush = new SolidColorBrush(Colors.Lime),
BorderThickness = new Thickness(2),
Margin = new Thickness(prediction.BoundingBox.Left * canvasWidth,
prediction.BoundingBox.Top * canvasHeight, 0, 0),
Width = prediction.BoundingBox.Width * canvasWidth,
Height = prediction.BoundingBox.Height * canvasHeight,
});
visualizationCanvas.Children.Add(
new Border
{
Width = 300,
Height = 40,
FlowDirection = FlowDirection.LeftToRight,
Margin = new Thickness(prediction.BoundingBox.Left * canvasWidth + prediction.BoundingBox.Width * canvasWidth - 300,
prediction.BoundingBox.Top * canvasHeight - 40, 0, 0),
Child = new Border
{
Background = new SolidColorBrush(Colors.Lime),
HorizontalAlignment = HorizontalAlignment.Left,
VerticalAlignment = VerticalAlignment.Bottom,
Child =
new TextBlock
{
Text = $"{prediction.TagName}",
FontSize = 24,
Foreground = new SolidColorBrush(Colors.Black),
Margin = new Thickness(6, 0, 6, 0)
}
}
});
}
}
}
}