使用 Google 视觉检测眨眼 API

Detect Eye Blink Using Google Vision API

我正在使用视觉 API 进行面部检测,现在我想实现眨眼但是 静止视觉 api 在一只眼睛关闭时检测眼睛。

请帮助我如何实现眨眼功能。

此答案假定您已经拥有检测人脸的代码运行。

Face class 有函数 float getIsLeftEyeOpenProbability()float getIsRightEyeOpenProbability(),你可以在每一帧上使用它们来判断其中一只眼睛是否眨了,如果其中一只函数 returns 一个大值,另一个 returns 一个较小的值。

可以找到 Face class 的官方文档 here

面部的 "eye open probability" 值是检测眨眼的关键。此外,您可以使用跟踪器来跟踪一段时间内的眼睛状态,以检测指示眨眼的事件序列:

双眼睁开->双眼闭上->双眼睁开

这是一个示例跟踪器:

public class BlinkTracker extends Tracker<Face> {
  private final float OPEN_THRESHOLD = 0.85;
  private final float CLOSE_THRESHOLD = 0.15;

  private int state = 0;

  public void onUpdate(Detector.Detections<Face> detections, Face face) {
    float left = face.getIsLeftEyeOpenProbability();
    float right = face.getIsRightEyeOpenProbability();
    if ((left == Face.UNCOMPUTED_PROBABILITY) ||
        (right == Face.UNCOMPUTED_PROBABILITY)) {
      // At least one of the eyes was not detected.
      return;
    }

    switch (state) {
      case 0:
        if ((left > OPEN_THRESHOLD) && (right > OPEN_THRESHOLD)) {
          // Both eyes are initially open
          state = 1;
        }
        break;

        case 1:
          if ((left < CLOSE_THRESHOLD) && (right < CLOSE_THRESHOLD)) {
            // Both eyes become closed
            state = 2;
          }
          break;

        case 2:
          if ((left > OPEN_THRESHOLD) && (right > OPEN_THRESHOLD)) {
            // Both eyes are open again
            Log.i("BlinkTracker", "blink occurred!");
            state = 0;
          }
        break;
    }
  }

}

请注意,您还需要启用 "classifications" 才能让检测器指示眼睛是否 open/closed:

FaceDetector detector = new FaceDetector.Builder(context)
    .setClassificationType(FaceDetector.ALL_CLASSIFICATIONS)
    .build();

然后添加跟踪器作为处理器,用于随着时间的推移从检测器接收面部更新。例如,此配置将用于跟踪视野中最大的人脸是否眨眼:

detector.setProcessor(
    new LargestFaceFocusingProcessor(detector, new BlinkTracker()));

或者,如果您有兴趣检测所有面孔(而不仅仅是最大的面孔)的眨眼,您可以使用多处理器而不是 LargestFaceFocusingProcessor。

here 是我使用 FaceDetectorAPi() 进行眼睛检测的植入应用程序,经测试其准确率超过 90%

private double leftEyeOpenProbability = -1.0;
private double rightEyeOpenProbability = -1.0;

private boolean isEyeBlinked(){

    if(mFaces.size()==0)
        return false;

    Face face = mFaces.valueAt(0);
    float currentLeftEyeOpenProbability = face.getIsLeftEyeOpenProbability();
    float currentRightEyeOpenProbability = face.getIsRightEyeOpenProbability();
    if(currentLeftEyeOpenProbability== -1.0 || currentRightEyeOpenProbability == -1.0){
        return false;
    }

    if(leftEyeOpenProbability>0.9 || rightEyeOpenProbability > 0.9){
        boolean blinked = false;
        if(currentLeftEyeOpenProbability<0.6 || rightEyeOpenProbability< 0.6){
            blinked = true;
        }
        leftEyeOpenProbability = currentLeftEyeOpenProbability;
        rightEyeOpenProbability = currentRightEyeOpenProbability;
        return blinked;
    }else{
        leftEyeOpenProbability = currentLeftEyeOpenProbability;
        rightEyeOpenProbability = currentRightEyeOpenProbability;
        return false;
    }
}