forked from microsoft/Cognitive-Samples-IntelligentKiosk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRealTimeDemo.xaml.cs
496 lines (421 loc) · 18.6 KB
/
RealTimeDemo.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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
//
// 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 Microsoft.Azure.CognitiveServices.Vision.Face.Models;
using ServiceHelpers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Xml.Serialization;
using Windows.Graphics.Imaging;
using Windows.UI.Popups;
using Windows.UI.ViewManagement;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Navigation;
namespace IntelligentKioskSample.Views
{
[KioskExperience(Id = "Real-time Kiosk",
DisplayName = "Realtime Crowd Insights",
Description = "Face analysis in near real-time from a camera stream",
ImagePath = "ms-appx:/Assets/DemoGallery/Realtime Crowd Insights.jpg",
ExperienceType = ExperienceType.Automated | ExperienceType.Business,
TechnologiesUsed = TechnologyType.Emotion | TechnologyType.Face,
TechnologyArea = TechnologyAreaType.Vision,
DateAdded = "2016/02/19")]
public sealed partial class RealTimeDemo : Page, IRealTimeDataProvider
{
private Task processingLoopTask;
private bool isProcessingLoopInProgress;
private bool isProcessingPhoto;
private DateTime lastResultsTimestamp = DateTime.MinValue;
private IEnumerable<DetectedFace> lastDetectedFaceSample;
private IEnumerable<Tuple<DetectedFace, IdentifiedPerson>> lastIdentifiedPersonSample;
private IEnumerable<SimilarFaceMatch> lastSimilarPersistedFaceSample;
private DemographicsData demographics;
private Dictionary<Guid, Visitor> visitors = new Dictionary<Guid, Visitor>();
public static bool ShowAgeAndGender { get { return SettingsHelper.Instance.ShowAgeAndGender; } }
public RealTimeDemo()
{
this.InitializeComponent();
this.DataContext = this;
Window.Current.Activated += CurrentWindowActivationStateChanged;
this.cameraControl.SetRealTimeDataProvider(this);
this.cameraControl.FilterOutSmallFaces = false;
this.cameraControl.HideCameraControls();
this.cameraControl.ShowDialogOnApiErrors = SettingsHelper.Instance.ShowDialogOnApiErrors;
this.cameraControl.CameraAspectRatioChanged += CameraControl_CameraAspectRatioChanged;
}
private void CameraControl_CameraAspectRatioChanged(object sender, EventArgs e)
{
this.UpdateCameraHostSize();
}
private void StartProcessingLoop()
{
this.isProcessingLoopInProgress = true;
if (this.processingLoopTask == null || this.processingLoopTask.Status != TaskStatus.Running)
{
this.processingLoopTask = Task.Run(() => this.ProcessingLoop());
}
}
private async void ProcessingLoop()
{
while (this.isProcessingLoopInProgress)
{
await this.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, async () =>
{
if (!this.isProcessingPhoto)
{
if (DateTime.Now.Hour != this.demographics.StartTime.Hour)
{
// We have been running through the hour. Reset the data...
await this.ResetDemographicsData();
this.UpdateDemographicsUI();
}
this.isProcessingPhoto = true;
if (this.cameraControl.NumFacesOnLastFrame == 0)
{
await this.ProcessCameraCapture(null);
}
else
{
await this.ProcessCameraCapture(await this.cameraControl.CaptureFrameAsync());
}
}
});
await Task.Delay(1000);
}
}
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);
}
}
private async Task ProcessCameraCapture(ImageAnalyzer e)
{
if (e == null)
{
this.lastDetectedFaceSample = null;
this.lastIdentifiedPersonSample = null;
this.lastSimilarPersistedFaceSample = null;
this.debugText.Text = "";
this.isProcessingPhoto = false;
return;
}
DateTime start = DateTime.Now;
// Compute Emotion, Age and Gender
await this.DetectFaceAttributesAsync(e);
// Compute Face Identification and Unique Face Ids
await Task.WhenAll(ComputeFaceIdentificationAsync(e), this.ComputeUniqueFaceIdAsync(e));
lastResultsTimestamp = DateTime.Now;
this.UpdateDemographics(e);
this.UpdateEmotionTimelineUI(e);
this.debugText.Text = string.Format("Latency: {0}ms", (int)(DateTime.Now - start).TotalMilliseconds);
this.isProcessingPhoto = false;
}
private async Task ComputeUniqueFaceIdAsync(ImageAnalyzer e)
{
await e.FindSimilarPersistedFacesAsync();
if (!e.SimilarFaceMatches.Any())
{
this.lastSimilarPersistedFaceSample = null;
}
else
{
this.lastSimilarPersistedFaceSample = e.SimilarFaceMatches;
}
}
private async Task ComputeFaceIdentificationAsync(ImageAnalyzer e)
{
await e.IdentifyFacesAsync();
if (!e.IdentifiedPersons.Any())
{
this.lastIdentifiedPersonSample = null;
}
else
{
this.lastIdentifiedPersonSample = e.DetectedFaces.Select(f => new Tuple<DetectedFace, IdentifiedPerson>(f, e.IdentifiedPersons.FirstOrDefault(p => p.FaceId == f.FaceId)));
}
}
private async Task DetectFaceAttributesAsync(ImageAnalyzer e)
{
await e.DetectFacesAsync(detectFaceAttributes: true);
if (e.DetectedFaces == null || !e.DetectedFaces.Any())
{
this.lastDetectedFaceSample = null;
}
else
{
this.lastDetectedFaceSample = e.DetectedFaces;
}
}
private void UpdateEmotionTimelineUI(ImageAnalyzer e)
{
if (!e.DetectedFaces.Any())
{
this.ShowTimelineFeedbackForNoFaces();
}
else
{
Emotion averageScores = new Emotion
{
Happiness = e.DetectedFaces.Average(f => f.FaceAttributes.Emotion.Happiness),
Anger = e.DetectedFaces.Average(f => f.FaceAttributes.Emotion.Anger),
Sadness = e.DetectedFaces.Average(f => f.FaceAttributes.Emotion.Sadness),
Contempt = e.DetectedFaces.Average(f => f.FaceAttributes.Emotion.Contempt),
Disgust = e.DetectedFaces.Average(f => f.FaceAttributes.Emotion.Disgust),
Neutral = e.DetectedFaces.Average(f => f.FaceAttributes.Emotion.Neutral),
Fear = e.DetectedFaces.Average(f => f.FaceAttributes.Emotion.Fear),
Surprise = e.DetectedFaces.Average(f => f.FaceAttributes.Emotion.Surprise)
};
this.emotionDataTimelineControl.DrawEmotionData(averageScores);
}
}
private void ShowTimelineFeedbackForNoFaces()
{
this.emotionDataTimelineControl.DrawEmotionData(new Emotion { Neutral = 1 });
}
protected override async void OnNavigatedTo(NavigationEventArgs e)
{
EnterKioskMode();
if (string.IsNullOrEmpty(SettingsHelper.Instance.FaceApiKey))
{
await new MessageDialog("Missing Face API Key. Please enter a key in the Settings page.", "Missing API Key").ShowAsync();
}
else
{
FaceListManager.FaceListsUserDataFilter = SettingsHelper.Instance.WorkspaceKey + "_RealTime";
await FaceListManager.Initialize();
await ResetDemographicsData();
this.UpdateDemographicsUI();
await this.cameraControl.StartStreamAsync(isForRealTimeProcessing: true);
this.StartProcessingLoop();
}
base.OnNavigatedTo(e);
}
private void UpdateDemographics(ImageAnalyzer img)
{
if (this.lastSimilarPersistedFaceSample != null)
{
bool demographicsChanged = false;
// Update the Visitor collection (either add new entry or update existing)
foreach (var item in this.lastSimilarPersistedFaceSample)
{
Visitor visitor;
Guid persistedFaceId = item.SimilarPersistedFace.PersistedFaceId.GetValueOrDefault();
if (this.visitors.TryGetValue(persistedFaceId, out visitor))
{
visitor.Count++;
}
else
{
demographicsChanged = true;
visitor = new Visitor { UniqueId = persistedFaceId, Count = 1 };
this.visitors.Add(visitor.UniqueId, visitor);
this.demographics.Visitors.Add(visitor);
// Update the demographics stats. We only do it for new visitors to avoid double counting.
AgeDistribution genderBasedAgeDistribution = null;
if (item.Face.FaceAttributes.Gender == Gender.Male)
{
this.demographics.OverallMaleCount++;
genderBasedAgeDistribution = this.demographics.AgeGenderDistribution.MaleDistribution;
}
else
{
this.demographics.OverallFemaleCount++;
genderBasedAgeDistribution = this.demographics.AgeGenderDistribution.FemaleDistribution;
}
if (item.Face.FaceAttributes.Age < 16)
{
genderBasedAgeDistribution.Age0To15++;
}
else if (item.Face.FaceAttributes.Age < 20)
{
genderBasedAgeDistribution.Age16To19++;
}
else if (item.Face.FaceAttributes.Age < 30)
{
genderBasedAgeDistribution.Age20s++;
}
else if (item.Face.FaceAttributes.Age < 40)
{
genderBasedAgeDistribution.Age30s++;
}
else if (item.Face.FaceAttributes.Age < 50)
{
genderBasedAgeDistribution.Age40s++;
}
else
{
genderBasedAgeDistribution.Age50sAndOlder++;
}
}
}
if (demographicsChanged)
{
this.ageGenderDistributionControl.UpdateData(this.demographics);
}
this.overallStatsControl.UpdateData(this.demographics);
}
}
private void UpdateDemographicsUI()
{
this.ageGenderDistributionControl.UpdateData(this.demographics);
this.overallStatsControl.UpdateData(this.demographics);
}
private async Task ResetDemographicsData()
{
this.initializingUI.Visibility = Visibility.Visible;
this.initializingProgressRing.IsActive = true;
this.demographics = new DemographicsData
{
StartTime = DateTime.Now,
AgeGenderDistribution = new AgeGenderDistribution { FemaleDistribution = new AgeDistribution(), MaleDistribution = new AgeDistribution() },
Visitors = new List<Visitor>()
};
this.visitors.Clear();
await FaceListManager.ResetFaceLists();
this.initializingUI.Visibility = Visibility.Collapsed;
this.initializingProgressRing.IsActive = false;
}
public async Task HandleApplicationShutdownAsync()
{
await ResetDemographicsData();
}
private void EnterKioskMode()
{
ApplicationView view = ApplicationView.GetForCurrentView();
if (!view.IsFullScreenMode)
{
view.TryEnterFullScreenMode();
}
}
protected override async void OnNavigatingFrom(NavigatingCancelEventArgs e)
{
this.isProcessingLoopInProgress = false;
Window.Current.Activated -= CurrentWindowActivationStateChanged;
this.cameraControl.CameraAspectRatioChanged -= CameraControl_CameraAspectRatioChanged;
await this.ResetDemographicsData();
await this.cameraControl.StopStreamAsync();
base.OnNavigatingFrom(e);
}
private void OnPageSizeChanged(object sender, SizeChangedEventArgs e)
{
this.UpdateCameraHostSize();
}
private void UpdateCameraHostSize()
{
this.cameraHostGrid.Width = this.cameraHostGrid.ActualHeight * (this.cameraControl.CameraAspectRatio != 0 ? this.cameraControl.CameraAspectRatio : 1.777777777777);
}
public DetectedFace GetLastFaceAttributesForFace(BitmapBounds faceBox)
{
if (this.lastDetectedFaceSample == null || !this.lastDetectedFaceSample.Any() || !CanReuseCachedResults())
{
return null;
}
return Util.FindFaceClosestToRegion(this.lastDetectedFaceSample, faceBox);
}
public IdentifiedPerson GetLastIdentifiedPersonForFace(BitmapBounds faceBox)
{
if (this.lastIdentifiedPersonSample == null || !this.lastIdentifiedPersonSample.Any() || !CanReuseCachedResults())
{
return null;
}
Tuple<DetectedFace, IdentifiedPerson> match =
this.lastIdentifiedPersonSample.Where(f => Util.AreFacesPotentiallyTheSame(faceBox, f.Item1.FaceRectangle))
.OrderBy(f => Math.Abs(faceBox.X - f.Item1.FaceRectangle.Left) + Math.Abs(faceBox.Y - f.Item1.FaceRectangle.Top)).FirstOrDefault();
if (match != null)
{
return match.Item2;
}
return null;
}
public SimilarFace GetLastSimilarPersistedFaceForFace(BitmapBounds faceBox)
{
if (this.lastSimilarPersistedFaceSample == null || !this.lastSimilarPersistedFaceSample.Any() || !CanReuseCachedResults())
{
return null;
}
SimilarFaceMatch match =
this.lastSimilarPersistedFaceSample.Where(f => Util.AreFacesPotentiallyTheSame(faceBox, f.Face.FaceRectangle))
.OrderBy(f => Math.Abs(faceBox.X - f.Face.FaceRectangle.Left) + Math.Abs(faceBox.Y - f.Face.FaceRectangle.Top)).FirstOrDefault();
return match?.SimilarPersistedFace;
}
private bool CanReuseCachedResults()
{
return (DateTime.Now - lastResultsTimestamp).TotalSeconds <= 3;
}
}
[XmlType]
public class Visitor
{
[XmlAttribute]
public Guid UniqueId { get; set; }
[XmlAttribute]
public int Count { get; set; }
}
[XmlType]
public class AgeDistribution
{
public int Age0To15 { get; set; }
public int Age16To19 { get; set; }
public int Age20s { get; set; }
public int Age30s { get; set; }
public int Age40s { get; set; }
public int Age50sAndOlder { get; set; }
}
[XmlType]
public class AgeGenderDistribution
{
public AgeDistribution MaleDistribution { get; set; }
public AgeDistribution FemaleDistribution { get; set; }
}
[XmlType]
[XmlRoot]
public class DemographicsData
{
public DateTime StartTime { get; set; }
public AgeGenderDistribution AgeGenderDistribution { get; set; }
public int OverallMaleCount { get; set; }
public int OverallFemaleCount { get; set; }
[XmlArrayItem]
public List<Visitor> Visitors { get; set; }
}
}