Created
June 27, 2016 23:52
-
-
Save anonymous/b32df66b97d235dd7c7cb03eb4879de3 to your computer and use it in GitHub Desktop.
face on moon + arduino
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
//libraries needed: opencv, video, java, arduino, serial | |
import gab.opencv.*; | |
import processing.video.*; | |
import java.awt.*; | |
import cc.arduino.*; | |
import processing.serial.*; | |
Arduino arduino; | |
//starfield | |
int starNum = 400; | |
float [] xpos = new float[starNum]; | |
float [] ypos = new float[starNum]; | |
int starInstance = 0; | |
int startingPointX= 350; | |
int startingPointY= 350; | |
int startingPointX1= 0; | |
int startingPointY1= 0; | |
int refreshPoint = 100; | |
PImage moonMask4; //filename for mask used to cover up ouside of moon region | |
int maskWidth = 0; | |
int maskHeight = 0; | |
Movie moonspin; //mp4 file of spinning moon | |
int moonPosX = 0; | |
int moonPosY = 0; | |
int moonDiameter = 0; | |
int moonRadius = 0; | |
Capture video; //input from webcam | |
OpenCV opencv; //opencv instance to deal with webcam | |
OpenCV opencv2; //opencv instance to deal with moon video | |
int vidWidth=0; //webcam parameters | |
int vidHeight=0; | |
void setup() { | |
//starfield stuff | |
{ | |
for (starInstance = 0; starInstance<starNum; starInstance++) { | |
//startingPointX = moonPosX+300; | |
//startingPointY = moonPosX+300; | |
xpos[starInstance] = int(random(-startingPointX, startingPointX)); | |
ypos[starInstance] = int(random(-startingPointY, startingPointY)); | |
} | |
} | |
//arduino stuff | |
arduino = new Arduino(this, Arduino.list()[2], 57600); | |
//arduino pin 7 | |
arduino.pinMode(7, Arduino.INPUT); | |
arduino.pinMode(3, Arduino.OUTPUT); | |
vidWidth = 640; | |
vidHeight = 480; | |
size(640, 480); | |
// mask is just a png that gets placed over sketch... a better way perhaps? | |
moonMask4 = loadImage("moonMask4.png"); | |
maskWidth = 1280; | |
maskHeight = 960; | |
moonspin = new Movie(this, "moonrotate.mp4"); | |
opencv2 = new OpenCV(this, 240, 240); | |
moonspin.loop(); | |
//moonspin.play(); | |
opencv2.startBackgroundSubtraction(5, 3, 0.5); | |
video = new Capture(this, vidWidth, vidHeight); | |
opencv = new OpenCV(this, vidWidth, vidHeight); | |
opencv.startBackgroundSubtraction(5, 3, .5); | |
opencv.loadCascade(OpenCV.CASCADE_FRONTALFACE); | |
video.start(); | |
frameRate(30); | |
} | |
void draw() { | |
//tried to do this section in a for loop but was unsuccessful | |
//each contour set needs its own name so nested for loop was unsuccessful | |
//next three sections are the contours to make "moon craters" from your face | |
noFill(); | |
opencv.loadImage(video); //webcam | |
opencv.threshold(70); | |
opencv.dilate(); | |
opencv.erode(); | |
stroke(20); | |
strokeWeight(1); | |
for (Contour contour3 : opencv.findContours()) { | |
contour3.draw(); | |
} | |
opencv.loadImage(video); | |
opencv.threshold(110); | |
opencv.dilate(); | |
opencv.erode(); | |
stroke(255); | |
strokeWeight(1); | |
for (Contour contour2 : opencv.findContours()) { | |
contour2.draw(); | |
} | |
opencv.loadImage(video); | |
opencv.threshold(95); | |
opencv.dilate(); | |
opencv.erode(); | |
stroke(60); | |
strokeWeight(1); | |
for (Contour contour1 : opencv.findContours()) { | |
contour1.draw(); | |
} | |
//face detection happening here | |
opencv.loadImage(video); | |
Rectangle[] faces = opencv.detect(); | |
println(faces.length); | |
//the ellipse that goes around the face | |
for (int i = 0; i < faces.length; i++) { | |
println(faces[i].x + "," + faces[i].y); | |
ellipseMode(CORNER); | |
moonPosX = faces[i].x; | |
moonPosY = faces[i].y; | |
moonDiameter = faces[i].width; | |
moonRadius = moonDiameter/2; | |
fill(255, 10); | |
noStroke(); | |
ellipse(moonPosX, moonPosY, 240, 240); | |
} | |
//masking shape | |
pushMatrix(); | |
translate(moonPosX+moonRadius, moonPosY+moonRadius); | |
blendMode(BLEND); | |
image(moonMask4, -maskWidth/2, -maskHeight/2); | |
popMatrix(); | |
//spinning moon video | |
opencv2.loadImage(moonspin); | |
opencv2.updateBackground(); | |
opencv2.dilate(); | |
opencv2.erode(); | |
fill(0, 30); | |
blendMode(BLEND); | |
rect(0, 0, width, height); | |
noFill(); | |
strokeWeight(.5); | |
stroke(255); | |
println(moonRadius); | |
pushMatrix(); | |
translate(moonPosX+moonRadius, moonPosY+moonRadius); | |
pushMatrix(); | |
translate(-120, -120); | |
for (Contour contour : opencv2.findContours()) { | |
// blendMode(ADD); | |
contour.draw(); | |
} | |
//ellipse for outline of moon, jittered slightly | |
noFill(); | |
stroke(255, 90); | |
noSmooth(); | |
strokeWeight(1); | |
ellipseMode(CORNER); | |
ellipse(0+random(-1, 1), 0+random(-1, 1), 240, 240); | |
popMatrix(); | |
popMatrix(); | |
//using a button press to create the starfield around moon | |
starField(); | |
} | |
//need voids for webcam and movie | |
void captureEvent(Capture c) { | |
c.read(); | |
} | |
void movieEvent(Movie m) { | |
m.read(); | |
} | |
void starField() { | |
blendMode(ADD); //otherwise moon face will be covered up | |
fill(0,10); | |
noStroke(); | |
rect(0,0,width,height); | |
if(arduino.digitalRead(7) == Arduino.HIGH) { | |
arduino.digitalWrite(3, 100); | |
pushMatrix(); | |
translate(width/2, height/2); | |
colorMode(HSB); | |
tint(random(1,255),250,250); | |
for (starInstance = 0; starInstance < starNum; starInstance ++) { | |
strokeWeight(random(1,4)); | |
stroke(random(1,255),200,200,starInstance); | |
point(xpos[starInstance], ypos[starInstance]); | |
xpos[starInstance]= xpos[starInstance]+(xpos[starInstance])/100.0; | |
ypos[starInstance]= ypos[starInstance]+(ypos[starInstance])/100.0; | |
if ((xpos[starInstance] < -width) || (xpos[starInstance] > width) || | |
(ypos[starInstance] < -height) || ( ypos[starInstance] > height)) { | |
xpos[starInstance] = random(-refreshPoint, refreshPoint); | |
ypos[starInstance] = random(-refreshPoint, refreshPoint); | |
} | |
} | |
popMatrix(); | |
} | |
else{ | |
noTint(); | |
arduino.digitalWrite(3, 0); | |
pushMatrix(); | |
translate(width/2, height/2); | |
for (starInstance = 0; starInstance < starNum; starInstance ++) { | |
strokeWeight(1); | |
stroke(255,starInstance); | |
point(xpos[starInstance], ypos[starInstance]); | |
xpos[starInstance]= xpos[starInstance]+(xpos[starInstance])/100.0; | |
ypos[starInstance]= ypos[starInstance]+(ypos[starInstance])/100.0; | |
if ((xpos[starInstance] < -width) || (xpos[starInstance] > width) || | |
(ypos[starInstance] < -height) || ( ypos[starInstance] > height)) { | |
xpos[starInstance] = random(-refreshPoint, refreshPoint); | |
ypos[starInstance] = random(-refreshPoint, refreshPoint); | |
} | |
} | |
popMatrix(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment