/
KinectController.cs
340 lines (293 loc) · 11 KB
/
KinectController.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
using Microsoft.Kinect;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
namespace Microsoft.Samples.Kinect.BodyBasics
{
public enum ArmPointing
{
Right = 1,
Left = 2,
Nothing = 3
}
class KinectController
{
public static float ACTIVATION_THRESHOLD = 0.5f;
private const float STILL_THRESHOLD = 0.04f;
private const int COUNTER_THRESHOLD = 30;
private const float BORDERX_THRESHOLD = 0.1f;
private const float BORDERY_THRESHOLD = 0f;
private const float MAXX = 1.5f;
private const float MAXY = 1f;
private const string OPT_FILE = "calibration.json";
public KinectSensor kinectSensor;
public BodyFrameReader bodyReader;
public Body[] bodies;
public Body nearest;
//parameters
public ArmPointing Arm;
public Joint ShoulderLeft;
public Joint ElbowLeft;
public Joint HandTipLeft;
public Joint HandLeft;
public Joint WristLeft;
public Joint ShoulderRight;
public Joint ElbowRight;
public Joint HandTipRight;
public Joint HandRight;
public Joint WristRight;
public float lastAveragePositionRight;
public float lastAveragePositionLeft;
public int frameCounterRight;
public int frameCounterLeft;
public ulong indexNearest;
private bool hasPointed;
public float offsetX, offsetY;
public KinectController()
{
kinectSensor = KinectSensor.GetDefault();
// open the reader for the body frames
bodyReader = kinectSensor.BodyFrameSource.OpenReader();
kinectSensor.Open();
Arm = ArmPointing.Nothing;
hasPointed = false;
lastAveragePositionLeft = 0f;
lastAveragePositionRight = 0f;
frameCounterLeft = 0;
frameCounterRight = 0;
if (!File.Exists(OPT_FILE))
{
offsetX = 0;
offsetY = 0;
} else
{
string data = File.ReadAllText(OPT_FILE);
List <float> offset = JsonConvert.DeserializeObject<List<float>>(data);
offsetX = offset[0];
offsetY = offset[1];
}
}
public void Controller_FrameArrived(object sender, BodyFrameArrivedEventArgs e)
{
using (BodyFrame frame = e.FrameReference.AcquireFrame())
{
if (frame != null)
{
if (this.bodies == null)
{
this.bodies = new Body[frame.BodyCount];
}
frame.GetAndRefreshBodyData(this.bodies);
nearest = getNearestBody(this.bodies);
// populate all variables for right
ShoulderRight = nearest.Joints[JointType.ShoulderRight];
ElbowRight = nearest.Joints[JointType.ElbowRight];
HandTipRight = nearest.Joints[JointType.HandTipRight];
HandRight = nearest.Joints[JointType.HandRight];
WristRight = nearest.Joints[JointType.WristRight];
// populate all variables for left
ShoulderLeft = nearest.Joints[JointType.ShoulderLeft];
ElbowLeft = nearest.Joints[JointType.ElbowLeft];
HandTipLeft = nearest.Joints[JointType.HandTipLeft];
HandLeft = nearest.Joints[JointType.HandLeft];
WristLeft = nearest.Joints[JointType.WristLeft];
if(!checkPointingLeft())
checkPointingRight();
}
}
}
public bool checkPointingRight()
{
bool isPointing = false;
if (!hasPointed && nearest.HandRightState != HandState.Closed &&
nearest.HandRightState != HandState.Open &&
(ShoulderRight.Position.Z - HandRight.Position.Z) +
Math.Abs(ShoulderRight.Position.X - HandRight.Position.X) > ACTIVATION_THRESHOLD)
{
float averagePosition = (HandRight.Position.X + HandRight.Position.Y) / 2;
if (Math.Abs(averagePosition - lastAveragePositionRight) < STILL_THRESHOLD)
{
frameCounterRight++;
}
else
{
frameCounterRight = 0;
lastAveragePositionRight = averagePosition;
}
if (frameCounterRight > COUNTER_THRESHOLD)
{
isPointing = true;
hasPointed = true;
Arm = ArmPointing.Right;
}
}
else if ((ShoulderRight.Position.Z - HandRight.Position.Z) +
Math.Abs(ShoulderRight.Position.X - HandRight.Position.X) < ACTIVATION_THRESHOLD)
{
hasPointed = false;
frameCounterRight = 0;
Arm = ArmPointing.Nothing;
}
if (frameCounterRight > COUNTER_THRESHOLD)
{
isPointing = true;
Arm = ArmPointing.Right;
}
return isPointing;
}
public bool checkPointingLeft()
{
bool isPointing = false;
if (!hasPointed && nearest.HandLeftState != HandState.Closed &&
nearest.HandLeftState != HandState.Open &&
(ShoulderLeft.Position.Z - HandLeft.Position.Z) +
Math.Abs(ShoulderLeft.Position.X - HandLeft.Position.X) > ACTIVATION_THRESHOLD)
{
float averagePosition = (HandLeft.Position.X + HandLeft.Position.Y) / 2;
if (Math.Abs(averagePosition - lastAveragePositionLeft) < STILL_THRESHOLD)
{
frameCounterLeft++;
}
else
{
frameCounterLeft = 0;
lastAveragePositionLeft = averagePosition;
}
if (frameCounterLeft > COUNTER_THRESHOLD)
{
isPointing = true;
hasPointed = true;
Arm = ArmPointing.Left;
}
}
else if ((ShoulderLeft.Position.Z - HandLeft.Position.Z) +
Math.Abs(ShoulderLeft.Position.X - HandLeft.Position.X) < ACTIVATION_THRESHOLD)
{
hasPointed = false;
frameCounterLeft = 0;
Arm = ArmPointing.Nothing;
}
if (frameCounterLeft > COUNTER_THRESHOLD)
{
isPointing = true;
Arm = ArmPointing.Left;
}
return isPointing;
}
public float GetPointedX()
{
if (Arm == ArmPointing.Right)
{
float pointedX = calculateX(ShoulderRight.Position,
HandTipRight.Position) - offsetX;
return pointedX;
}
else if (Arm == ArmPointing.Left)
{
float pointedX = calculateX(ShoulderLeft.Position,
HandTipLeft.Position) - offsetX;
return pointedX;
}
return 0f;
}
public float GetPointedY()
{
if (Arm == ArmPointing.Right)
{
float pointedY = calculateY(ShoulderRight.Position,
HandTipRight.Position) - offsetY;
return pointedY;
}
else if (Arm == ArmPointing.Left)
{
float pointedY = calculateY(ShoulderLeft.Position,
HandTipLeft.Position) - offsetY;
return pointedY;
}
return 0f;
}
public int GetPointedZone()
{
if (Arm != ArmPointing.Nothing) {
float pointedX = GetPointedX();
float pointedY = GetPointedY();
if (pointedX < -BORDERX_THRESHOLD && pointedY < -BORDERY_THRESHOLD &&
pointedX > -MAXX && pointedY > -MAXY)
return 3;
else if (pointedX > BORDERX_THRESHOLD && pointedY < -BORDERY_THRESHOLD &&
pointedX < MAXX && pointedY > -MAXY)
return 4;
else if (pointedX > BORDERX_THRESHOLD && pointedY > BORDERY_THRESHOLD &&
pointedX < MAXX && pointedY < MAXY)
return 2;
else if (pointedX < -BORDERX_THRESHOLD && pointedY > BORDERY_THRESHOLD &&
pointedX > -MAXX && pointedY < MAXY)
return 1;
else
return 0;
}
return 0;
}
public float calculateX(CameraSpacePoint one, CameraSpacePoint two)
{
return one.X + ((one.Z * one.X - (one.Z * two.X)) / (two.Z - one.Z));
}
public float calculateY(CameraSpacePoint one, CameraSpacePoint two)
{
return one.Y + ((one.Z * one.Y - (one.Z * two.Y)) / (two.Z - one.Z));
}
public Body getNearestBody(Body[] bodies)
{
Body nearest = bodies[0];
int counter = 0;
foreach (Body b in bodies)
{
if (b.IsTracked)
{
if (!nearest.IsTracked)
{
nearest = b;
indexNearest = b.TrackingId;
}
else
{
IReadOnlyDictionary<JointType, Joint> joints = b.Joints;
IReadOnlyDictionary<JointType, Joint> nearestJoints = nearest.Joints;
float nearestPointOriginal = getNearestPointZ(joints);
float nearestPoint = getNearestPointZ(nearestJoints);
if (nearestPointOriginal < nearestPoint)
{
nearest = b;
indexNearest = b.TrackingId;
}
}
}
counter++;
}
return nearest;
}
public float getNearestPointZ(IReadOnlyDictionary<JointType, Joint> joints)
{
float result = 100;
foreach (JointType jointType in joints.Keys)
{
CameraSpacePoint position = joints[jointType].Position;
if (position.Z > 0.1f && position.Z < result)
{
result = position.Z;
}
}
return result;
}
public void Reset()
{
Arm = ArmPointing.Nothing;
hasPointed = false;
lastAveragePositionLeft = 0f;
lastAveragePositionRight = 0f;
frameCounterLeft = 0;
frameCounterRight = 0;
}
}
}