Face recognition with OpenCV

One of the greatest features of OpenCV is that it allows you to do face detection. In this recipe, we’ll take a look at how you can do this with a minimum amount of code.
How to do it…
You need to start by importing the OpenCV library, just like you did in previous OpenCV
recipes. You also need to import the java.awt.Rectangle class, because the face
detection algorithm returns rectangle objects. You’ll need to type this line yourself, since this is not available from a menu. Inside the setup() function, we’ll configure OpenCV and use the cascade() method to configure how face tracking works.

import hypermedia.video.*;
import java.awt.Rectangle;
OpenCV opencv;
void setup()
size( 640, 480 );
opencv = new OpenCV( this );
opencv.capture( 320, 240 );
opencv.cascade( OpenCV.CASCADE_FRONTALFACE_ALT );

In the draw() function, we’ll read a new frame from the webcam, flip it, convert it to a
grayscale image, and display it on the screen. The detect() method is used to detect faces
in the image. I’ve drawn a black rectangle where a face is detected.
void draw()
background( 0 );
opencv.flip( OpenCV.FLIP_HORIZONTAL );
opencv.convert( GRAY );
scale( 2 );
image( opencv.image(), 0, 0 );
Rectangle[] faces = opencv.detect();
fill( 0 );
for ( int i = 0; i < faces.length; i++ ) {
rect( faces[i].x, faces[i].y, faces[i].width, faces[i].height
If you run the sketch, the result should look like a black rectangle in your face.

How it works…
The first thing you need to do is pick a detection method for OpenCV to use with the cascade() method. I’ve used the OpenCV.CASCADE_FRONTALFACE_ALT haar cascade classifier. This is basically an XML file with a description for OpenCV, so that it can detect faces.

opencv.cascade( OpenCV.CASCADE_FRONTALFACE_ALT );

There are some other cascades available for you to use if the CASCADE_FRONTALFACE_ALT
one doesn’t work for you. You can also use OpenCV to detect the profile of a face or the bodyof a person. This is the full list:


Face tracking works best on smaller, grayscale images. Large images only slow your sketch down. That’s why I’ve set the webcam size to 320 x 240 pixels, and used scale(2) to
display everything on the screen.
The detect() method checks the current OpenCV image to see if it contains faces. It returns an array of rectangle objects. These rectangle objects can be used to draw something on the screen at the position of the face.
Rectangle[] faces = opencv.detect();