FaceDetector 检测方法中的图像尺寸不一致

Inconsistent Image dimensions in FaceDetector detect method

我正在尝试使用 com.google.android.gms.vision.face.FaceDetector class,

facedetector 对象创建为,

 detector = new FaceDetector.Builder(context)
            .setClassificationType(FaceDetector.FAST_MODE)
            .setProminentFaceOnly(true)
            .setTrackingEnabled(true)
            .build();


 detector.setProcessor(
            new MultiProcessor.Builder<>(new GraphicFaceTrackerFactory())
                    .build());

相机源是用 Camera2 创建的 Api as,

mCameraSource = new CameraSource.Builder(context, detector)
                .setFacing(CameraSource.CAMERA_FACING_FRONT)
                .setRequestedFps(15.0f)
                .build();

然后在一个按钮上点击调用 CameraSource 中的 takePicture 方法来处理图像,

mCameraSource.takePicture(null, new CameraSource.PictureCallback(){

            @Override
            public void onPictureTaken(byte[] bytes) {
                BitmapFactory.Options options = new BitmapFactory.Options();
                Bitmap temp = BitmapFactory.decodeByteArray(bytes, 0,
                        bytes.length, options);
                Frame frame = new Frame.Builder().setBitmap(temp).build();
                SparseArray<Face> faces =detector.detect(frame);
                System.out.println("faces: "+faces.size());

            }
        });

但我收到以下错误,

inconsistent image dimensions

Native face detection failed

java.lang.RuntimeException: Error detecting faces.

com.google.android.gms.vision.face.NativeFaceDetectorImpl.detectFacesJni(Native Method)
    at com.google.android.gms.vision.face.FaceDetector.detect(Unknown Source:41)

我终于让它工作了,从文件中检测人脸,

解决方案是使用设置创建检测器对象, trackingEnabled ->false,如果要跟踪多个人脸,setProminentFaceOnly->false.

启用跟踪后,检测器可以很好地与关联的相机预览配合使用,以跟踪带有 ID 的面部。将 post 详细解释此解决方案背后的原因,作为创建检测器对象的原因,

detector = new FaceDetector.Builder(context)
            .setClassificationType(FaceDetector.FAST_MODE)
            .setProminentFaceOnly(false) // <-- set to false
            .setTrackingEnabled(false) // <-- set to false
            .build();