androidcomputer-visionandroid-cameraandroid-visioneye-detection

getIsLeftEyeOpenProbability from mobile vision API gives a vlaue of -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?


Solution

  • 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
    }