11

I would like to use some Android 4 APIs in a non-Camera application.

The API includes some very nice Face Detection classes, including the Camera.Face class available since API 14. I would like to apply the same Face Detection classes in order to implement face detection on images saved on the device. I would prefer to use this to process pictures stored on the device itself (ex: social tagging, face image manipulation, etc.)

I require guidance on how to accomplish this re-use task.

JoshDM
  • 4,939
  • 7
  • 43
  • 72
Lisa Anne
  • 4,482
  • 17
  • 83
  • 157

3 Answers3

6

If what you need is to detect faces in images stored on the device, you can definitely do this without hacking the source code of android!

There is a FaceDetector API that is available under the package android.media since API 1, which accepts Bitmap as input (formatted in 565 format) and give you the position of faces in that picture.

Here are the steps you need:

1- Load the Bitmap and convert it to 565 format (assuming you have facesPicture file under your drawable resources)

Bitmap originalBitmap = 
            BitmapFactory.decodeResource(getResources(),R.drawable.facesPicture);

Bitmap bitmap = originalBitmap .copy(Bitmap.Config.RGB_565, true);

originalBitmap .recycle(); // allow the GC to collect this object

2- Define Face array to hold the detected faces information and initialize the FaceDetector

int MAX_FACES = 20; // assuming that the image will have maximum 20 faces

FaceDetector.Face[] faces = new FaceDetector.Face[MAX_FACES];

FaceDetector faceDetector = 
             new FaceDetector(bitmap.getWidth(), bitmap.getHeight(), MAX_FACES);

3- Search for the faces and process results

int facesCount = faceDetector.findFaces(bitmap, faces);

for(int i=0; i<facesCount; i++) {
    FaceDetector.Face face = faces[i];

    float detectionConfidence = face.confidence(); // over 0.3 is OK

    PointF eyesMidPoint = new PointF();

    face.getMidPoint(eyesMidPoint);

    float eyesDistance = face.eyesDistance();

    float rotationX = face.pose(FaceDetector.Face.EULER_X);

    float rotationY = face.pose(FaceDetector.Face.EULER_Y);

    float rotationZ = face.pose(FaceDetector.Face.EULER_Z);

    // Do something with these values

}

You can download a complete project example here which is explained in this article Face Detection with Android APIs

If you want something more advanced you should consider using OpenCV

iTech
  • 18,192
  • 4
  • 57
  • 80
  • Thank you! but as you can see in the comment above, I am looking into detecting nose and mouth as well, which `FaceDetector` is not able to provide. – Lisa Anne Feb 16 '13 at 19:22
  • 1
    You can either use `OpenCV`, or try to implement a nose and mouth detection algorithm given that you know the eyes location. You see the code from OpenCV to get the idea – iTech Feb 16 '13 at 19:29
  • indeed that is what I am currently doing. Thanks! – Lisa Anne Feb 16 '13 at 19:38
3

The FaceDetectionListener is what you'd want to use to detect faces, but it only listens on the camera. That's its only native function. If you really want use it on pictures on the user's device, I'd suggest just downloading the source code for the camera API and adapting the method you want to your needs.

You can find the source for all stock android code here: https://android.googlesource.com/

Good luck!

NathanTempelman
  • 1,301
  • 9
  • 34
  • Thanks, but I haven't been able to actually find the the source code for the Camera API (I searched). Any help on this appreciated. – Lisa Anne Feb 11 '13 at 15:34
  • Try looking through the code in here: https://android.googlesource.com/platform/packages/apps/Camera/+/master If it's not in there, it'll most likely be in hardware frameworks. Let me know how it goes. – NathanTempelman Feb 11 '13 at 17:06
  • Actually there is nothing in: http://code.google.com/a/eclipselabs.org/p/face-recognition-android/source/browse/ Thanks anyway – Lisa Anne Feb 11 '13 at 17:20
  • What about the other two links? – NathanTempelman Feb 11 '13 at 18:23
  • Hi Nathan and thanks, so far no success with the other links. I might be looking in the wrong places – Lisa Anne Feb 11 '13 at 18:36
  • Seeing as it's built into the android source, if you really want to find it, you could try pulling down the entire android source repo from github or google code and searching the entire project for FaceDetection. It's a bit of work, but you're sure to find it then. – NathanTempelman Feb 11 '13 at 18:50
2

Are you aware of FaceDetector class? It's there since API v1 and it works quite well. If you need something more advanced you can always use some dedicated frameworks, like OpenCV (provided with some Java bindings as well, in order to be easily integrated within Android apps)

You can check a couple of (little dated, but still useful) articles I made sometime ago with a colleague:

  1. Face Detection on Andriod Part-I
  2. Face Detection on Andriod Part-II
a.bertucci
  • 12,142
  • 2
  • 31
  • 32
  • Thank you! I am aware of the `FaceDetector ` class, but as your probably know it only detects face, eyes and mid-eyes point. Not nose and mouth. Regarding OpenCV, it is actually what I am looking into now! – Lisa Anne Feb 16 '13 at 19:19
  • 1
    I see, you need something more powerful. OpenCV is the best choice then.Just a note: you can use the same cascade-classifier (the Haar-based is the most accurate) along with an entire set of training-sets in order to identify different facial features. – a.bertucci Feb 16 '13 at 19:35