如何绘制用位图创建的FirebaseVisionFace的轮廓?[关闭]


我使用Firebase ML Kit制作了一个人脸轮廓检测器,我使用fromBitmap(Bitmap bitmap)方法创建了一个FirebaseVisionImage对象和一个带有特定选项的FirebaseVisionFaceDetector。我使用fromBitmap(Bitmap bitmap)方法创建了一个FirebaseVisionImage对象和一个带有特定选项的FirebaseVisionFaceDetector。问题是:有没有什么类可以帮助我在我的应用程序已经检测到的人脸周围绘制轮廓?

已有的工作。

  • 应用程序已经检测到了人脸和它的微笑概率,左右眼睁开的概率… …。

我创建了什么?

  • 一个类,它能返回一个列表给我,比如说,一个眼睛的点。

MainActivity.java:

private ImageView pictureImageView;
private ImageButton takePictureButton, choosePictureButton;


private static final int REQUEST_IMAGE_CAPTURE = 1;

private static final int REQUEST_IMAGE_READ = 42;


private FirebaseVisionImage faceImage;

private FirebaseVisionFaceDetector faceDetector;

 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    pictureImageView = findViewById(R.id.picture_image_view);
    takePictureButton = findViewById(R.id.take_photo_image_view);
    choosePictureButton = findViewById(R.id.choose_photo_image_view);
}


public void takePicture(View v){
    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

    if(takePictureIntent.resolveActivity(getPackageManager()) != null)
        startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
}


public void choosePicture(View v){
    Intent choosePictureIntent = new Intent(Intent.ACTION_OPEN_DOCUMENT).setType("image/*");

    if(choosePictureIntent.resolveActivity(getPackageManager()) != null){
        startActivityForResult(choosePictureIntent, REQUEST_IMAGE_READ);
    }
}



@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data){
    super.onActivityResult(requestCode, resultCode, data);

    if(requestCode == REQUEST_IMAGE_CAPTURE && resultCode == Activity.RESULT_OK){
        Bundle extras = data.getExtras();

        Bitmap bitmap = (Bitmap) extras.get("data");

        try { detectFace(bitmap); }
        catch (Exception e) { e.printStackTrace(); }
    }
}

private void detectFace(final Bitmap bitmap) throws Exception {
    FaceUtils faceUtils = new FaceUtils();

    FirebaseVisionFaceDetectorOptions options = faceUtils.getFaceDetectorOptions();

    faceImage = FirebaseVisionImage.fromBitmap(bitmap);

    faceDetector = FirebaseVision.getInstance().getVisionFaceDetector(options);

    faceDetector.detectInImage(faceImage).addOnSuccessListener(new OnSuccessListener<List<FirebaseVisionFace>>() {
        @Override
        public void onSuccess(List<FirebaseVisionFace> firebaseVisionFaces) {
            if(firebaseVisionFaces.isEmpty()){
                Toast.makeText(getApplicationContext(), "Nessuna faccia trovata", Toast.LENGTH_SHORT).show();
            } else {
                String resultText = " ";
                int i = 1;

                for (FirebaseVisionFace face : firebaseVisionFaces) {
                    resultText = "\n" + i + " faccia trovata.\n" + "Sorriso: "
                                    + faceUtils.getSmilingProbability(face) * 100 + "%\nOcchio sinistro: "
                                        + faceUtils.getLeftEyeOpenProbability(face) * 100 + "%\nOcchio destro: "
                                            + faceUtils.getRightEyeOpenProbability(face) * 100 + "%";

                    i++;
                }

                pictureImageView.setImageBitmap(bitmap);

                Bundle bundle = new Bundle();
                bundle.putString(BundleTags.REQUEST_TEXT, resultText);

                DialogFragment resultDialog = new ResultDialog();
                resultDialog.setArguments(bundle);
                resultDialog.setCancelable(false);
                resultDialog.show(getSupportFragmentManager(), BundleTags.REQUEST_DIALOG);
            }
        }
    });
}

}

FaceUtils

