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
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()));
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With