2

我正在尝试在 camera2 api 中使用人脸检测器。我将预览生成器设置为使用面部检测器:

                        previewBuilder.set(CaptureRequest.STATISTICS_FACE_DETECT_MODE,
                        CameraMetadata.STATISTICS_FACE_DETECT_MODE_FULL);
                        CR = previewBuilder.build();
                        CS = session;
                        CS.setRepeatingRequest(CR, CScallback, null);

但是我不明白代码在检测到人脸时会到达哪里?它是否在 CameraCaptureSession.CaptureCallback 中返回人脸?它在哪里发回有关检测到的人脸的信息?

4

2 回答 2

6

我刚刚发现使用Camera2 Basic 示例,在正确设置捕获会话后,从 Camera2 预览中获取人脸数组相对容易。

在 Camera2 Basic 示例的 setUpCameraOutputs 方法中添加以下检查:

int[] FD =characteristics.get(CameraCharacteristics.STATISTICS_INFO_AVAILABLE_FACE_DETECT_MODES);
int maxFD=characteristics.get(CameraCharacteristics.STATISTICS_INFO_MAX_FACE_COUNT);

if (FD.length>0) {
    List<Integer> fdList = new ArrayList<>();
    for (int FaceD : FD
            ) {
        fdList.add(FaceD);
        Log.d(TAG, "setUpCameraOutputs: FD type:" + Integer.toString(FaceD));
    }
    Log.d(TAG, "setUpCameraOutputs: FD count" + Integer.toString(maxFD));

    if (maxFD > 0) {
        mFaceDetectSupported = true;
        mFaceDetectMode = Collections.max(fdList);
    }
}

然后在创建捕获会话的 createCameraPreviewSession() 方法中添加以下人脸检测方法调用:

@Override
public void onConfigured(@NonNull CameraCaptureSession cameraCaptureSession) {
    // The camera is already closed
    if (null == mCameraDevice) {
        return;
    }
    // When the session is ready, we start displaying the preview. ...
    // Flash is automatically enabled when necessary.
    setAutoFlash(mPreviewRequestBuilder);

    //>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>HERE>>>>>>>>>>>>>>>>                                   
    //Face detection setup
    setFaceDetect(mPreviewRequestBuilder,mFaceDetectMode);...

setFaceDetect(mPreviewRequestBuilder,mFaceDetectMode); 代码可以是这样的:

private void setFaceDetect(CaptureRequest.Builder requestBuilder , int faceDetectMode){
    if (mFaceDetectSupported){
        requestBuilder.set(CaptureRequest.STATISTICS_FACE_DETECT_MODE,faceDetectMode);
    }

}

只需添加几行代码,即可在 CameraCaptureSession.CaptureCallback 中完成人脸检测:

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

private void process(CaptureResult result) {
    switch (mState) {
        case STATE_PREVIEW: {
            // We have nothing to do when the camera preview is working normally.
            //But we can for example detect faces 
            Face face[]=result.get(CaptureResult.STATISTICS_FACES);
            if (face.length>0 ){
                Log.d(TAG, "face detected " + Integer.toString(face.length));
                takePicture();
            }

            break;
        }

我测试了代码的这种更改并且它有效(一旦检测到我的脸,我就会捕捉自拍......您可以在此处查看 Face 类的更多选项: Face

于 2016-03-20T14:36:56.313 回答
1

http://developer.android.com/reference/android/hardware/camera2/CaptureResult.html

因此,面部数据似乎仅在拍摄照片后可用,或者换句话说,在预览发生时不可用。当您为预览构建器设置人脸检测模式时,这意味着捕获结果将在拍摄照片时获取元数据。我会尝试在预览中找到一种方法,我想我可以拍摄预览的快照并使用后台线程运行人脸检测。我将为这种情况打开一个单独的问题。

于 2015-11-26T03:31:02.163 回答