public FirebaseVisionFaceDetectorOptions getFaceDetectorOptions(){
    FirebaseVisionFaceDetectorOptions options = new FirebaseVisionFaceDetectorOptions.Builder()
            .setPerformanceMode(FirebaseVisionFaceDetectorOptions.ACCURATE)
            .setContourMode(FirebaseVisionFaceDetectorOptions.ALL_CONTOURS)
            .enableTracking()
            .setLandmarkMode(FirebaseVisionFaceDetectorOptions.ALL_LANDMARKS)
            .setClassificationMode(FirebaseVisionFaceDetectorOptions.ALL_CLASSIFICATIONS)
            .build();

    return options;
}

public float getLeftEyeOpenProbability(FirebaseVisionFace face){
    if(face.getLeftEyeOpenProbability() != FirebaseVisionFace.UNCOMPUTED_PROBABILITY && new FaceLandmarksPosition().getLeftEyePosition(face) != null){
        float leftEyeProbability = face.getLeftEyeOpenProbability();
        return leftEyeProbability;
    }

    return -1;
}

public float getRightEyeOpenProbability(FirebaseVisionFace face){
    if(face.getRightEyeOpenProbability() != FirebaseVisionFace.UNCOMPUTED_PROBABILITY && new FaceLandmarksPosition().getLeftEyePosition(face) != null){
        float rightEyeProbability = face.getRightEyeOpenProbability();
        return rightEyeProbability;
    }

    return -1;
}

public float getSmilingProbability(FirebaseVisionFace face){
    if(face.getSmilingProbability() != FirebaseVisionFace.UNCOMPUTED_PROBABILITY){
        float smilingProbability = face.getSmilingProbability();
        return smilingProbability;
    }

    return -1;
}

public int getTrackingId(FirebaseVisionFace face){
    if(face.getTrackingId() != FirebaseVisionFace.INVALID_ID){
        int faceId = face.getTrackingId();
        return faceId;
    }

    return -1;
}

}

面部标记位置

 package com.example.progettoesame;

 import com.google.firebase.FirebaseApiNotAvailableException;
 import com.google.firebase.ml.vision.FirebaseVision;
 import com.google.firebase.ml.vision.common.FirebaseVisionPoint;
 import com.google.firebase.ml.vision.face.FirebaseVisionFace;
 import com.google.firebase.ml.vision.face.FirebaseVisionFaceContour;
 import com.google.firebase.ml.vision.face.FirebaseVisionFaceLandmark;

 import java.util.List;


 public class FaceLandmarksPosition {

public float getEulerAngleY(FirebaseVisionFace face){
    return face.getHeadEulerAngleY();
}

public float getEulerAngleZ(FirebaseVisionFace face){
    return face.getHeadEulerAngleZ();
}

public FirebaseVisionPoint getLeftEyePosition(FirebaseVisionFace face){
    FirebaseVisionFaceLandmark leftEye = face.getLandmark(FirebaseVisionFaceLandmark.LEFT_EYE);

    if(leftEye != null){
        FirebaseVisionPoint leftEyePosition = leftEye.getPosition();
        return leftEyePosition;
    }

    return null;
}

public FirebaseVisionPoint getRightEyePosition(FirebaseVisionFace face){
    FirebaseVisionFaceLandmark rightEye = face.getLandmark(FirebaseVisionFaceLandmark.RIGHT_EYE);

    if(rightEye != null){
        FirebaseVisionPoint rightEyePosition = rightEye.getPosition();
        return rightEyePosition;
    }

    return null;
}

List<FirebaseVisionPoint> getLeftEyeContour(FirebaseVisionFace face){
    FirebaseVisionPoint leftEyePosition = getLeftEyePosition(face);

    if(leftEyePosition != null){
        List<FirebaseVisionPoint> leftEyeContour = face.getContour(FirebaseVisionFaceContour.LEFT_EYE).getPoints();
        return leftEyeContour;
    }

    return null;
}

List<FirebaseVisionPoint> getRightEyeContour(FirebaseVisionFace face){
    FirebaseVisionPoint rightEyePosition = getRightEyePosition(face);

    if(rightEyePosition != null){
        List<FirebaseVisionPoint> rightEyeContour = face.getContour(FirebaseVisionFaceContour.RIGHT_EYE).getPoints();
        return rightEyeContour;
    }

    return null;
}

}

