Intermediate: Me Gusta Face Detection


Do you have processing installed?

Do you know about Motherfucking Classes?

If the answer is yes, then you are ready to learn Computer Vision in Processing.

 

You better be.

Click here now: http://ubaa.net/shared/processing/opencv/

Install the Library with the instructions on the landing page.

 

 

Install the library before continuing. Problem?

Place your  meGusta.png inside your sketch folder.

 

Copy, paste, read and run:

 

//Import the libraries.
//This one is for OpenCV
import hypermedia.video.*;
//This one is for the array of faces detected returned as rectangles.
import java.awt.Rectangle;

//Create a computer vision Object
OpenCV opencv;

//Create a Processing Image object.
PImage meGusta;

void setup() {

//Set a small size unless you have a powerful processor.
size(320,240);

//Use the smooth() function
//Trust me, the edges will look much nicer.
smooth();

//Let’s initialize the openCV object.
opencv = new OpenCV( this );
//Because we already declared the size we use the width and height var
//to set the size of the capture.
opencv.capture( width, height );
//This is what it will look for.
//Check all that can OpenCV can do here:
//http://ubaa.net/shared/processing/opencv/opencv.html
opencv.cascade( OpenCV.CASCADE_FRONTALFACE_ALT );

//Load your megusta png picture.
//Remember it needs to be inside your folder. Check caps. Problem?
meGusta = loadImage(“MeGusta.png”);
}

void draw() {

// grab a new frame
opencv.read();
// and convert to gray
opencv.convert( GRAY );

// proceed detection
Rectangle[] faces = opencv.detect( 1.2, 2, OpenCV.HAAR_DO_CANNY_PRUNING, 40, 40 );

// display the image
image( opencv.image(), 0, 0 );

//if there’s a detected face the length will be greater that 0.
if(faces.length >= 1)
{
//scale the meGusta.png to desired size.
scale(.6);
//place image using the first face in the [0] index.
image(meGusta,faces[0].x+60,faces[0].y-10);
}

}

 

If you want to start playing with the reference:

http://www.processing.org/reference/

 


Flattr this

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s