-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathKinectManager.cs
506 lines (419 loc) · 17.2 KB
/
KinectManager.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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Threading;
using System.Windows.Forms;
using OpenNI;
using System.Diagnostics;
using KinectProvider;
using KinectProvider.Properties;
namespace KinectProvider
{
/// <summary>
/// This class manages all data capturing and setup for getting imagininary/virtual touchinput from a
/// kinect by usind NITE and OPENNI
/// </summary>
/// <author>Christopher-Eyk Hrabia - www.ceh-photo.de</author>
class KinectManager : IDisposable
{
private readonly string CONFIG_XML_FILE = @"OpenNIConfig.xml";
private readonly InputProvider inputProvider;
/// <summary>
/// Offset of the virtual touchscreen relative to the user
/// Only used if relativeTouchDetectionEnabled = true
/// </summary>
private int virtualScreenZRelativeOffset = 0;
/// <summary>
/// /// <summary>
/// Offset of the virtual touchscreen absolute to the sensor
/// Only used if relativeTouchDetectionEnabled = false
/// </summary>
/// </summary>
private int virtualScreenZAbsoluteOffset = 0;
/// <summary>
/// Select if relative user or absolute virtual touchscreen mode
/// </summary>
private bool relativeTouchDetectionEnabled = true;
//Kinect/OpenNi handling objects
private Context context;
private ScriptNode scriptNode;
private DepthGenerator depth;
private UserGenerator userGenerator;
private SkeletonCapability skeletonCapbility;
private PoseDetectionCapability poseDetectionCapability;
private string calibPose;
/// <summary>
/// Thread for processing te kinect data
/// </summary>
private Thread readerThread;
/// <summary>
/// For updating settings
/// </summary>
private Thread settingsUpdateThread;
/// <summary>
/// Flag for stopping the threads
/// </summary>
private bool shouldRun;
private Dictionary<int, User> users;
/// <summary>
/// Not used in the moment, could be useful for handling out of bounds from the touch area
/// </summary>
private int maxDepth;
//Used to convert kinect resolution to
//Screen coordinates
private float virtualScreenConvertFactorX;
private float virtualScreenConvertFactorY;
private int virtualScreenXStart = 0;
private int virtualScreenYStart = 0;
//counting updateframes
private long framecounter = 0;
//Crop corners of sensor frame, because they are less reliable and difficult to use
private int kinectXCrop = 20;
private int kinectYCrop = 20;
/// <summary>
/// Save mapMode for determining device resolution
/// </summary>
MapOutputMode mapMode;
/// <summary>
/// Lock for releasing and clearing users
/// </summary>
Object userReleaseLock = new Object();
public KinectManager(InputProvider inputProvider)
{
this.inputProvider = inputProvider;
//get configuration
String OpenNiconfigPath = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location) + "\\" + CONFIG_XML_FILE;
this.context = Context.CreateFromXmlFile(OpenNiconfigPath, out scriptNode);
this.depth = context.FindExistingNode(NodeType.Depth) as DepthGenerator;
if (this.depth == null)
{
throw new Exception("Viewer must have a depth node!");
}
this.maxDepth = this.depth.DeviceMaxDepth;
this.userGenerator = new UserGenerator(this.context);
this.skeletonCapbility = this.userGenerator.SkeletonCapability;
this.poseDetectionCapability = this.userGenerator.PoseDetectionCapability;
this.calibPose = this.skeletonCapbility.CalibrationPose;
this.userGenerator.NewUser += userGenerator_NewUser;
this.userGenerator.LostUser += userGenerator_LostUser;
this.poseDetectionCapability.PoseDetected += poseDetectionCapability_PoseDetected;
this.skeletonCapbility.CalibrationComplete += skeletonCapbility_CalibrationComplete;
this.skeletonCapbility.SetSkeletonProfile(SkeletonProfile.Upper);
this.users = new Dictionary<int, User>();
this.userGenerator.StartGenerating();
this.mapMode = this.depth.MapOutputMode;
//load settings
updateSettings();
//start threads
this.shouldRun = true;
this.readerThread = new Thread(ReaderThread);
this.readerThread.Start();
this.settingsUpdateThread = new Thread(runUpdateSettings);
this.settingsUpdateThread.Start();
Console.WriteLine("Device initialized");
}
private void runUpdateSettings()
{
while (this.shouldRun)
{
updateSettings();
//Thread.Sleep(5000);
Thread.Sleep(500);
}
}
private void updateSettings()
{
//Read settings
int virtualScreenZRelativeOffsetNew = Settings.Default.TouchPlaneOffset;
int virtualScreenZAbsoluteOffsetNew = Settings.Default.TouchPlaneAbsOffset;
bool relativeTouchDetectionEnabledNew = Settings.Default.TouchRelativeEnabled;
//compare with stored settings
if (virtualScreenZRelativeOffset != virtualScreenZRelativeOffsetNew ||
virtualScreenZAbsoluteOffset != virtualScreenZAbsoluteOffsetNew ||
relativeTouchDetectionEnabled != relativeTouchDetectionEnabledNew)
{
virtualScreenZRelativeOffset = virtualScreenZRelativeOffsetNew;
virtualScreenZAbsoluteOffset = virtualScreenZAbsoluteOffsetNew;
relativeTouchDetectionEnabled = relativeTouchDetectionEnabledNew;
Console.WriteLine("Current configuration:");
Console.WriteLine("Virtual-Touch-Plane-Offset: " + virtualScreenZRelativeOffset);
Console.WriteLine("Virtual-Touch-Plane-Abs-Offset: " + virtualScreenZAbsoluteOffset);
Console.WriteLine("Enabled relative touch detection: " + relativeTouchDetectionEnabled);
Rectangle virtualScreen = SystemInformation.VirtualScreen;
virtualScreenConvertFactorX = (virtualScreen.Width / (float)(mapMode.XRes - kinectXCrop * 2));
virtualScreenConvertFactorY = (virtualScreen.Height / (float)(mapMode.YRes - kinectYCrop * 2));
//determine screen working area
Screen[] screens = Screen.AllScreens;
for (int index = 0; index <= screens.GetUpperBound(0); index++)
{
int workingX = screens[index].WorkingArea.X;
int workingY = screens[index].WorkingArea.Y;
if (workingX < virtualScreenXStart)
{
virtualScreenXStart = workingX;
}
if (workingY < virtualScreenYStart)
{
virtualScreenYStart = workingY;
}
}
Console.WriteLine("Virtual screen x:" + virtualScreen.Width + " y:" + virtualScreen.Height);
Console.WriteLine("Virtual screen Start coordinates x:" + virtualScreenXStart + " y:" + virtualScreenYStart);
Console.WriteLine("Device depth map x:" + mapMode.XRes + " y:" + mapMode.YRes);
Console.WriteLine("Device depth map cropped x:" + (mapMode.XRes - kinectXCrop * 2) + " y:" + (mapMode.YRes - kinectYCrop * 2));
Console.WriteLine("Convert Factor x:" + virtualScreenConvertFactorX + " y:" + virtualScreenConvertFactorY);
}
}
void skeletonCapbility_CalibrationComplete(object sender, CalibrationProgressEventArgs e)
{
if (e.Status == CalibrationStatus.OK)
{
Console.WriteLine("User added: " + e.ID);
this.skeletonCapbility.StartTracking(e.ID);
this.users.Add(e.ID, new User(e.ID, new Dictionary<SkeletonJoint, SkeletonJointPosition>()));
}
else if (e.Status != CalibrationStatus.ManualAbort)
{
if (this.skeletonCapbility.DoesNeedPoseForCalibration)
{
this.poseDetectionCapability.StartPoseDetection(calibPose, e.ID);
}
else
{
this.skeletonCapbility.RequestCalibration(e.ID, true);
}
}
}
void poseDetectionCapability_PoseDetected(object sender, PoseDetectedEventArgs e)
{
this.poseDetectionCapability.StopPoseDetection(e.ID);
this.skeletonCapbility.RequestCalibration(e.ID, true);
}
void userGenerator_NewUser(object sender, NewUserEventArgs e)
{
if (this.skeletonCapbility.DoesNeedPoseForCalibration)
{
this.poseDetectionCapability.StartPoseDetection(this.calibPose, e.ID);
}
else
{
this.skeletonCapbility.RequestCalibration(e.ID, true);
}
}
void userGenerator_LostUser(object sender, UserLostEventArgs e)
{
lock (userReleaseLock)
{
if (this.users.ContainsKey(e.ID))
{
User user = null;
user = this.users[e.ID];
Console.WriteLine("Lost user: " + e.ID);
releaseUserTouches(e.ID);
this.users.Remove(e.ID);
if (user != null)
{
user.Dispose();
}
}
}
}
public void Dispose()
{
this.shouldRun = false;
this.readerThread.Join();
this.settingsUpdateThread.Join();
this.context.Release();
}
private void GetJoint(int user, SkeletonJoint joint)
{
SkeletonJointPosition pos = this.skeletonCapbility.GetSkeletonJointPosition(user, joint);
if (pos.Position.Z == 0)
{
pos.Confidence = 0;
}
else
{
pos.Position = this.depth.ConvertRealWorldToProjective(pos.Position);
}
this.users[user].Joints[joint] = pos;
}
private void GetJoints(int user)
{
GetJoint(user, SkeletonJoint.Torso);
GetJoint(user, SkeletonJoint.LeftHand);
GetJoint(user, SkeletonJoint.RightHand);
}
/// <summary>
///
/// </summary>
/// <param name="user"></param>
/// <param name="joint"></param>
/// <param name="jointRef"></param>
/// <param name="screenPlaneZoffset"></param>
/// <returns>true if touched </returns>
private void DetectVirtualRelativeTouch(User user, uint hand, SkeletonJoint jointRef, int screenPlaneZoffset)
{
SkeletonJoint jointHand;
if (hand == 0)
{
jointHand = SkeletonJoint.LeftHand;
}
else
{
jointHand = SkeletonJoint.RightHand;
}
Point3D pos = user.Joints[jointHand].Position;
Point3D posRef = user.Joints[jointRef].Position;
InputStatus status;
//hand not probably tracked return
if (user.Joints[jointHand].Confidence < 0.6)
{
sendContact(user.updateHand(hand, InputStatus.UNKNOWN));
return;
} //if the reference is not valid, only cursor mode will be reliable
else if (user.Joints[jointRef].Confidence < 0.6)
{
status = InputStatus.CURSOR;
}
else
{ //both joints are well tracked, touch detection is executed
if (pos.Z > (posRef.Z - screenPlaneZoffset))
{//No Touch
status = InputStatus.CURSOR;
}
else
{//Touch
status = InputStatus.TOUCHED;
}
}
//convert kinectcoordinates to screencoordinates
pos.X = virtualScreenXStart + virtualScreenConvertFactorX * (pos.X - kinectXCrop);
pos.Y = virtualScreenYStart + virtualScreenConvertFactorY * (pos.Y - kinectYCrop);
HandContact contact = user.updateHand(pos, hand, status);
sendContact(contact);
}
private void sendContact(HandContact contact)
{
if (contact != null)
{
inputProvider.EnqueueContact(contact);
}
}
private void DetectVirtualAbsoluteTouch(User user, uint hand, int screenPlaneZoffset)
{
SkeletonJoint jointHand;
if (hand == 0)
{
jointHand = SkeletonJoint.LeftHand;
}
else
{
jointHand = SkeletonJoint.RightHand;
}
Point3D pos = user.Joints[jointHand].Position;
InputStatus status;
//hand not probably tracked return
if (user.Joints[jointHand].Confidence < 0.6)
{
sendContact(user.updateHand(hand, InputStatus.UNKNOWN));
return;
}
else
{ //both joints are well tracked, touch detection is executed
if (pos.Z > screenPlaneZoffset)
{//No Touch
status = InputStatus.CURSOR;
}
else
{//Touch
status = InputStatus.TOUCHED;
}
}
//convert kinectcoordinates to screencoordinates
pos.X = virtualScreenXStart + virtualScreenConvertFactorX * (pos.X - kinectXCrop);
pos.Y = virtualScreenYStart + virtualScreenConvertFactorY * (pos.Y - kinectYCrop);
HandContact contact = user.updateHand(pos, hand, status);
if (contact != null)
{
inputProvider.EnqueueContact(contact);
}
}
private void releaseUserTouches(int userId)
{
if (users.ContainsKey(userId))
{
User user = this.users[userId];
HandContact contact = user.updateHand(0, InputStatus.UNKNOWN);
HandContact contactTwo = user.updateHand(1, InputStatus.UNKNOWN);
if (contact != null)
{
inputProvider.EnqueueContact(contact);
}
if (contactTwo != null)
{
inputProvider.EnqueueContact(contactTwo);
}
}
}
/// <summary>
/// Detect touch and movement of hands
/// </summary>
/// <param name="user"></param>
private void DetectTouches(int user)
{
GetJoints(user);
if (relativeTouchDetectionEnabled)
{
DetectVirtualRelativeTouch(this.users[user], 0, SkeletonJoint.Torso, virtualScreenZRelativeOffset);
DetectVirtualRelativeTouch(this.users[user], 1, SkeletonJoint.Torso, virtualScreenZRelativeOffset);
}
else
{
DetectVirtualAbsoluteTouch(this.users[user], 0, virtualScreenZAbsoluteOffset);
DetectVirtualAbsoluteTouch(this.users[user], 1, virtualScreenZAbsoluteOffset);
}
}
private unsafe void ReaderThread()
{
DepthMetaData depthMD = new DepthMetaData();
while (this.shouldRun)
{
try
{
this.context.WaitOneUpdateAll(this.depth);
}
catch (Exception e)
{
Console.WriteLine(e.Message + e.StackTrace);
//Debugger.Break();
}
this.depth.GetMetaData(depthMD);
lock (this)
{
int[] users = this.userGenerator.GetUsers();
foreach (int user in users)
{
if (this.skeletonCapbility.IsTracking(user))
{
DetectTouches(user);
}
else
{
//Do not wait for access
if(Monitor.TryEnter(userReleaseLock))
{
releaseUserTouches(user);
Monitor.Exit(userReleaseLock);
}
}
}
}
inputProvider.raiseFrame(framecounter++);
}
}
}
}