-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKinectV2SkeletonOSC.pde
196 lines (159 loc) · 6.47 KB
/
KinectV2SkeletonOSC.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
/*
Based on Skeleton Colour sketch by Thomas Sanchez Lengeling.
KinectPV2, Kinect for Windows v2 library for processing
Skeleton color map example + Skeleton3D Position Output over OSC
Skeleton (x,y) screen relative (?) positions are mapped to match the color Frame
Skeleton (x,y,z) positions (floats) sent over OSC
*/
import KinectPV2.KJoint;
import KinectPV2.*;
import netP5.*;
import oscP5.*;
KinectPV2 kinect;
OscP5 oscP5;
NetAddress oscOut;
void setup() {
size(1920, 1080, P3D);
kinect = new KinectPV2(this);
kinect.enableSkeletonColorMap(true);
kinect.enableColorImg(true);
//enable 3d with (x,y,z) position
kinect.enableSkeleton3DMap(true);
kinect.init();
oscP5 = new OscP5(this, 12000);
oscOut = new NetAddress("127.0.0.1",33333);
}
void draw() {
background(0);
image(kinect.getColorImage(), 0, 0, width, height);
ArrayList<KSkeleton> skeletonArray = kinect.getSkeletonColorMap(); // SCREEN RELATIVE?
ArrayList<KSkeleton> skeletonArray3D = kinect.getSkeleton3d(); // FLOATS
//individual JOINTS
for (int i = 0; i < skeletonArray.size(); i++) {
KSkeleton skeleton = (KSkeleton) skeletonArray.get(i);
KSkeleton skeleton3D = (KSkeleton) skeletonArray3D.get(i);
if (skeleton.isTracked()) {
KJoint[] joints = skeleton.getJoints();
KJoint[] joints3D = skeleton3D.getJoints();
PVector headPos = new PVector();
headPos.x = joints3D[KinectPV2.JointType_Head].getX();
headPos.y = joints3D[KinectPV2.JointType_Head].getY();
headPos.z = joints3D[KinectPV2.JointType_Head].getZ();
OscMessage head = new OscMessage("/head_pos/");
head.add(headPos.x);
head.add(headPos.y);
head.add(headPos.z);
oscP5.send(head, oscOut);
PVector rightHand = new PVector();
rightHand.x = joints3D[KinectPV2.JointType_HandRight].getX();
rightHand.y = joints3D[KinectPV2.JointType_HandRight].getY();
rightHand.z = joints3D[KinectPV2.JointType_HandRight].getZ();
OscMessage rHand = new OscMessage("/right_hand/");
rHand.add(rightHand.x);
rHand.add(rightHand.y);
rHand.add(rightHand.z);
oscP5.send(rHand, oscOut);
color col = skeleton.getIndexColor();
fill(col);
stroke(col);
drawBody(joints);
//draw different color for each hand state
drawHandState(joints[KinectPV2.JointType_HandRight]);
drawHandState(joints[KinectPV2.JointType_HandLeft]);
}
}
fill(255, 0, 0);
text(frameRate, 50, 50);
}
//DRAW BODY
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);
}
//draw joint
void drawJoint(KJoint[] joints, int jointType) {
pushMatrix();
translate(joints[jointType].getX(), joints[jointType].getY(), joints[jointType].getZ());
ellipse(0, 0, 25, 25);
popMatrix();
}
//draw bone
void drawBone(KJoint[] joints, int jointType1, int jointType2) {
pushMatrix();
translate(joints[jointType1].getX(), joints[jointType1].getY(), joints[jointType1].getZ());
ellipse(0, 0, 25, 25);
popMatrix();
line(joints[jointType1].getX(), joints[jointType1].getY(), joints[jointType1].getZ(), joints[jointType2].getX(), joints[jointType2].getY(), joints[jointType2].getZ());
}
//draw hand state
void drawHandState(KJoint joint) {
noStroke();
handState(joint.getState());
pushMatrix();
translate(joint.getX(), joint.getY(), joint.getZ());
ellipse(0, 0, 70, 70);
popMatrix();
}
/*
Different hand state
KinectPV2.HandState_Open
KinectPV2.HandState_Closed
KinectPV2.HandState_Lasso
KinectPV2.HandState_NotTracked
*/
void handState(int handState) {
switch(handState) {
case KinectPV2.HandState_Open:
fill(0, 255, 0);
break;
case KinectPV2.HandState_Closed:
fill(255, 0, 0);
break;
case KinectPV2.HandState_Lasso:
fill(0, 0, 255);
break;
case KinectPV2.HandState_NotTracked:
fill(255, 255, 255);
break;
}
}
/* Incoming OSC messages are forwarded to the oscEvent method */
void oscEvent(OscMessage theOscMessage) {
/* print the address pattern and the typetag of the received message */
print("### received an OSC message ###");
print(" addrpattern: "+theOscMessage.addrPattern());
println(" typetag: "+theOscMessage.typetag());
}