Charlie
Charlie

Reputation: 73

Face detection with android.hardware.Camera2

Im using https://github.com/googlesamples/android-Camera2Basic.

I set face recognition mode to FULL.

´mPreviewRequestBuilder.set(CaptureRequest.STATISTICS_FACE_DETECT_MODE,

CameraMetadata.STATISTICS_FACE_DETECT_MODE_FULL);

My CaptureCallback:

private CameraCaptureSession.CaptureCallback mCaptureCallback
= new CameraCaptureSession.CaptureCallback() {

private void process(CaptureResult result) {
            Integer mode = result.get(CaptureResult.STATISTICS_FACE_DETECT_MODE);
            Face [] faces = result.get(CaptureResult.STATISTICS_FACES);
            if(faces != null && mode != null)
                Log.e("tag", "faces : " + faces.length + " , mode : " + mode ); 
}

@Override
public void onCaptureProgressed(CameraCaptureSession session, CaptureRequest request,
                                CaptureResult partialResult) {
    process(partialResult);
}

@Override
public void onCaptureCompleted(CameraCaptureSession session, CaptureRequest request,
                               TotalCaptureResult result) {
    process(result);
}

Output: faces : 0 , mode : 2

Faces length is constantly 0. Looks like it doesn't recognise a face properly or I missed something.

Upvotes: 2

Views: 2470

Answers (1)

Alexandre Bodi
Alexandre Bodi

Reputation: 454

You should read the face detection modes available for the device by using:

CameraManager manager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);
for (String cameraId : manager.getCameraIdList()) {
    CameraCharacteristics characteristics
                    = manager.getCameraCharacteristics(cameraId);
    int[] faceDetectModes = characteristics.get(CameraCharacteristics.STATISTICS_INFO_AVAILABLE_FACE_DETECT_MODES);
}

Use the index with the maximum value from that int array, since 0 means STATISTICS_FACE_DETECT_MODE_OFF, 1 means STATISTICS_FACE_DETECT_MODE_SIMPLE and 2 is STATISTICS_FACE_DETECT_MODE_FULL.

Your device might be returning either 0 or 1. If thats the case, "simple face detection" will just have to do, rather than "full".

If that still doesn't help, check if you're using front camera or rear camera and try with the opposite one (the sample source code you mentioned is pretty straightforward in how to accomplish that).

Upvotes: 4

Related Questions