1

I'm using getIsLeftEyeOpenProbability from mobile vision API to know if the eye is open or not. However, something strange happens, I always get the probability as -1 even if the eye is opened.

Here is the code:

FaceDetector faceDetector = new FaceDetector.Builder(getApplicationContext())
                                            .setTrackingEnabled(false)
                                            .setLandmarkType(FaceDetector.ALL_LANDMARKS)
                                            .build();

Frame frame = new Frame.Builder().setBitmap(obtainedBitmap).build();
SparseArray < Face > facesForLandmarks = faceDetector.detect(frame);
faceDetector.release();
Thread homeSwipeThread;

for (int a = 0; a < facesForLandmarks.size(); a++) {
    Face thisFace = facesForLandmarks.valueAt(a);
    List < Landmark > landmarks = thisFace.getLandmarks();

    for (int b = 0; b < landmarks.size(); b++) {
        if (landmarks.get(b).getType() == landmarks.get(b).LEFT_EYE) {
            leftEye = new Point(landmarks.get(b).getPosition().x, landmarks.get(b).getPosition().y - 3);
        } else if (landmarks.get(b).getType() == landmarks.get(b).RIGHT_EYE) {
            rightEye = new Point(landmarks.get(b).getPosition().x, landmarks.get(b).getPosition().y - 3);
        } //end else if.
    } //end inner
    //for every detected face check eyes probability:

    if (thisFace.getIsLeftEyeOpenProbability() <= 0.1) {
        //some code
    }
 }

Why does this happen, and how can I solve it?

Chris Stillwell
  • 10,266
  • 10
  • 67
  • 77
Dania
  • 1,648
  • 4
  • 31
  • 57

1 Answers1

4

You are missing the detector option for classifying eyes open/closed, via "setClassificationType". The faceDetector should be created like this:

FaceDetector faceDetector =
    new FaceDetector.Builder(getApplicationContext())
        .setTrackingEnabled(false)
        .setLandmarkType(FaceDetector.ALL_LANDMARKS)
        .setClassificationType(FaceDetector.ALL_CLASSIFICATIONS)
        .build();

You could omit "setLandmarkType" in this case, since it is an implied dependency for "setClassificationType".

Also, even with this option set, it's possible to get -1, which is the "UNCOMPUTED_PROBABILITY" value mentioned in the docs:

https://developers.google.com/android/reference/com/google/android/gms/vision/face/Face.html#public-methods

Getting UNCOMPUTED_PROBABILITY back usually means that the eye was not detected, so it isn't possible to determine if the eye is open or closed. So I think you want this instead:

float leftOpen = thisFace.getIsLeftEyeOpenProbability();
if ((leftOpen != Face.UNCOMPUTED_PROBABILITY) && (leftOpen <= 0.1)) {
    //some code
}
pm0733464
  • 2,862
  • 14
  • 16
  • Thank you for this great explanation. But this line boolean isLeftOpen = thisFace.getIsLeftEyeOpenProbability(); gives me error because float cannot be converted to boolean, can you please clarify what do you mean by this line? Or, did you mean that isLeftOpen is float? – Dania Apr 01 '16 at 20:33
  • 1
    That was a typo. I fixed it. – pm0733464 Apr 01 '16 at 21:50