-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainPage.xaml.cs
1087 lines (897 loc) · 47.5 KB
/
MainPage.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
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//*********************************************************
//
// Copyright (c) Microsoft. All rights reserved.
// This code is licensed under the MIT License (MIT).
// THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
// ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
// IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
// PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
//
//*********************************************************
using System;
using System.Reflection;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using Windows.ApplicationModel;
using Windows.Devices.Enumeration;
using Windows.Devices.Sensors;
using Windows.Foundation;
using Windows.Foundation.Metadata;
using Windows.Graphics.Display;
using Windows.Graphics.Imaging;
using Windows.Media;
using Windows.Media.Core;
using Windows.Media.Capture;
using Windows.Media.MediaProperties;
using Windows.Phone.UI.Input;
using Windows.Storage;
using Windows.Storage.FileProperties;
using Windows.Storage.Streams;
using Windows.System.Display;
using Windows.UI.Core;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using Windows.UI.Xaml.Shapes;
using Windows.Media.FaceAnalysis;
using Windows.UI;
using System.Collections.Generic;
using Microsoft.ProjectOxford.Emotion;
using Microsoft.ProjectOxford.Emotion.Contract;
using System.IO;
using Microsoft.ProjectOxford.Common;
namespace FaceDetection
{
public sealed partial class MainPage : Page
{
// Receive notifications about rotation of the device and UI and apply any necessary rotation to the preview stream and UI controls
private readonly DisplayInformation _displayInformation = DisplayInformation.GetForCurrentView();
private readonly SimpleOrientationSensor _orientationSensor = SimpleOrientationSensor.GetDefault();
private SimpleOrientation _deviceOrientation = SimpleOrientation.NotRotated;
private DisplayOrientations _displayOrientation = DisplayOrientations.Portrait;
// Rotation metadata to apply to the preview stream and recorded videos (MF_MT_VIDEO_ROTATION)
// Reference: http://msdn.microsoft.com/en-us/library/windows/apps/xaml/hh868174.aspx
private static readonly Guid RotationKey = new Guid("C380465D-2271-428C-9B83-ECEA3B4A85C1");
// Prevent the screen from sleeping while the camera is running
private readonly DisplayRequest _displayRequest = new DisplayRequest();
// For listening to media property changes
private readonly SystemMediaTransportControls _systemMediaControls = SystemMediaTransportControls.GetForCurrentView();
// MediaCapture and its state variables
private MediaCapture _mediaCapture;
private IMediaEncodingProperties _previewProperties;
private bool _isInitialized;
private bool _isRecording;
// Information about the camera device
private bool _mirroringPreview;
private bool _externalCamera;
private FaceDetectionEffect _faceDetectionEffect;
#region Constructor, lifecycle and navigation
public MainPage()
{
this.InitializeComponent();
// Do not cache the state of the UI when suspending/navigating
NavigationCacheMode = NavigationCacheMode.Disabled;
// Useful to know when to initialize/clean up the camera
Application.Current.Suspending += Application_Suspending;
Application.Current.Resuming += Application_Resuming;
}
private async void Application_Suspending(object sender, SuspendingEventArgs e)
{
// Handle global application events only if this page is active
if (Frame.CurrentSourcePageType == typeof(MainPage))
{
var deferral = e.SuspendingOperation.GetDeferral();
await CleanupCameraAsync();
await CleanupUiAsync();
deferral.Complete();
}
}
private async void Application_Resuming(object sender, object o)
{
// Handle global application events only if this page is active
if (Frame.CurrentSourcePageType == typeof(MainPage))
{
await SetupUiAsync();
await InitializeCameraAsync();
}
}
protected override async void OnNavigatedTo(NavigationEventArgs e)
{
await SetupUiAsync();
await InitializeCameraAsync();
}
protected override async void OnNavigatingFrom(NavigatingCancelEventArgs e)
{
// Handling of this event is included for completenes, as it will only fire when navigating between pages and this sample only includes one page
await CleanupCameraAsync();
await CleanupUiAsync();
}
#endregion Constructor, lifecycle and navigation
#region Event handlers
/// <summary>
/// In the event of the app being minimized this method handles media property change events. If the app receives a mute
/// notification, it is no longer in the foregroud.
/// </summary>
/// <param name="sender"></param>
/// <param name="args"></param>
private async void SystemMediaControls_PropertyChanged(SystemMediaTransportControls sender, SystemMediaTransportControlsPropertyChangedEventArgs args)
{
await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, async () =>
{
// Only handle this event if this page is currently being displayed
if (args.Property == SystemMediaTransportControlsProperty.SoundLevel && Frame.CurrentSourcePageType == typeof(MainPage))
{
// Check to see if the app is being muted. If so, it is being minimized.
// Otherwise if it is not initialized, it is being brought into focus.
if (sender.SoundLevel == SoundLevel.Muted)
{
await CleanupCameraAsync();
}
else if (!_isInitialized)
{
await InitializeCameraAsync();
}
}
});
}
/// <summary>
/// Occurs each time the simple orientation sensor reports a new sensor reading.
/// </summary>
/// <param name="sender">The event source.</param>
/// <param name="args">The event data.</param>
private async void OrientationSensor_OrientationChanged(SimpleOrientationSensor sender, SimpleOrientationSensorOrientationChangedEventArgs args)
{
if (args.Orientation != SimpleOrientation.Faceup && args.Orientation != SimpleOrientation.Facedown)
{
// Only update the current orientation if the device is not parallel to the ground. This allows users to take pictures of documents (FaceUp)
// or the ceiling (FaceDown) in portrait or landscape, by first holding the device in the desired orientation, and then pointing the camera
// either up or down, at the desired subject.
//Note: This assumes that the camera is either facing the same way as the screen, or the opposite way. For devices with cameras mounted
// on other panels, this logic should be adjusted.
_deviceOrientation = args.Orientation;
await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => UpdateButtonOrientation());
}
}
/// <summary>
/// This event will fire when the page is rotated, when the DisplayInformation.AutoRotationPreferences value set in the SetupUiAsync() method cannot be not honored.
/// </summary>
/// <param name="sender">The event source.</param>
/// <param name="args">The event data.</param>
private async void DisplayInformation_OrientationChanged(DisplayInformation sender, object args)
{
_displayOrientation = sender.CurrentOrientation;
if (_previewProperties != null)
{
await SetPreviewRotationAsync();
}
await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => UpdateButtonOrientation());
}
private async void PhotoButton_Tapped(object sender, TappedRoutedEventArgs e)
{
await TakePhotoAsync();
}
private async void FaceDetectionButton_Tapped(object sender, TappedRoutedEventArgs e)
{
if (_faceDetectionEffect == null || !_faceDetectionEffect.Enabled)
{
// Clear any rectangles that may have been left over from a previous instance of the effect
FacesCanvas.Children.Clear();
await CreateFaceDetectionEffectAsync();
}
else
{
await CleanUpFaceDetectionEffectAsync();
}
UpdateCaptureControls();
}
private async void HardwareButtons_CameraPressed(object sender, CameraEventArgs e)
{
await TakePhotoAsync();
}
private async void MediaCapture_Failed(MediaCapture sender, MediaCaptureFailedEventArgs errorEventArgs)
{
Debug.WriteLine("MediaCapture_Failed: (0x{0:X}) {1}", errorEventArgs.Code, errorEventArgs.Message);
await CleanupCameraAsync();
await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => UpdateCaptureControls());
}
private async void FaceDetectionEffect_FaceDetected(FaceDetectionEffect sender, FaceDetectedEventArgs args)
{
// Ask the UI thread to render the face bounding boxes
await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => HighlightDetectedFaces(args.ResultFrame.DetectedFaces));
}
#endregion Event handlers
#region MediaCapture methods
/// <summary>
/// Initializes the MediaCapture, registers events, gets camera device information for mirroring and rotating, starts preview and unlocks the UI
/// </summary>
/// <returns></returns>
private async Task InitializeCameraAsync()
{
Debug.WriteLine("InitializeCameraAsync");
if (_mediaCapture == null)
{
// Attempt to get the front camera if one is available, but use any camera device if not
var cameraDevice = await FindCameraDeviceByPanelAsync(Windows.Devices.Enumeration.Panel.Front);
if (cameraDevice == null)
{
Debug.WriteLine("No camera device found!");
return;
}
// Create MediaCapture and its settings
_mediaCapture = new MediaCapture();
_mediaCapture.Failed += MediaCapture_Failed;
var settings = new MediaCaptureInitializationSettings { VideoDeviceId = cameraDevice.Id };
// Initialize MediaCapture
try
{
await _mediaCapture.InitializeAsync(settings);
_isInitialized = true;
}
catch (UnauthorizedAccessException)
{
Debug.WriteLine("The app was denied access to the camera");
}
// If initialization succeeded, start the preview
if (_isInitialized)
{
// Figure out where the camera is located
if (cameraDevice.EnclosureLocation == null || cameraDevice.EnclosureLocation.Panel == Windows.Devices.Enumeration.Panel.Unknown)
{
// No information on the location of the camera, assume it's an external camera, not integrated on the device
_externalCamera = true;
}
else
{
// Camera is fixed on the device
_externalCamera = false;
// Only mirror the preview if the camera is on the front panel
_mirroringPreview = (cameraDevice.EnclosureLocation.Panel == Windows.Devices.Enumeration.Panel.Front);
}
await StartPreviewAsync();
UpdateCaptureControls();
}
}
}
/// <summary>
/// Starts the preview and adjusts it for for rotation and mirroring after making a request to keep the screen on
/// </summary>
/// <returns></returns>
private async Task StartPreviewAsync()
{
// Prevent the device from sleeping while the preview is running
_displayRequest.RequestActive();
// Set the preview source in the UI and mirror it if necessary
PreviewControl.Source = _mediaCapture;
PreviewControl.FlowDirection = _mirroringPreview ? FlowDirection.RightToLeft : FlowDirection.LeftToRight;
// Start the preview
await _mediaCapture.StartPreviewAsync();
_previewProperties = _mediaCapture.VideoDeviceController.GetMediaStreamProperties(MediaStreamType.VideoPreview);
// Initialize the preview to the current orientation
if (_previewProperties != null)
{
_displayOrientation = _displayInformation.CurrentOrientation;
await SetPreviewRotationAsync();
}
}
/// <summary>
/// Gets the current orientation of the UI in relation to the device (when AutoRotationPreferences cannot be honored) and applies a corrective rotation to the preview
/// </summary>
private async Task SetPreviewRotationAsync()
{
// Only need to update the orientation if the camera is mounted on the device
if (_externalCamera) return;
// Calculate which way and how far to rotate the preview
int rotationDegrees = ConvertDisplayOrientationToDegrees(_displayOrientation);
// The rotation direction needs to be inverted if the preview is being mirrored
if (_mirroringPreview)
{
rotationDegrees = (360 - rotationDegrees) % 360;
}
// Add rotation metadata to the preview stream to make sure the aspect ratio / dimensions match when rendering and getting preview frames
var props = _mediaCapture.VideoDeviceController.GetMediaStreamProperties(MediaStreamType.VideoPreview);
props.Properties.Add(RotationKey, rotationDegrees);
await _mediaCapture.SetEncodingPropertiesAsync(MediaStreamType.VideoPreview, props, null);
}
/// <summary>
/// Stops the preview and deactivates a display request, to allow the screen to go into power saving modes
/// </summary>
/// <returns></returns>
private async Task StopPreviewAsync()
{
// Stop the preview
_previewProperties = null;
await _mediaCapture.StopPreviewAsync();
// Use the dispatcher because this method is sometimes called from non-UI threads
await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
{
// Cleanup the UI
PreviewControl.Source = null;
// Allow the device screen to sleep now that the preview is stopped
_displayRequest.RequestRelease();
});
}
/// <summary>
/// Adds face detection to the preview stream, registers for its events, enables it, and gets the FaceDetectionEffect instance
/// </summary>
/// <returns></returns>
private async Task CreateFaceDetectionEffectAsync()
{
// Create the definition, which will contain some initialization settings
var definition = new FaceDetectionEffectDefinition();
// To ensure preview smoothness, do not delay incoming samples
definition.SynchronousDetectionEnabled = false;
// In this scenario, choose detection speed over accuracy
definition.DetectionMode = FaceDetectionMode.HighPerformance;
// Add the effect to the preview stream
_faceDetectionEffect = (FaceDetectionEffect)await _mediaCapture.AddVideoEffectAsync(definition, MediaStreamType.VideoPreview);
// Register for face detection events
_faceDetectionEffect.FaceDetected += FaceDetectionEffect_FaceDetected;
// Choose the shortest interval between detection events
_faceDetectionEffect.DesiredDetectionInterval = TimeSpan.FromMilliseconds(33);
// Start detecting faces
_faceDetectionEffect.Enabled = true;
}
/// <summary>
/// Disables and removes the face detection effect, and unregisters the event handler for face detection
/// </summary>
/// <returns></returns>
private async Task CleanUpFaceDetectionEffectAsync()
{
// Disable detection
_faceDetectionEffect.Enabled = false;
// Unregister the event handler
_faceDetectionEffect.FaceDetected -= FaceDetectionEffect_FaceDetected;
// Remove the effect from the preview stream
await _mediaCapture.ClearEffectsAsync(MediaStreamType.VideoPreview);
// Clear the member variable that held the effect instance
_faceDetectionEffect = null;
}
/// <summary>
/// Takes a photo to a StorageFile and adds rotation metadata to it
/// </summary>
/// <returns></returns>
private async Task TakePhotoAsync()
{
var stream = new InMemoryRandomAccessStream();
try
{
Debug.WriteLine("Taking photo...");
await _mediaCapture.CapturePhotoToStreamAsync(ImageEncodingProperties.CreateJpeg(), stream);
Debug.WriteLine("Photo taken!");
var photoOrientation = ConvertOrientationToPhotoOrientation(GetCameraOrientation());
var file = await ReencodeAndSavePhotoAsync(stream, photoOrientation);
Emotion[] emotions = await GetEmotionsAsync(file);
await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => HighlightDetectedFacesWithEmotions(emotions));
Debug.WriteLine($"{emotions}");
//await ReencodeAndSavePhotoAsync(stream, photoOrientation);
}
catch (Exception ex)
{
// File I/O errors are reported as exceptions
Debug.WriteLine("Exception when taking a photo: {0}", ex.ToString());
}
}
private async Task<Emotion[]> GetEmotionsAsync(StorageFile imageFile) {
//
// Create Project Oxford Emotion API Service client
//
EmotionServiceClient emotionServiceClient = new EmotionServiceClient("9fa5d104b72046f085aa1b4b379f03a3");
Debug.WriteLine("Calling EmotionServiceClient.RecognizeAsync()...");
try
{
using (Stream imageFileStream = await Task.Run<Stream>(() => File.OpenRead(imageFile.Path)))
{
//
// Detect the emotions in the file
//
var emotionResult = await emotionServiceClient.RecognizeAsync(imageFileStream);
return emotionResult;
}
}
catch (Exception exception)
{
Debug.WriteLine(exception.ToString());
return null;
}
}
/// <summary>
/// Cleans up the camera resources (after stopping any video recording and/or preview if necessary) and unregisters from MediaCapture events
/// </summary>
/// <returns></returns>
private async Task CleanupCameraAsync()
{
Debug.WriteLine("CleanupCameraAsync");
if (_isInitialized)
{
if (_faceDetectionEffect != null)
{
await CleanUpFaceDetectionEffectAsync();
}
if (_previewProperties != null)
{
// The call to stop the preview is included here for completeness, but can be
// safely removed if a call to MediaCapture.Dispose() is being made later,
// as the preview will be automatically stopped at that point
await StopPreviewAsync();
}
_isInitialized = false;
}
if (_mediaCapture != null)
{
_mediaCapture.Failed -= MediaCapture_Failed;
_mediaCapture.Dispose();
_mediaCapture = null;
}
}
#endregion MediaCapture methods
#region Helper functions
/// <summary>
/// Attempts to lock the page orientation, hide the StatusBar (on Phone) and registers event handlers for hardware buttons and orientation sensors
/// </summary>
/// <returns></returns>
private async Task SetupUiAsync()
{
// Attempt to lock page to landscape orientation to prevent the CaptureElement from rotating, as this gives a better experience
DisplayInformation.AutoRotationPreferences = DisplayOrientations.Landscape;
// Hide the status bar
if (ApiInformation.IsTypePresent("Windows.UI.ViewManagement.StatusBar"))
{
await Windows.UI.ViewManagement.StatusBar.GetForCurrentView().HideAsync();
}
// Populate orientation variables with the current state
_displayOrientation = _displayInformation.CurrentOrientation;
if (_orientationSensor != null)
{
_deviceOrientation = _orientationSensor.GetCurrentOrientation();
}
RegisterEventHandlers();
}
/// <summary>
/// Unregisters event handlers for hardware buttons and orientation sensors, allows the StatusBar (on Phone) to show, and removes the page orientation lock
/// </summary>
/// <returns></returns>
private async Task CleanupUiAsync()
{
UnregisterEventHandlers();
// Show the status bar
if (ApiInformation.IsTypePresent("Windows.UI.ViewManagement.StatusBar"))
{
await Windows.UI.ViewManagement.StatusBar.GetForCurrentView().ShowAsync();
}
// Revert orientation preferences
DisplayInformation.AutoRotationPreferences = DisplayOrientations.None;
}
/// <summary>
/// This method will update the icons, enable/disable and show/hide the photo/video buttons depending on the current state of the app and the capabilities of the device
/// </summary>
private void UpdateCaptureControls()
{
// The buttons should only be enabled if the preview started sucessfully
PhotoButton.IsEnabled = _previewProperties != null;
FaceDetectionButton.IsEnabled = _previewProperties != null;
// Update the face detection icon depending on whether the effect exists or not
FaceDetectionDisabledIcon.Visibility = (_faceDetectionEffect == null || !_faceDetectionEffect.Enabled) ? Visibility.Visible : Visibility.Collapsed;
FaceDetectionEnabledIcon.Visibility = (_faceDetectionEffect != null && _faceDetectionEffect.Enabled) ? Visibility.Visible : Visibility.Collapsed;
// Hide the face detection canvas and clear it
FacesCanvas.Visibility = Visibility.Visible;//(_faceDetectionEffect != null && _faceDetectionEffect.Enabled) ? Visibility.Visible : Visibility.Collapsed;
// If the camera doesn't support simultaneosly taking pictures and recording video, disable the photo button on record
if (_isInitialized && !_mediaCapture.MediaCaptureSettings.ConcurrentRecordAndPhotoSupported)
{
PhotoButton.IsEnabled = !_isRecording;
// Make the button invisible if it's disabled, so it's obvious it cannot be interacted with
PhotoButton.Opacity = PhotoButton.IsEnabled ? 1 : 0;
}
}
/// <summary>
/// Registers event handlers for hardware buttons and orientation sensors, and performs an initial update of the UI rotation
/// </summary>
private void RegisterEventHandlers()
{
if (ApiInformation.IsTypePresent("Windows.Phone.UI.Input.HardwareButtons"))
{
HardwareButtons.CameraPressed += HardwareButtons_CameraPressed;
}
// If there is an orientation sensor present on the device, register for notifications
if (_orientationSensor != null)
{
_orientationSensor.OrientationChanged += OrientationSensor_OrientationChanged;
// Update orientation of buttons with the current orientation
UpdateButtonOrientation();
}
_displayInformation.OrientationChanged += DisplayInformation_OrientationChanged;
_systemMediaControls.PropertyChanged += SystemMediaControls_PropertyChanged;
}
/// <summary>
/// Unregisters event handlers for hardware buttons and orientation sensors
/// </summary>
private void UnregisterEventHandlers()
{
if (ApiInformation.IsTypePresent("Windows.Phone.UI.Input.HardwareButtons"))
{
HardwareButtons.CameraPressed -= HardwareButtons_CameraPressed;
}
if (_orientationSensor != null)
{
_orientationSensor.OrientationChanged -= OrientationSensor_OrientationChanged;
}
_displayInformation.OrientationChanged -= DisplayInformation_OrientationChanged;
_systemMediaControls.PropertyChanged -= SystemMediaControls_PropertyChanged;
}
/// <summary>
/// Attempts to find and return a device mounted on the panel specified, and on failure to find one it will return the first device listed
/// </summary>
/// <param name="desiredPanel">The desired panel on which the returned device should be mounted, if available</param>
/// <returns></returns>
private static async Task<DeviceInformation> FindCameraDeviceByPanelAsync(Windows.Devices.Enumeration.Panel desiredPanel)
{
// Get available devices for capturing pictures
var allVideoDevices = await DeviceInformation.FindAllAsync(DeviceClass.VideoCapture);
// Get the desired camera by panel
DeviceInformation desiredDevice = allVideoDevices.FirstOrDefault(x => x.EnclosureLocation != null && x.EnclosureLocation.Panel == desiredPanel);
// If there is no device mounted on the desired panel, return the first device found
return desiredDevice ?? allVideoDevices.FirstOrDefault();
}
/// <summary>
/// Applies the given orientation to a photo stream and saves it as a StorageFile
/// </summary>
/// <param name="stream">The photo stream</param>
/// <param name="photoOrientation">The orientation metadata to apply to the photo</param>
/// <returns></returns>
private static async Task<StorageFile> ReencodeAndSavePhotoAsync(IRandomAccessStream stream, PhotoOrientation photoOrientation)
{
using (var inputStream = stream)
{
var decoder = await BitmapDecoder.CreateAsync(inputStream);
var file = await KnownFolders.PicturesLibrary.CreateFileAsync("SimplePhoto.jpeg", CreationCollisionOption.GenerateUniqueName);
using (var outputStream = await file.OpenAsync(FileAccessMode.ReadWrite))
{
var encoder = await BitmapEncoder.CreateForTranscodingAsync(outputStream, decoder);
var properties = new BitmapPropertySet { { "System.Photo.Orientation", new BitmapTypedValue(photoOrientation, PropertyType.UInt16) } };
await encoder.BitmapProperties.SetPropertiesAsync(properties);
await encoder.FlushAsync();
}
return file;
}
}
#endregion Helper functions
#region Rotation helpers
/// <summary>
/// Calculates the current camera orientation from the device orientation by taking into account whether the camera is external or facing the user
/// </summary>
/// <returns>The camera orientation in space, with an inverted rotation in the case the camera is mounted on the device and is facing the user</returns>
private SimpleOrientation GetCameraOrientation()
{
if (_externalCamera)
{
// Cameras that are not attached to the device do not rotate along with it, so apply no rotation
return SimpleOrientation.NotRotated;
}
var result = _deviceOrientation;
// Account for the fact that, on portrait-first devices, the camera sensor is mounted at a 90 degree offset to the native orientation
if (_displayInformation.NativeOrientation == DisplayOrientations.Portrait)
{
switch (result)
{
case SimpleOrientation.Rotated90DegreesCounterclockwise:
result = SimpleOrientation.NotRotated;
break;
case SimpleOrientation.Rotated180DegreesCounterclockwise:
result = SimpleOrientation.Rotated90DegreesCounterclockwise;
break;
case SimpleOrientation.Rotated270DegreesCounterclockwise:
result = SimpleOrientation.Rotated180DegreesCounterclockwise;
break;
case SimpleOrientation.NotRotated:
result = SimpleOrientation.Rotated270DegreesCounterclockwise;
break;
}
}
// If the preview is being mirrored for a front-facing camera, then the rotation should be inverted
if (_mirroringPreview)
{
// This only affects the 90 and 270 degree cases, because rotating 0 and 180 degrees is the same clockwise and counter-clockwise
switch (result)
{
case SimpleOrientation.Rotated90DegreesCounterclockwise:
return SimpleOrientation.Rotated270DegreesCounterclockwise;
case SimpleOrientation.Rotated270DegreesCounterclockwise:
return SimpleOrientation.Rotated90DegreesCounterclockwise;
}
}
return result;
}
/// <summary>
/// Converts the given orientation of the device in space to the corresponding rotation in degrees
/// </summary>
/// <param name="orientation">The orientation of the device in space</param>
/// <returns>An orientation in degrees</returns>
private static int ConvertDeviceOrientationToDegrees(SimpleOrientation orientation)
{
switch (orientation)
{
case SimpleOrientation.Rotated90DegreesCounterclockwise:
return 90;
case SimpleOrientation.Rotated180DegreesCounterclockwise:
return 180;
case SimpleOrientation.Rotated270DegreesCounterclockwise:
return 270;
case SimpleOrientation.NotRotated:
default:
return 0;
}
}
/// <summary>
/// Converts the given orientation of the app on the screen to the corresponding rotation in degrees
/// </summary>
/// <param name="orientation">The orientation of the app on the screen</param>
/// <returns>An orientation in degrees</returns>
private static int ConvertDisplayOrientationToDegrees(DisplayOrientations orientation)
{
switch (orientation)
{
case DisplayOrientations.Portrait:
return 90;
case DisplayOrientations.LandscapeFlipped:
return 180;
case DisplayOrientations.PortraitFlipped:
return 270;
case DisplayOrientations.Landscape:
default:
return 0;
}
}
/// <summary>
/// Converts the given orientation of the device in space to the metadata that can be added to captured photos
/// </summary>
/// <param name="orientation">The orientation of the device in space</param>
/// <returns></returns>
private static PhotoOrientation ConvertOrientationToPhotoOrientation(SimpleOrientation orientation)
{
switch (orientation)
{
case SimpleOrientation.Rotated90DegreesCounterclockwise:
return PhotoOrientation.Rotate90;
case SimpleOrientation.Rotated180DegreesCounterclockwise:
return PhotoOrientation.Rotate180;
case SimpleOrientation.Rotated270DegreesCounterclockwise:
return PhotoOrientation.Rotate270;
case SimpleOrientation.NotRotated:
default:
return PhotoOrientation.Normal;
}
}
/// <summary>
/// Uses the current device orientation in space and page orientation on the screen to calculate the rotation
/// transformation to apply to the controls
/// </summary>
/// <returns>An angle in degrees to rotate the controls so they remain upright to the user regardless of device and page
/// orientation</returns>
private void UpdateButtonOrientation()
{
int device = ConvertDeviceOrientationToDegrees(_deviceOrientation);
int display = ConvertDisplayOrientationToDegrees(_displayOrientation);
if (_displayInformation.NativeOrientation == DisplayOrientations.Portrait)
{
device -= 90;
}
// Combine both rotations and make sure that 0 <= result < 360
var angle = (360 + display + device) % 360;
// Rotate the buttons in the UI to match the rotation of the device
var transform = new RotateTransform { Angle = angle };
// The RenderTransform is safe to use (i.e. it won't cause layout issues) in this case, because these buttons have a 1:1 aspect ratio
PhotoButton.RenderTransform = transform;
FaceDetectionButton.RenderTransform = transform;
}
/// <summary>
/// Uses the current display orientation to calculate the rotation transformation to apply to the face detection bounding box canvas
/// and mirrors it if the preview is being mirrored
/// </summary>
private void SetFacesCanvasRotation()
{
// Calculate how much to rotate the canvas
int rotationDegrees = ConvertDisplayOrientationToDegrees(_displayOrientation);
// The rotation direction needs to be inverted if the preview is being mirrored, just like in SetPreviewRotationAsync
if (_mirroringPreview)
{
rotationDegrees = (360 - rotationDegrees) % 360;
}
// Apply the rotation
var transform = new RotateTransform { Angle = rotationDegrees };
FacesCanvas.RenderTransform = transform;
var previewArea = GetPreviewStreamRectInControl(_previewProperties as VideoEncodingProperties, PreviewControl);
// For portrait mode orientations, swap the width and height of the canvas after the rotation, so the control continues to overlap the preview
if (_displayOrientation == DisplayOrientations.Portrait || _displayOrientation == DisplayOrientations.PortraitFlipped)
{
FacesCanvas.Width = previewArea.Height;
FacesCanvas.Height = previewArea.Width;
// The position of the canvas also needs to be adjusted, as the size adjustment affects the centering of the control
Canvas.SetLeft(FacesCanvas, previewArea.X - (previewArea.Height - previewArea.Width) / 2);
Canvas.SetTop(FacesCanvas, previewArea.Y - (previewArea.Width - previewArea.Height) / 2);
}
else
{
FacesCanvas.Width = previewArea.Width;
FacesCanvas.Height = previewArea.Height;
Canvas.SetLeft(FacesCanvas, previewArea.X);
Canvas.SetTop(FacesCanvas, previewArea.Y);
}
// Also mirror the canvas if the preview is being mirrored
FacesCanvas.FlowDirection = _mirroringPreview ? FlowDirection.RightToLeft : FlowDirection.LeftToRight;
}
#endregion Rotation helpers
#region Face detection helpers
/// <summary>
/// Iterates over all detected faces, creating and adding Rectangles to the FacesCanvas as face bounding boxes
/// </summary>
/// <param name="faces">The list of detected faces from the FaceDetected event of the effect</param>
private void HighlightDetectedFaces(IReadOnlyList<DetectedFace> faces)
{
// Remove any existing rectangles from previous events
FacesCanvas.Children.Clear();
// For each detected face
for (int i = 0; i < faces.Count; i++)
{
// Face coordinate units are preview resolution pixels, which can be a different scale from our display resolution, so a conversion may be necessary
Windows.UI.Xaml.Shapes.Rectangle faceBoundingBox = ConvertPreviewToUiRectangle(faces[i].FaceBox);
// Set bounding box stroke properties
faceBoundingBox.StrokeThickness = 2;
// Highlight the first face in the set
faceBoundingBox.Stroke = (i == 0 ? new SolidColorBrush(Colors.Blue) : new SolidColorBrush(Colors.DeepSkyBlue));
// Add grid to canvas containing all face UI objects
FacesCanvas.Children.Add(faceBoundingBox);
}
// Update the face detection bounding box canvas orientation
SetFacesCanvasRotation();
}
private void HighlightDetectedFacesWithEmotions(Emotion[] faces)
{
// Remove any existing rectangles from previous events
FacesCanvas.Children.Clear();
// For each detected face
for (int i = 0; i < faces?.Length; i++)
{
Emotion face = faces[i];
// Face coordinate units are preview resolution pixels, which can be a different scale from our display resolution, so a conversion may be necessary
Windows.UI.Xaml.Shapes.Rectangle faceBoundingBox = ConvertPreviewToUiRectangle(face.FaceRectangle);
// Set bounding box stroke properties
faceBoundingBox.StrokeThickness = 2;
// Highlight the first face in the set
faceBoundingBox.Stroke = (i == 0 ? new SolidColorBrush(Colors.Blue) : new SolidColorBrush(Colors.DeepSkyBlue));
// Add grid to canvas containing all face UI objects
FacesCanvas.Children.Add(faceBoundingBox);
var left = Canvas.GetLeft(faceBoundingBox);
var bottom = Canvas.GetTop(faceBoundingBox) + faceBoundingBox.Height;
var k = 0;
var scores = face.Scores;
foreach (PropertyInfo pI in scores.GetType().GetProperties()) {
var score = (float) pI.GetValue(scores);
FacesCanvas.Children.Add(CreateText($"{pI.Name}: {score.ToString("0.00")}", left, bottom + 20 * k, 20));
k++;
}
}
// Update the face detection bounding box canvas orientation
SetFacesCanvasRotation();
}
private Windows.UI.Xaml.Shapes.Rectangle ConvertPreviewToUiRectangle(Microsoft.ProjectOxford.Common.Rectangle faceRectangle)
{
var result = new Windows.UI.Xaml.Shapes.Rectangle();
var previewStream = _previewProperties as VideoEncodingProperties;
// If there is no available information about the preview, return an empty rectangle, as re-scaling to the screen coordinates will be impossible
if (previewStream == null) return result;
// Similarly, if any of the dimensions is zero (which would only happen in an error case) return an empty rectangle
if (previewStream.Width == 0 || previewStream.Height == 0) return result;
double streamWidth = previewStream.Width;
double streamHeight = previewStream.Height;
// For portrait orientations, the width and height need to be swapped
if (_displayOrientation == DisplayOrientations.Portrait || _displayOrientation == DisplayOrientations.PortraitFlipped)
{
streamHeight = previewStream.Width;
streamWidth = previewStream.Height;
}
// Get the rectangle that is occupied by the actual video feed
var previewInUI = GetPreviewStreamRectInControl(previewStream, PreviewControl);
// Scale the width and height from preview stream coordinates to window coordinates
result.Width = (faceRectangle.Width / streamWidth) * previewInUI.Width;
result.Height = (faceRectangle.Height / streamHeight) * previewInUI.Height;
// Scale the X and Y coordinates from preview stream coordinates to window coordinates
var x = (faceRectangle.Left / streamWidth) * previewInUI.Width;
var y = (faceRectangle.Top / streamHeight) * previewInUI.Height;
Canvas.SetLeft(result, x);
Canvas.SetTop(result, y);
return result;
}
/// <summary>
/// Takes face information defined in preview coordinates and returns one in UI coordinates, taking
/// into account the position and size of the preview control.
/// </summary>
/// <param name="faceBoxInPreviewCoordinates">Face coordinates as retried from the FaceBox property of a DetectedFace, in preview coordinates.</param>
/// <returns>Rectangle in UI (CaptureElement) coordinates, to be used in a Canvas control.</returns>
private Windows.UI.Xaml.Shapes.Rectangle ConvertPreviewToUiRectangle(BitmapBounds faceBoxInPreviewCoordinates)
{
var result = new Windows.UI.Xaml.Shapes.Rectangle();
var previewStream = _previewProperties as VideoEncodingProperties;
// If there is no available information about the preview, return an empty rectangle, as re-scaling to the screen coordinates will be impossible
if (previewStream == null) return result;
// Similarly, if any of the dimensions is zero (which would only happen in an error case) return an empty rectangle
if (previewStream.Width == 0 || previewStream.Height == 0) return result;
double streamWidth = previewStream.Width;
double streamHeight = previewStream.Height;
// For portrait orientations, the width and height need to be swapped
if (_displayOrientation == DisplayOrientations.Portrait || _displayOrientation == DisplayOrientations.PortraitFlipped)
{
streamHeight = previewStream.Width;
streamWidth = previewStream.Height;