我正在尝试在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中返回一个面吗?它在哪里发回有关检测到的面孔的信息?
答案 0 :(得分:5)
我只是想到使用Camera2 Basic sample,在正确设置捕获会话后,从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);
}
}
然后在创建捕获会话的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
答案 1 :(得分:1)
http://developer.android.com/reference/android/hardware/camera2/CaptureResult.html
因此,面部数据似乎仅在拍摄照片后可用,或者换句话说,在预览发生时不可用。为预览构建器启用面部检测模式时,这意味着捕获结果将在拍摄照片时获取元数据。我会尝试在预览中找到一种方法,我想我可以拍摄预览的快照并使用后台线程运行面部检测。我将针对那种情况打开一个单独的问题。