如果眼睑闭合超过3秒发出警报

时间:2019-03-12 05:26:02

标签: android firebase firebase-mlkit

我要在用户的眼睑闭合3秒以上后发出警报,但是我的代码有问题。在关闭警报3秒后,如何使boolean right_eye和boolean left_eye触发警报?这是我写的一些代码。

public void eyeTracking(FirebaseVisionFace face){
    boolean right_eye = face.getRightEyeOpenProbability() < 0.5;
    boolean left_eye =  face.getLeftEyeOpenProbability() < 0.5;
    Handler mHandler = new Handler();
    Runnable runnable = new Runnable() {
        @Override
        public void run() {

        }
    };

    if (right_eye && left_eye) {
        begin = System.currentTimeMillis();
        Log.d(TAG, "eyeTracking: " + sleep);
        mHandler.postDelayed(runnable, 3000);
        sleep = true;
    }
    else {
        sleep = false;
    }

    if(sleep){
        stop = System.currentTimeMillis();
        Log.d(TAG, "eyeTracking: " + begin + "stop" + stop);
        if(begin - stop >= 3000) {
            alertBox();
        }
    }
}

当我尝试记录我的begin和stop变量时,它返回相同的值 ?我做错了吗?

1 个答案:

答案 0 :(得分:1)

您不应在每次检测到眼睛闭合时都设置begin变量。在您的项目上尝试下面的代码。希望这对您有帮助:

  long begin=0;
  public void eyeTracking(FirebaseVisionFace face){
    boolean right_eye = face.getRightEyeOpenProbability() < 0.5;
    boolean left_eye =  face.getLeftEyeOpenProbability() < 0.5;

    if (right_eye && left_eye) {
        //if your begin variable is reset
        if(begin==0){
        begin = System.currentTimeMillis();
        }
        Log.d(TAG, "eyeTracking: " + sleep);
        sleep = true;
    }
    else {
        //reset your begin variable
        begin=0;
        sleep = false;
    }

    Log.d(TAG, "Eyes closed time: "+System.currentTimeMillis()-begin);
    if(sleep && System.currentTimeMillis()-begin>3000){
        Log.d(TAG, "Show alert");
            alertBox();
    }
}