-
Notifications
You must be signed in to change notification settings - Fork 66
/
Copy pathProgram.cs
461 lines (399 loc) · 12.1 KB
/
Program.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
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using nuitrack;
using nuitrack.issues;
namespace nuitrack
{
public class Program
{
static public void Main()
{
Console.CancelKeyPress += delegate {
Nuitrack.Release();
GC.Collect();
GC.WaitForPendingFinalizers();
};
try
{
Application.Run(new MainForm());
}
catch (Exception exception)
{
Console.WriteLine(exception.ToString());
}
}
}
public class DirectBitmap : IDisposable
{
public Bitmap Bitmap { get; private set; }
public Int32[] Bits { get; private set; }
public bool Disposed { get; private set; }
public int Height { get; private set; }
public int Width { get; private set; }
protected GCHandle BitsHandle { get; private set; }
public DirectBitmap(int width, int height)
{
Width = width;
Height = height;
Bits = new Int32[width * height];
BitsHandle = GCHandle.Alloc(Bits, GCHandleType.Pinned);
Bitmap = new Bitmap(width, height, width * 4, PixelFormat.Format32bppPArgb, BitsHandle.AddrOfPinnedObject());
}
public void SetPixel(int x, int y, Color colour)
{
int index = x + (y * Width);
int col = colour.ToArgb();
Bits[index] = col;
}
public Color GetPixel(int x, int y)
{
int index = x + (y * Width);
int col = Bits[index];
Color result = Color.FromArgb(col);
return result;
}
public void Dispose()
{
if (Disposed)
return;
Disposed = true;
Bitmap.Dispose();
BitsHandle.Free();
}
}
public class MainForm : Form
{
private DirectBitmap _bitmap;
private bool _visualizeColorImage = false;
private bool _colorStreamEnabled = false;
private DepthSensor _depthSensor;
private ColorSensor _colorSensor;
private UserTracker _userTracker;
private SkeletonTracker _skeletonTracker;
private GestureRecognizer _gestureRecognizer;
private HandTracker _handTracker;
private DepthFrame _depthFrame;
private SkeletonData _skeletonData;
private HandTrackerData _handTrackerData;
private IssuesData _issuesData;
public MainForm()
{
// Initialize Nuitrack. This should be called before using any Nuitrack module.
// By passing the default arguments we specify that Nuitrack must determine
// the location automatically.
try
{
Nuitrack.Init("");
}
catch (Exception exception)
{
Console.WriteLine("Cannot initialize Nuitrack.");
throw exception;
}
try
{
// Create and setup all required modules
_depthSensor = DepthSensor.Create();
_colorSensor = ColorSensor.Create();
_userTracker = UserTracker.Create();
_skeletonTracker = SkeletonTracker.Create();
_handTracker = HandTracker.Create();
_gestureRecognizer = GestureRecognizer.Create();
}
catch (Exception exception)
{
Console.WriteLine("Cannot create Nuitrack module.");
throw exception;
}
//_depthSensor.SetMirror(false);
// Add event handlers for all modules
_depthSensor.OnUpdateEvent += onDepthSensorUpdate;
_colorSensor.OnUpdateEvent += onColorSensorUpdate;
_userTracker.OnUpdateEvent += onUserTrackerUpdate;
_userTracker.OnNewUserEvent += onUserTrackerNewUser;
_userTracker.OnLostUserEvent += onUserTrackerLostUser;
_skeletonTracker.OnSkeletonUpdateEvent += onSkeletonUpdate;
_handTracker.OnUpdateEvent += onHandTrackerUpdate;
_gestureRecognizer.OnNewGesturesEvent += onNewGestures;
// Add an event handler for the IssueUpdate event
Nuitrack.onIssueUpdateEvent += onIssueDataUpdate;
// Create and configure the Bitmap object according to the depth sensor output mode
OutputMode mode = _depthSensor.GetOutputMode();
OutputMode colorMode = _colorSensor.GetOutputMode();
if (mode.XRes < colorMode.XRes)
mode.XRes = colorMode.XRes;
if (mode.YRes < colorMode.YRes)
mode.YRes = colorMode.YRes;
_bitmap = new DirectBitmap(mode.XRes, mode.YRes);
for (int y = 0; y < mode.YRes; ++y)
{
for (int x = 0; x < mode.XRes; ++x)
_bitmap.SetPixel(x, y, Color.FromKnownColor(KnownColor.Aqua));
}
// Set fixed form size
this.MinimumSize = this.MaximumSize = new Size(mode.XRes, mode.YRes);
// Disable unnecessary caption bar buttons
this.MinimizeBox = this.MaximizeBox = false;
// Enable double buffering to prevent flicker
this.DoubleBuffered = true;
// Run Nuitrack. This starts sensor data processing.
try
{
Nuitrack.Run();
}
catch (Exception exception)
{
Console.WriteLine("Cannot start Nuitrack.");
throw exception;
}
this.Show();
}
~MainForm()
{
_bitmap.Dispose();
}
protected override void OnFormClosing(FormClosingEventArgs e)
{
// Release Nuitrack and remove all modules
try
{
Nuitrack.onIssueUpdateEvent -= onIssueDataUpdate;
_depthSensor.OnUpdateEvent -= onDepthSensorUpdate;
_colorSensor.OnUpdateEvent -= onColorSensorUpdate;
_userTracker.OnUpdateEvent -= onUserTrackerUpdate;
_skeletonTracker.OnSkeletonUpdateEvent -= onSkeletonUpdate;
_handTracker.OnUpdateEvent -= onHandTrackerUpdate;
_gestureRecognizer.OnNewGesturesEvent -= onNewGestures;
Nuitrack.Release();
}
catch (Exception exception)
{
Console.WriteLine("Nuitrack release failed.");
throw exception;
}
}
// Switch visualization mode on a mouse click
protected override void OnClick(EventArgs args)
{
base.OnClick(args);
_visualizeColorImage = !_visualizeColorImage;
}
protected override void OnPaint(PaintEventArgs args)
{
base.OnPaint(args);
// Update Nuitrack data. Data will be synchronized with skeleton time stamps.
try
{
Nuitrack.Update(_skeletonTracker);
}
catch (LicenseNotAcquiredException exception)
{
Console.WriteLine("LicenseNotAcquired exception. Exception: ", exception);
throw exception;
}
catch (Exception exception)
{
Console.WriteLine("Nuitrack update failed. Exception: ", exception);
}
// Draw a bitmap
args.Graphics.DrawImage(_bitmap.Bitmap, new Point(0, 0));
// Draw skeleton joints
if (_skeletonData != null)
{
const int jointSize = 10;
foreach (var skeleton in _skeletonData.Skeletons)
{
SolidBrush brush = new SolidBrush(Color.FromArgb(255 - 40 * skeleton.ID, 0, 0));
foreach (var joint in skeleton.Joints)
{
args.Graphics.FillEllipse(brush, joint.Proj.X * _bitmap.Width - jointSize / 2,
joint.Proj.Y * _bitmap.Height - jointSize / 2, jointSize, jointSize);
}
}
}
// Draw hand pointers
if (_handTrackerData != null)
{
foreach (var userHands in _handTrackerData.UsersHands)
{
if (userHands.LeftHand != null)
{
HandContent hand = userHands.LeftHand.Value;
int size = hand.Click ? 20 : 30;
Brush brush = new SolidBrush(Color.Aquamarine);
args.Graphics.FillEllipse(brush, hand.X * _bitmap.Width - size / 2, hand.Y * _bitmap.Height - size / 2, size, size);
}
if (userHands.RightHand != null)
{
HandContent hand = userHands.RightHand.Value;
int size = hand.Click ? 20 : 30;
Brush brush = new SolidBrush(Color.DarkBlue);
args.Graphics.FillEllipse(brush, hand.X * _bitmap.Width - size / 2, hand.Y * _bitmap.Height - size / 2, size, size);
}
}
}
// Update Form
this.Invalidate();
}
private void onIssueDataUpdate(IssuesData issuesData)
{
if (_issuesData != null)
_issuesData.Dispose();
_issuesData = (IssuesData)issuesData.Clone();
}
// Event handler for the DepthSensorUpdate event
private void onDepthSensorUpdate(DepthFrame depthFrame)
{
if (_depthFrame != null)
_depthFrame.Dispose();
_depthFrame = (DepthFrame)depthFrame.Clone();
}
// Event handler for the ColorSensorUpdate event
private void onColorSensorUpdate(ColorFrame colorFrame)
{
if (!_visualizeColorImage)
return;
_colorStreamEnabled = true;
float wStep = (float)_bitmap.Width / colorFrame.Cols;
float hStep = (float)_bitmap.Height / colorFrame.Rows;
float nextVerticalBorder = hStep;
int colorPtr = 0;
int bitmapPtr = 0;
const int elemSizeInBytes = 3;
unsafe
{
byte* data = (byte*)colorFrame.Data.ToPointer();
for (int i = 0; i < _bitmap.Height; ++i)
{
if (i == (int)nextVerticalBorder)
{
colorPtr += colorFrame.Cols * elemSizeInBytes;
nextVerticalBorder += hStep;
}
int offset = 0;
int argb = data[colorPtr]
| (data[colorPtr + 1] << 8)
| (data[colorPtr + 2] << 16)
| (0xFF << 24);
float nextHorizontalBorder = wStep;
for (int j = 0; j < _bitmap.Width; ++j)
{
if (j == (int)nextHorizontalBorder)
{
offset += elemSizeInBytes;
argb = data[colorPtr + offset]
| (data[colorPtr + offset + 1] << 8)
| (data[colorPtr + offset + 2] << 16)
| (0xFF << 24);
nextHorizontalBorder += wStep;
}
_bitmap.Bits[bitmapPtr++] = argb;
}
}
}
}
// Event handler for the UserTrackerUpdate event
private void onUserTrackerUpdate(UserFrame userFrame)
{
using (UserFrame _userFrame = (UserFrame)userFrame.Clone())
{
if (_visualizeColorImage && _colorStreamEnabled)
return;
if (_depthFrame == null)
return;
const int MAX_LABELS = 7;
bool[] labelIssueState = new bool[MAX_LABELS];
for (UInt16 label = 0; label < MAX_LABELS; ++label)
{
labelIssueState[label] = false;
if (_issuesData != null)
{
FrameBorderIssue frameBorderIssue = _issuesData.GetUserIssue<FrameBorderIssue>(label);
labelIssueState[label] = (frameBorderIssue != null);
}
}
float wStep = (float)_bitmap.Width / _depthFrame.Cols;
float hStep = (float)_bitmap.Height / _depthFrame.Rows;
float nextVerticalBorder = hStep;
unsafe
{
byte* dataDepth = (byte*)_depthFrame.Data.ToPointer();
byte* dataUser = (byte*)_userFrame.Data.ToPointer();
int dataPtr = 0;
int bitmapPtr = 0;
const int elemSizeInBytes = 2;
for (int i = 0; i < _bitmap.Height; ++i)
{
if (i == (int)nextVerticalBorder)
{
dataPtr += _depthFrame.Cols * elemSizeInBytes;
nextVerticalBorder += hStep;
}
int offset = 0;
int argb = 0;
int label = dataUser[dataPtr] | dataUser[dataPtr + 1] << 8;
int depth = Math.Min(255, (dataDepth[dataPtr] | dataDepth[dataPtr + 1] << 8) / 32);
float nextHorizontalBorder = wStep;
for (int j = 0; j < _bitmap.Width; ++j)
{
if (j == (int)nextHorizontalBorder)
{
offset += elemSizeInBytes;
label = dataUser[dataPtr + offset] | dataUser[dataPtr + offset + 1] << 8;
if (label == 0)
depth = Math.Min(255, (dataDepth[dataPtr + offset] | dataDepth[dataPtr + offset + 1] << 8) / 32);
nextHorizontalBorder += wStep;
}
if (label > 0)
{
int user = label * 40;
if (!labelIssueState[label])
user += 40;
argb = 0 | (user << 8) | (0 << 16) | (0xFF << 24);
}
else
{
argb = depth | (depth << 8) | (depth << 16) | (0xFF << 24);
}
_bitmap.Bits[bitmapPtr++] = argb;
}
}
}
}
}
// Event handler for the NewUser event
private void onUserTrackerNewUser(int id)
{
Console.WriteLine("New User {0}", id);
}
// Event handler for the LostUser event
private void onUserTrackerLostUser(int id)
{
Console.WriteLine("Lost User {0}", id);
}
// Event handler for the SkeletonUpdate event
private void onSkeletonUpdate(SkeletonData skeletonData)
{
if (_skeletonData != null)
_skeletonData.Dispose();
_skeletonData = (SkeletonData)skeletonData.Clone();
}
// Event handler for the HandTrackerUpdate event
private void onHandTrackerUpdate(HandTrackerData handTrackerData)
{
if (_handTrackerData != null)
_handTrackerData.Dispose();
_handTrackerData = (HandTrackerData)handTrackerData.Clone();
}
// Event handler for the gesture detection event
private void onNewGestures(GestureData gestureData)
{
// Display the information about detected gestures in the console
foreach (var gesture in gestureData.Gestures)
Console.WriteLine("Recognized {0} from user {1}", gesture.Type.ToString(), gesture.UserID);
}
}
}