Recently a friend of mine emailed me and asked about how to do face detection in Android. I created a sample application for him, and I think it is better to share it with you all.
Android provides a convenient class FaceDetector for detecting faces in a bitmap object. This class gives you an array of Face objects that represents the faces found in the given bitmap. The Faces class contains the information about each face detected such as distance between the eyes, mid-point between the eyes and the pose (the rotation around X, Y and Z axis) of the face.
First we initialize the FaceDetector class by passing the height and width of the bitmap and maximum faces to detect. Once we created the FaceDetector object, we can use the findFaces method to start face detection. The findFaces method accepts a bitmap and an array of Face objects. The length of the array should be equal to the number we passed to the FaceDetector constructor. Once the system finished detecting faces, the method returns the number of faces detected and fills the array with the information about the faces detected.
On limitation of this class is that it only supports bitmaps in RGB_565 format. So if you have a bitmap in another format you have to convert it to RGB_565 format.
The sample application is a simple one just to show the use of FaceDetector class. It allows you to take a picture using the camera. Once the picture is taken the bitmap is converted to RGB_565 format and pass it to FaceDetector and draws a red rectangle around the detected faces. Hope this is helpful to you.
Happy Face Detection!