Search code examples
androidgoogle-apiface-detection

Unable to detect different images for face detection Mobile Vision API


I am using Mobile Vision API in my app for face detection so far I have been successful in doing so. It works fine for a particular image which I have set when I was running the app for the first time but after that.. I have tried to replace different images for face detection it gives the error java.lang.OutOfMemoryError

Following is my code

      Bitmap myBitmap;
      FaceDetector detector;
      @Override
      protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
           //Load An Image////
      BitmapFactory.Options options = new BitmapFactory.Options();
      options.inMutable=true;
       Bitmap b =  = BitmapFactory.decodeResource(
       getApplicationContext().getResources(),
       R.drawable.image,options);
        myBitmap = b.copy(Bitmap.Config.RGB_565, true);
        b.recycle();
        ////////////////
        detector = new FaceDetector.Builder(getApplicationContext())
        .setTrackingEnabled(false)
         .setLandmarkType(FaceDetector.ALL_LANDMARKS)
            .build();
       if (!detector.isOperational())
              {    Toast.makeText(MainActivity.this, "NO face deteted."Toast.LENGTH_SHORT)
          .show();} 
            else {
          Frame frame = new  Frame.Builder().setBitmap(myBitmap).build();
                  SparseArray<com.google.android.gms.vision.face.Face> faces =                    detector.detect(frame);
                 FaceView faceView = (FaceView) findViewById(R.id.faceView);
                 faceView.setContent(myBitmap, faces);
       }

here is my logcat

                     java.lang.OutOfMemoryError
                      at com.google.android.gms.vision.Frame.zzEx(Unknown Source)
                        at   com.google.android.gms.vision.Frame.getGrayscaleImageData(Unknown    Source)
        at com.google.android.gms.vision.face.FaceDetector.detect(Unknown Source)
        at com.chat.elearnplayer.mobilevisionapi.MainActivity.onCreate(MainActivity.java:43)
        at android.app.Activity.performCreate(Activity.java:5296)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2283)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2370)
        at android.app.ActivityThread.access$800(ActivityThread.java:155)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1243)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:136)
        at android.app.ActivityThread.main(ActivityThread.java:5426)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084)
        at dalvik.system.NativeStart.main(Native Method)

... Need your help ...


Solution

  • Try this

    Bitmap myBitmap;
    FaceDetector detector;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //Load An Image////
    
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inMutable=true;
        Bitmap b =  = BitmapFactory.decodeResource(
        getApplicationContext().getResources(),
        R.drawable.image,options);
        myBitmap = b.copy(Bitmap.Config.RGB_565, true);
        b.recycle();
        //////////////////
        detector = new FaceDetector.Builder(getApplicationContext())
                .setTrackingEnabled(false)
                .setLandmarkType(FaceDetector.ALL_LANDMARKS)
                .build();
    
        Detector<Face> safeDetector = new SafeFaceDetector(detector);
    
        Frame frame = new Frame.Builder().setBitmap(myBitmap).build();
        SparseArray<com.google.android.gms.vision.face.Face> faces = safeDetector.detect(frame);
    
        if (!safeDetector.isOperational()) {
            Toast.makeText(MainActivity.this, "NO face deteted."Toast.LENGTH_SHORT)
                    .show();
        } else {
    
            FaceView faceView = (FaceView) findViewById(R.id.faceView);
            faceView.setContent(myBitmap, faces);
    
            safeDetector.release();
        }
    }
    

    for reuse facedetector release(); must be called after use .

    and for SafeDetector usage : face detector have a bug for small images and safeDetector is patch for that bug

    add this line at activity imports

    import com.google.android.gms.samples.vision.face.patch.SafeFaceDetector;