Processing | Face Detection and Recognition using OpenCV

Recently, i have been looking into the OpenCV library. OpenCV offers a range of examples of different approaches to interactive media design through processing. With this in mind, I looked into facial recognition examples, and how they actually worked. The process of trial and error with this particular type of program is a key element to success in my opinion. I wanted to use the code as an understanding; to use the code to create another piece of visual text just by changing numbers here and there.

When I opened the example named “LiveCamTest” from the OpenCV example library, I came across a specific chunk of code that interested me simply because I felt like my mind had been blown in terms of the understanding  the piece. The code below is the what I found:

void draw() {
scale(2);
opencv.loadImage(video);

image(video, 0, 0 );

noFill();

stroke(0,0,0);
strokeWeight(1);
Rectangle[] faces = opencv.detect();
println(faces.length);

This piece of code is the first part of the program installation. This chunk of code represents the output of the camera (in this case the image the camera is trying to detect is your face), what will be drawn by the void function when it detects a face in the space depending on the size of the face detected it will scale it appropriately.

The simplified understanding of this chunk is that once a face has been detected in the space, the program will draw a rectangle or a square around it. This square will be transparent so you will be able to see the image behind (this will be the detected face). The square perimeter will be black, with a stroke weight of one, so the lines won’t be very thick.

The next piece of code is unfortunately something I need to figure out properly. I can hack it by using it in different projects, but I do not fully understand it as of yet. I understand what it does, but I cannot explain why the code works correctly. Here is the code:

for (int i = 0; i < faces.length; i++) {
 println(faces[i].x + "," + faces[i].y);
 rect(faces[i].x, faces[i].y, faces[i].width, faces[i].height);
 }
}

void captureEvent(Capture c) {
 c.read();
}

This is the formulation of what the program requires in terms of facial recognition in order to execute its actions. The last line of code under the “for” function indicated the scaling of the rectangle drawn depending on the size of the face. The print line function occurs frequently to keep a record of the sizes of the faces appearing in the space.

This process of learning the code by altering it step by step is helping me drastically in my opinion. I will continue to use facial recognition as part of my final project for this assignment, as I am finding it the most interesting or at least above all other approaches ive been experimenting with. below is an image of the outcome of the example code, along with the code in full.

Screen Shot 2015-01-11 at 16.50.16

import gab.opencv.*;
import processing.video.*;
import java.awt.*;

Capture video;
OpenCV opencv;


void setup() {
 size(640, 480);
 video = new Capture(this, 640/2, 480/2);
 opencv = new OpenCV(this, 640/2, 480/2);
 opencv.loadCascade(OpenCV.CASCADE_FRONTALFACE); 

 video.start();
}

void draw() {
 scale(2);
 opencv.loadImage(video);

 image(video, 0, 0 );

 noFill();
 
 stroke(0,0,0);
 strokeWeight(1);
 Rectangle[] faces = opencv.detect();
 println(faces.length);

 for (int i = 0; i < faces.length; i++) {
 println(faces[i].x + "," + faces[i].y);
 rect(faces[i].x, faces[i].y, faces[i].width, faces[i].height);
 }
}

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