Search code examples
androidfirebase-mlkit

How to convert FirebaseVisionFace to Bitmap from here?


I implemented Firebase ML kit, can you guys help me how can I create a bitmap from this information and show it to imageview? Please help me how can I create bitmap from this stage and save it to device memory? I tried this code, help me to go deeper.

       public void onSuccess(List<FirebaseVisionFace> faces) {
                            // Task completed successfully
                            // ...
                            Log.d("Success: ", "success");
                            for (FirebaseVisionFace face : faces) {
                                Rect bounds = face.getBoundingBox();
                                float rotY = face.getHeadEulerAngleY();  // Head is rotated to the right rotY degrees
                                float rotZ = face.getHeadEulerAngleZ();  // Head is tilted sideways rotZ degrees
                                Log.d("rotY: ", ""+rotY);
                                Log.d("rotZ: ", ""+rotZ);
                                // If landmark detection was enabled (mouth, ears, eyes, cheeks, and
                                // nose available):
                                FirebaseVisionFaceLandmark leftEar = face.getLandmark(FirebaseVisionFaceLandmark.LEFT_EAR);
                                if (leftEar != null) {
                                    FirebaseVisionPoint leftEarPos = leftEar.getPosition();
                                }

                                // If contour detection was enabled:
                                List<FirebaseVisionPoint> leftEyeContour =
                                        face.getContour(FirebaseVisionFaceContour.LEFT_EYE).getPoints();
                                List<FirebaseVisionPoint> upperLipBottomContour =
                                        face.getContour(FirebaseVisionFaceContour.UPPER_LIP_BOTTOM).getPoints();

                                // If classification was enabled:
                                if (face.getSmilingProbability() != FirebaseVisionFace.UNCOMPUTED_PROBABILITY) {
                                    float smileProb = face.getSmilingProbability();
                                }
                                if (face.getRightEyeOpenProbability() != FirebaseVisionFace.UNCOMPUTED_PROBABILITY) {
                                    float rightEyeOpenProb = face.getRightEyeOpenProbability();
                                }

                                // If face tracking was enabled:
                                if (face.getTrackingId() != FirebaseVisionFace.INVALID_ID) {
                                    int id = face.getTrackingId();
                                }

                            }
                        }

Solution

  • You can do as follows :

        // bitmap is the image you already detected faces
    
        Paint myRectPain = new Paint();
        myRectPain.setStrokeWidth(10);
        myRectPain.setColor(Color.RED);
        myRectPain.setStyle(Paint.Style.STROKE);
    
        Bitmap tempBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(),Bitmap.Config.RGB_565);
    
        Canvas tempCanvas = new Canvas(tempBitmap);
        tempCanvas.drawBitmap(bitmap,0,0,null);
    
        for (FirebaseVisionFace face: faces) {
            Rect bound = face.getBoundingBox();
            tempCanvas.drawRect(bound,myRectPain);
        }
    
        // imgView is an ImageView
    
        imgView.setImageDrawable(new BitmapDrawable(getResources(),tempBitmap));