/
MainWindow.xaml.cs
891 lines (770 loc) · 33.1 KB
/
MainWindow.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
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="MainWindow.xaml.cs" company="Microsoft">
// Copyright (c) Microsoft Corporation. All rights reserved.
// </copyright>
// --------------------------------------------------------------------------------------------------------------------
namespace Microsoft.Samples.Kinect.HDFaceBasics
{
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using Microsoft.Kinect;
using System.Windows.Media.Media3D;
using Microsoft.Kinect.Face;
/// <summary>
/// Main Window
/// </summary>
public partial class MainWindow : Window, INotifyPropertyChanged, IDisposable
{
string MyFile = "C:\\Users\\Georgios Papangelaki\\Desktop\\HDFaceBasics-WPF\\KinectHDFaceFrameReader.txt";
System.IO.StreamWriter Writer;
int totalFrames = 0;
string TOPRINT = "";
int FrameBlink = 0;
int BetweenBlink = 0;
int EndBlink = 0;
public List<TimeSpan> Clock;
public List<List<CameraSpacePoint>> PointsData;
public List<CameraSpacePoint> TempList;
public List<int> LEyeIndex;
public List<int> REyeIndex;
public List<int> FrameBetFreq;
public struct BiometricData
{
public String Name;
public int TestSubject;
public double FreqOfBlinks;
public List<List<List<Double>>> SpeedOfBlinks;
public List<List<List<Double>>> AccelOfBlinks;
}
BiometricData NewSubject;
/// <summary>
/// Currently used KinectSensor
/// </summary>
private KinectSensor sensor = null;
/// <summary>
/// Body frame source to get a BodyFrameReader
/// </summary>
private BodyFrameSource bodySource = null;
/// <summary>
/// Body frame reader to get body frames
/// </summary>
private BodyFrameReader bodyReader = null;
/// <summary>
/// HighDefinitionFaceFrameSource to get a reader and a builder from.
/// Also to set the currently tracked user id to get High Definition Face Frames of
/// </summary>
private HighDefinitionFaceFrameSource highDefinitionFaceFrameSource = null;
/// <summary>
/// HighDefinitionFaceFrameReader to read HighDefinitionFaceFrame to get FaceAlignment
/// </summary>
private HighDefinitionFaceFrameReader highDefinitionFaceFrameReader = null;
private FaceFrameSource faceFrameSource = null;
/// <summary>
/// Face frame readers
/// </summary>
private FaceFrameReader faceFrameReader = null;
/// <summary>
/// Storage for face frame results
/// </summary>
private FaceFrameResult faceFrameResult = null;
private int bodyCount;
private CoordinateMapper coordinateMapper = null;
/// <summary>
/// FaceAlignment is the result of tracking a face, it has face animations location and orientation
/// </summary>
private FaceAlignment currentFaceAlignment = null;
/// <summary>
/// FaceModel is a result of capturing a face
/// </summary>
private FaceModel currentFaceModel = null;
/// <summary>
/// FaceModelBuilder is used to produce a FaceModel
/// </summary>
private FaceModelBuilder faceModelBuilder = null;
/// <summary>
/// The currently tracked body
/// </summary>
private Body currentTrackedBody = null;
/// <summary>
/// The currently tracked body
/// </summary>
private ulong currentTrackingId = 0;
/// <summary>
/// Gets or sets the current tracked user id
/// </summary>
private string currentBuilderStatus = string.Empty;
/// <summary>
/// Gets or sets the current status text to display
/// </summary>
private string statusText = "Ready To Start Capture";
/// <summary>
/// Initializes a new instance of the MainWindow class.
/// </summary>
public MainWindow()
{
this.InitializeComponent();
this.DataContext = this;
this.Clock = new List<TimeSpan>();
PointsData = new List<List<CameraSpacePoint>>();
Writer = new System.IO.StreamWriter(MyFile, true);
LEyeIndex = new List<int> { 113, 121, 123, 137, 153, 185, 187, 188, 211, 212, 229, 230, 234, 236, 237, 238, 240, 241, 242, 243, 244, 265, 287, 316, 325, 326, 327, 328, 330, 331, 332, 333, 341, 343, 348, 349, 350, 467, 1075, 1103, 1104, 1105, 1106, 1107, 1108, 1109, 1111, 1112, 1113, 1114 };
REyeIndex = new List<int> { 728, 729, 730, 731, 732, 733, 749, 750, 751, 752, 775, 776, 777, 778, 821, 826, 841, 842, 845, 846, 862, 863, 865, 869, 870, 871, 873, 874, 875, 876, 877, 882, 883, 925, 985, 986, 987, 992, 1089, 1090, 1091, 1092, 1093, 1094, 1095, 1096, 1097, 1099, 1100, 1101 };
FrameBetFreq = new List<int>();
NewSubject.SpeedOfBlinks = new List<List<List<double>>>();
NewSubject.AccelOfBlinks = new List<List<List<double>>>();
NewSubject.Name = "TO SET";
NewSubject.TestSubject = 1;
}
/// <summary>
/// INotifyPropertyChangedPropertyChanged event to allow window controls to bind to changeable data
/// </summary>
public event PropertyChangedEventHandler PropertyChanged;
/// <summary>
/// Gets or sets the current status text to display
/// </summary>
public string StatusText
{
get
{
return this.statusText;
}
set
{
if (this.statusText != value)
{
this.statusText = value;
// notify any bound elements that the text has changed
if (this.PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs("StatusText"));
}
}
}
}
/// <summary>
/// Gets or sets the current tracked user id
/// </summary>
private ulong CurrentTrackingId
{
get
{
return this.currentTrackingId;
}
set
{
this.currentTrackingId = value;
this.StatusText = this.MakeStatusText();
}
}
/// <summary>
/// Gets or sets the current Face Builder instructions to user
/// </summary>
private string CurrentBuilderStatus
{
get
{
return this.currentBuilderStatus;
}
set
{
this.currentBuilderStatus = value;
this.StatusText = this.MakeStatusText();
}
}
/// <summary>
/// Called when disposed of
/// </summary>
public void Dispose()
{
this.Dispose(true);
GC.SuppressFinalize(this);
}
/// <summary>
/// Dispose based on whether or not managed or native resources should be freed
/// </summary>
/// <param name="disposing">Set to true to free both native and managed resources, false otherwise</param>
protected virtual void Dispose(bool disposing)
{
if (disposing)
{
if (this.currentFaceModel != null)
{
this.currentFaceModel.Dispose();
this.currentFaceModel = null;
}
}
}
/// <summary>
/// Returns the length of a vector from origin
/// </summary>
/// <param name="point">Point in space to find it's distance from origin</param>
/// <returns>Distance from origin</returns>
private static double VectorLength(CameraSpacePoint point)
{
var result = Math.Pow(point.X, 2) + Math.Pow(point.Y, 2) + Math.Pow(point.Z, 2);
result = Math.Sqrt(result);
return result;
}
private double DistBetPoints(CameraSpacePoint P1, CameraSpacePoint P2)
{
return Math.Sqrt(((P2.X - P1.X) * (P2.X - P1.X)) + ((P2.Y - P1.Y) * (P2.Y - P1.Y)) + ((P2.Z - P1.Z) * (P2.Z - P1.Z)));
}
private double SpeedBetPoints(CameraSpacePoint P1, CameraSpacePoint P2)
{
return DistBetPoints(P1, P2) / 0.0333333333;
}
private double AccelBetPoints(CameraSpacePoint P1, CameraSpacePoint P2, CameraSpacePoint P3)
{
return (SpeedBetPoints(P2, P3) - SpeedBetPoints(P1, P2)) / 0.0333333333;
}
private List<List<Double>> CalculateAccel(List<List<CameraSpacePoint>> PData)
{
List<List<Double>> APoints = new List<List<double>>();
for (int i = 0; i < (PData.Count) - 2; i++)
{
List<Double> TList = new List<Double>();
List<CameraSpacePoint> P1 = PData[i];
List<CameraSpacePoint> P2 = PData[i + 1];
List<CameraSpacePoint> P3 = PData[i + 2];
for (int j = 0; j < (PData[0].Count); j++)
{
TList.Add(AccelBetPoints(P1[j], P2[j], P3[j]));
}
APoints.Add(TList);
}
return APoints;
}
private List<List<Double>> CalculateSpeed(List<List<CameraSpacePoint>> PData)
{
List<List<Double>> SPoints = new List<List<double>>();
for (int i = 0; i < (PData.Count) - 1; i++)
{
List<Double> TList = new List<Double>();
List<CameraSpacePoint> P1 = PData[i];
List<CameraSpacePoint> P2 = PData[i + 1];
for (int j = 0; j < PData[0].Count; j++)
{
TList.Add(SpeedBetPoints(P1[j], P2[j]));
}
SPoints.Add(TList);
}
return SPoints;
}
private double CalcFreq(List<int> Hz)
{
double TF = 0;
for (int i = 0; i < Hz.Count; i++)
{
TF += Hz[i];
}
TF *= 0.03333333333;
return (Hz.Count) / TF;
}
/// <summary>
/// Finds the closest body from the sensor if any
/// </summary>
/// <param name="bodyFrame">A body frame</param>
/// <returns>Closest body, null of none</returns>
private static Body FindClosestBody(BodyFrame bodyFrame)
{
Body result = null;
double closestBodyDistance = double.MaxValue;
Body[] bodies = new Body[bodyFrame.BodyCount];
bodyFrame.GetAndRefreshBodyData(bodies);
foreach (var body in bodies)
{
if (body.IsTracked)
{
var currentLocation = body.Joints[JointType.SpineBase].Position;
var currentDistance = VectorLength(currentLocation);
if (result == null || currentDistance < closestBodyDistance)
{
result = body;
closestBodyDistance = currentDistance;
}
}
}
return result;
}
/// <summary>
/// Find if there is a body tracked with the given trackingId
/// </summary>
/// <param name="bodyFrame">A body frame</param>
/// <param name="trackingId">The tracking Id</param>
/// <returns>The body object, null of none</returns>
private static Body FindBodyWithTrackingId(BodyFrame bodyFrame, ulong trackingId)
{
Body result = null;
Body[] bodies = new Body[bodyFrame.BodyCount];
bodyFrame.GetAndRefreshBodyData(bodies);
foreach (var body in bodies)
{
if (body.IsTracked)
{
if (body.TrackingId == trackingId)
{
result = body;
break;
}
}
}
return result;
}
/// <summary>
/// Gets the current collection status
/// </summary>
/// <param name="status">Status value</param>
/// <returns>Status value as text</returns>
private static string GetCollectionStatusText(FaceModelBuilderCollectionStatus status)
{
string res = string.Empty;
if ((status & FaceModelBuilderCollectionStatus.FrontViewFramesNeeded) != 0)
{
res = "FrontViewFramesNeeded";
return res;
}
if ((status & FaceModelBuilderCollectionStatus.LeftViewsNeeded) != 0)
{
res = "LeftViewsNeeded";
return res;
}
if ((status & FaceModelBuilderCollectionStatus.RightViewsNeeded) != 0)
{
res = "RightViewsNeeded";
return res;
}
if ((status & FaceModelBuilderCollectionStatus.TiltedUpViewsNeeded) != 0)
{
res = "TiltedUpViewsNeeded";
return res;
}
if ((status & FaceModelBuilderCollectionStatus.Complete) != 0)
{
res = "Complete";
return res;
}
if ((status & FaceModelBuilderCollectionStatus.MoreFramesNeeded) != 0)
{
res = "TiltedUpViewsNeeded";
return res;
}
return res;
}
/// <summary>
/// Helper function to format a status message
/// </summary>
/// <returns>Status text</returns>
private string MakeStatusText()
{
string status = string.Format(System.Globalization.CultureInfo.CurrentCulture, "Builder Status: {0}, Current Tracking ID: {1}", this.CurrentBuilderStatus, this.CurrentTrackingId);
return status;
}
/// <summary>
/// Fires when Window is Loaded
/// </summary>
/// <param name="sender">object sending the event</param>
/// <param name="e">event arguments</param>
private void Window_Loaded(object sender, RoutedEventArgs e)
{
this.InitializeHDFace();
}
/// <summary>
/// Initialize Kinect object
/// </summary>
private void InitializeHDFace()
{
this.CurrentBuilderStatus = "Ready To Start Capture";
this.sensor = KinectSensor.GetDefault();
this.bodySource = this.sensor.BodyFrameSource;
this.bodyReader = this.bodySource.OpenReader();
this.bodyReader.FrameArrived += this.BodyReader_FrameArrived;
this.bodyCount = this.sensor.BodyFrameSource.BodyCount;
this.highDefinitionFaceFrameSource = new HighDefinitionFaceFrameSource(this.sensor);
this.highDefinitionFaceFrameSource.TrackingIdLost += this.HdFaceSource_TrackingIdLost;
this.highDefinitionFaceFrameReader = this.highDefinitionFaceFrameSource.OpenReader();
this.highDefinitionFaceFrameReader.FrameArrived += this.HdFaceReader_FrameArrived;
this.currentFaceModel = new FaceModel();
this.currentFaceAlignment = new FaceAlignment();
this.coordinateMapper = this.sensor.CoordinateMapper;
FaceFrameFeatures faceFrameFeatures =
FaceFrameFeatures.BoundingBoxInColorSpace
| FaceFrameFeatures.PointsInColorSpace
| FaceFrameFeatures.RotationOrientation
| FaceFrameFeatures.FaceEngagement
| FaceFrameFeatures.Glasses
| FaceFrameFeatures.Happy
| FaceFrameFeatures.LeftEyeClosed
| FaceFrameFeatures.RightEyeClosed
| FaceFrameFeatures.LookingAway
| FaceFrameFeatures.MouthMoved
| FaceFrameFeatures.MouthOpen;
// create the face frame source with the required face frame features and an initial tracking Id of 0
this.faceFrameSource = new FaceFrameSource(this.sensor, 0, faceFrameFeatures);
// open the corresponding reader
this.faceFrameReader = this.faceFrameSource.OpenReader();
this.faceFrameResult = null;
// wire handler for face frame arrival
if (this.faceFrameReader != null)
{
// wire handler for face frame arrival
this.faceFrameReader.FrameArrived += this.Reader_FaceFrameArrived;
}
this.InitializeMesh();
this.UpdateMesh();
this.sensor.Open();
}
/// <summary>
///
/// //////////////////////////////START OF FACE READER CODE///////////////////
///
///
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Reader_FaceFrameArrived(object sender, FaceFrameArrivedEventArgs e)
{
using (FaceFrame faceFrame = e.FrameReference.AcquireFrame())
{
if (faceFrame != null)
{
this.faceFrameResult = faceFrame.FaceFrameResult;
if (this.faceFrameResult != null)
{
string RClosed = DetectionResult.Maybe.ToString();
string LClosed = DetectionResult.Maybe.ToString();
var faceProperties = this.faceFrameResult.FaceProperties;
foreach (var PropF in faceProperties)
{
if (PropF.Key.ToString() == "RightEyeClosed")
{
TOPRINT += PropF.Key.ToString() + " : ";
if (PropF.Value == DetectionResult.Maybe)
{
TOPRINT += DetectionResult.No + Environment.NewLine;
}
else
{
TOPRINT += PropF.Value.ToString() + Environment.NewLine;
}
RClosed = PropF.Value.ToString();
}
if (PropF.Key.ToString() == "LeftEyeClosed")
{
TOPRINT += PropF.Key.ToString() + " : ";
if (PropF.Value == DetectionResult.Maybe)
{
TOPRINT += DetectionResult.No + Environment.NewLine;
}
else
{
TOPRINT += PropF.Value.ToString() + Environment.NewLine;
}
LClosed = PropF.Value.ToString();
}
}
if (LClosed == "Yes" && RClosed == "Yes")
{
EndBlink = 1;
if (BetweenBlink > 0)
{
FrameBetFreq.Add(BetweenBlink);
if (FrameBetFreq.Count > 20)
{
FrameBetFreq.RemoveAt(0);
}
}
BetweenBlink = 0;
FrameBlink += 1;
if (FrameBlink >= 15)
{
TOPRINT += "Not a blink, Eyes kept closed " + Environment.NewLine;
EndBlink = 0;
}
}
else
{
if (EndBlink == 1)
{
NewSubject.AccelOfBlinks.Add(CalculateAccel(PointsData));
TOPRINT += "Accel of Left MidTop = " + Environment.NewLine;
for(int i = 0; i < NewSubject.AccelOfBlinks[0].Count; i++)
{
TOPRINT += NewSubject.AccelOfBlinks[0][i][17].ToString() + Environment.NewLine;
}
TOPRINT += "Accel of Left MidBottom = " + Environment.NewLine;
for (int i = 0; i < NewSubject.AccelOfBlinks[0].Count; i++)
{
TOPRINT += NewSubject.AccelOfBlinks[0][i][40].ToString() + Environment.NewLine;
}
TOPRINT += "Accel of Right MidTop = " + Environment.NewLine;
for (int i = 0; i < NewSubject.AccelOfBlinks[0].Count; i++)
{
TOPRINT += NewSubject.AccelOfBlinks[0][i][17].ToString() + Environment.NewLine;
}
TOPRINT += "Accel of Right MidTBottom = " + Environment.NewLine;
for (int i = 0; i < NewSubject.AccelOfBlinks[0].Count; i++)
{
TOPRINT += NewSubject.AccelOfBlinks[0][i][49].ToString() + Environment.NewLine;
}
NewSubject.AccelOfBlinks.Clear();
}
FrameBlink = 0;
BetweenBlink += 1;
TOPRINT += "Frame betweem blink, Frame: " + BetweenBlink.ToString() + Environment.NewLine;
if (LClosed == "Yes" || RClosed == "Yes")
{
TOPRINT += "Wink! not a blink!" + Environment.NewLine;
}
EndBlink = 0;
}
}
}
Writer.WriteLine(TOPRINT);
TOPRINT = "";
}
}
/////////////////////END OF FACE READER CODE/////////////////////
/// <summary>
/// Initializes a 3D mesh to deform every frame
/// </summary>
private void InitializeMesh()
{
Writer.WriteLine("----------------New Data Set-------------------" + Environment.NewLine);
totalFrames = 0;
var vertices = this.currentFaceModel.CalculateVerticesForAlignment(this.currentFaceAlignment);
var triangleIndices = this.currentFaceModel.TriangleIndices;
var indices = new Int32Collection(triangleIndices.Count);
for (int i = 0; i < triangleIndices.Count; i += 3)
{
uint index01 = triangleIndices[i];
uint index02 = triangleIndices[i + 1];
uint index03 = triangleIndices[i + 2];
indices.Add((int)index03);
indices.Add((int)index02);
indices.Add((int)index01);
}
this.theGeometry.TriangleIndices = indices;
this.theGeometry.Normals = null;
this.theGeometry.Positions = new Point3DCollection();
this.theGeometry.TextureCoordinates = new PointCollection();
for (int i = 0; i < vertices.Count; i++)
{
var vert = vertices[i];
this.theGeometry.Positions.Add(new Point3D(vert.X, vert.Y, -vert.Z));
this.theGeometry.TextureCoordinates.Add(new Point());
}
TOPRINT += "PRE CAPTURE FRAME" + Environment.NewLine;
Writer.WriteLine(TOPRINT);
TOPRINT = "";
}
/// <summary>
/// Sends the new deformed mesh to be drawn
/// </summary>
private void UpdateMesh()
{
var vertices = this.currentFaceModel.CalculateVerticesForAlignment(this.currentFaceAlignment);
TempList = new List<CameraSpacePoint>();
TempList.Clear();
for (int i = 0; i < vertices.Count; i++)
{
var vert = vertices[i];
this.theGeometry.Positions[i] = new Point3D(vert.X, vert.Y, -vert.Z);
}
foreach (int Index in LEyeIndex)
{
TempList.Add(vertices[Index]);
}
foreach (int Index in REyeIndex)
{
TempList.Add(vertices[Index]);
}
PointsData.Add(TempList);
if (PointsData.Count > 12)
{
PointsData.RemoveAt(0);
}
Writer.WriteLine(TOPRINT);
TOPRINT = "";
}
/// <summary>
/// Start a face capture on clicking the button
/// </summary>
/// <param name="sender">object sending the event</param>
/// <param name="e">event arguments</param>
private void StartCapture_Button_Click(object sender, RoutedEventArgs e)
{
this.StartCapture();
}
/// <summary>
/// This event fires when a BodyFrame is ready for consumption
/// </summary>
/// <param name="sender">object sending the event</param>
/// <param name="e">event arguments</param>
private void BodyReader_FrameArrived(object sender, BodyFrameArrivedEventArgs e)
{
this.CheckOnBuilderStatus();
var frameReference = e.FrameReference;
using (var frame = frameReference.AcquireFrame())
{
if (frame == null)
{
// We might miss the chance to acquire the frame, it will be null if it's missed
return;
}
if (this.currentTrackedBody != null)
{
this.currentTrackedBody = FindBodyWithTrackingId(frame, this.CurrentTrackingId);
if (this.currentTrackedBody != null)
{
return;
}
}
Body selectedBody = FindClosestBody(frame);
if (selectedBody == null)
{
return;
}
this.currentTrackedBody = selectedBody;
this.CurrentTrackingId = selectedBody.TrackingId;
this.highDefinitionFaceFrameSource.TrackingId = this.CurrentTrackingId;
this.faceFrameSource.TrackingId = this.currentTrackingId;
}
}
/// <summary>
/// This event is fired when a tracking is lost for a body tracked by HDFace Tracker
/// </summary>
/// <param name="sender">object sending the event</param>
/// <param name="e">event arguments</param>
private void HdFaceSource_TrackingIdLost(object sender, TrackingIdLostEventArgs e)
{
var lostTrackingID = e.TrackingId;
if (this.CurrentTrackingId == lostTrackingID)
{
this.CurrentTrackingId = 0;
this.currentTrackedBody = null;
if (this.faceModelBuilder != null)
{
this.faceModelBuilder.Dispose();
this.faceModelBuilder = null;
}
this.highDefinitionFaceFrameSource.TrackingId = 0;
}
}
/// <summary>
/// This event is fired when a new HDFace frame is ready for consumption
/// </summary>
/// <param name="sender">object sending the event</param>
/// <param name="e">event arguments</param>
private void HdFaceReader_FrameArrived(object sender, HighDefinitionFaceFrameArrivedEventArgs e)
{
using (var frame = e.FrameReference.AcquireFrame())
{
// We might miss the chance to acquire the frame; it will be null if it's missed.
// Also ignore this frame if face tracking failed.
if (frame == null || !frame.IsFaceTracked)
{
return;
}
frame.GetAndRefreshFaceAlignmentResult(this.currentFaceAlignment);
this.UpdateMesh();
if (frame != null)
{
this.Clock.Add(frame.RelativeTime);
}
int sizeofClock;
sizeofClock = this.Clock.Count;
if (sizeofClock == 1)
{
TOPRINT += "Frame " + totalFrames.ToString() + " NO DATA" + Environment.NewLine;
}
if (sizeofClock > 1)
{
double dFramesDropped = ((Clock[1].TotalSeconds - Clock[0].TotalSeconds) / (0.0333333333)) - 1;
int FramesDropped = (int)dFramesDropped;
totalFrames += FramesDropped;
TOPRINT += "Frame " + totalFrames.ToString() + Environment.NewLine;
if (FramesDropped >= 1)
{
TOPRINT += "Frames Dropped: " + FramesDropped.ToString() + Environment.NewLine;
}
Clock.RemoveAt(0);
}
totalFrames += 1;
}
}
/// <summary>
/// Start a face capture operation
/// </summary>
private void StartCapture()
{
this.StopFaceCapture();
this.faceModelBuilder = null;
this.faceModelBuilder = this.highDefinitionFaceFrameSource.OpenModelBuilder(FaceModelBuilderAttributes.None);
this.faceModelBuilder.BeginFaceDataCollection();
this.faceModelBuilder.CollectionCompleted += this.HdFaceBuilder_CollectionCompleted;
}
/// <summary>
/// Cancel the current face capture operation
/// </summary>
private void StopFaceCapture()
{
if (this.faceModelBuilder != null)
{
this.faceModelBuilder.Dispose();
this.faceModelBuilder = null;
}
}
/// <summary>
/// This event fires when the face capture operation is completed
/// </summary>
/// <param name="sender">object sending the event</param>
/// <param name="e">event arguments</param>
private void HdFaceBuilder_CollectionCompleted(object sender, FaceModelBuilderCollectionCompletedEventArgs e)
{
var modelData = e.ModelData;
this.currentFaceModel = modelData.ProduceFaceModel();
this.faceModelBuilder.Dispose();
this.faceModelBuilder = null;
this.CurrentBuilderStatus = "Capture Complete";
}
/// <summary>
/// Check the face model builder status
/// </summary>
private void CheckOnBuilderStatus()
{
if (this.faceModelBuilder == null)
{
return;
}
string newStatus = string.Empty;
var captureStatus = this.faceModelBuilder.CaptureStatus;
newStatus += captureStatus.ToString();
var collectionStatus = this.faceModelBuilder.CollectionStatus;
newStatus += ", " + GetCollectionStatusText(collectionStatus);
this.CurrentBuilderStatus = newStatus;
}
static void DAnalyze(BiometricData Unknown)
{
Process Analyze = new Process();
Analyze.StartInfo.FileName = "cmd.exe";
Analyze.StartInfo.Arguments = "/c DIR"; // Note the /c command (*)
Analyze.StartInfo.UseShellExecute = false;
Analyze.StartInfo.RedirectStandardOutput = true;
Analyze.StartInfo.RedirectStandardError = true;
Analyze.Start();
//* Read the output (or the error)
string output = Analyze.StandardOutput.ReadToEnd();
Console.WriteLine(output);
string err = Analyze.StandardError.ReadToEnd();
Console.WriteLine(err);
Analyze.WaitForExit();
}
}
}