-
Notifications
You must be signed in to change notification settings - Fork 0
/
KinectStream.pde
206 lines (182 loc) · 6.84 KB
/
KinectStream.pde
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
import KinectPV2.KJoint;
import KinectPV2.*;
import java.nio.FloatBuffer;
class KinectStream {
KinectPV2 k;
final float zVal = 300;
final float rotX = PI;
final int zScale = 4;
HashMap<Integer, ArrayList<MovingVector>> joints = new HashMap();
PGraphics3D canvas;
KinectStream(PApplet p) {
k = new KinectPV2(p);
k.enableBodyTrackImg(true);
k.enableDepthMaskImg(true);
k.enableSkeleton3DMap(true);
k.init();
}
void drawDebugs(PGraphics3D canvas) {
this.canvas = canvas;
canvas.image(k.getDepthMaskImage(), 0, 0, 320, 240);
ArrayList<PImage> bodyTrackList = k.getBodyTrackUser();
for (int i = 0; i < bodyTrackList.size(); i++) {
PImage bodyTrackImg = (PImage)bodyTrackList.get(i);
if (i <= 2)
canvas.image(bodyTrackImg, 320 + 240*i, 0, 320, 240);
else
canvas.image(bodyTrackImg, 320 + 240*(i - 3), 424, 320, 240 );
}
drawSkels();
}
ArrayList<KJoint[]> allJoints() {
ArrayList<KJoint[]> joints = new ArrayList();
for (KSkeleton skeleton : k.getSkeleton3d()) {
if (skeleton.isTracked()) {
joints.add(skeleton.getJoints());
}
}
return joints;
}
KJoint[] handJoints(KJoint[] joints) {
return new KJoint[] {
joints[KinectPV2.JointType_HandRight],
joints[KinectPV2.JointType_HandLeft]
};
}
ArrayList<MovingVector> vectorsFor(int[] types) {
ArrayList<KJoint[]> jointMatrix = allJoints();
ArrayList<MovingVector> all = new ArrayList();
for (int type : types) {
if (joints.get(type) == null) {
joints.put(type, new ArrayList());
}
ArrayList<MovingVector> list = joints.get(type);
for (int i = 0; i<jointMatrix.size(); i++) {
KJoint[] joints = jointMatrix.get(i);
KJoint right = joints[type];
try {
list.get(i).update(posFor(right).mult(zVal));
}
catch (IndexOutOfBoundsException ie) {
list.add(new MovingVector(posFor(right).mult(zVal)));
}
}
for (MovingVector i : list) {
all.add(i);
}
}
return all;
}
ArrayList<MovingVector> jointFor(int type) {
ArrayList<KJoint[]> jointMatrix = allJoints();
if (joints.get(type) == null) {
joints.put(type, new ArrayList());
}
ArrayList<MovingVector> list = joints.get(type);
for (int i = 0; i<jointMatrix.size(); i++) {
KJoint[] joints = jointMatrix.get(i);
KJoint right = joints[type];
try {
list.get(i).update(right.getPosition().mult(zVal));
}
catch (IndexOutOfBoundsException ie) {
list.add(new MovingVector(right.getPosition().mult(zVal)));
}
}
return list;
}
void drawSkels() {
//translate the scene to the center
canvas.pushMatrix();
canvas.translate(width/2, height/2, 0);
canvas.rotateX(rotX);
canvas.fill(255, 40, 30);
canvas.stroke(255, 40, 30);
for (KJoint[] joints : allJoints()) {
drawBody(joints);
for (KJoint hand : handJoints(joints)) {
drawHandState(hand);
}
}
canvas.popMatrix();
}
void drawBody(KJoint[] joints) {
drawBone(joints, KinectPV2.JointType_Head, KinectPV2.JointType_Neck);
drawBone(joints, KinectPV2.JointType_Neck, KinectPV2.JointType_SpineShoulder);
drawBone(joints, KinectPV2.JointType_SpineShoulder, KinectPV2.JointType_SpineMid);
drawBone(joints, KinectPV2.JointType_SpineMid, KinectPV2.JointType_SpineBase);
drawBone(joints, KinectPV2.JointType_SpineShoulder, KinectPV2.JointType_ShoulderRight);
drawBone(joints, KinectPV2.JointType_SpineShoulder, KinectPV2.JointType_ShoulderLeft);
drawBone(joints, KinectPV2.JointType_SpineBase, KinectPV2.JointType_HipRight);
drawBone(joints, KinectPV2.JointType_SpineBase, KinectPV2.JointType_HipLeft);
// Right Arm
drawBone(joints, KinectPV2.JointType_ShoulderRight, KinectPV2.JointType_ElbowRight);
drawBone(joints, KinectPV2.JointType_ElbowRight, KinectPV2.JointType_WristRight);
drawBone(joints, KinectPV2.JointType_WristRight, KinectPV2.JointType_HandRight);
drawBone(joints, KinectPV2.JointType_HandRight, KinectPV2.JointType_HandTipRight);
drawBone(joints, KinectPV2.JointType_WristRight, KinectPV2.JointType_ThumbRight);
// Left Arm
drawBone(joints, KinectPV2.JointType_ShoulderLeft, KinectPV2.JointType_ElbowLeft);
drawBone(joints, KinectPV2.JointType_ElbowLeft, KinectPV2.JointType_WristLeft);
drawBone(joints, KinectPV2.JointType_WristLeft, KinectPV2.JointType_HandLeft);
drawBone(joints, KinectPV2.JointType_HandLeft, KinectPV2.JointType_HandTipLeft);
drawBone(joints, KinectPV2.JointType_WristLeft, KinectPV2.JointType_ThumbLeft);
// Right Leg
drawBone(joints, KinectPV2.JointType_HipRight, KinectPV2.JointType_KneeRight);
drawBone(joints, KinectPV2.JointType_KneeRight, KinectPV2.JointType_AnkleRight);
drawBone(joints, KinectPV2.JointType_AnkleRight, KinectPV2.JointType_FootRight);
// Left Leg
drawBone(joints, KinectPV2.JointType_HipLeft, KinectPV2.JointType_KneeLeft);
drawBone(joints, KinectPV2.JointType_KneeLeft, KinectPV2.JointType_AnkleLeft);
drawBone(joints, KinectPV2.JointType_AnkleLeft, KinectPV2.JointType_FootLeft);
drawJoint(joints, KinectPV2.JointType_HandTipLeft);
drawJoint(joints, KinectPV2.JointType_HandTipRight);
drawJoint(joints, KinectPV2.JointType_FootLeft);
drawJoint(joints, KinectPV2.JointType_FootRight);
drawJoint(joints, KinectPV2.JointType_ThumbLeft);
drawJoint(joints, KinectPV2.JointType_ThumbRight);
drawJoint(joints, KinectPV2.JointType_Head);
}
PVector posFor(KJoint joint) {
return joint.getPosition().copy();
}
void drawJoint(KJoint[] joints, int jointType) {
canvas.fill(255, 40, 30);
canvas.stroke(255, 40, 30);
PVector pos = posFor(joints[jointType]).mult(zVal);
canvas.strokeWeight(2.0f + pos.z * zScale / zVal);
canvas.point(pos.x, pos.y);
}
void drawBone(KJoint[] joints, int jointType1, int jointType2) {
canvas.fill(2, 40, 230);
canvas.stroke(25, 40, 230);
PVector pos = posFor(joints[jointType2]).mult(zVal);
PVector pos1 = posFor(joints[jointType1]).mult(zVal);
canvas.strokeWeight(2.0f + pos.z * zScale / zVal);
canvas.line(pos1.x, pos1.y, pos.x, pos.y);
canvas.point(pos.x, pos.y);
}
void drawHandState(KJoint joint) {
PVector pos = posFor(joint).mult(zVal);
handState(joint.getState());
canvas.strokeWeight(((2.0f + pos.z * zScale / zVal) * 4));
canvas.point(pos.x, pos.y);
}
void handState(int handState) {
println(handState);
switch(handState) {
case KinectPV2.HandState_Open:
canvas.stroke(0, 255, 0);
break;
case KinectPV2.HandState_Closed:
canvas.stroke(255, 0, 0);
break;
case KinectPV2.HandState_Lasso:
canvas.stroke(0, 0, 255);
break;
case KinectPV2.HandState_NotTracked:
canvas.stroke(100, 100, 100);
break;
}
}
}