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
/
RealtimeDriverMonitoring.xaml.cs
517 lines (430 loc) · 20.5 KB
/
RealtimeDriverMonitoring.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
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
//
// 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 KioskRuntimeComponent;
using Microsoft.Azure.CognitiveServices.Vision.Face.Models;
using ServiceHelpers;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Windows.Media.Editing;
using Windows.Media.Effects;
using Windows.Storage;
using Windows.Storage.Pickers;
using Windows.UI;
using Windows.UI.Popups;
using Windows.UI.ViewManagement;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Media.Imaging;
using Windows.UI.Xaml.Navigation;
namespace IntelligentKioskSample.Views
{
[KioskExperience(Id = "RealtimeDriverMonitoring",
DisplayName = "Realtime Driver Monitoring",
Description = "See how AI can identify a distracted driver",
ImagePath = "ms-appx:/Assets/DemoGallery/Realtime Driver Monitoring.jpg",
ExperienceType = ExperienceType.Guided | ExperienceType.Business,
TechnologiesUsed = TechnologyType.Face | TechnologyType.Vision,
TechnologyArea = TechnologyAreaType.Vision,
DateAdded = "2016/11/09")]
public sealed partial class RealtimeDriverMonitoring : Page
{
private Task processingLoopTask;
private bool isProcessingLoopInProgress;
private bool isProcessingPhoto;
private bool isDescribingPhoto;
private bool isProcessingDriverId;
public const double SleepingApertureThreshold = 0.45;
public const double YawningApertureThreshold = 0.7;
public const int LookingAwayAngleThreshold = 20;
private bool isInputSourceFromVideo = false;
private Queue<VideoFrameData> queuedVideoFrames = new Queue<VideoFrameData>();
private HashSet<double> processedFrames = new HashSet<double>();
static SolidColorBrush BarChartColor = new SolidColorBrush(Color.FromArgb(0xff, 0x42, 0xbb, 0xfa));
static SolidColorBrush BarChartAlertColor = new SolidColorBrush(Color.FromArgb(0xff, 0xff, 0x44, 0x44));
public RealtimeDriverMonitoring()
{
this.InitializeComponent();
Window.Current.Activated += CurrentWindowActivationStateChanged;
this.cameraControl.FilterOutSmallFaces = true;
this.cameraControl.HideCameraControls();
this.cameraControl.ShowDialogOnApiErrors = SettingsHelper.Instance.ShowDialogOnApiErrors;
this.cameraControl.CameraAspectRatioChanged += CameraControl_CameraAspectRatioChanged;
this.inputSourceComboBox.SelectionChanged += this.InputSourceChanged;
}
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 (isInputSourceFromVideo)
{
if (FrameRelayVideoEffect.LatestSoftwareBitmap != null &&
(this.videoPlayer.CurrentState == MediaElementState.Playing || this.videoPlayer.CurrentState == MediaElementState.Paused))
{
this.ProcessVideoFrame();
}
}
else
{
if (!this.isProcessingPhoto)
{
this.isProcessingPhoto = true;
if (this.cameraControl.NumFacesOnLastFrame == 0)
{
await this.ProcessImage(null);
}
else
{
await this.ProcessImage(await this.cameraControl.TakeAutoCapturePhoto());
}
}
}
await Task.Delay(500);
});
}
}
private async void ProcessVideoFrame()
{
int frameNumber = GetVideoFrameNumber();
if (!this.processedFrames.Contains(frameNumber))
{
this.processedFrames.Add(frameNumber);
ImageAnalyzer img = new ImageAnalyzer(await Util.GetPixelBytesFromSoftwareBitmapAsync(FrameRelayVideoEffect.LatestSoftwareBitmap));
img.UpdateDecodedImageSize(FrameRelayVideoEffect.LatestSoftwareBitmap.PixelHeight, FrameRelayVideoEffect.LatestSoftwareBitmap.PixelWidth);
this.queuedVideoFrames.Enqueue(
new VideoFrameData
{
FrameNumber = (int)frameNumber,
Image = img
});
}
if (this.isProcessingPhoto || !this.queuedVideoFrames.Any())
{
return;
}
this.isProcessingPhoto = true;
var queuedVideoFrame = this.queuedVideoFrames.Dequeue();
var analyzer = queuedVideoFrame.Image;
await this.ProcessImage(analyzer);
}
private int GetVideoFrameNumber()
{
// Equivalent of 2 fps
return (int)(this.videoPlayer.Position.TotalMilliseconds / 500);
}
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 ProcessImage(ImageAnalyzer e)
{
if (e == null)
{
this.UpdateUIForNoDriverDetected();
this.isProcessingPhoto = false;
return;
}
DateTime start = DateTime.Now;
await e.DetectFacesAsync(detectFaceAttributes: true, detectFaceLandmarks: true);
TimeSpan latency = DateTime.Now - start;
this.faceLantencyDebugText.Text = string.Format("Face API latency: {0}ms", (int)latency.TotalMilliseconds);
this.highLatencyWarning.Visibility = latency.TotalSeconds <= 1 ? Visibility.Collapsed : Visibility.Visible;
this.StartCaptioningAsync(e);
this.StartDriverIdAsync(e);
await UpdateStateFromFacialFeatures(e);
this.isProcessingPhoto = false;
}
private async void StartCaptioningAsync(ImageAnalyzer e)
{
if (this.isDescribingPhoto || !e.DetectedFaces.Any())
{
return;
}
DateTime start = DateTime.Now;
await e.DescribeAsync();
TimeSpan latency = DateTime.Now - start;
this.visionLantencyDebugText.Text = string.Format("Vision API latency: {0}ms", (int)latency.TotalMilliseconds);
this.highLatencyWarning.Visibility = latency.TotalSeconds <= 3 ? Visibility.Collapsed : Visibility.Visible;
string desc = e.AnalysisResult.Description?.Captions?.FirstOrDefault()?.Text;
bool distractionDetected = desc.Contains("phone") || desc.Contains("banana");
this.distractionChart.DrawDataPoint(distractionDetected ? 1 : 0.02,
distractionDetected ? BarChartAlertColor : BarChartColor,
await GetFaceCropAsync(e),
this.isInputSourceFromVideo ? Controls.WrapBehavior.Slide : Controls.WrapBehavior.Clear);
this.isDescribingPhoto = false;
}
private void UpdateUIForNoDriverDetected()
{
this.faceLantencyDebugText.Text = "";
this.visionLantencyDebugText.Text = "";
this.highLatencyWarning.Visibility = Visibility.Collapsed;
this.driverId.Text = "No faces detected";
}
private async void StartDriverIdAsync(ImageAnalyzer e)
{
if (this.isProcessingDriverId)
{
return;
}
if (!e.DetectedFaces.Any())
{
this.UpdateUIForNoDriverDetected();
return;
}
await Task.WhenAll(e.IdentifyFacesAsync(), e.FindSimilarPersistedFacesAsync());
SimilarFaceMatch faceMatch = e.SimilarFaceMatches.FirstOrDefault();
if (faceMatch != null)
{
string name = "Unknown";
IdentifiedPerson p = e.IdentifiedPersons.FirstOrDefault(f => f.FaceId == faceMatch.Face.FaceId);
if (p != null)
{
name = p.Person.Name;
}
else if (SettingsHelper.Instance.ShowAgeAndGender)
{
switch (faceMatch.Face.FaceAttributes.Gender)
{
case Gender.Male:
name = "Unknown male";
break;
case Gender.Female:
name = "Unknown female";
break;
}
}
this.driverId.Text = string.Format("{0}", name, faceMatch.SimilarPersistedFace.PersistedFaceId.GetValueOrDefault().ToString("N").Substring(0, 4));
}
this.isProcessingDriverId = false;
}
private async Task UpdateStateFromFacialFeatures(ImageAnalyzer e)
{
var f = e.DetectedFaces.FirstOrDefault();
if (f == null)
{
this.UpdateUIForNoDriverDetected();
return;
}
this.ProcessHeadPose(f, await GetFaceCropAsync(e));
this.ProcessMouth(f, await GetFaceCropAsync(e));
this.ProcessEyes(f, await GetFaceCropAsync(e));
}
private void ProcessHeadPose(DetectedFace f, Image img)
{
double headPoseDeviation = Math.Abs(f.FaceAttributes.HeadPose.Yaw);
double deviationRatio = f.FaceAttributes.HeadPose.Yaw / 35;
this.headPoseChart.DrawDataPoint(deviationRatio >= 0 ? Math.Min(Math.Max(0.05, deviationRatio), 1) : Math.Max(Math.Min(-0.05, deviationRatio), -1),
headPoseDeviation <= LookingAwayAngleThreshold ? BarChartColor : BarChartAlertColor,
img,
this.isInputSourceFromVideo ? Controls.WrapBehavior.Slide : Controls.WrapBehavior.Clear);
}
private void ProcessMouth(DetectedFace f, Image img)
{
double mouthWidth = Math.Abs(f.FaceLandmarks.MouthRight.X - f.FaceLandmarks.MouthLeft.X);
double mouthHeight = Math.Abs(f.FaceLandmarks.UpperLipBottom.Y - f.FaceLandmarks.UnderLipTop.Y);
double mouthAperture = mouthHeight / mouthWidth;
mouthAperture = Math.Min((mouthAperture - 0.1) / 0.4, 1);
this.mouthApertureChart.DrawDataPoint(Math.Max(0.05, mouthAperture),
mouthAperture <= YawningApertureThreshold ? BarChartColor : BarChartAlertColor,
img,
this.isInputSourceFromVideo ? Controls.WrapBehavior.Slide : Controls.WrapBehavior.Clear);
}
private void ProcessEyes(DetectedFace f, Image img)
{
double leftEyeWidth = Math.Abs(f.FaceLandmarks.EyeLeftInner.X - f.FaceLandmarks.EyeLeftOuter.X);
double leftEyeHeight = Math.Abs(f.FaceLandmarks.EyeLeftBottom.Y - f.FaceLandmarks.EyeLeftTop.Y);
double rightEyeWidth = Math.Abs(f.FaceLandmarks.EyeRightInner.X - f.FaceLandmarks.EyeRightOuter.X);
double rightEyeHeight = Math.Abs(f.FaceLandmarks.EyeRightBottom.Y - f.FaceLandmarks.EyeRightTop.Y);
double eyeAperture = Math.Max(leftEyeHeight / leftEyeWidth, rightEyeHeight / rightEyeWidth);
eyeAperture = Math.Min((eyeAperture - 0.2) / 0.3, 1);
this.eyeApertureChart.DrawDataPoint(Math.Max(0.05, eyeAperture),
eyeAperture >= SleepingApertureThreshold ? BarChartColor : BarChartAlertColor,
img,
this.isInputSourceFromVideo ? Controls.WrapBehavior.Slide : Controls.WrapBehavior.Clear);
}
protected override async void OnNavigatedTo(NavigationEventArgs e)
{
EnterKioskMode();
if (string.IsNullOrEmpty(SettingsHelper.Instance.FaceApiKey) || string.IsNullOrEmpty(SettingsHelper.Instance.VisionApiKey))
{
await new MessageDialog("Missing Face API or Vision API Key. Please enter a key in the Settings page.", "Missing API Key").ShowAsync();
}
else
{
FaceListManager.FaceListsUserDataFilter = SettingsHelper.Instance.WorkspaceKey + "_RealTimeDriverMonitoring";
await this.cameraControl.StartStreamAsync(isForRealTimeProcessing: true);
this.StartProcessingLoop();
}
base.OnNavigatedTo(e);
}
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 FaceListManager.ResetFaceLists();
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);
}
private async void OpenVideoClicked(object sender, RoutedEventArgs e)
{
FileOpenPicker fileOpenPicker = new FileOpenPicker { SuggestedStartLocation = PickerLocationId.Downloads, ViewMode = PickerViewMode.Thumbnail };
fileOpenPicker.FileTypeFilter.Add(".mp4");
var pickedFile = await fileOpenPicker.PickSingleFileAsync();
if (pickedFile != null)
{
//Set the stream source to the MediaElement control
await this.StartVideoAsync(pickedFile);
this.ResetState();
StartProcessingLoop();
}
}
private async Task StartVideoAsync(StorageFile file)
{
try
{
if (this.videoPlayer.CurrentState == MediaElementState.Playing)
{
this.videoPlayer.Stop();
}
FrameRelayVideoEffect.ResetState();
MediaClip clip = await MediaClip.CreateFromFileAsync(file);
clip.VideoEffectDefinitions.Add(new VideoEffectDefinition(typeof(FrameRelayVideoEffect).FullName));
MediaComposition compositor = new MediaComposition();
compositor.Clips.Add(clip);
this.videoPlayer.SetMediaStreamSource(compositor.GenerateMediaStreamSource());
}
catch (Exception ex)
{
await Util.GenericApiCallExceptionHandler(ex, "Error starting playback.");
}
}
private async void InputSourceChanged(object sender, SelectionChangedEventArgs e)
{
if (this.videoInputSource.IsSelected)
{
await this.cameraControl.StopStreamAsync();
this.cameraControl.Visibility = Visibility.Collapsed;
this.videoPlayer.Visibility = Visibility.Visible;
this.isInputSourceFromVideo = true;
}
else
{
await this.cameraControl.StartStreamAsync(isForRealTimeProcessing: true);
this.cameraControl.Visibility = Visibility.Visible;
this.videoPlayer.Visibility = Visibility.Collapsed;
this.isInputSourceFromVideo = false;
}
this.ResetState();
}
private void ResetState()
{
this.queuedVideoFrames.Clear();
this.processedFrames.Clear();
this.distractionChart.Clear();
this.headPoseChart.Clear();
this.mouthApertureChart.Clear();
this.eyeApertureChart.Clear();
}
private async Task<Image> GetFaceCropAsync(ImageAnalyzer img)
{
ImageSource croppedImage;
if (img.DetectedFaces == null || !img.DetectedFaces.Any())
{
croppedImage = new BitmapImage();
await ((BitmapImage)croppedImage).SetSourceAsync((await img.GetImageStreamCallback()).AsRandomAccessStream());
}
else
{
// Crop the primary face
FaceRectangle rect = img.DetectedFaces.First().FaceRectangle;
double heightScaleFactor = 1.8;
double widthScaleFactor = 1.8;
FaceRectangle biggerRectangle = new FaceRectangle
{
Height = Math.Min((int)(rect.Height * heightScaleFactor), img.DecodedImageHeight),
Width = Math.Min((int)(rect.Width * widthScaleFactor), img.DecodedImageWidth)
};
biggerRectangle.Left = Math.Max(0, rect.Left - (int)(rect.Width * ((widthScaleFactor - 1) / 2)));
biggerRectangle.Top = Math.Max(0, rect.Top - (int)(rect.Height * ((heightScaleFactor - 1) / 1.4)));
croppedImage = await Util.GetCroppedBitmapAsync(img.GetImageStreamCallback, biggerRectangle.ToRect());
}
return new Image { Source = croppedImage, Height = 200 };
}
}
internal class VideoFrameData
{
public double FrameNumber { get; set; }
public ImageAnalyzer Image { get; set; }
}
}