Skip to content

Instantly share code, notes, and snippets.

@yz3440
Created September 30, 2018 22:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yz3440/701272237b79cb275b772725aac45675 to your computer and use it in GitHub Desktop.
Save yz3440/701272237b79cb275b772725aac45675 to your computer and use it in GitHub Desktop.
Sensors, Body, Motion Midterm Progress #0
import controlP5.*;
import java.util.*;
ControlP5 cp5;
boolean guiToggle;
int resolution = 400; //intermediate value for convinience and future conversions
float cropFactor = 1; //zoom in the face that OpenCV returns
void setupGui() {
guiToggle = true;
int sliderW = 100;
int sliderH = 15;
int startX = 10;
int startY = 35;
int spacing = 20;
cp5 = new ControlP5( this );
cp5.addSlider("cropFactor")
.setPosition(startX, startY+spacing*1)
.setSize(sliderW, sliderH)
.setRange(0.1, 1)
.setValue(1)
;
cp5.addSlider("resolution")
.setPosition(startX, startY+spacing*2)
.setSize(sliderW, sliderH)
.setRange(100, 600)
.setValue(400)
;
cp5.setAutoDraw(false);
}
void drawGui() {
cp5.draw();
}
import processing.video.*;
import gab.opencv.*;
import java.awt.Rectangle;
OpenCV opencv;
Capture cam;
Rectangle[] faces;
PImage [] faceImages;
PImage biggestFace;
PImage sampleHand;
int handCount = 3;
PImage smallerImg;
int scale = 8;
void setup() {
size(1280, 960);
cam = new Capture(this, 1280, 960);
cam.start();
opencv = new OpenCV(this, cam.width/scale, cam.height/scale);
opencv.loadCascade(OpenCV.CASCADE_FRONTALFACE);
biggestFace = createImage(resolution, resolution, RGB);
smallerImg = createImage(opencv.width, opencv.height, RGB);
sampleHand = loadImage("Hand.jpg");
setupGui();
}
void draw() {
background(0);
image(cam, 0, 0);
updateFaceDetection();
//image(biggestFace, 0, 0);
updateHandDetection();
if (guiToggle) drawGui();
}
void captureEvent(Capture cam) {
cam.read();
smallerImg.copy(cam,
0, 0, cam.width, cam.height,
0, 0, smallerImg.width, smallerImg.height);
smallerImg.updatePixels();
}
void updateFaceDetection() {
opencv.loadImage(smallerImg);
faces = opencv.detect();
if (faces != null && faces.length > 0) {
Rectangle biggestRect = faces[0];
for (int i = 1; i < faces.length; i++) {
if (faces[i].width > biggestRect.width)
biggestRect = faces[i];
}
biggestFace = createImage(resolution, resolution, RGB);
biggestFace.copy(cam, biggestRect.x * scale, biggestRect.y * scale,
biggestRect.width * scale, biggestRect.height * scale,
0, 0, resolution, resolution);
biggestFace = centerCrop(biggestFace, cropFactor);
}
//for (int i = 0; i < faces.length; i++) {
// strokeWeight(2);
// stroke(255, 0, 0);
// noFill();
// //for all detected faces, draw a red rect
// rect(faces[i].x*scale, faces[i].y*scale,
// faces[i].width*scale, faces[i].height*scale);
//}
}
void updateHandDetection() {
for (int i = 0; i < handCount; i++) {
PImage maskImage = sampleHand;
int posX = mouseX;
int posY = mouseY;
projectFaceOnHand(posX+i*200, posY+i*200, biggestFace, maskImage);
}
}
void projectFaceOnHand(int posX, int posY, PImage face, PImage hand) {
PImage temp = createImage(hand.width, hand.height, RGB);
temp.copy(face, 0, 0, face.width, face.height,
0, 0, temp.width, temp.height);
temp.mask(hand);
image(temp, posX-temp.width/2, posY-temp.height/2);
}
PImage centerCrop(PImage img, float percentage) {
if (percentage < 1) {
int w = int(img.width * percentage);
int h = int(img.height * percentage);
img.copy(img, int((img.width - w)*.5), int((img.height - h)*.5),
w, h,
0, 0, img.width, img.height);
}
return img;
}
void keyPressed() {
if (key == ' ') guiToggle = !guiToggle;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment