无法检测不同图像以进行人脸检测 Mobile Vision API

Unable to detect different images for face detection Mobile Vision API

我在我的应用程序中使用 Mobile Vision API 进行人脸检测,到目前为止我已经成功地这样做了。 它适用于我第一次使用 运行 应用程序时设置的特定图像,但在那之后..我尝试替换不同的图像进行人脸检测,但它给出了错误 java.lang.OutOfMemoryError

以下是我的代码

      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);
       }

这是我的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.access0(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)

...需要您的帮助...

试试这个

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();
    }
}

为了重用facedetector release();必须在使用后调用。

SafeDetector 用法:人脸检测器有一个小图像错误,safeDetector 是该错误的补丁

在 activity 导入处添加此行

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