-
Notifications
You must be signed in to change notification settings - Fork 0
/
FaceTrackingViewer.xaml.cs
425 lines (355 loc) · 14.7 KB
/
FaceTrackingViewer.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
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="FaceTrackingViewer.xaml.cs" company="Microsoft">
// Copyright (c) Microsoft Corporation. All rights reserved.
// </copyright>
// --------------------------------------------------------------------------------------------------------------------
namespace FaceTrackingBasics
{
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using Microsoft.Kinect;
using Microsoft.Kinect.Toolkit.FaceTracking;
using Point = System.Windows.Point;
/// <summary>
/// Class that uses the Face Tracking SDK to display a face mask for
/// tracked skeletons
/// </summary>
public partial class FaceTrackingViewer : UserControl, IDisposable
{
//added variables
//
public static readonly DependencyProperty KinectProperty = DependencyProperty.Register(
"Kinect",
typeof(KinectSensor),
typeof(FaceTrackingViewer),
new PropertyMetadata(
null, (o, args) => ((FaceTrackingViewer)o).OnSensorChanged((KinectSensor)args.OldValue, (KinectSensor)args.NewValue)));
private const uint MaxMissedFrames = 100;
private readonly Dictionary<int, SkeletonFaceTracker> trackedSkeletons = new Dictionary<int, SkeletonFaceTracker>();
private byte[] colorImage;
private ColorImageFormat colorImageFormat = ColorImageFormat.Undefined;
private short[] depthImage;
private DepthImageFormat depthImageFormat = DepthImageFormat.Undefined;
private bool disposed;
private Skeleton[] skeletonData;
public FaceTrackingViewer()
{
this.InitializeComponent();
}
~FaceTrackingViewer()
{
this.Dispose(false);
}
public KinectSensor Kinect
{
get
{
return (KinectSensor)this.GetValue(KinectProperty);
}
set
{
this.SetValue(KinectProperty, value);
}
}
public void Dispose()
{
this.Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
if (!this.disposed)
{
this.ResetFaceTracking();
this.disposed = true;
}
}
protected override void OnRender(DrawingContext drawingContext)
{
base.OnRender(drawingContext);
foreach (SkeletonFaceTracker faceInformation in this.trackedSkeletons.Values)
{
faceInformation.DrawFaceModel(drawingContext);
}
}
private void OnAllFramesReady(object sender, AllFramesReadyEventArgs allFramesReadyEventArgs)
{
ColorImageFrame colorImageFrame = null;
DepthImageFrame depthImageFrame = null;
SkeletonFrame skeletonFrame = null;
try
{
colorImageFrame = allFramesReadyEventArgs.OpenColorImageFrame();
depthImageFrame = allFramesReadyEventArgs.OpenDepthImageFrame();
skeletonFrame = allFramesReadyEventArgs.OpenSkeletonFrame();
if (colorImageFrame == null || depthImageFrame == null || skeletonFrame == null)
{
return;
}
// Check for image format changes. The FaceTracker doesn't
// deal with that so we need to reset.
if (this.depthImageFormat != depthImageFrame.Format)
{
this.ResetFaceTracking();
this.depthImage = null;
this.depthImageFormat = depthImageFrame.Format;
}
if (this.colorImageFormat != colorImageFrame.Format)
{
this.ResetFaceTracking();
this.colorImage = null;
this.colorImageFormat = colorImageFrame.Format;
}
// Create any buffers to store copies of the data we work with
if (this.depthImage == null)
{
this.depthImage = new short[depthImageFrame.PixelDataLength];
}
if (this.colorImage == null)
{
this.colorImage = new byte[colorImageFrame.PixelDataLength];
}
// Get the skeleton information
if (this.skeletonData == null || this.skeletonData.Length != skeletonFrame.SkeletonArrayLength)
{
this.skeletonData = new Skeleton[skeletonFrame.SkeletonArrayLength];
}
colorImageFrame.CopyPixelDataTo(this.colorImage);
depthImageFrame.CopyPixelDataTo(this.depthImage);
skeletonFrame.CopySkeletonDataTo(this.skeletonData);
// Update the list of trackers and the trackers with the current frame information
foreach (Skeleton skeleton in this.skeletonData)
{
if (skeleton.TrackingState == SkeletonTrackingState.Tracked
|| skeleton.TrackingState == SkeletonTrackingState.PositionOnly)
{
// We want keep a record of any skeleton, tracked or untracked.
if (!this.trackedSkeletons.ContainsKey(skeleton.TrackingId))
{
this.trackedSkeletons.Add(skeleton.TrackingId, new SkeletonFaceTracker());
}
// Give each tracker the upated frame.
SkeletonFaceTracker skeletonFaceTracker;
if (this.trackedSkeletons.TryGetValue(skeleton.TrackingId, out skeletonFaceTracker))
{
skeletonFaceTracker.OnFrameReady(this.Kinect, colorImageFormat, colorImage, depthImageFormat, depthImage, skeleton);
skeletonFaceTracker.LastTrackedFrame = skeletonFrame.FrameNumber;
}
}
}
this.RemoveOldTrackers(skeletonFrame.FrameNumber);
this.InvalidateVisual();
}
finally
{
if (colorImageFrame != null)
{
colorImageFrame.Dispose();
}
if (depthImageFrame != null)
{
depthImageFrame.Dispose();
}
if (skeletonFrame != null)
{
skeletonFrame.Dispose();
}
}
}
private void OnSensorChanged(KinectSensor oldSensor, KinectSensor newSensor)
{
if (oldSensor != null)
{
oldSensor.AllFramesReady -= this.OnAllFramesReady;
this.ResetFaceTracking();
}
if (newSensor != null)
{
newSensor.AllFramesReady += this.OnAllFramesReady;
}
}
/// <summary>
/// Clear out any trackers for skeletons we haven't heard from for a while
/// </summary>
private void RemoveOldTrackers(int currentFrameNumber)
{
var trackersToRemove = new List<int>();
foreach (var tracker in this.trackedSkeletons)
{
uint missedFrames = (uint)currentFrameNumber - (uint)tracker.Value.LastTrackedFrame;
if (missedFrames > MaxMissedFrames)
{
// There have been too many frames since we last saw this skeleton
trackersToRemove.Add(tracker.Key);
}
}
foreach (int trackingId in trackersToRemove)
{
this.RemoveTracker(trackingId);
}
}
private void RemoveTracker(int trackingId)
{
this.trackedSkeletons[trackingId].Dispose();
this.trackedSkeletons.Remove(trackingId);
}
private void ResetFaceTracking()
{
foreach (int trackingId in new List<int>(this.trackedSkeletons.Keys))
{
this.RemoveTracker(trackingId);
}
}
public Vector3DF ReturnRotationValues()
{
foreach (SkeletonFaceTracker tracker in trackedSkeletons.Values)
{
Vector3DF vector = tracker.getRotation();
if (vector != null)
return vector;
}
return new Vector3DF();
}
public float ReturnMouthState(AnimationUnit au)
{
foreach (SkeletonFaceTracker tracker in trackedSkeletons.Values)
{
return tracker.getState(au);
}
return 0;
}
public System.Windows.Rect ReturnFaceRect()
{
foreach (SkeletonFaceTracker tracker in trackedSkeletons.Values)
{
System.Windows.Rect rect = tracker.getFaceRect();
if (rect != null)
return rect;
}
return new System.Windows.Rect();
}
private class SkeletonFaceTracker : IDisposable
{
private FaceTrackFrame frame;
private static FaceTriangle[] faceTriangles;
private EnumIndexableCollection<FeaturePoint, PointF> facePoints;
private FaceTracker faceTracker;
private bool lastFaceTrackSucceeded;
private SkeletonTrackingState skeletonTrackingState;
public int LastTrackedFrame { get; set; }
public void Dispose()
{
if (this.faceTracker != null)
{
this.faceTracker.Dispose();
this.faceTracker = null;
}
}
public Vector3DF getRotation()
{
if(frame != null)
return frame.Rotation;
return new Vector3DF();
}
public float getState(AnimationUnit au)
{
if(frame != null)
return frame.GetAnimationUnitCoefficients()[au];
return 0;
}
public System.Windows.Rect getFaceRect()
{
if (frame != null)
{
var kinectRect = frame.FaceRect;
System.Windows.Rect rect = new System.Windows.Rect(kinectRect.Left, kinectRect.Top, kinectRect.Width, kinectRect.Height);
return rect;
}
return new System.Windows.Rect();
}
public void DrawFaceModel(DrawingContext drawingContext)
{
if (!this.lastFaceTrackSucceeded || this.skeletonTrackingState != SkeletonTrackingState.Tracked)
{
return;
}
var faceModelPts = new List<Point>();
var faceModel = new List<FaceModelTriangle>();
for (int i = 0; i < this.facePoints.Count; i++)
{
faceModelPts.Add(new Point(this.facePoints[i].X + 0.5f, this.facePoints[i].Y + 0.5f));
}
foreach (var t in faceTriangles)
{
var triangle = new FaceModelTriangle();
triangle.P1 = faceModelPts[t.First];
triangle.P2 = faceModelPts[t.Second];
triangle.P3 = faceModelPts[t.Third];
faceModel.Add(triangle);
}
var faceModelGroup = new GeometryGroup();
for (int i = 0; i < faceModel.Count; i++)
{
var faceTriangle = new GeometryGroup();
faceTriangle.Children.Add(new LineGeometry(faceModel[i].P1, faceModel[i].P2));
faceTriangle.Children.Add(new LineGeometry(faceModel[i].P2, faceModel[i].P3));
faceTriangle.Children.Add(new LineGeometry(faceModel[i].P3, faceModel[i].P1));
faceModelGroup.Children.Add(faceTriangle);
}
drawingContext.DrawGeometry(Brushes.LightYellow, new Pen(Brushes.LightYellow, 1.0), faceModelGroup);
}
/// <summary>
/// Updates the face tracking information for this skeleton
/// </summary>
internal void OnFrameReady(KinectSensor kinectSensor, ColorImageFormat colorImageFormat, byte[] colorImage, DepthImageFormat depthImageFormat, short[] depthImage, Skeleton skeletonOfInterest)
{
this.skeletonTrackingState = skeletonOfInterest.TrackingState;
if (this.skeletonTrackingState != SkeletonTrackingState.Tracked)
{
// nothing to do with an untracked skeleton.
return;
}
if (this.faceTracker == null)
{
try
{
this.faceTracker = new FaceTracker(kinectSensor);
}
catch (InvalidOperationException)
{
// During some shutdown scenarios the FaceTracker
// is unable to be instantiated. Catch that exception
// and don't track a face.
Debug.WriteLine("AllFramesReady - creating a new FaceTracker threw an InvalidOperationException");
this.faceTracker = null;
}
}
if (this.faceTracker != null)
{
frame = this.faceTracker.Track(
colorImageFormat, colorImage, depthImageFormat, depthImage, skeletonOfInterest);
this.lastFaceTrackSucceeded = frame.TrackSuccessful;
if (this.lastFaceTrackSucceeded)
{
if (faceTriangles == null)
{
// only need to get this once. It doesn't change.
faceTriangles = frame.GetTriangles();
}
this.facePoints = frame.GetProjected3DShape();
}
}
}
private struct FaceModelTriangle
{
public Point P1;
public Point P2;
public Point P3;
}
}
}
}