Search code examples
androidface-detectionandroid-visiongoogle-vision

How to save a frame using google vision face detect


I am trying to following the google-vision face-tracker sample listed in here. I would like to know how to get the number of face detected by the application and how to save a frame to the phone memory, is it possible from the application ?


Solution

  • Yes, you can use the following code:

    import android.content.Context;
    import android.graphics.Bitmap;
    import android.graphics.PointF;
    import android.media.FaceDetector;
    import android.util.SparseArray;
    
    import com.google.android.gms.vision.Frame;
    import com.piisoft.upecfacerecognition.utility.Image;
    
    import java.io.File;
    import java.util.List;
    
    
    
    
    public class extractFacesFromImage {
    
        public  extractFacesFromImage(String imagePath ,  String OutPutPath , Context context){
            File folder = new File(OutPutPath);
            if(!folder.exists()){
                folder.mkdirs();
            }
            detectFacesInImage(imagePath , OutPutPath, context);
        }
    
        private  void detectFacesInImage(String imagePath ,  String OutPutPath){
            //ImageWindow[] imageWindow = null;
            Bitmap image =  Image.bitmapFromJpg(imagePath);
            FaceDetector.Face[] faces = detectFaces(image);
            for(FaceDetector.Face fs:faces)
            {
                if(fs == null){continue;}
                PointF midPoint=new PointF();
                fs.getMidPoint(midPoint);
                float eyeDistance=fs.eyesDistance();
    
                int left = (int)(midPoint.x - (float)(1.4 * eyeDistance));
                int top = (int)(midPoint.y - (float)(1.8 * eyeDistance));
    
                Bitmap bmFace = Bitmap.createBitmap(image, (int) left, (int) top, (int) (2.8 * eyeDistance), (int) (3.6 * eyeDistance));
                Bitmap bmp= bmFace.createBitmap(bmFace.getWidth(), bmFace.getHeight(), Bitmap.Config.ARGB_8888);
                Image.saveBitmapToJpg(bmp,OutPutPath, "face_" +  System.currentTimeMillis()  +".jpg" ,256,256);
    
                //ImageWindow Iw = new ImageWindow(fs.)
    
            }
            //return  imageWindow;
        }
    
    
        private  void detectFacesInImage(String imagePath ,  String OutPutPath, Context context){
            //ImageWindow[] imageWindow = null;
            Bitmap image =  Image.bitmapFromJpg(imagePath);
            if(image == null){
                return;
            }
            SparseArray<com.google.android.gms.vision.face.Face> faces = detectFaces(image,context);
    
            for (int i = 0; i < faces.size(); ++i) {
                com.google.android.gms.vision.face.Face face = faces.valueAt(i);
                if(face == null){continue;}
                try {
    
                    Bitmap bmFace = Bitmap.createBitmap(image, (int) face.getPosition().x, (int) face.getPosition().y, (int) face.getWidth(), (int) face.getHeight());
                    Image.saveBitmapToJpg(bmFace, OutPutPath, "face_" + System.currentTimeMillis() + ".jpg",256);
                }
                catch (Exception e){
                    e.printStackTrace();
    
                }
    
            }
    
            new File(imagePath).delete();
        }
    
    
    
    
        private SparseArray<com.google.android.gms.vision.face.Face> detectFaces(Bitmap image , Context context) {
    
            int h = image.getHeight();
            int w = image.getWidth();
            int max = 10;
            Frame frame = new Frame.Builder().setBitmap(image).build();
            com.google.android.gms.vision.face.FaceDetector detector = new com.google.android.gms.vision.face.FaceDetector.Builder(context)
                    .setTrackingEnabled(false)
                    .setLandmarkType(com.google.android.gms.vision.face.FaceDetector.ALL_LANDMARKS)
                    .build();
            SparseArray<com.google.android.gms.vision.face.Face> faces = detector.detect(frame);
            detector.release();
            return  faces;
    
    
    
    
        }
    
        private FaceDetector.Face[] detectFaces(Bitmap image ) {
    
            int h = image.getHeight();
            int w = image.getWidth();
            int max = 10;
    
            FaceDetector detector = new FaceDetector(w, h, max);
            FaceDetector.Face[] faces = new FaceDetector.Face[max];
    
    
            int facesFound = detector.findFaces(image, faces);
            return  faces;
    
    
        }
    
    
    }