Steven Cross | Web Cam Face Detect code

Here is my code for face detect. It just changes the filter to color if a face is present. If no face is present the filter is gray.

 

//INSTALL openCV library from Sketch > Import Library > Add Library > Type ‘opencv’ in the search bar

//BELOW WE IMPORT LIBRARIES
import gab.opencv.*;
import processing.video.*;
import java.awt.*;

//WE CREATE TWO OBJECTS video OF TYPE Capture AND myopencv OF TYPE OPENCV
Capture video;
OpenCV myopencv;

void setup() {
//SETTING UP REGULAR STUFF
size(640, 480);
//TELLING video AND myopencv ITS SIZE
video = new Capture(this, 640/2, 480/2);
myopencv = new OpenCV(this, 640/2, 480/2);
//TELLING myopencv TO LOAD FRONT CAMERA (WEBCAM) SO IT CAN START DETECTING
myopencv.loadCascade(OpenCV.CASCADE_FRONTALFACE);
//START THE CAMERA
video.start();
}

void draw() {
//MAKING THE CAMERA IMAGE BIGGER
scale(2);
//LOADING IMAGES FROM THE VIDEO CAPTURE
myopencv.loadImage(video);
//DISPLAYING THE VIDEO ON THE SCREEN
image(video, 0, 0 );
//COLOR AND STROKE SETTINGS FOR THE GREEN RECTANGLE
noFill();
stroke(0, 255, 0);
strokeWeight(3);
//STORING THE FACES DETECTED SO THAT WE CAN USE THIS INFORMATION LATER
Rectangle[] faces = myopencv.detect();
//PRINTING IN THE CONSOLE NUMBER OF FACES DETECTED
println(“Number of Faces Detected:” + faces.length);

//DRAWING RECTANGLES ON THE FACES SO THAT WE CAN SEE A VISUAL INDICATION THAT IT DETECTED
//for (int i = 0; i < faces.length; i++) {
// rect(faces[i].x, faces[i].y, faces[i].width, faces[i].height);
// println(faces);

//Gray filter applied when no face detected, color when faceis detected
if (faces.length > 0){
filter(0);
}else{
filter(GRAY);
}
}
//KEEPS CHECKING IF THE CAMERA IS ON, IF IT IS, IT KEEPS READING VIDEO DATA FROM IT
void captureEvent(Capture c) {
c.read();

}

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