-
Notifications
You must be signed in to change notification settings - Fork 1
/
MainWindow.xaml.cs
615 lines (531 loc) · 23.8 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
namespace KinectBackCurveDetector
{
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using Microsoft.Kinect;
using System.Collections;
using System.Collections.Generic;
using System.Timers;
/// <summary>
/// Interaction logic for the MainWindow
/// </summary>
public partial class MainWindow : Window, INotifyPropertyChanged
{
private const bool debugData = false;
/// <summary>
/// Size of the RGB pixel in the bitmap
/// </summary>
private const int BytesPerPixel = 4;
/// <summary>
/// Collection of colors to be used to display the BodyIndexFrame data.
/// </summary>
private static readonly uint[] BodyColor =
{
0x0000FF00,
0x00FF0000,
0xFFFF4000,
0x40FFFF00,
0xFF40FF00,
0xFF808000,
};
/// <summary>
/// Active Kinect sensor
/// </summary>
private KinectSensor kinectSensor = null;
private StreamWriter writetext = new StreamWriter("write.txt");
/// <summary>
/// Reader for body index frames
/// </summary>
private BodyIndexFrameReader bodyIndexFrameReader = null;
private MultiSourceFrameReader depthAndBodyIndexReader = null;
/// <summary>
/// Description of the data contained in the body index frame
/// </summary>
private FrameDescription bodyIndexFrameDescription = null;
private FrameDescription depthFrameDescription;
private Body[] bodies = null;
/// <summary>
/// Bitmap to display
/// </summary>
private WriteableBitmap bodyIndexBitmap = null;
private WriteableBitmap spineBitmap;
/// <summary>
/// Intermediate storage for frame data converted to color
/// </summary>
private uint[] bodyIndexPixels = null;
private uint[] spinePixels = null;
/// <summary>
/// Current status text to display
/// </summary>
private string statusText = null;
private double angle;
/// <summary>
/// Initializes a new instance of the MainWindow class.
/// </summary>
public void OnWindowClosing(object sender, CancelEventArgs e)
{
writetext.Close();
// Handle closing logic, set e.Cancel as needed
}
public MainWindow()
{
// get the kinectSensor object
this.kinectSensor = KinectSensor.GetDefault();
// open the reader for the depth frames
this.bodyIndexFrameReader = this.kinectSensor.BodyIndexFrameSource.OpenReader();
// wire handler for frame arrival
//this.bodyIndexFrameReader.FrameArrived += this.Reader_FrameArrived;
Closing += OnWindowClosing;
this.bodyIndexFrameDescription = this.kinectSensor.BodyIndexFrameSource.FrameDescription;
this.depthFrameDescription = kinectSensor.DepthFrameSource.FrameDescription;
// allocate space to put the pixels being converted
this.bodyIndexPixels = new uint[this.bodyIndexFrameDescription.Width * this.bodyIndexFrameDescription.Height];
this.spinePixels = new uint[depthFrameDescription.Width * depthFrameDescription.Height];
// create the bitmap to display
this.bodyIndexBitmap = new WriteableBitmap(this.bodyIndexFrameDescription.Width, this.bodyIndexFrameDescription.Height, 96.0, 96.0, PixelFormats.Bgr32, null);
this.spineBitmap = new WriteableBitmap(this.depthFrameDescription.Width, this.depthFrameDescription.Height, 96.0, 96.0, PixelFormats.Bgr32, null);
// set IsAvailableChanged event notifier
this.kinectSensor.IsAvailableChanged += this.Sensor_IsAvailableChanged;
// Create reader for depth and body data
// To calculate back position
this.depthAndBodyIndexReader = this.kinectSensor.OpenMultiSourceFrameReader(FrameSourceTypes.Depth | FrameSourceTypes.BodyIndex | FrameSourceTypes.Body);
this.depthAndBodyIndexReader.MultiSourceFrameArrived += this.MultisorceReader_FrameArrived;
// open the sensor
this.kinectSensor.Open();
// set the status text
this.StatusText = this.kinectSensor.IsAvailable ? "Kinect is running"
: "Kinect is not available";
// use the window object as the view model in this simple example
this.DataContext = this;
// initialize the components (controls) of the window
this.InitializeComponent();
}
private Body getActiveBody(BodyFrame bodyFrame)
{
if (this.bodies == null)
this.bodies = new Body[6];
bodyFrame.GetAndRefreshBodyData(this.bodies);
Body activeBody = null;
foreach (var body in bodies)
{
if (body.IsTracked)
{
if (activeBody == null)
activeBody = body;
else
{
StatusText = "More than 1 body found";
return null;
}
}
}
return activeBody;
}
private bool aquireFrames(MultiSourceFrameArrivedEventArgs e, out BodyFrame bodyFrame, out DepthFrame depthFrame, out BodyIndexFrame bodyIndexFrame)
{
MultiSourceFrame multiSourceFrame = e.FrameReference.AcquireFrame();
bodyFrame = null;
depthFrame = null;
bodyIndexFrame = null;
if (multiSourceFrame == null)
return false;
bodyFrame = multiSourceFrame.BodyFrameReference.AcquireFrame();
if (bodyFrame == null)
return false;
depthFrame = multiSourceFrame.DepthFrameReference.AcquireFrame();
if (depthFrame == null)
{
bodyFrame.Dispose();
return false;
}
bodyIndexFrame = multiSourceFrame.BodyIndexFrameReference.AcquireFrame();
if (bodyIndexFrame == null)
{
bodyFrame.Dispose();
depthFrame.Dispose();
return false;
}
return true;
}
private bool aquireBodyDataAndBuffers(
BodyFrame bodyFrame, DepthFrame depthFrame, BodyIndexFrame bodyIndexFrame,
out Body body, out KinectBuffer depthBuffer, out KinectBuffer bodyIndexBuffer)
{
depthBuffer = null;
bodyIndexBuffer = null;
body = getActiveBody(bodyFrame);
if (body == null)
return false;
depthBuffer = depthFrame.LockImageBuffer();
var width = depthFrameDescription.Width;
var height = depthFrameDescription.Height;
if (depthBuffer == null ||
(width * height) != (depthBuffer.Size / this.depthFrameDescription.BytesPerPixel))
return false;
bodyIndexBuffer = bodyIndexFrame.LockImageBuffer();
if (bodyIndexBuffer == null || bodyIndexBuffer.Size * 2 != depthBuffer.Size)
{
depthBuffer.Dispose();
return false;
}
return true;
}
private void MultisorceReader_FrameArrived(object sender, MultiSourceFrameArrivedEventArgs e)
{
// acquire all frames and then process them
if (!aquireFrames(e, out var bodyFrame, out var depthFrame, out var bodyIndexFrame))
return;
if (!aquireBodyDataAndBuffers(bodyFrame, depthFrame, bodyIndexFrame, out var body, out var depthBuffer, out var bodyIndexBuffer))
{
bodyFrame.Dispose();
depthFrame.Dispose();
bodyIndexFrame.Dispose();
return;
}
timer.Restart();
// calculate and show the back position on the screen
var spinePoints = GetSpinePoints(body, depthBuffer, bodyIndexBuffer);
drawSpinePoints(spinePoints);
RenderBodyIndexPixels();
timer.Stop();
writetext.WriteLine($"{1000.0 / timer.ElapsedMilliseconds} fps, {timer.ElapsedMilliseconds} ms");
// dispose of data
depthBuffer.Dispose();
bodyIndexBuffer.Dispose();
bodyFrame.Dispose();
depthFrame.Dispose();
bodyIndexFrame.Dispose();
}
Comparison<Point> pointCompare = delegate (Point p1, Point p2)
{
return p1.X.CompareTo(p2.X);
};
Stopwatch timer = new Stopwatch();
private void drawSpinePoints(List<Point> spinePoints)
{
if (spinePoints == null)
return;
Array.Clear(spinePixels, 0, spinePixels.Length);
var width = depthFrameDescription.Width;
var height = depthFrameDescription.Height;
// calculate max and mix points
// of the spine
int maxHeightPoint = 0, minHeightPoint = 0;
int maxZPoint = 0, minZPoint = 0;
for (int i = 0; i < spinePoints.Count; i++)
{
if (spinePoints[minHeightPoint].Y > spinePoints[i].Y)
minHeightPoint = i;
if (spinePoints[maxHeightPoint].Y < spinePoints[i].Y)
maxHeightPoint = i;
if (spinePoints[minZPoint].X > spinePoints[i].X)
minZPoint = i;
if (spinePoints[maxZPoint].X < spinePoints[i].X)
maxZPoint = i;
}
double roundFactor = 0.3;
// round them, otherwise the picture is to shaky
// because max and min value are slightly different from frame to frame
double maxHeight = BackTrackerHelper.upperBound(spinePoints[maxHeightPoint].Y, roundFactor);
double minHeight = BackTrackerHelper.lowerBound(spinePoints[minHeightPoint].Y, roundFactor);
double maxZ = BackTrackerHelper.upperBound(spinePoints[maxZPoint].X, roundFactor);
double minZ = BackTrackerHelper.lowerBound(spinePoints[minZPoint].X, roundFactor);
double conversionRateY = height / (maxHeight - minHeight);
double conversionRateZ = width / (maxZ - minZ);
double conversionRate = Math.Min(conversionRateY, conversionRateZ);
// convert coordinates of a spine to pixels on the screen
for (int i = 0; i < spinePoints.Count; i++)
{
int pictureY = height - 1 - (int)(conversionRate * (spinePoints[i].Y - minHeight));
int pictureX = (int)(conversionRate * (spinePoints[i].X - minZ));
this.spinePixels[pictureY * width + pictureX] = BodyColor[0];
}
this.spineBitmap.WritePixels(
new Int32Rect(0, 0, this.spineBitmap.PixelWidth, this.spineBitmap.PixelHeight),
this.spinePixels,
this.spineBitmap.PixelWidth * (int)BytesPerPixel,
0);
}
private unsafe List<Point> GetSpinePoints(Body activeBody,KinectBuffer depthBuffer, KinectBuffer bodyIndexBuffer)
{
var joints = activeBody.Joints;
if (!BackTrackerHelper.isBodyTracked(joints))
return null;
// calculateRotationAngle(leftShoulder, rightShoulder, out double tan, out double cos);
var width = depthFrameDescription.Width;
var height = depthFrameDescription.Height;
var bodyIndexFrameData = bodyIndexBuffer.UnderlyingBuffer;
byte* frameData = (byte*)bodyIndexFrameData;
ushort* depthFrameData = (ushort*)depthBuffer.UnderlyingBuffer;
// preparation work
List<Point> spinePoints = new List<Point>(height);
BackArea backArea = new BackArea(kinectSensor, joints);
int lastPointY = -1;
Point point = new Point();
var depthSpacePoint = new DepthSpacePoint();
var point3D = new CameraSpacePoint();
if (debugData)
writetext.WriteLine("############## FRAME START ##############");
List<Point> rowPoints = new List<Point>();
var size = bodyIndexBuffer.Size;
for (int i = 0; i < (int)size; ++i)
{
// the BodyColor array has been sized to match
// BodyFrameSource.BodyCount
// check if point belong to a person
if (frameData[i] < BodyColor.Length)
{
point.X = i % width;
point.Y = i / width;
depthSpacePoint.X = i % width;
depthSpacePoint.Y = i / width;
// check if point is on the back
var pointIsOnBack = backArea.isPointInSpineArea(point);
if (pointIsOnBack)
this.bodyIndexPixels[i] = 0xFFFFFFFF;
else
this.bodyIndexPixels[i] = BodyColor[frameData[i]];
if (!pointIsOnBack)
continue;
// fill the current row of points (pixels that have the same Y)
point3D = kinectSensor.CoordinateMapper.MapDepthPointToCameraSpace(depthSpacePoint, depthFrameData[i]);
rowPoints.Add(new Point(point3D.Z, point3D.Y));
if (debugData)
writetext.WriteLine($"{point3D.Z}, {point3D.Y}");
if (i / width != lastPointY && rowPoints.Count != 0)
{
// if the current pixel doesn't belong to the same row
// process the current row and calculate the spine point from it
rowPoints.Sort(pointCompare);
spinePoints.Add(BackTrackerHelper.calculateSpinePoint(rowPoints));
rowPoints.Clear();
if (debugData)
writetext.WriteLine("-100, -100");
}
lastPointY = i / width;
}
else
{
this.bodyIndexPixels[i] = 0x00000000;
}
}
if (debugData)
writetext.WriteLine("************** FRAME END **************");
// process the last line of pixels
if (rowPoints.Count != 0)
{
rowPoints.Sort(pointCompare);
spinePoints.Add(BackTrackerHelper.calculateSpinePoint(rowPoints));
rowPoints.Clear();
}
return spinePoints;
}
/// <summary>
/// INotifyPropertyChangedPropertyChanged event to allow window controls to bind to changeable data
/// </summary>
public event PropertyChangedEventHandler PropertyChanged;
/// <summary>
/// Gets the bitmap to display
/// </summary>
public ImageSource ImageSource
{
get
{
return this.bodyIndexBitmap;
}
}
public ImageSource BackImageSource
{
get
{
return this.spineBitmap;
}
}
/// <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)
{
this.PropertyChanged(this, new PropertyChangedEventArgs("StatusText"));
}
}
}
}
/// <summary>
/// Execute shutdown tasks
/// </summary>
/// <param name="sender">object sending the event</param>
/// <param name="e">event arguments</param>
private void MainWindow_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
if (this.bodyIndexFrameReader != null)
{
// remove the event handler
this.bodyIndexFrameReader.FrameArrived -= this.Reader_FrameArrived;
// BodyIndexFrameReder is IDisposable
this.bodyIndexFrameReader.Dispose();
this.bodyIndexFrameReader = null;
}
if (this.depthAndBodyIndexReader != null)
{
this.depthAndBodyIndexReader.MultiSourceFrameArrived -= this.MultisorceReader_FrameArrived;
this.depthAndBodyIndexReader.Dispose();
this.depthAndBodyIndexReader = null;
}
if (this.kinectSensor != null)
{
this.kinectSensor.Close();
this.kinectSensor = null;
}
}
/// <summary>
/// Handles the user clicking on the screenshot button
/// </summary>
/// <param name="sender">object sending the event</param>
/// <param name="e">event arguments</param>
private void ScreenshotButton_Click(object sender, RoutedEventArgs e)
{
if (this.bodyIndexBitmap != null)
{
// create a png bitmap encoder which knows how to save a .png file
BitmapEncoder encoder = new PngBitmapEncoder();
// create frame from the writable bitmap and add to encoder
encoder.Frames.Add(BitmapFrame.Create(this.bodyIndexBitmap));
string time = System.DateTime.UtcNow.ToString("hh'-'mm'-'ss", CultureInfo.CurrentUICulture.DateTimeFormat);
string myPhotos = Environment.GetFolderPath(Environment.SpecialFolder.MyPictures);
string path = Path.Combine(myPhotos, "KinectScreenshot-BodyIndex-" + time + ".png");
// write the new file to disk
try
{
// FileStream is IDisposable
using (FileStream fs = new FileStream(path, FileMode.Create))
{
encoder.Save(fs);
}
this.StatusText = string.Format(CultureInfo.CurrentCulture, Properties.Resources.SavedScreenshotStatusTextFormat, path);
}
catch (IOException)
{
this.StatusText = string.Format(CultureInfo.CurrentCulture, Properties.Resources.FailedScreenshotStatusTextFormat, path);
}
}
}
/// <summary>
/// Handles the body index frame data arriving from the sensor
/// </summary>
/// <param name="sender">object sending the event</param>
/// <param name="e">event arguments</param>
private void Reader_FrameArrived(object sender, BodyIndexFrameArrivedEventArgs e)
{
bool bodyIndexFrameProcessed = false;
using (BodyIndexFrame bodyIndexFrame = e.FrameReference.AcquireFrame())
{
if (bodyIndexFrame != null)
{
// the fastest way to process the body index data is to directly access
// the underlying buffer
using (Microsoft.Kinect.KinectBuffer bodyIndexBuffer = bodyIndexFrame.LockImageBuffer())
{
// verify data and write the color data to the display bitmap
if (((this.bodyIndexFrameDescription.Width * this.bodyIndexFrameDescription.Height) == bodyIndexBuffer.Size) &&
(this.bodyIndexFrameDescription.Width == this.bodyIndexBitmap.PixelWidth) && (this.bodyIndexFrameDescription.Height == this.bodyIndexBitmap.PixelHeight))
{
this.ProcessBodyIndexFrameData(bodyIndexBuffer.UnderlyingBuffer, bodyIndexBuffer.Size);
bodyIndexFrameProcessed = true;
}
}
}
}
if (bodyIndexFrameProcessed)
{
this.RenderBodyIndexPixels();
}
}
/// <summary>
/// Directly accesses the underlying image buffer of the BodyIndexFrame to
/// create a displayable bitmap.
/// This function requires the /unsafe compiler option as we make use of direct
/// access to the native memory pointed to by the bodyIndexFrameData pointer.
/// </summary>
/// <param name="bodyIndexFrameData">Pointer to the BodyIndexFrame image data</param>
/// <param name="bodyIndexFrameDataSize">Size of the BodyIndexFrame image data</param>
private unsafe void ProcessBodyIndexFrameData(IntPtr bodyIndexFrameData, uint bodyIndexFrameDataSize)
{
byte* frameData = (byte*)bodyIndexFrameData;
// convert body index to a visual representation
for (int i = 0; i < (int)bodyIndexFrameDataSize; ++i)
{
// the BodyColor array has been sized to match
// BodyFrameSource.BodyCount
if (frameData[i] < BodyColor.Length)
{
// this pixel is part of a player,
// display the appropriate color
this.bodyIndexPixels[i] = BodyColor[frameData[i]];
}
else
{
// this pixel is not part of a player
// display black
this.bodyIndexPixels[i] = 0x00000000;
}
}
}
/// <summary>
/// Renders color pixels into the writeableBitmap.
/// </summary>
private void RenderBodyIndexPixels()
{
this.bodyIndexBitmap.WritePixels(
new Int32Rect(0, 0, this.bodyIndexBitmap.PixelWidth, this.bodyIndexBitmap.PixelHeight),
this.bodyIndexPixels,
this.bodyIndexBitmap.PixelWidth * (int)BytesPerPixel,
0);
}
/// <summary>
/// Handles the event which the sensor becomes unavailable (E.g. paused, closed, unplugged).
/// </summary>
/// <param name="sender">object sending the event</param>
/// <param name="e">event arguments</param>
private void Sensor_IsAvailableChanged(object sender, IsAvailableChangedEventArgs e)
{
// on failure, set the status text
this.StatusText = this.kinectSensor.IsAvailable ? Properties.Resources.RunningStatusText
: Properties.Resources.SensorNotAvailableStatusText;
this.StatusText = this.kinectSensor.IsAvailable ? "Kinect is connected"
: "Kinect is not available";
}
private void TextBox_KeyUp(object sender, System.Windows.Input.KeyEventArgs e)
{
if (e.Key == System.Windows.Input.Key.Enter)
{
var text = ((TextBox)sender).Text;
if (Double.TryParse(text, out double angle))
this.angle = angle;
else
StatusText = "Please input an integer";
}
}
}
}