有没有什么课能帮我画出那个轮廓?我现在想开始画左边和眼睛的权利

解决方案:

使用叠加在位图上显示检测到的人脸。

下面是一段代码 取自MLKit启动程序 演示如何绘制你的轮廓

/** Draws the face annotations for position on the supplied canvas.  */
override fun draw(canvas: Canvas) {
    val face = firebaseVisionFace ?: return

    // Draws a circle at the position of the detected face, with the face's track id below.
    val x = translateX(face.boundingBox.centerX().toFloat())
    val y = translateY(face.boundingBox.centerY().toFloat())
    canvas.drawCircle(x, y, FACE_POSITION_RADIUS, facePositionPaint)
    canvas.drawText("id: ${face.trackingId}", x + ID_X_OFFSET, y + ID_Y_OFFSET, idPaint)

    // Draws a bounding box around the face.
    val xOffset = scaleX(face.boundingBox.width() / 2.0f)
    val yOffset = scaleY(face.boundingBox.height() / 2.0f)
    val left = x - xOffset
    val top = y - yOffset
    val right = x + xOffset
    val bottom = y + yOffset
    canvas.drawRect(left, top, right, bottom, boxPaint)

    val contour = face.getContour(FirebaseVisionFaceContour.ALL_POINTS)
    for (point in contour.points) {
        val px = translateX(point.x)
        val py = translateY(point.y)
        canvas.drawCircle(px, py, FACE_POSITION_RADIUS, facePositionPaint)
    }

    if (face.smilingProbability >= 0) {
        canvas.drawText(
                "happiness: ${String.format("%.2f", face.smilingProbability)}",
                x + ID_X_OFFSET * 3,
                y - ID_Y_OFFSET,
                idPaint)
    }

    if (face.rightEyeOpenProbability >= 0) {
        canvas.drawText(
                "right eye: ${String.format("%.2f", face.rightEyeOpenProbability)}",
                x - ID_X_OFFSET,
                y,
                idPaint)
    }
    if (face.leftEyeOpenProbability >= 0) {
        canvas.drawText(
                "left eye: ${String.format("%.2f", face.leftEyeOpenProbability)}",
                x + ID_X_OFFSET * 6,
                y,
                idPaint)
    }
    val leftEye = face.getLandmark(FirebaseVisionFaceLandmark.LEFT_EYE)
    leftEye?.position?.let {
        canvas.drawCircle(
                translateX(it.x),
                translateY(it.y),
                FACE_POSITION_RADIUS,
                facePositionPaint)
    }
    val rightEye = face.getLandmark(FirebaseVisionFaceLandmark.RIGHT_EYE)
    rightEye?.position?.let {
        canvas.drawCircle(
                translateX(it.x),
                translateY(it.y),
                FACE_POSITION_RADIUS,
                facePositionPaint)
    }
    val leftCheek = face.getLandmark(FirebaseVisionFaceLandmark.LEFT_CHEEK)
    leftCheek?.position?.let {
        canvas.drawCircle(
                translateX(it.x),
                translateY(it.y),
                FACE_POSITION_RADIUS,
                facePositionPaint)
    }

    val rightCheek = face.getLandmark(FirebaseVisionFaceLandmark.RIGHT_CHEEK)
    rightCheek?.position?.let {
        canvas.drawCircle(
                translateX(it.x),
                translateY(it.y),
                FACE_POSITION_RADIUS,
                facePositionPaint)
    }
}

给TA打赏
共{{data.count}}人
人已打赏
未分类

整洁数字挑战和 "每一个 "方法

2022-9-9 8:26:16

未分类

Angular。如何在Angular单元测试中使用路由器?

2022-9-9 8:26:18

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索