首页 > 解决方案 > 从实时视频中提取视频帧以在 android 中进行实时面部地标分析

问题描述

我正在使用 android 的 ML Kit 构建一个实时面部地标检测应用程序。目前,我可以使用 android camerax 库捕获视频。为了对捕获的视频启用实时面部标志检测,我需要提取视频帧并将帧传递给 ML Kit 面部标志检测功能,该功能将处理和检测视频中存在的标志。

我附上了下面的代码片段。有人可以帮我提取视频帧吗?如果还有其他方法,请告诉我。

谢谢你。

 btnVideo.setOnClickListener(v -> {
        if(mCameraView.isRecording()){return;}

        SimpleDateFormat mDateFormat = new SimpleDateFormat("yyyyMMddHHmmss", Locale.US);
        File file = new File(getBatchDirectoryName(), mDateFormat.format(new Date()) + ".mp4");

        mCameraView.setCaptureMode(CameraView.CaptureMode.VIDEO);

        InputImage image = InputImage.fromBitmap(bitmap, rotationDegree);

        FaceDetectorOptions highAccuracyOpts =
                new FaceDetectorOptions.Builder()
                        .setPerformanceMode(FaceDetectorOptions.PERFORMANCE_MODE_ACCURATE)
                        .setLandmarkMode(FaceDetectorOptions.LANDMARK_MODE_ALL)
                        .setContourMode(FaceDetectorOptions.CONTOUR_MODE_ALL)
                        .setClassificationMode(FaceDetectorOptions.CLASSIFICATION_MODE_ALL)
                        .build();

        FaceDetector detector = FaceDetection.getClient(highAccuracyOpts);

        Task<List<Face>> result =
                detector.process(image)
                        .addOnSuccessListener(
                                new OnSuccessListener<List<Face>>() {
                                    @Override
                                    public void onSuccess(List<Face> faces) {
                                        for (Face face : faces) {
                                            Rect bounds = face.getBoundingBox();
                                            float rotY = face.getHeadEulerAngleY();
                                            float rotZ = face.getHeadEulerAngleZ();

                                            FaceLandmark leftEar = face.getLandmark(FaceLandmark.LEFT_EAR);
                                            if (leftEar != null) {
                                                PointF leftEarPos = leftEar.getPosition();
                                            }
                                            List<PointF> leftEyeContour =
                                                    face.getContour(FaceContour.LEFT_EYE).getPoints();
                                            List<PointF> upperLipBottomContour =
                                                    face.getContour(FaceContour.UPPER_LIP_BOTTOM).getPoints();

                                            if (face.getSmilingProbability() != null) {
                                                float smileProb = face.getSmilingProbability();
                                            }
                                            if (face.getRightEyeOpenProbability() != null) {
                                                float rightEyeOpenProb = face.getRightEyeOpenProbability();
                                            }

                                            if (face.getTrackingId() != null) {
                                                int id = face.getTrackingId();
                                            }
                                        }
                                    }
                                })
                        .addOnFailureListener(
                                new OnFailureListener() {
                                    @Override
                                    public void onFailure(@NonNull Exception e) {
                                    }
                                });


        mCameraView.startRecording(file, executor, new VideoCapture.OnVideoSavedCallback() {

            @Override
            public void onVideoSaved(@NonNull OutputFileResults outputFileResults) {
                galleryAddPic(file, 1);
            }

            @Override
            public void onError(int videoCaptureError, @NonNull String message, @Nullable Throwable cause) {
                //Log.i("TAG",message);
                mCameraView.stopRecording();
            }

        });
    });

标签: androidface-detectiongoogle-mlkit

解决方案


推荐阅读