Search code examples
javabitmapfirebase-mlkitrect

Face Detection and extracting the faces using Bounding Box and creating a new Bitmap


How do I use the Rect rect = face.getBoundingBox() data to crop out the detected face from the bitmap and save it as a new bitmap. Ive attempted to construct the bitmap using rect.left etc and simply display the extracted face in imageview.. but it does not seem to work.

Also, is it possible to access the faces directly? If I understand correctly the detector creates a List of FirebaseVisionFace, what are these listings? How does it list a face? Is it possible to access them?

private void processFaceDetection(final Bitmap bitmap) {
        FirebaseVisionImage firebaseVisionImage = FirebaseVisionImage.fromBitmap(bitmap);  //firebaseVisionImage is an object created from bitmap firebase uses to detect faces

        FirebaseVisionFaceDetectorOptions firebaseVisionFaceDetectorOptions  = new FirebaseVisionFaceDetectorOptions.Builder().build();

        FirebaseVisionFaceDetector firebaseVisionFaceDetector = FirebaseVision.getInstance().getVisionFaceDetector(firebaseVisionFaceDetectorOptions);

        firebaseVisionFaceDetector.detectInImage(firebaseVisionImage).addOnSuccessListener(new OnSuccessListener<List<FirebaseVisionFace>>() {
            @Override
            public void onSuccess(List<FirebaseVisionFace> firebaseVisionFaces) {
                int counter = 0;

                for (FirebaseVisionFace face : firebaseVisionFaces) {
                    Rect rect = face.getBoundingBox();
                    RectOverlay rectOverlay = new RectOverlay(graphicOverlay, rect);
                    graphicOverlay.add(rectOverlay);
                    Bitmap faceSaved = Bitmap.createBitmap(Math.round(Math.abs(rect.left - rect.right)), Math.round(Math.abs(rect.top - rect.bottom)), Bitmap.Config.ALPHA_8);
                    imageview.setImageBitmap(facesaved);
                    imageview.setVisibility(View.VISIBLE);
                    counter++;

                }

Solution

  • ANSWER: To use the rect data, which can be gathered using rect.toShortString(), produces 4 values for left, top, right, bottom. i.e. [280,495][796,1011]. These are created by the FirebaseVisionFaceDetector and are stored in a list (List) for each detected face.

    To save the bitmap data contained within different rects(faces)

    for (FirebaseVisionFace face : firebaseVisionFaces) {
    Rect rect = face.getBoundingBox();
    
    Bitmap original = Bitmap.createScaledBitmap(capturedImage, cameraView.getWidth(), cameraView.getHeight(), false); //scaled bitmap created from captured image
    
    Bitmap faceCrop = Bitmap.createBitmap(original, rect.left, rect.top, rect.width(), rect.height()); //face cropped using rect values
    
    

    faceCrop contains the face-only bitmap data contained within the parameters of the rect.

    Hope this helps....