Created
May 16, 2017 09:02
-
-
Save mapaulac/f1614084b2e94fd4b21c84bc222bd5a6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import ddf.minim.*; | |
import ddf.minim.analysis.*; | |
import ddf.minim.effects.*; | |
import ddf.minim.signals.*; | |
import ddf.minim.spi.*; | |
import ddf.minim.ugens.*; | |
import processing.sound.*; | |
import processing.video.*; | |
import blobDetection.*; | |
import com.thomasdiewald.ps3eye.PS3EyeP5; | |
Minim minim; | |
AudioPlayer rainMusic; | |
AudioPlayer snore; | |
BlobDetection theBlobDetection; | |
PS3EyeP5 ps3eye; | |
PImage img; | |
PImage frame01; | |
PImage frame1; | |
PImage frame2; | |
PImage frame3; | |
PImage frame4; | |
PImage frame5; | |
PImage frame6; | |
PImage frame7; | |
PImage frame8; | |
PImage hand; | |
boolean newFrame=false; | |
//setting up variables | |
float placeHolderX; | |
float placeHolderY; | |
float object1X; | |
float object1Y; | |
float object2X; | |
float object2Y; | |
float minimumX; | |
float maximumX; | |
boolean umbrella1 = false; | |
boolean umbrella2 = false; | |
float x; | |
boolean umbrellaON = false; | |
boolean glovesON = true; | |
float blobDistance = 10; | |
float umbrellaDistance = 50; | |
boolean firstImage = true; | |
float totoroXleft; | |
float totoroXright; | |
float totoroYleft; | |
float totoroYright; | |
boolean showEye; | |
PImage destination; | |
Drop[] drops = new Drop[500]; | |
void setup() | |
{ | |
//size(1280, 720, P3D); | |
fullScreen(P3D); | |
ps3eye = PS3EyeP5.getDevice(this); | |
if(ps3eye == null){ | |
System.out.println("No PS3Eye connected. Good Bye!"); | |
exit(); | |
return; | |
} | |
ps3eye.start(); | |
// BlobDetection | |
img = new PImage(80,60); | |
theBlobDetection = new BlobDetection(img.width, img.height); | |
theBlobDetection.setPosDiscrimination(true); | |
theBlobDetection.setThreshold(0.05f); // will detect bright areas whose luminosity > 0.2f; | |
theBlobDetection.setBlobMaxNumber(2); | |
theBlobDetection.blobWidthMin=2; //MAYBE CHANGE LATER | |
theBlobDetection.blobHeightMin=2; | |
destination = createImage(80,60,RGB); | |
//CREATING OBJECTS | |
for (int i = 0; i < drops.length; i++) { | |
drops[i] = new Drop(); | |
} | |
//loading animation frames | |
frame01 = loadImage("backgroundfinal.png"); | |
frame1 = loadImage("1totoro.png"); | |
frame2 = loadImage("2totoro.png"); | |
frame3 = loadImage("3totoro.png"); | |
frame4 = loadImage("4totoro.png"); | |
frame5 = loadImage("5totoro.png"); | |
frame6 = loadImage("6totoro.png"); | |
frame7 = loadImage("7totoro.png"); | |
frame8 = loadImage("8totoro.png"); | |
hand = loadImage("handwhite2.png"); | |
//LOADING SOUND | |
minim = new Minim(this); | |
rainMusic = minim.loadFile("rain2.wav"); | |
snore = minim.loadFile("snore3.wav",512); | |
rainMusic.loop(); | |
} | |
//CREATING RAIN - credits: Daniel Shiffman | |
class Drop { | |
float positionX; | |
float x = random(width); | |
float y = random(-500,-50); | |
float alpha = 100; | |
float z = random(0,20); | |
float len = map(z, 0, 20, 10, 30); | |
float yspeed = map(z, 0, 20, 1, 8); | |
void fall() { | |
y = y + yspeed; | |
float grav = map(z, 0, 20, 0, 0.2); | |
yspeed = yspeed + grav; | |
//SETTING UP RAIN RANGE | |
if (y > height) { | |
y = random(-200,-100); | |
yspeed = map(z, 0, 20, 1, 8); | |
} | |
if (umbrellaON == true){ | |
if (minimumX <= x && x <= maximumX){ | |
alpha = 0; | |
} | |
else{ | |
alpha = 100; | |
} | |
} | |
else if (umbrellaON == false){ | |
//glovesON = true; //quitar despues de tener esto listo | |
alpha = 100; | |
} | |
} | |
void show() { | |
float thick = map(z, 0, 20, 1, 3); | |
strokeWeight(thick); | |
stroke(255,alpha); | |
line(x,y,x,y+len); | |
} | |
} | |
void draw() | |
{ | |
//ADJUSTING BRIGTHNESS OF PIXELS | |
float threshold = 100; | |
PImage cam = ps3eye.getFrame(); | |
//BLOB DETECTION CODE | |
newFrame=false; | |
img.copy(cam, 0, 0, cam.width, cam.height, | |
0, 0, img.width, img.height); | |
fastblur(img, 1); | |
destination.loadPixels(); | |
img.loadPixels(); | |
//SETTING UP THE THRESHOLD | |
for (int xvalue = 0; xvalue < img.width; xvalue++) { | |
for (int yvalue = 0; yvalue < img.height; yvalue++ ) { | |
int loc = xvalue + yvalue*img.width; | |
// Test the brightness against the threshold | |
if (brightness(img.pixels[loc]) > 40) { | |
destination.pixels[loc] = color(255); // White | |
} else { | |
destination.pixels[loc] = color(0); // Black | |
} | |
} | |
} | |
img.updatePixels(); | |
// We changed the pixels in destination | |
destination.updatePixels(); | |
theBlobDetection.computeBlobs(destination.pixels); | |
drawBlobsAndEdges(true,false); | |
//loading the first image before anything | |
if (firstImage == true){ | |
image(frame01,0,0,width,height); | |
showEye = true; | |
} | |
//LOADING INITIAL IMAGE WHEN IN UMBRELLA MODE | |
if (umbrellaON == true){ | |
if (object1X <= 0 && object1X <=168){ | |
image(frame3,0,0,width,height); | |
showEye = true; | |
} | |
else if (object1X <= 169 && object1X <= 369){ | |
image(frame4,0,0,width,height); | |
showEye = true; | |
} | |
else if (object1X <= 370 && object1X <= 658){ | |
image(frame5,0,0,width,height); | |
showEye = false; | |
} | |
else if (object1X <= 659 && object1X <= 859){ | |
image(frame6,0,0,width,height); | |
showEye = false; | |
} | |
else if (object1X <= 860 && object1X <= 1061){ | |
image(frame7,0,0,width,height); | |
showEye = false; | |
if (snore.isPlaying()){ | |
} | |
else{ | |
snore.rewind(); | |
snore.play(); | |
} | |
} | |
else if (object1X <= 1062 && object1X <= 1347){ | |
image(frame2,0,0,width,height); | |
showEye = true; | |
} | |
else if (object1X <= 1348 && object1X <= 1600){ | |
image(frame01,0,0,width,height); | |
showEye = true; | |
} | |
} | |
//SHOWING DROPS ON SCREEN AND MAKING THEM FALL | |
for (int i = 0; i < drops.length; i++) { | |
drops[i].fall(); | |
drops[i].show(); | |
} | |
tint(255,255); | |
//GLOVES MODE (ANIMATING TOTORO FRAMES) | |
if (glovesON == true){ | |
println(object1X); | |
if (object1X >= 749 && object1X <= 825 && object1Y >= 239 && object1Y <= 860 || object1X >= 826 && object1X <= 1292 && object1Y >= 239 && object1Y <= 301 || object1X >= 1293 && object1X <= 1362 && object1Y >= 239 && object1Y <= 860 ){ | |
image(frame2,0,0,width,height); | |
//println("SHOWING FRAME 2"); | |
showEye = true; | |
} | |
else if (object1X >= 825 && object1X <= 879 && object1Y >= 302 && object1Y <= 860 || object1X >= 880 && object1X <= 1246 && object1Y >= 302 && object1Y <= 358 || object1X >= 1247 && object1X <= 1294 && object1Y >= 302 && object1Y <= 860 ){ | |
image(frame3,0,0,width,height); | |
//println("SHOWING FRAME 3"); | |
showEye = true; | |
} | |
else if (object1X >=879 && object1X <= 946 && object1Y >= 359 && object1Y <= 860 || object1X >= 947 && object1X <= 1184 && object1Y >= 359 && object1Y <= 465 || object1X >= 1185 && object1X <= 1247 && object1Y >= 359 && object1Y <= 860 ){ | |
image(frame4,0,0,width,height); | |
//println("SHOWING FRAME 5"); | |
showEye = true; | |
} | |
else if (object1X >= 946 && object1X <= 985 && object1Y >= 466 && object1Y <= 860 || object1X >= 986 && object1X <= 1148 && object1Y >= 466 && object1Y <= 505 || object1X >= 1149 && object1X <= 1184 && object1Y >= 466 && object1Y <= 860 || object1X >= 986 && object1X <= 1148 && object1Y >= 660 && object1Y <= 860 ){ | |
image(frame5,0,0,width,height); | |
//println("SHOWING FRAME 6"); | |
showEye = false; | |
} | |
else if (object1X >= 985 && object1X <= 1026 && object1Y >= 506 && object1Y <= 659 || object1X >= 1027 && object1X <= 1106 && object1Y >= 506 && object1Y <= 548 || object1X >= 1107 && object1X <= 1152 && object1Y >= 506 && object1Y <= 659 || object1X >= 1027 && object1X <= 1107 && object1Y >= 613 && object1Y <= 659 ){ | |
image(frame6,0,0,width,height); | |
//println("SHOWING FRAME 7"); | |
showEye = false; | |
} | |
else if (object1X >= 1026 && object1X <= 1107 && object1Y >= 549 && object1Y <= 612){ | |
image(frame7,0,0,width,height); | |
//println("SHOWING FRAME 7"); | |
showEye = false; | |
if (snore.isPlaying()){ | |
} | |
else{ | |
snore.rewind(); | |
snore.play(); | |
//println("ROAR PLAYTING"); | |
} | |
} | |
else{ | |
image(frame01,0,0,width,height); | |
showEye = true; | |
} | |
} | |
//ADDING IMAGE OF HAND | |
if (glovesON == true){ | |
pushMatrix(); | |
translate(-100,-100); | |
tint(255, 155); | |
image(hand,object1X,object1Y); | |
popMatrix(); | |
} | |
//MOVING TOTORO'S EYES | |
if (showEye == true){ | |
totoroXleft = map(object1X,0,1600,966,987); | |
totoroYleft = map(object1Y,0,900,213,238); | |
totoroXright = map(object1X,0,1600,1156,1175); | |
totoroYright = map(object1Y,0,900,215,238); | |
noStroke(); | |
fill(0); | |
ellipse(totoroXleft,totoroYleft,20,20); | |
fill(0); | |
ellipse(totoroXright,totoroYright,20,20); | |
} | |
//image(destination,0,0,width,height); //SE PRENDE PARA VER THRESHOLD | |
} | |
// ================================================== | |
// drawBlobsAndEdges() | |
// ================================================== | |
void drawBlobsAndEdges(boolean drawBlobs, boolean drawEdges) | |
{ | |
noFill(); | |
// | |
Blob b; | |
EdgeVertex eA,eB; | |
for (int n=0 ; n<theBlobDetection.getBlobNb() ; n++){ | |
b=theBlobDetection.getBlob(n); | |
if (b!=null){ | |
//OBTAINING COORDINATES | |
if ( glovesON == true && theBlobDetection.getBlobNb() == 1){ | |
//println("GLOVE MODE ON"); | |
object1X = b.x*width; | |
object1Y = b.y*height; | |
}//if umbrella mode | |
else if( umbrellaON == true && theBlobDetection.getBlobNb() == 2){ | |
//println("UMBRELLA MODE ON"); | |
if (n==0){ | |
object1X = b.x*width; | |
object1Y = b.y*height; | |
} | |
else if (n==1){ | |
object2X = b.x*width; | |
object2Y = b.y*height; | |
} | |
} | |
//if it finds two blobs: CHANGE LATER SO IT IS NOT HARDCODED | |
if (umbrellaON == true){ | |
//println("UMBRELLA ON TRUE"); | |
//stroke(255); | |
//line(object1X, object1Y,object2X,object2Y); | |
if (object1X < object2X){ | |
minimumX = object1X; | |
maximumX = object2X; | |
} | |
else{ | |
minimumX = object2X; | |
maximumX = object1X; | |
} | |
} | |
//take out all the code that has previous whatever values. Instead, check for the blob number and save those values there, so I don't comment them out later | |
if (drawEdges){ | |
strokeWeight(3); | |
stroke(0,255,0); | |
for (int m=0;m<b.getEdgeNb();m++){ | |
eA = b.getEdgeVertexA(m); | |
eB = b.getEdgeVertexB(m); | |
if (eA !=null && eB !=null){ | |
//line( | |
// eA.x*width, eA.y*height, | |
// eB.x*width, eB.y*height | |
// ); | |
} | |
} | |
} | |
// DRAWING BLOBS | |
if (drawBlobs){ | |
if (glovesON == true){ | |
//fill(255,150); | |
//ellipse(b.x*width,b.y*height,30,30); | |
////image(hand,b.x*width,b.y*height); | |
//strokeWeight(1); | |
//stroke(255,0,0); | |
//rect( | |
//b.xMin*width,b.yMin*height, | |
//b.w*width,b.h*height | |
//); | |
//image(hand,b.xMin*width,b.yMin*height); | |
////pushMatrix(); | |
////translate(width,0); | |
////scale(-1,1); | |
////image(hand,object1X,object1Y); | |
////popMatrix(); | |
} | |
} | |
} | |
} | |
} | |
// ================================================== | |
// Super Fast Blur v1.1 | |
// by Mario Klingemann | |
// <http://incubator.quasimondo.com> | |
// ================================================== | |
void fastblur(PImage img,int radius) | |
{ | |
if (radius<1){ | |
return; | |
} | |
int w=img.width; | |
int h=img.height; | |
int wm=w-1; | |
int hm=h-1; | |
int wh=w*h; | |
int div=radius+radius+1; | |
int r[]=new int[wh]; | |
int g[]=new int[wh]; | |
int b[]=new int[wh]; | |
int rsum,gsum,bsum,x,y,i,p,p1,p2,yp,yi,yw; | |
int vmin[] = new int[max(w,h)]; | |
int vmax[] = new int[max(w,h)]; | |
int[] pix=img.pixels; | |
int dv[]=new int[256*div]; | |
for (i=0;i<256*div;i++){ | |
dv[i]=(i/div); | |
} | |
yw=yi=0; | |
for (y=0;y<h;y++){ | |
rsum=gsum=bsum=0; | |
for(i=-radius;i<=radius;i++){ | |
p=pix[yi+min(wm,max(i,0))]; | |
rsum+=(p & 0xff0000)>>16; | |
gsum+=(p & 0x00ff00)>>8; | |
bsum+= p & 0x0000ff; | |
} | |
for (x=0;x<w;x++){ | |
r[yi]=dv[rsum]; | |
g[yi]=dv[gsum]; | |
b[yi]=dv[bsum]; | |
if(y==0){ | |
vmin[x]=min(x+radius+1,wm); | |
vmax[x]=max(x-radius,0); | |
} | |
p1=pix[yw+vmin[x]]; | |
p2=pix[yw+vmax[x]]; | |
rsum+=((p1 & 0xff0000)-(p2 & 0xff0000))>>16; | |
gsum+=((p1 & 0x00ff00)-(p2 & 0x00ff00))>>8; | |
bsum+= (p1 & 0x0000ff)-(p2 & 0x0000ff); | |
yi++; | |
} | |
yw+=w; | |
} | |
for (x=0;x<w;x++){ | |
rsum=gsum=bsum=0; | |
yp=-radius*w; | |
for(i=-radius;i<=radius;i++){ | |
yi=max(0,yp)+x; | |
rsum+=r[yi]; | |
gsum+=g[yi]; | |
bsum+=b[yi]; | |
yp+=w; | |
} | |
yi=x; | |
for (y=0;y<h;y++){ | |
pix[yi]=0xff000000 | (dv[rsum]<<16) | (dv[gsum]<<8) | dv[bsum]; | |
if(x==0){ | |
vmin[y]=min(y+radius+1,hm)*w; | |
vmax[y]=max(y-radius,0)*w; | |
} | |
p1=x+vmin[y]; | |
p2=x+vmax[y]; | |
rsum+=r[p1]-r[p2]; | |
gsum+=g[p1]-g[p2]; | |
bsum+=b[p1]-b[p2]; | |
yi+=w; | |
} | |
} | |
} | |
void keyPressed(){ | |
if (key == 'u'){ | |
umbrellaON = true; | |
glovesON = false; | |
minimumX=0; | |
maximumX=0; | |
//println("CHANGE TO UMBRELLA ON, RESTART RAIN TRUE"); | |
} | |
if (key == 'g'){ | |
umbrellaON = false; | |
glovesON = true; | |
//println("CHANGE TO GLOVES ON"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment