gpt4 book ai didi

android - Camera2人脸检测回调

转载 作者:行者123 更新时间:2023-12-02 12:13:24 24 4
gpt4 key购买 nike

我正在尝试使用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 中返回人脸?它把检测到的人脸信息发回哪里?

最佳答案

我刚刚想出使用 Camera2 Basic sample正确设置捕获 session 后,从 Camera2 预览中获取 Face 数组相对容易。

在 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);
}
}

然后在创建捕获 session 的 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

关于android - Camera2人脸检测回调,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33748760/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com