Download sample code
Face Detection Sample [PDF 206KB]
Introduction
Face detection is an important functionality for many categories of mobile applications. It can provide additional search capabilities in photo catalogs, social applications, etc.
Face detection is also a first step in implementing face recognition functionality.
This article will review a standard Android API to detect faces on a saved image.
Implementation
Android SDK contains an API for Face Detection: android.media.FaceDetector class. This class detects faces on the image. To detect faces call findFaces method of FaceDetector class. findFaces method returns a number of detected faces and fills the FaceDetector.Faces[] array. Please note, that findFaces method supports only bitmaps in RGB_565 format at this time.
Each instance of the FaceDetector.Face class contains the following information:
- Confidence that it’s actually a face – a float value between 0 and 1.
- Distance between the eyes – in pixels.
- Position (x, y) of the mid-point between the eyes.
- Pose rotations (X, Y, Z).
Unfortunately, it doesn’t contain a framing rectangle that includes the detected face.
Sample code
Here is sample source code for face detection. This sample code enables a custom View that shows a saved image from an SD Card and draws transparent red circles on the detected faces.
Source code:
class Face_Detection_View extends View { private static final int MAX_FACES = 10; private static final String IMAGE_FN = "face.jpg"; private Bitmap background_image; private FaceDetector.Face[] faces; private int face_count; // preallocate for onDraw(...) private PointF tmp_point = new PointF(); private Paint tmp_paint = new Paint(); public Face_Detection_View(Context context) { super(context); // Load an image from SD Card updateImage(Environment.getExternalStorageDirectory() + "/" + IMAGE_FN); } public void updateImage(String image_fn) { // Set internal configuration to RGB_565 BitmapFactory.Options bitmap_options = new BitmapFactory.Options(); bitmap_options.inPreferredConfig = Bitmap.Config.RGB_565; background_image = BitmapFactory.decodeFile(image_fn, bitmap_options); FaceDetector face_detector = new FaceDetector( background_image.getWidth(), background_image.getHeight(), MAX_FACES); faces = new FaceDetector.Face[MAX_FACES]; // The bitmap must be in 565 format (for now). face_count = face_detector.findFaces(background_image, faces); Log.d("Face_Detection", "Face Count: " + String.valueOf(face_count)); } public void onDraw(Canvas canvas) { canvas.drawBitmap(background_image, 0, 0, null); for (int i = 0; i < face_count; i++) { FaceDetector.Face face = faces[i]; tmp_paint.setColor(Color.RED); tmp_paint.setAlpha(100); face.getMidPoint(tmp_point); canvas.drawCircle(tmp_point.x, tmp_point.y, face.eyesDistance(), tmp_paint); } } }
This sample will not scale the picture to fit on the screen for simplicity. In real applications you will need to scale the picture or scale the FaceDetector.Face attributes to match the provided screen space.
Conclusion
Android SDK provides a standard API for face detection on a saved image. To detect a face in the camera preview frame consider using the Camera.FaceDetectionListener class.
Unfortunately, Android SDK doesn’t support API to implement face recognition functionality.
References
FaceDetector class:
http://developer.android.com/reference/android/media/FaceDetector.html
FaceDetector.Face class:
http://developer.android.com/reference/android/media/FaceDetector.Face.html
Camera.FaceDetectionListener class:
http://developer.android.com/reference/android/hardware/Camera.FaceDetectionListener.html
Intel, the Intel logo, Atom, and Core are trademarks of Intel Corporation in the U.S. and/or other countries. Copyright © 2013 Intel Corporation. All rights reserved. *Other names and brands may be claimed as the property of others.