-
Notifications
You must be signed in to change notification settings - Fork 1
/
MainWindow.xaml.cs
420 lines (356 loc) · 15.2 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
using Microsoft.Kinect;
using Microsoft.Kinect.Face;
using System.Numerics;
using System.Windows;
using System.Windows.Forms;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using WindowsInput;
namespace K4W.Expressions
{
public partial class MainWindow : Window
{
/// <summary>
/// Instance of Kinect sensor
/// </summary>
private KinectSensor _kinect;
/// <summary>
/// Flages for hotkey presses
/// </summary>
private bool MoveWindowRightHotkeyPressFlag = false;
private bool MoveWindowLeftHotkeyPressFlag = false;
private bool AltTabHotkeyPressFlag = false;
private bool ShiftAltTabHotkeyPressFlag = false;
private const int BlinkTime = 10;
private int CountTime = 0;
/// <summary>
/// Body reader
/// </summary>
public BodyFrameReader _bodyReader;
/// <summary>
/// Collection of all tracked bodies
/// </summary>
public Body[] _bodies;
/// <summary>
/// Requested face features
/// </summary>
private const FaceFrameFeatures _faceFrameFeatures = FaceFrameFeatures.BoundingBoxInInfraredSpace
| FaceFrameFeatures.PointsInInfraredSpace
| FaceFrameFeatures.MouthMoved
| FaceFrameFeatures.MouthOpen
| FaceFrameFeatures.LeftEyeClosed
| FaceFrameFeatures.RightEyeClosed
| FaceFrameFeatures.LookingAway
| FaceFrameFeatures.Happy
| FaceFrameFeatures.FaceEngagement
| FaceFrameFeatures.Glasses;
/// <summary>
/// Face Source
/// </summary>
private FaceFrameSource _faceSource;
/// <summary>
/// Face Reader
/// </summary>
private FaceFrameReader _faceReader;
/// <summary>
/// Default CTOR
/// </summary>
public MainWindow()
{
// Initialize Components
InitializeComponent();
// Initialize Kinect
InitializeKinect();
}
/// <summary>
/// Initialize Kinect
/// </summary>
private void InitializeKinect()
{
// Get Kinect sensor
_kinect = KinectSensor.GetDefault();
if (_kinect == null) return;
// Initialize Camera
InitializeCamera();
// Initialize body tracking
InitializeBodyTracking();
// Start receiving
_kinect.Open();
}
/// <summary>
/// Initialize body tracking
/// </summary>
private void InitializeBodyTracking()
{
// Body Reader
_bodyReader = _kinect.BodyFrameSource.OpenReader();
// Wire event
_bodyReader.FrameArrived += OnBodyFrameReceived;
}
/// <summary>
/// Process body frames
/// </summary>
private void OnBodyFrameReceived(object sender, BodyFrameArrivedEventArgs e)
{
// Get Frame ref
BodyFrameReference bodyRef = e.FrameReference;
if (bodyRef == null) return;
// Get body frame
using (BodyFrame frame = bodyRef.AcquireFrame())
{
if (frame == null) return;
// Allocate array when required
if (_bodies == null)
_bodies = new Body[frame.BodyCount];
// Refresh bodies
frame.GetAndRefreshBodyData(_bodies);
foreach (Body body in _bodies)
{
if (body.IsTracked && _faceSource == null)
{
// Create new sources with body TrackingId
_faceSource = new FaceFrameSource(_kinect)
{
FaceFrameFeatures = _faceFrameFeatures,
TrackingId = body.TrackingId
};
// Create new reader
_faceReader = _faceSource.OpenReader();
// Wire events
_faceReader.FrameArrived += OnFaceFrameArrived;
_faceSource.TrackingIdLost += OnTrackingIdLost;
}
}
}
}
/// <summary>
/// Process the face frame
/// </summary>
private void OnFaceFrameArrived(object sender, FaceFrameArrivedEventArgs e)
{
// Retrieve the face reference
FaceFrameReference faceRef = e.FrameReference;
if (faceRef == null) return;
// Acquire the face frame
using (FaceFrame faceFrame = faceRef.AcquireFrame())
{
if (faceFrame == null) return;
// Retrieve the face frame result
FaceFrameResult frameResult = faceFrame.FaceFrameResult;
// Display the values
HappyResult.Text = frameResult.FaceProperties[FaceProperty.Happy].ToString();
EngagedResult.Text = frameResult.FaceProperties[FaceProperty.Engaged].ToString();
GlassesResult.Text = frameResult.FaceProperties[FaceProperty.WearingGlasses].ToString();
LeftEyeResult.Text = frameResult.FaceProperties[FaceProperty.LeftEyeClosed].ToString();
RightEyeResult.Text = frameResult.FaceProperties[FaceProperty.RightEyeClosed].ToString();
MouthOpenResult.Text = frameResult.FaceProperties[FaceProperty.MouthOpen].ToString();
MouthMovedResult.Text = frameResult.FaceProperties[FaceProperty.MouthMoved].ToString();
LookingAwayResult.Text = frameResult.FaceProperties[FaceProperty.LookingAway].ToString();
this.MoveWindowLeftHotkeyPress(frameResult);
this.MoveWindowRightHotkeyPress(frameResult);
//this.AltTabHotkeyPress(frameResult);
}
}
/// <summary>
/// Shift window left with left eye wink at camera
/// </summary>
private void MoveWindowLeftHotkeyPress(FaceFrameResult frameResult)
{
if (this.IsEngaged(frameResult) && this.IsLeftEyeClosed(frameResult) && !this.IsRightEyeClosed(frameResult) && this.AllFlagsAreFalse())
{
this.MoveWindowLeftHotkeyPressFlag = true;
this.CountTime = 0;
}
if (this.MoveWindowLeftHotkeyPressFlag == true && this.CountTime >= BlinkTime)
{
if (this.IsEngaged(frameResult) && this.IsLeftEyeClosed(frameResult) && !this.IsRightEyeClosed(frameResult))
{
this.MoveWindowLeftHotkeyPressFlag = true;
// Simulate each key stroke
InputSimulator.SimulateKeyDown(VirtualKeyCode.LWIN);
InputSimulator.SimulateKeyPress(VirtualKeyCode.LEFT);
InputSimulator.SimulateKeyUp(VirtualKeyCode.LWIN);
}
else if (!this.IsLeftEyeClosed(frameResult) && !this.IsRightEyeClosed(frameResult))
{
this.MoveWindowLeftHotkeyPressFlag = false;
}
}
else if (this.MoveWindowLeftHotkeyPressFlag == true)
{
this.CountTime += 1;
}
}
/// <summary>
/// Shift window right with right eye wink at the camera
/// </summary>
private bool AllFlagsAreFalse()
{
return !(this.AltTabHotkeyPressFlag || this.MoveWindowLeftHotkeyPressFlag || this.MoveWindowRightHotkeyPressFlag || this.ShiftAltTabHotkeyPressFlag);
}
/// <summary>
/// Shift window right with right eye wink at the camera
/// </summary>
private void MoveWindowRightHotkeyPress(FaceFrameResult frameResult)
{
if (this.IsEngaged(frameResult) && !this.IsLeftEyeClosed(frameResult) && this.IsRightEyeClosed(frameResult) && this.AllFlagsAreFalse())
{
this.MoveWindowRightHotkeyPressFlag = true;
this.CountTime = 0;
}
if (this.MoveWindowRightHotkeyPressFlag == true && this.CountTime >= BlinkTime)
{
if (this.IsEngaged(frameResult) && !this.IsLeftEyeClosed(frameResult) && this.IsRightEyeClosed(frameResult))
{
this.MoveWindowRightHotkeyPressFlag = true;
// Simulate each key stroke
InputSimulator.SimulateKeyDown(VirtualKeyCode.LWIN);
InputSimulator.SimulateKeyPress(VirtualKeyCode.RIGHT);
InputSimulator.SimulateKeyUp(VirtualKeyCode.LWIN);
}
else if (!this.IsLeftEyeClosed(frameResult) && !this.IsRightEyeClosed(frameResult))
{
this.MoveWindowLeftHotkeyPressFlag = false;
}
}
else if (this.MoveWindowLeftHotkeyPressFlag == true)
{
this.CountTime += 1;
}
}
/// <summary>
/// Alt-tab left with left eye wink at camera while being happy
/// </summary>
private void AltTabHotkeyPress(FaceFrameResult frameResult)
{
if (this.IsEngaged(frameResult) && this.IsHappy(frameResult) && this.IsLeftEyeClosed(frameResult) && !this.IsRightEyeClosed(frameResult) && this.AltTabHotkeyPressFlag == false)
{
System.Threading.Thread.Sleep(BlinkTime);
if (this.IsEngaged(frameResult) && this.IsLeftEyeClosed(frameResult) && !this.IsRightEyeClosed(frameResult))
{
this.AltTabHotkeyPressFlag = true;
// Simulate each key stroke
InputSimulator.SimulateKeyPress(VirtualKeyCode.TAB);
}
}
else if (!this.IsLeftEyeClosed(frameResult) && !this.IsRightEyeClosed(frameResult))
{
this.AltTabHotkeyPressFlag = false;
}
}
/// <summary>
/// Left Eye is Closed?
/// </summary>
private bool IsLeftEyeClosed(FaceFrameResult frameResult)
{
return frameResult.FaceProperties[FaceProperty.LeftEyeClosed].Equals(DetectionResult.Yes);
}
/// <summary>
/// Right Eye is Closed?
/// </summary>
private bool IsRightEyeClosed(FaceFrameResult frameResult)
{
return frameResult.FaceProperties[FaceProperty.RightEyeClosed].Equals(DetectionResult.Yes);
}
/// <summary>
/// Is the user looking at the camera?
/// </summary>
private bool IsEngaged(FaceFrameResult frameResult)
{
return frameResult.FaceProperties[FaceProperty.Engaged].Equals(DetectionResult.Yes);
}
/// <summary>
/// Is the user Happy?
/// </summary>
private bool IsHappy(FaceFrameResult frameResult)
{
return frameResult.FaceProperties[FaceProperty.Happy].Equals(DetectionResult.Yes);
}
/// <summary>
/// Handle when the tracked body is gone
/// </summary>
private void OnTrackingIdLost(object sender, TrackingIdLostEventArgs e)
{
// Update UI
HappyResult.Text = "No face tracked";
EngagedResult.Text = "No face tracked";
GlassesResult.Text = "No face tracked";
LeftEyeResult.Text = "No face tracked";
RightEyeResult.Text = "No face tracked";
MouthOpenResult.Text = "No face tracked";
MouthMovedResult.Text = "No face tracked";
LookingAwayResult.Text = "No face tracked";
// Reset values for next body
_faceReader = null;
_faceSource = null;
}
#region CAMERA
/// <summary>
/// Color WriteableBitmap linked to our UI
/// </summary>
private WriteableBitmap _colorBitmap = null;
/// <summary>
/// Array of color pixels
/// </summary>
private byte[] _colorPixels = null;
/// <summary>
/// FrameReader for our coloroutput
/// </summary>
private ColorFrameReader _colorReader = null;
/// <summary>
/// Size fo the RGB pixel in bitmap
/// </summary>
private readonly int _bytePerPixel = (PixelFormats.Bgr32.BitsPerPixel + 7) / 8;
private void InitializeCamera()
{
if (_kinect == null) return;
// Get frame description for the color output
FrameDescription desc = _kinect.ColorFrameSource.FrameDescription;
// Get the framereader for Color
_colorReader = _kinect.ColorFrameSource.OpenReader();
// Allocate pixel array
_colorPixels = new byte[desc.Width * desc.Height * _bytePerPixel];
// Create new WriteableBitmap
_colorBitmap = new WriteableBitmap(desc.Width, desc.Height, 96, 96, PixelFormats.Bgr32, null);
// Link WBMP to UI
CameraImage.Source = _colorBitmap;
// Hook-up event
_colorReader.FrameArrived += OnColorFrameArrived;
}
/// <summary>
/// Process color frames & show in UI
/// </summary>
private void OnColorFrameArrived(object sender, ColorFrameArrivedEventArgs e)
{
// Get the reference to the color frame
ColorFrameReference colorRef = e.FrameReference;
if (colorRef == null) return;
// Acquire frame for specific reference
ColorFrame frame = colorRef.AcquireFrame();
// It's possible that we skipped a frame or it is already gone
if (frame == null) return;
using (frame)
{
// Get frame description
FrameDescription frameDesc = frame.FrameDescription;
// Check if width/height matches
if (frameDesc.Width == _colorBitmap.PixelWidth && frameDesc.Height == _colorBitmap.PixelHeight)
{
// Copy data to array based on image format
if (frame.RawColorImageFormat == ColorImageFormat.Bgra)
{
frame.CopyRawFrameDataToArray(_colorPixels);
}
else frame.CopyConvertedFrameDataToArray(_colorPixels, ColorImageFormat.Bgra);
// Copy output to bitmap
_colorBitmap.WritePixels(
new Int32Rect(0, 0, frameDesc.Width, frameDesc.Height),
_colorPixels,
frameDesc.Width * _bytePerPixel,
0);
}
}
}
#endregion CAMERA
}
}