forked from dphiffer/atlasgloves
-
Notifications
You must be signed in to change notification settings - Fork 1
/
atlas_gloves_01.pde
executable file
·457 lines (396 loc) · 11.3 KB
/
atlas_gloves_01.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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
/**
* Atlas Gloves (http://atlasgloves.org/)
* Release 01 / May 2006
* A DIY Hand Gesture Interface for Google Earth
* By Dan Phiffer ([email protected]) and Mushon Zer-Aviv ([email protected])
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the FreeSoftware
* Foundation; either version 2 of the License, or (at your option) any later
* version.
*
* This program is distributed in the hope that it will be useful, but
WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along
with
* this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
// The robot library gives us virtual hands (click / drag simulation)
import java.awt.Robot;
// The video library gives us virtual eyes (motion tracking)
import processing.video.*;
// The three main actors
Robot robot;
Capture video;
Target target;
// An instructional demo animation
Movie demo;
// Assigning proportions
int videoW = 102;
int videoH = 77;
int screenW = 1024;
int screenH = 768;
int scl = 4; //scaling the screen and the input
int frame_rate = 24;
// Brightness threshold for blob inclusion
// (Press + / - keys to adjust these at runtime)
float threshold = 200.0;
// These boolean variables control which mouse events will be simulated
// (Press m / c to control each of these, respectively)
boolean simulateMove = false;
boolean simulateClick = false;
// Mode thresholds, based on blob width and height
int panW = 4;
int panH = 4;
int zoomH = 8;
int tiltW = 12;
int menuH = 30;
// Zoom thresholds
int zoomInMax = 10;
int zoomOutMin = 30;
boolean mouse1 = false; // Left mouse button status
boolean mouse2 = false; // Middle mouse button status
int angle; // The angle of the line connecting the lights
int oldAngle = 0; // Used to calculate the change in angle
int oldHeight = 0; // Used to calculate the change in blob height
int panBuffer = 0;
// These variables determine which corners of the blob comprise the line
boolean firstIncluded = true;
int firstCol = 0;
int firstRow = 0;
// Keeps track of interaction modes
int mode = -1; /*
* -1 = waiting
* 0 = zoom
* 1 = tilt
* 2 = pan
* 3 = rotate
*/
// Mode images:
PImage demo_mode;
PImage practice_mode;
PImage control_mode;
int playMode = 1; // Start out in demo mode
int resetCount = frame_rate * 15; // Reset after 15 sec
int resetTimer = -1;
int demoAlpha = 200;
void setup() {
// The robot needs a try/catch block to be initiated
try {
robot = new Robot();
//press_alt_tab();
} catch (Exception e) {
println("Oops, something went wrong.");
}
// Basic processing setup
framerate(frame_rate);
size(scl * videoW, scl * videoH);
background(0);
stroke(51, 255, 102);
strokeWeight(3);
// Initiate the video, resolution and frame rate
video = new Capture(this, videoW, videoH, frame_rate);
// Make our target object
target = new Target();
// Load and play the demo movie file
demo = new Movie(this, "demo.mov");
demo.play();
// Load the images
demo_mode = loadImage ("demo_mode.gif");
practice_mode = loadImage ("practice_mode.gif");
control_mode = loadImage ("control_mode.gif");
}
void draw() {
if (playMode == 0) {
if (demo.available()) {
demo.read();
}
capture();
smooth();
tint(255, 255, 255, demoAlpha);
image(demo, -60, -15);
if (demo.duration() == demo.time()) {
demoAlpha -= 10;
if (demoAlpha < 50) {
playMode = 1;
}
}
} else {
noTint();
capture();
smooth();
}
switch(playMode) {
case 0:
image(demo_mode, 5, 5); break;
case 1:
image(practice_mode, 5, 5); break;
case 2:
image(control_mode, 5, 5); break;
}
}
void capture() {
firstIncluded = true;
// Iterate over each pixel in the video frame to find a light blob
for (int row = 0; row < video.height; row++) { // For each row
for(int col = 0; col < video.width; col++) { // For each column
color pixel = video.pixels[row * video.width + col];
// If the pixel is bright enough, include it in the blob
if (target.isSimilar(pixel)){
target.include(col, row);
// The first time we include a light determines the angle
// of the line we draw within the blob
if (firstIncluded) {
firstIncluded = false;
firstCol = col;
firstRow = row;
}
}
}
}
// Flip the video image to make it mirror-like
pushMatrix();
scale(-1, 1);
image(video, -width, 0, videoW * scl, videoH * scl);
target.drawIt();
popMatrix(); //pops out to work with different proportions
}
// Video tracking algorithm based on code from Dan O'Sullivan
// http://itp.nyu.edu/~dbo3/cgi-bin/ClassWiki.cgi?ICMVideo
class Target {
Rectangle blob;
void include(int _x, int _y) {
if (blob == null) {
blob = new Rectangle(_x,_y,1,1);
}
blob.add(_x,_y);
}
boolean isSimilar(color thisPixel) {
if (brightness(thisPixel) > threshold){
return true;
}
return false;
}
void drawIt() {
if (blob != null) {
resetTimer = -1;
// Draw a line to the video capture
this.drawLine();
// Switch to practice mode
if (playMode == 0 && blob.width > panW &&
blob.y + blob.height > videoH * 0.9) {
playMode = 1;
demoAlpha = 0;
}
// Switch to control mode from demo or practice
// (hands held high)
if (playMode < 2 && blob.width > panW &&
blob.y + blob.height < videoH * 0.1) {
playMode = 2;
press_alt_tab();
simulateClick = true;
simulateMove = true;
}
// Switch from control mode to demo mode
// (hands held low)
if (playMode == 2 && blob.width > panW &&
blob.y + blob.height > videoH * 0.9) {
resetTimer = resetCount + 1;
}
if (simulateMove &&
mouse1 &&
mode == 2 &&
blob.width <= panW &&
blob.height <= panH) {
// If we're already panning, move the mouse around
this.move();
}
if (simulateClick) {
switch(mode) {
case -1:
this.chooseMode(); break;
case 0:
this.zoom(); break;
case 1:
this.tilt(); break;
case 2:
this.pan(); break;
case 3:
this.rotate(); break;
}
}
} else if (panBuffer < 3) {
panBuffer++;
} else {
panBuffer = 0;
mode = -1;
if (mouse1) {
robot.mouseRelease(InputEvent.BUTTON1_MASK);
mouse1 = false;
}
if (mouse2) {
robot.mouseRelease(InputEvent.BUTTON2_MASK);
mouse2 = false;
}
}
blob = null; //collapse the box again
if (resetTimer == -1) {
resetTimer = 0;
} else if (resetTimer > resetCount) {
press_alt_tab();
simulateClick = false;
simulateMove = false;
playMode = 0;
resetTimer = -1;
demo.jump(0.0);
demo.play();
demoAlpha = 200;
}
if (playMode == 2) {
resetTimer++;
}
}
void drawLine() {
int x1 = scl * blob.x;
int y1 = scl * blob.y;
int x2 = scl * (blob.x + blob.width);
int y2 = scl * (blob.y + blob.height);
if (abs(x1 - scl * firstCol) < 5 && abs(y1 - scl * firstRow) < 5) {
line(x1 - width, y1, x2 - width, y2);
angle = (int) degrees(atan2 (y2-y1,(x2-width)-(x1-width)));
} else {
line(x1 - width, y2, x2 - width, y1);
angle = (int) degrees(atan2 (y1-y2, (x2-width)-(x1-width)));
}
}
void chooseMode() {
if (!mouse2 && blob.width >= panW && blob.height < zoomH) {
mode = 0;
println("Entering zoom mode.");
this.zoom();
} else if (blob.height >= panH && blob.width < tiltW) {
mode = 1;
println("Entering tilt mode.");
this.tilt();
} else if (blob.width < panW && blob.height < panH) {
mode = 2;
println("Entering pan mode.");
this.pan();
} else {
mode = 3;
println("Entering rotate mode.");
this.rotate();
}
}
void move() {
int x = screenW - (2 * blob.x + blob.width) / 2 * screenW / videoW;
int y = (2 * blob.y + blob.height) / 2 * screenH / videoH;
if (y < menuH) {
y = menuH;
}
robot.mouseMove(x, y);
}
void pan() {
if (blob.width > tiltW || blob.height > zoomH) {
if (mouse1) {
robot.mouseRelease(InputEvent.BUTTON1_MASK);
}
this.chooseMode();
} else {
if (mouse2) {
robot.mouseRelease(InputEvent.BUTTON2_MASK);
mouse2 = false;
} else if (mouse1) {
return;
} else if (panBuffer < 3) {
panBuffer++;
} else {
panBuffer = 0;
this.move();
robot.mousePress(InputEvent.BUTTON1_MASK);
mouse1 = true;
}
}
}
void zoom() {
panBuffer = 0;
if (blob.width < panW && blob.height < panH) {
return;
}
if (blob.width < zoomInMax ) {
robot.mouseWheel(1);
} else if (blob.width > zoomOutMin) {
robot.mouseWheel(-1);
}
}
void rotate() {
panBuffer = 0;
if (blob.width < panW && blob.height < panH) {
return;
}
if (!mouse2) {
robot.mouseMove(screenW / 2, screenH / 2);
robot.mousePress(InputEvent.BUTTON2_MASK);
oldAngle = angle;
mouse2 = true;
} else {
int angleDiff = (oldAngle - angle) * 10;
robot.mouseMove(screenW / 2 + angleDiff, screenH / 2);
}
}
void tilt() {
panBuffer = 0;
if (blob.width < panW && blob.height < panH) {
return;
}
if (!mouse2) {
robot.mouseMove(screenW / 2, screenH / 2);
robot.mousePress(InputEvent.BUTTON2_MASK);
oldHeight = blob.height;
mouse2 = true;
} else {
int heightDiff = (oldHeight - blob.height);
robot.mouseMove(screenW / 2, screenH / 2 + heightDiff * 10);
}
}
}
// Handle incoming video data
void captureEvent(Capture camera) {
camera.read();
}
void keyPressed() {
// Down arrow
if (keyCode == 38){
threshold++;
println("Threshold: " + threshold);
} else if (keyCode == 40) {
threshold--;
println("Threshold: " + threshold);
} else if (keyCode == 67) {
simulateClick = !simulateClick;
if (simulateClick) {
println("Click: on");
} else {
println("Click: off");
}
} else if (keyCode == 77) {
simulateMove = !simulateMove;
if (simulateMove) {
println("Move: on");
} else {
println("Move: off");
}
}
}
void press_alt_tab() {
robot.keyPress(18);
robot.keyPress(9);
robot.keyRelease(9);
robot.keyRelease(18);
}