forked from atduskgreg/Skelestreamer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
skelestreamer.pde
265 lines (217 loc) · 8.63 KB
/
skelestreamer.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
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
import processing.net.*;
import SimpleOpenNI.*;
import fsm.*;
SimpleOpenNI kinect;
FSM app;
CakeThread cakeThread;
String recordingUUID;
State setupMode = new State(this, "enterSetup", "doSetup", "exitSetup");
State streamMode = new State(this, "enterStream", "doStream", "exitStream");
boolean readyToStream = false;
int userId;
IntVector userList;
void setup() {
size(640,480);
// frameRate(30);
app = new FSM(setupMode);
kinect = new SimpleOpenNI(this);
kinect.enableDepth();
kinect.enableUser(SimpleOpenNI.SKEL_PROFILE_ALL);
kinect.setMirror(true);
userList = new IntVector();
enterSetup(); // FSM Bug: it should start by firing off the enter function for the starting state!
}
void draw() {
kinect.update();
image(kinect.depthImage(), 0, 0);
app.update();
//text(frameRate, 10,10);
// addSkeleton();
}
void mousePressed() {
if (app.isInState(setupMode) && readyToStream) {
app.transitionTo(streamMode);
}
}
void enterSetup() {
calculateNewUUID();
println("entering setup");
}
void doSetup() {
fill(0, 255,0);
textSize(30);
kinect.getUsers(userList);
if(userList.size() > 0) {
userId = userList.get(0);
if (kinect.isTrackingSkeleton(userId)) {
readyToStream = true;
text("Ready to stream.\nUUID: " + recordingUUID, 5, 25);
}
else {
text("Waiting for user calibration.\nUUID: "+ recordingUUID, 5, 25);
}
}
else {
text("Waiting for user.\nUUID: " + recordingUUID, 5, 25);
}
}
void exitSetup() {
}
void enterStream() {
cakeThread = new CakeThread(this);
cakeThread.start();
println("entering stream");
}
void doStream() {
fill(0, 255,0);
textSize(30);
text("Streaming: "+ recordingUUID + "\nQueueSize: " + cakeThread.queue.size(), 5, 25);
drawSkeleton(userId);
addSkeleton();
//Skeletor last = (Skeletor)cakeThread.queue.get(cakeThread.queue.size() - 1);
println(cakeThread.queue.size());
}
void exitStream(){}
void calculateNewUUID() {
recordingUUID = "SK" + random(100) + "A" + random(100) + "A" + random(100) + "A";
println(recordingUUID);
}
void addSkeleton() {
PVector head = new PVector();
PVector neck = new PVector();
PVector rightShoulder = new PVector();
PVector rightElbow = new PVector();
PVector rightHand = new PVector();
PVector leftShoulder = new PVector();
PVector leftElbow = new PVector();
PVector leftHand = new PVector();
PVector torso = new PVector();
PVector rightHip = new PVector();
PVector rightKnee = new PVector();
PVector rightFoot = new PVector();
PVector leftHip = new PVector();
PVector leftKnee = new PVector();
PVector leftFoot = new PVector();
kinect.getJointPositionSkeleton(userId, kinect.SKEL_HEAD, head);
kinect.getJointPositionSkeleton(userId, kinect.SKEL_NECK, neck);
kinect.getJointPositionSkeleton(userId, kinect.SKEL_RIGHT_SHOULDER, rightShoulder);
kinect.getJointPositionSkeleton(userId, kinect.SKEL_RIGHT_ELBOW, rightElbow);
kinect.getJointPositionSkeleton(userId, kinect.SKEL_RIGHT_HAND, rightHand);
kinect.getJointPositionSkeleton(userId, kinect.SKEL_LEFT_SHOULDER, leftShoulder);
kinect.getJointPositionSkeleton(userId, kinect.SKEL_LEFT_ELBOW, leftElbow);
kinect.getJointPositionSkeleton(userId, kinect.SKEL_LEFT_HAND, leftHand);
kinect.getJointPositionSkeleton(userId, kinect.SKEL_TORSO, torso);
kinect.getJointPositionSkeleton(userId, kinect.SKEL_RIGHT_HIP, rightHip);
kinect.getJointPositionSkeleton(userId, kinect.SKEL_RIGHT_KNEE, rightKnee);
kinect.getJointPositionSkeleton(userId, kinect.SKEL_RIGHT_FOOT, rightFoot);
kinect.getJointPositionSkeleton(userId, kinect.SKEL_LEFT_HIP, leftHip);
kinect.getJointPositionSkeleton(userId, kinect.SKEL_LEFT_KNEE, leftKnee);
kinect.getJointPositionSkeleton(userId, kinect.SKEL_LEFT_FOOT, leftFoot);
Skeletor skel = new Skeletor();
skel.head = head;
skel.neck = neck;
skel.rightShoulder = rightShoulder;
skel.rightElbow = rightElbow;
skel.rightHand = rightHand;
skel.leftShoulder = leftShoulder;
skel.leftElbow = leftElbow;
skel.leftHand = leftHand;
skel.torso = torso;
skel.rightHip = rightHip;
skel.rightKnee = rightKnee;
skel.rightFoot = rightFoot;
skel.leftHip = leftHip;
skel.leftKnee = leftKnee;
skel.leftFoot = leftFoot;
skel.uuid = recordingUUID;
cakeThread.queue.add(skel);
}
void fakeSkeleton() {
Skeletor skel = new Skeletor();
skel.head = new PVector(random(1000), random(1000), random(1000));
skel.neck = new PVector(random(1000), random(1000), random(1000));
skel.rightShoulder = new PVector(random(1000), random(1000), random(1000));
skel.rightElbow = new PVector(random(1000), random(1000), random(1000));
skel.rightHand = new PVector(random(1000), random(1000), random(1000));
skel.leftShoulder = new PVector(random(1000), random(1000), random(1000));
skel.leftElbow = new PVector(random(1000), random(1000), random(1000));
skel.leftHand = new PVector(random(1000), random(1000), random(1000));
skel.torso = new PVector(random(1000), random(1000), random(1000));
skel.rightHip = new PVector(random(1000), random(1000), random(1000));
skel.rightKnee = new PVector(random(1000), random(1000), random(1000));
skel.rightFoot = new PVector(random(1000), random(1000), random(1000));
skel.leftHip = new PVector(random(1000), random(1000), random(1000));
skel.leftKnee = new PVector(random(1000), random(1000), random(1000));
skel.leftFoot =new PVector(random(1000), random(1000), random(1000));
skel.uuid = recordingUUID;
cakeThread.queue.add(skel);
}
void drawSkeleton(int userId) {
stroke(0);
strokeWeight(5);
kinect.drawLimb(userId, SimpleOpenNI.SKEL_HEAD, SimpleOpenNI.SKEL_NECK);
kinect.drawLimb(userId, SimpleOpenNI.SKEL_NECK, SimpleOpenNI.SKEL_LEFT_SHOULDER);
kinect.drawLimb(userId, SimpleOpenNI.SKEL_LEFT_SHOULDER, SimpleOpenNI.SKEL_LEFT_ELBOW);
kinect.drawLimb(userId, SimpleOpenNI.SKEL_LEFT_ELBOW, SimpleOpenNI.SKEL_LEFT_HAND);
kinect.drawLimb(userId, SimpleOpenNI.SKEL_NECK, SimpleOpenNI.SKEL_RIGHT_SHOULDER);
kinect.drawLimb(userId, SimpleOpenNI.SKEL_RIGHT_SHOULDER, SimpleOpenNI.SKEL_RIGHT_ELBOW);
kinect.drawLimb(userId, SimpleOpenNI.SKEL_RIGHT_ELBOW, SimpleOpenNI.SKEL_RIGHT_HAND);
kinect.drawLimb(userId, SimpleOpenNI.SKEL_LEFT_SHOULDER, SimpleOpenNI.SKEL_TORSO);
kinect.drawLimb(userId, SimpleOpenNI.SKEL_RIGHT_SHOULDER, SimpleOpenNI.SKEL_TORSO);
kinect.drawLimb(userId, SimpleOpenNI.SKEL_TORSO, SimpleOpenNI.SKEL_LEFT_HIP);
kinect.drawLimb(userId, SimpleOpenNI.SKEL_LEFT_HIP, SimpleOpenNI.SKEL_LEFT_KNEE);
kinect.drawLimb(userId, SimpleOpenNI.SKEL_LEFT_KNEE, SimpleOpenNI.SKEL_LEFT_FOOT);
kinect.drawLimb(userId, SimpleOpenNI.SKEL_TORSO, SimpleOpenNI.SKEL_RIGHT_HIP);
kinect.drawLimb(userId, SimpleOpenNI.SKEL_RIGHT_HIP, SimpleOpenNI.SKEL_RIGHT_KNEE);
kinect.drawLimb(userId, SimpleOpenNI.SKEL_RIGHT_KNEE, SimpleOpenNI.SKEL_RIGHT_FOOT);
kinect.drawLimb(userId, SimpleOpenNI.SKEL_RIGHT_HIP, SimpleOpenNI.SKEL_LEFT_HIP);
noStroke();
fill(255, 0, 0);
drawJoint(userId, SimpleOpenNI.SKEL_HEAD);
drawJoint(userId, SimpleOpenNI.SKEL_NECK);
drawJoint(userId, SimpleOpenNI.SKEL_LEFT_SHOULDER);
drawJoint(userId, SimpleOpenNI.SKEL_LEFT_ELBOW);
drawJoint(userId, SimpleOpenNI.SKEL_NECK);
drawJoint(userId, SimpleOpenNI.SKEL_RIGHT_SHOULDER);
drawJoint(userId, SimpleOpenNI.SKEL_RIGHT_ELBOW);
drawJoint(userId, SimpleOpenNI.SKEL_TORSO);
drawJoint(userId, SimpleOpenNI.SKEL_LEFT_HIP);
drawJoint(userId, SimpleOpenNI.SKEL_LEFT_KNEE);
drawJoint(userId, SimpleOpenNI.SKEL_RIGHT_HIP);
drawJoint(userId, SimpleOpenNI.SKEL_LEFT_FOOT);
drawJoint(userId, SimpleOpenNI.SKEL_RIGHT_KNEE);
drawJoint(userId, SimpleOpenNI.SKEL_LEFT_HIP);
drawJoint(userId, SimpleOpenNI.SKEL_RIGHT_FOOT);
drawJoint(userId, SimpleOpenNI.SKEL_RIGHT_HAND);
drawJoint(userId, SimpleOpenNI.SKEL_LEFT_HAND);
}
void drawJoint(int userId, int jointID) {
PVector joint = new PVector();
float confidence = kinect.getJointPositionSkeleton(userId, jointID, joint);
if (confidence < 0.5) {
return;
}
PVector convertedJoint = new PVector();
kinect.convertRealWorldToProjective(joint, convertedJoint);
ellipse(convertedJoint.x, convertedJoint.y, 5, 5);
}
// user-tracking callbacks!
void onNewUser(int userId) {
println("start pose detection");
kinect.startPoseDetection("Psi", userId);
}
void onEndCalibration(int userId, boolean successful) {
if (successful) {
println(" User calibrated !!!");
kinect.startTrackingSkeleton(userId);
}
else {
println(" Failed to calibrate user !!!");
kinect.startPoseDetection("Psi", userId);
}
}
void onStartPose(String pose, int userId) {
println("Started pose for user");
kinect.stopPoseDetection(userId);
kinect.requestCalibrationSkeleton(userId, true);
}