-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLeapMotion_Index_finger.pde
348 lines (263 loc) · 8.98 KB
/
LeapMotion_Index_finger.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
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
//adapted from https://github.com/nok/leap-motion-processing/blob/master/examples/e1_basic/e1_basic.pde
//Sends 15 features ((x,y,z) tip of each finger) to Wekinator
// sends to port 6448 using /wek/inputs message
import de.voidplus.leapmotion.*;
import oscP5.*;
import netP5.*;
int num=0;
OscP5 oscP5;
NetAddress dest;
LeapMotion leap;
int numFound = 0;
PVector index_velocity;
//float[] features = new float[15];
float[] features = new float[3];//changed to 3 to only get index finger
void setup() {
size(1200, 800, OPENGL);
background(255);
// ...
/* start oscP5, listening for incoming messages at port 12000 */
oscP5 = new OscP5(this,9000);
dest = new NetAddress("127.0.0.1",6448);
leap = new LeapMotion(this);
sendInputNames();
}
void draw() {
background(255);
// ...
int fps = leap.getFrameRate();
// ========= HANDS =========
numFound = 0;
for (Hand hand : leap.getHands ()) {
numFound++;
// ----- BASICS -----
int hand_id = hand.getId();
PVector hand_position = hand.getPosition();
PVector hand_stabilized = hand.getStabilizedPosition();
PVector hand_direction = hand.getDirection();
PVector hand_dynamics = hand.getDynamics();
float hand_roll = hand.getRoll();
float hand_pitch = hand.getPitch();
float hand_yaw = hand.getYaw();
boolean hand_is_left = hand.isLeft();
boolean hand_is_right = hand.isRight();
float hand_grab = hand.getGrabStrength();
float hand_pinch = hand.getPinchStrength();
float hand_time = hand.getTimeVisible();
PVector sphere_position = hand.getSpherePosition();
float sphere_radius = hand.getSphereRadius();
// ----- SPECIFIC FINGER -----
Finger finger_thumb = hand.getThumb();
// or hand.getFinger("thumb");
// or hand.getFinger(0);
Finger finger_index = hand.getIndexFinger();
// or hand.getFinger("index");
// or hand.getFinger(1);
Finger finger_middle = hand.getMiddleFinger();
// or hand.getFinger("middle");
// or hand.getFinger(2);
Finger finger_ring = hand.getRingFinger();
// or hand.getFinger("ring");
// or hand.getFinger(3);
Finger finger_pink = hand.getPinkyFinger();
// or hand.getFinger("pinky");
// or hand.getFinger(4);
//===========My Code to track finger velocity==================//HawkMan
index_velocity = finger_index.getVelocity();
textSize(100);
if (frameCount % 10 == 0 && index_velocity.mag() > 2000) {
text(index_velocity.mag(), 100, 100);
}
textSize(10);
// ----- DRAWING -----
hand.draw();
// hand.drawSphere();
// ========= ARM =========
if (hand.hasArm()) {
Arm arm = hand.getArm();
float arm_width = arm.getWidth();
PVector arm_wrist_pos = arm.getWristPosition();
PVector arm_elbow_pos = arm.getElbowPosition();
}
// ========= FINGERS =========
for (Finger finger : hand.getFingers()) {
// Alternatives:
// hand.getOutstrechtedFingers();
// hand.getOutstrechtedFingersByAngle();
// ----- BASICS -----
int finger_id = finger.getId();
PVector finger_position = finger.getPosition();
PVector finger_stabilized = finger.getStabilizedPosition();
PVector finger_velocity = finger.getVelocity();
PVector finger_direction = finger.getDirection();
float finger_time = finger.getTimeVisible();
// ----- SPECIFIC FINGER -----
/*switch(finger.getType()) {
case 0:
// System.out.println("thumb");
PVector pos = finger.getPosition();
features[0] = pos.x;
features[1] = pos.y;
features[2] = pos.z;
break;
case 1:
// System.out.println("index");
pos = finger.getPosition();
features[3] = pos.x;
features[4] = pos.y;
features[5] = pos.z;
break;
case 2:
// System.out.println("middle");
pos = finger.getPosition();
features[6] = pos.x;
features[7] = pos.y;
features[8] = pos.z;
break;
case 3:
// System.out.println("ring");
pos = finger.getPosition();
features[9] = pos.x;
features[10] = pos.y;
features[11] = pos.z;
break;
case 4:
// System.out.println("pinky");
pos = finger.getPosition();
features[12] = pos.x;
features[13] = pos.y;
features[14] = pos.z;
break;
}*/
//========JUST TO GET INDEX FINGER====================
if (finger.getType() == 1){
PVector pos = finger.getPosition();
features[0] = pos.x;
features[1] = pos.y;
features[2] = pos.z;
}
// ----- SPECIFIC BONE -----
Bone bone_distal = finger.getDistalBone();
// or finger.get("distal");
// or finger.getBone(0);
Bone bone_intermediate = finger.getIntermediateBone();
// or finger.get("intermediate");
// or finger.getBone(1);
Bone bone_proximal = finger.getProximalBone();
// or finger.get("proximal");
// or finger.getBone(2);
Bone bone_metacarpal = finger.getMetacarpalBone();
// or finger.get("metacarpal");
// or finger.getBone(3);
// ----- DRAWING -----
// finger.draw(); // = drawLines()+drawJoints()
// finger.drawLines();
// finger.drawJoints();
// ----- TOUCH EMULATION -----
int touch_zone = finger.getTouchZone();
float touch_distance = finger.getTouchDistance();
switch(touch_zone) {
case -1: // None
break;
case 0: // Hovering
// println("Hovering (#"+finger_id+"): "+touch_distance);
break;
case 1: // Touching
// println("Touching (#"+finger_id+")");
break;
}
}
// ========= TOOLS =========
for (Tool tool : hand.getTools ()) {
// ----- BASICS -----
int tool_id = tool.getId();
PVector tool_position = tool.getPosition();
PVector tool_stabilized = tool.getStabilizedPosition();
PVector tool_velocity = tool.getVelocity();
PVector tool_direction = tool.getDirection();
float tool_time = tool.getTimeVisible();
// ----- DRAWING -----
// tool.draw();
// ----- TOUCH EMULATION -----
int touch_zone = tool.getTouchZone();
float touch_distance = tool.getTouchDistance();
switch(touch_zone) {
case -1: // None
break;
case 0: // Hovering
// println("Hovering (#"+tool_id+"): "+touch_distance);
break;
case 1: // Touching
// println("Touching (#"+tool_id+")");
break;
}
}
}
// ========= DEVICES =========
for (Device device : leap.getDevices ()) {
float device_horizontal_view_angle = device.getHorizontalViewAngle();
float device_verical_view_angle = device.getVerticalViewAngle();
float device_range = device.getRange();
}
// =========== OSC ============
if (num % 3 == 0) {
sendOsc();
}
num++;
}
// ========= CALLBACKS =========
void leapOnInit() {
// println("Leap Motion Init");
}
void leapOnConnect() {
// println("Leap Motion Connect");
}
void leapOnFrame() {
// println("Leap Motion Frame");
}
void leapOnDisconnect() {
// println("Leap Motion Disconnect");
}
void leapOnExit() {
// println("Leap Motion Exit");
}
//====== OSC SEND ======
void sendOsc() {
OscMessage msg = new OscMessage("/wek/inputs");
if (numFound > 0) {
for (int i = 0; i < features.length; i++) {
msg.add(features[i]);
}
} else {
for (int i = 0; i < features.length; i++) {
msg.add(0.);
}
}
//OscMessage msg2 = new OscMessage("/wek/inputs");
msg.add(index_velocity.mag());
oscP5.send(msg, dest);
//oscP5.send(msg2, dest);
// println(features);
}
void sendInputNames() {
OscMessage msg = new OscMessage("/wekinator/control/setInputNames");
//String[] fingerNames = {"thumb", "index", "middle", "ring", "pinky"};
String[] fingerNames = {"index"};
String coordinates[] = {"_x", "_y", "_z"};
int n = 0;
for (int i = 0; i < fingerNames.length; i++) {
for (int j = 0; j< coordinates.length; j++) {
msg.add(fingerNames[i] + coordinates[j]);
n++;
}
}
oscP5.send(msg, dest);
println("Sent finger names" + n);
//println("Sent finger names" + fingerNames);
}
/*
OscMessage msg = new OscMessage("/wek/inputs");
msg.add((float)x);
msg.add((float)y);
msg.add((float)w);
oscP5.send(msg, dest);*/