Search code examples
androidface-detectiongoogle-vision

Face detection not working


Following this simple example I incorporated face detection into my photo app.

I've removed all the shape drawing stuff for simplicity and am just looking for the API to count the number of heads in a photo.

Using the front facing camera I take a picture and consistently and without fail it doesn't detect any faces.

Also there's a very suspicious warning in the logs that happens every time I run the code (which doesn't seem to have anything to do with what I'm doing, but nevertheless appears every time - the warnings are:

W/ResourcesManager: Asset path '/system/framework/com.android.media.remotedisplay.jar' does not exist or contains no resources.

W/ResourcesManager: Asset path '/system/framework/com.android.location.provider.jar' does not exist or contains no resources.

here's my code

Photo Callback

PictureCallback jpegCallback = new PictureCallback() {
    public void onPictureTaken(byte[] data, Camera camera) {
        try {

            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inPreferQualityOverSpeed = true;
            options.inPreferredConfig = Bitmap.Config.ARGB_8888;
            options.inPurgeable = true;
            options.inInputShareable = true;
            options.inMutable = true;
            Bitmap temp = BitmapFactory.decodeByteArray(data, 0,
                    data.length, options);

            countHeads(temp);               

        } catch (Exception e) {
            Log.d(TAG, "onPictureTaken callback failed : " + e);
        } 
    }
};

Head Counter

private void countHeads(Bitmap b){
    Frame frame = new Frame.Builder().setBitmap(b).build();

    FaceDetector faceDetector = new FaceDetector.Builder(getApplicationContext()).setTrackingEnabled(false)
            .build();

    if(!faceDetector.isOperational()){
        BPCAlertDialog.alert(this, "Can't build face detection");
        return;
    }
    SparseArray<Face> faces = faceDetector.detect(frame);
    //this always prints 0
    Log.d(TAG, "I COUNT " + faces.size() + " FACES IN THIS PHOTO"); 
}

Solution

  • It turned out that the problem was that I was taking the photos sideways (holding the phone in portrait) even though the Activity is set-up for Landscape. I haven't done a thorough introspection yet of what the specific limitations are, but it would appear that the faces need to be lined up with the expected orientation. I'll add to this answer as I figure out more.