-
Notifications
You must be signed in to change notification settings - Fork 3
/
StyleBlasterGrabber_Mexico.pde
308 lines (264 loc) · 7.19 KB
/
StyleBlasterGrabber_Mexico.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
import processing.opengl.*;
import processing.video.*;
import org.seltar.Bytes2Web.*;
import java.awt.Rectangle;
//import gifAnimation.*;
OpticalFlow of;
Capture cam;
Capture sensor;
Timer cameraTimer, sensorTimer;
int numPixels;
boolean blast; //turns photo-taking on or off
boolean ignoreSensor = false;
boolean debug = false;
boolean uploading = false;
boolean checkRight = false;
boolean grab = false;
boolean disable = false;
boolean recordGif = false;
boolean doGifs = false;
ImageToWeb img;
byte[] imgBytes;
PImage grabImage;
//GifMaker gifExport;
MotionSensor motionSensor;
//SETUP VARS
String version = "1.5";
int startHour = 7; //am
int endHour = 16; //3:59pm
int endMinute = 25;
boolean production = true;
String nycUploadURL = "http://styleblaster.herokuapp.com/upload/nyc";
String gdlUploadURL = "http://styleblaster.herokuapp.com/upload/gdl";
String devUploadURL = "http://styleblaster.herokuapp.com/upload/dev";
// select the production endpoint for the compiled build
String uploadURL = gdlUploadURL;
String tag = "nyc";
int camWidth;
int camHeight = 720;
int sensorThreshold = 13;
int flowDirection = 1; //-1 = right to left, 1 = left to right
int flowThreshold = 220;
float sensorRes = 1;
public void setup() {
int camWidth = 1280;//(16*camHeight)/9; //get correct aspect ratio for width
//camHeight = 2;
int sketchHeight = 1000;
int sketchWidth = 666;
float m = .7;
size(round(sketchWidth*m), round(sketchHeight*m));
// size(1280, 720);
String[] devices = Capture.list();
// uncomment the line below to print a list of devices ready for img capture
println(devices);
fill(255, 50, 50);
noFill();
String[] cameras = Capture.list();
if (version == "2.0") {
cam = new Capture(this, 1280, 960, "Logitech Camera");
}
else {
// cam = new Capture(this, 2592,1944);
//Logitech 910c
//cam = new Capture(this, 1280, 960);
//Microsoft Studio
cam = new Capture(this, 1920, 1080);
// cam = new Capture(this, 1280, 720);
}
if (version == "2.0") {
// cam.start();
}
//set global framerate
int f = 25;
frameRate(f);
cam.frameRate(f);
cameraTimer = new Timer(1000);
sensorTimer = new Timer(1000);
//initialize the hit areas
motionSensor = new MotionSensor();
//initialize sensor position
motionSensor._r.width = 20;
motionSensor._r.height = 20;
motionSensor._r.x = width/2 - motionSensor._r.width/2;
motionSensor._r.y = height * 2/3;
motionSensor.update();
of = new OpticalFlow(cam);
}
void draw() {
background(0);
blast = false;
if (hour()>=startHour) {
if (hour()<endHour) {
if (cam.available()) {
if (disable == false) {
blast = true;
}
}
}
}
if (mousePressed) {
rectMode(CORNER);
motionSensor._bDiff = 0;
ignoreSensor = true;
int sensorWidth = round((mouseX - motionSensor._r.x));
int sensorHeight = mouseY - motionSensor._r.y;
motionSensor._r.width = sensorWidth;
motionSensor._r.height = sensorHeight;
motionSensor.update();
blast = false;
}
if (! uploading) {
cam.read();
grabImage = cam.get(cam.width/2-width/2, cam.height/2-height/2, width, height);
image(grabImage, 0, 0);
of.updateImage(grabImage);
of.draw();
if (ignoreSensor) {
ignoreSensor = false;
}
else {
if (grab) {
println("!!!HIT!!! @ : ");
fill(255, 0, 0);
onHit();
}
else {
noFill();
}
}
}
stroke(255, 100, 100);
//***DRAW DEBUG SHIT TO SCREEN***
if (debug) {
rectMode(CORNER);
noFill();
//date
text(getTimestamp(), 5, 15);
motionSensor.draw();
fill(255);
text("threshold: "+sensorThreshold, 5, height-5);
text("xFlowSum: "+of.xFlowSum, width - 150, height - 5); // time (msec) for this frame
}
if (blast) {
//BLAST OFF!
boolean hit = false;
grab = false;
//update the reference image on the sensors
motionSensor._image = grabImage;
//GIF EXPORT*********START
if (doGifs){
if (of.xFlowSum < flowThreshold) {
if (!recordGif) {
// gifExport = new GifMaker(this, getTimestamp()+".gif");
// gifExport.setRepeat(0); // make it an "endless" animation
}
//start recording gif
//gifExport.setDelay(40);
//gifExport.addFrame();
recordGif = true;
}
else if (recordGif) {
//stop recording gif
//gifExport.finish();
recordGif = false;
// gifExport = new GifMaker(this, "export.gif");
}}
//GIF EXPORT**********END
hit = motionSensor.checkHitArea();
if (hit) {
motionSensor.reset();
boolean dir = false;
if(flowDirection == -1){
//right to left
dir = of.xFlowSum < flowThreshold*flowDirection;
}
else{
//left to right
dir = of.xFlowSum > flowThreshold;
}
if (dir) {
grab = true;
}
}
}
}
void mousePressed() {
motionSensor._r.x = mouseX;
motionSensor._r.y = mouseY;
ignoreSensor = true;
}
void onHit() {
//IS THE CAMERA TIMER NEEDED HERE?
if (cameraTimer.isFinished()) {
takePicture();
cameraTimer.start();
}
}
String getTimestamp() {
String filename = "";
filename += String.valueOf(year());
filename += "-";
filename += String.valueOf(month());
filename += "-";
filename += String.valueOf(day());
filename += "-";
filename += String.valueOf(hour());
filename += "-";
filename += String.valueOf(minute());
filename += "-";
filename += String.valueOf(second());
filename += "-";
filename += production ? tag : "dev";
return filename;
}
void takePicture() {
/* PGraphics pg = createGraphics(grabImage.width, grabImage.height, P2D); // I create a PGraphics from it
pg.loadPixels();
grabImage.loadPixels();
for (int i = 0; i < grabImage.pixels.length; i++)
{
pg.pixels = grabImage.pixels;
}*/
// "this" references the processing PApplet itself and is mandatory here
img = new ImageToWeb(this);
img.setType(ImageToWeb.PNG);
// load the raw bytes from the thing
imgBytes = img.getBytes();
// upload the picture
uploadPicture();
}
void uploadPicture() {
// img.post(String project, String url, String filename, boolean popup, byte[] bytes)
if (production) {
img.post("test", uploadURL, getTimestamp() + ".png", false, imgBytes);
} else {
img.post("test", devUploadURL, getTimestamp() + ".png", false, imgBytes);
}
cameraTimer.start();
}
void keyPressed() {
if (key == ' ') {
debug = !debug;
}
else if (key == 'c') {
//open camera settings
cam.settings();
ignoreSensor = true;
}
else if (key == '.') {
//increase the threshold
sensorThreshold += 1;
motionSensor._thresh = sensorThreshold;
}
else if (key == ',') {
//increase the threshold
sensorThreshold -= 1;
motionSensor._thresh = sensorThreshold;
}
else if (key=='w') of.flagseg=!of.flagseg; // segmentation on/off
else if (key=='s') of.flagsound=!of.flagsound; // sound on/off
else if (key=='m') of.flagmirror=!of.flagmirror; // mirror on/off
else if (key=='f') of.flagflow=!of.flagflow; // show opticalflow on/off
else if (key=='v') production=!production; // send to production endpoint
else if (key=='d') disable=!disable; // disable/enable
}