camera2人脸检测回调
camera2 face detection call back
我正在尝试使用 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);
但是我不明白代码在检测人脸时命中了哪里?它 return 是 CameraCaptureSession.CaptureCallback 中的一张脸吗?它在哪里发回有关检测到的面孔的信息?
http://developer.android.com/reference/android/hardware/camera2/CaptureResult.html
因此,面部数据似乎只有在拍照后才可用,换句话说,在预览时不可用。当您为预览构建器设置人脸检测模式时,这意味着捕获结果将在拍摄照片时获取元数据。我会尝试在预览时找到一种方法,我想我可以拍摄预览的快照和 运行 使用后台线程进行人脸检测。我将为该场景打开一个单独的问题。
我刚刚发现使用 Camera2 Basic sample 在正确设置捕获会话后,从 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;
}
我测试了这个代码更改并且它有效(一旦检测到我的脸,我就会捕捉自拍......您可以在此处查看面部 class 的更多选项:
Face
我正在尝试使用 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);
但是我不明白代码在检测人脸时命中了哪里?它 return 是 CameraCaptureSession.CaptureCallback 中的一张脸吗?它在哪里发回有关检测到的面孔的信息?
http://developer.android.com/reference/android/hardware/camera2/CaptureResult.html
因此,面部数据似乎只有在拍照后才可用,换句话说,在预览时不可用。当您为预览构建器设置人脸检测模式时,这意味着捕获结果将在拍摄照片时获取元数据。我会尝试在预览时找到一种方法,我想我可以拍摄预览的快照和 运行 使用后台线程进行人脸检测。我将为该场景打开一个单独的问题。
我刚刚发现使用 Camera2 Basic sample 在正确设置捕获会话后,从 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;
}
我测试了这个代码更改并且它有效(一旦检测到我的脸,我就会捕捉自拍......您可以在此处查看面部 class 的更多选项: Face