Android Face Detection Example


In this sample application we will detect the faces in the given photograph. The application will detect the number of faces in the image and draws a rectangle around each face in the image. So here we will load an image into our project and detect if it has faces or not. In real life you would probably
want to capture the image using the camera, or choose the image from a Gallery to detect face in it.

To do face detection we use android.media.FaceDetector class which identifies the location of the face in a bitmap.

So lets start:

1. Create a new project File -> Android Project. While creating a new project give activity name as FaceDetection(FaceDetection.java).

2. A custom class myView is created which extends View where the face detection is done. It would be used to serve as UI for our Activity.

FaceDetection.java
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PointF;
import android.media.FaceDetector;
import android.media.FaceDetector.Face;
import android.os.Bundle;
import android.view.View;

public class FaceDetection extends Activity {

 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(new myView(this));
 }

 private class myView extends View {

  private int imageWidth, imageHeight;
  private int numberOfFace = 5;
  private FaceDetector myFaceDetect;
  private FaceDetector.Face[] myFace;
  float myEyesDistance;
  int numberOfFaceDetected;

  Bitmap myBitmap;

  public myView(Context context) {
   super(context);

   BitmapFactory.Options BitmapFactoryOptionsbfo = new BitmapFactory.Options();
   BitmapFactoryOptionsbfo.inPreferredConfig = Bitmap.Config.RGB_565;
   myBitmap = BitmapFactory.decodeResource(getResources(),
     R.drawable.jennifer_lopez, BitmapFactoryOptionsbfo);
   imageWidth = myBitmap.getWidth();
   imageHeight = myBitmap.getHeight();
   myFace = new FaceDetector.Face[numberOfFace];
   myFaceDetect = new FaceDetector(imageWidth, imageHeight,
     numberOfFace);
   numberOfFaceDetected = myFaceDetect.findFaces(myBitmap, myFace);

  }

  @Override
  protected void onDraw(Canvas canvas) {

   canvas.drawBitmap(myBitmap, 0, 0, null);

   Paint myPaint = new Paint();
   myPaint.setColor(Color.GREEN);
   myPaint.setStyle(Paint.Style.STROKE);
   myPaint.setStrokeWidth(3);

   for (int i = 0; i < numberOfFaceDetected; i++) {
    Face face = myFace[i];
    PointF myMidPoint = new PointF();
    face.getMidPoint(myMidPoint);
    myEyesDistance = face.eyesDistance();

    canvas.drawRect((int) (myMidPoint.x - myEyesDistance * 2),
      (int) (myMidPoint.y - myEyesDistance * 2),
      (int) (myMidPoint.x + myEyesDistance * 2),
      (int) (myMidPoint.y + myEyesDistance * 2), myPaint);
   }
  }
 }

}
3.Do not forget to drop an image into drawable folder.

4.Run the project by rightclicking project Run as → android project.

Output:

The output of this example would be similar to the one as follows: