首页 > 解决方案 > 如何使用 MLKit firebase FaceDetection 捕获检测到的人脸图像

问题描述

我只想在camerasource上捕获检测到的面部图像而不是整个图像。

我就是这样的落水箱

public void draw(Canvas canvas) {
        Face face = mFace;
        if (face == null) {
            return;
        }

        // Draws a circle at the position of the detected face, with the face's track id below.
        float x = translateX(face.getPosition().x + face.getWidth() / 2);
        float y = translateY(face.getPosition().y + face.getHeight() / 2);
        canvas.drawCircle(x, y, FACE_POSITION_RADIUS, mFacePositionPaint);
        canvas.drawText("id: " + mFaceId, x + ID_X_OFFSET, y + ID_Y_OFFSET, mIdPaint);
        canvas.drawText("happiness: " + String.format("%.2f", face.getIsSmilingProbability()), x - ID_X_OFFSET, y - ID_Y_OFFSET, mIdPaint);
        canvas.drawText("right eye: " + String.format("%.2f", face.getIsRightEyeOpenProbability()), x + ID_X_OFFSET * 2, y + ID_Y_OFFSET * 2, mIdPaint);
        canvas.drawText("left eye: " + String.format("%.2f", face.getIsLeftEyeOpenProbability()), x - ID_X_OFFSET*2, y - ID_Y_OFFSET*2, mIdPaint);

        // Draws a bounding box around the face.
        float xOffset = scaleX(face.getWidth() / 2.0f);
        float yOffset = scaleY(face.getHeight() / 2.0f);
        float left = x - xOffset;
        float top = y - yOffset;
        float right = x + xOffset;
        float bottom = y + yOffset;


        this.faceTrackingCords.y = top;
        this.faceTrackingCords.width = right;
        this.faceTrackingCords.x = left-right;
        this.faceTrackingCords.height = bottom;


        canvas.drawRect(left, top, right, bottom, mBoxPaint);
    }

并尝试像这样捕获图像,但裁剪后的图像不如预期。


                mCameraSource.takePicture(null, new CameraSource.PictureCallback() {
                    @Override
                    public void onPictureTaken(byte[] bytes) {

                        Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);

                        Bitmap bitmapCropped = Bitmap.createBitmap(bitmap,(int)faceTrackingCords.x, (int)faceTrackingCords.y,
                                (int)faceTrackingCords.width, (int)faceTrackingCords.height);
                        screenImage.setImageBitmap(bitmapCropped);
                    }
                });

请帮忙

标签: androidfirebaseface-detectionfirebase-mlkitjava-canvas

解决方案


推荐阅读