0

I am trying to get the faces of an image taken using the ACTION_IMAGE_CAPTURE intent. But for some reason the FaceDetector never finds an image. This is my code

    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inPreferredConfig = Bitmap.Config.RGB_565;
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(filePath, options);

    options.inSampleSize = calculateInSampleSize(options,desiredWidth,desiredHeight);

    options.inJustDecodeBounds = false;
    Bitmap bitmap = BitmapFactory.decodeFile(filePath,options);



    ExifInterface exif = new ExifInterface(filePath);
    int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_UNDEFINED);
    Matrix matrix = new Matrix();
    boolean recycle = true;
    switch (orientation) {
        case ExifInterface.ORIENTATION_ROTATE_90:
            matrix.postRotate(90);
            break;
        case ExifInterface.ORIENTATION_ROTATE_180:
            matrix.postRotate(180);
            break;
        case ExifInterface.ORIENTATION_ROTATE_270:
            matrix.postRotate(270);
            break;
        default:
            recycle = false;
            break;
    }
    Bitmap finalBM = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
    if(recycle)
        bitmap.recycle();

    FaceDetector.Face[] faces = new FaceDetector.Face[1];
    FaceDetector faceDetector = new FaceDetector(bitmap.getWidth(), bitmap.getHeight(),1);
    int numFaces = faceDetector.findFaces(bitmap,faces);

    return  finalBM;

At this point all i am doing is checking the numFaces variable to see if any face was detected. But every single time is 0 even thought the picture is just my own face. Also I am rotating the image if necessary. I tried to detect the faces before the rotation and it did not work.

Neorcisa
  • 124
  • 2
  • 9
  • if you just want to detect faces quickly and simply you could use: https://github.com/blundell/WoodyFaceDetection (or look at this to understand how it does dace detection) – Blundell Jan 01 '17 at 19:46
  • Apart from that - looking at your code the only way anyone can help you is if they have used the exact same API in the same way as you have here, before. If you want help, you should broaden who can help you by explaining what the code above does and link to whatever documentation or explanation you need. *then* you are more likely to get help / an answer – Blundell Jan 01 '17 at 19:48

1 Answers1

0

After some digging i was able to find the problem with this code. There are two requirements one the Bitmap should be decoded using RGB_565 and on whatever orientation the image has the face should be upward. The problem in this code is that first yes it was decoding using the right format but the face was sideways. Once the image was rotated it was then created not in RGB_565. So I had to convert the rotated image to RGB_565 which can be easily done with Bitmap.copy https://developer.android.com/reference/android/graphics/Bitmap.html#copy(android.graphics.Bitmap.Config, boolean)

Then just run the FaceDetector code.

Neorcisa
  • 124
  • 2
  • 9