Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- tags:
- Processing face detection opencv sketch pde example windows
- Date: september 2012
- This sketch works for me in Windows, using the latest processing 1.5.1 and the latest version of OpenCV (2.3 I believe)
- This is how I got openCV installed:
- http://codeanticode.wordpress.com/2011/11/21/opencv-2-in-processing/
- I've installed processsing and openCV directly in C:\ (root of the disk)
- My sketches are in a 'sketches' folder directly in me processing folder. (you can change location in Processing's settings.)
- My libraries are in a libraries folder inside the sketches folder (this is how processing always wants it)
- The only library in there is the GSVideo library, which makes my laptop's webcam accessible to Processing (and thus openCV).
- If you look through the code, you'll see that I put a dogface in front of people's face. Make sure you have a dog.png or some other picture you want in the same folder as the processing sketch.
- (this sketch used to be some code that would make your face invisible, which I've hacked to show a picture instead)
- */
- import codeanticode.gsvideo.*;
- import monclubelec.javacvPro.*;
- import java.awt.*;
- OpenCV opencv; //Main OpenCV variable
- GSCapture cam; // Connects to a camera
- Rectangle[] faces;
- boolean detectBackground = true;
- PImage bg, fg;
- void setup() {
- size( 320, 240 ); //Larger screen sizes make it difficult to do this in real time
- //PImage currentImg = loadImage("dog.png");
- cam = new GSCapture(this, width,height);
- cam.start();
- bg = createImage(width,height,RGB); // Create an image to hold the background
- fg = createImage(width,height,RGB); // Image for the foreground
- //Create the openCV buffer that's used to detect faces
- opencv = new OpenCV(this);
- opencv.allocate( width, height ); //Create a buffer for OpenCV where we can detect faces
- // Make this point to where you've put this file on your harddrive:
- opencv.cascade("C:\\processing-1.5.1\\sketches\\Haarfeatures_video\\","haarcascade_frontalface_alt.xml");
- }
- //Computes an enlarged version of the face box
- Rectangle enlargeFaceBox (float incPct, int x, int y, int w,int h) {
- float r = dist(0,0,w,h) / 2; //Computes radius of the center diagonal
- float theta = atan2(h,w); //Computes the angle of the diagonal
- float dx = r*incPct*cos(theta); //Finds
- float dy = r*incPct*sin(theta);
- return new Rectangle( (int) (x - dx), (int) (y - dy), (int) (w + 2*dx), (int) (h + 2*dy));
- }
- void draw() {
- cam.read(); //Pull in the image from the webcam
- if (detectBackground) {
- image( cam, 0, 0 );
- text("Step out of scene and press any key", 10,20);
- } else {
- fg.copy(cam,0,0,width,height,0,0,width,height); //Copy the camera image into fg
- opencv.copy(cam); //Copy the image into openCV's buffer
- //Detect faces and replace them with background image
- Rectangle[] faces = opencv.detect(); // detect anything ressembling a FRONTALFACe
- bg.loadPixels();
- Rectangle faceBox = new Rectangle(); //Create a rectangle that will hold the enlarged box for the face
- for( int i=0; i<faces.length; i++ ) {
- faceBox = enlargeFaceBox(0.1, faces[i].x, faces[i].y, faces[i].width, faces[i].height);
- //Test boundaries to make sure box is still inside the visible screen area
- if (faceBox.x < 0) {faceBox.x = 0; }
- if (faceBox.x + faceBox.width > width) { faceBox.width = width - faceBox.x; }
- if (faceBox.y < 0) { faceBox.y = 0; }
- if (faceBox.y + faceBox.height > height) { faceBox.height = height - faceBox.y; }
- //Now replace the foreground image of the face with the same positions in the background
- fg.loadPixels();
- PImage currentImg = loadImage("dog.png");
- currentImg.resize(faceBox.width, faceBox.height);
- fg.copy(currentImg, 0, 0, currentImg.height, currentImg.width, faceBox.x, faceBox.y, faceBox.width, faceBox.height );
- // image(currentImg, startx, starty);
- // currentImg.resize(memeWidth, memeHeight);
- //image(currentImg, startx, starty);
- fg.updatePixels();
- }
- fg.updatePixels();
- image(fg,0,0);
- //image(opencv.image(), 0, 0);
- }
- }
- //Flip the mode
- void keyPressed() {
- detectBackground = !detectBackground;
- //Copy the image into a background
- bg.copy(cam,0,0,width,height,0,0,width,height);
- bg.updatePixels();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement