Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: FaceDetector doesn't work. findface always detects zero face;

I have a problem with face detection in Android using android.media.FaceDetector I have tried to detetect faces using this code:

BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig =  Bitmap.Config.RGB_565;
Bitmap b = BitmapFactory.decodeFile(Environment.getExternalStorageDirectory()+"/myimage.jpg", options);
FaceDetector fd = new FaceDetector(b.getWidth(), b.getHeight(), 1);
Face[] face = new Face[1];
int detected_face = fd.findFaces(b, face);

detected_face it is always 0;

I've tried to use different images, but I received the same results. Someone could explain me what's wrong with my code?

Regards

like image 442
Rino Avatar asked Sep 02 '26 00:09

Rino


1 Answers

The code below works for me, and as I remember the faces on the photo must be upright, meaning that if in the picture one stands on his head, then you have to rotate the bitmap by 180-degree before feeding it to the FaceDetector, or his face will not be detected)

private void detectFaces() {
    int max = 5;
    BitmapFactory.Options bfo = new BitmapFactory.Options();
    bfo.inPreferredConfig = Bitmap.Config.RGB_565;
    bfo.inScaled = false;
    bfo.inDither = false;
    Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.myphoto, bfo);
    int w = bitmap.getWidth();
    int h = bitmap.getHeight();
    FaceDetector fd = new FaceDetector(w, h, max);
    Face[] faces = new Face[max];
    int c = fd.findFaces(bitmap, faces);
    for (int i=0;i<c;i++) {
        Log.d("TAG", Float.toString(faces[i].eyesDistance()));
    }
}
like image 104
Ziteng Chen Avatar answered Sep 03 '26 15:09

Ziteng Chen