Reputation: 49
I have the following code and the problem is that the variable numbOfBlinks
is incremented by four each time. I'm assuming that the onUpdate method is being called 4 times per second. My goal is to count the number of times a user blinks. I'm not sure the best way to do it.
@Override
public void onUpdate(FaceDetector.Detections<Face> detectionResults, Face face) {
mOverlay.add(mFaceGraphic);
mFaceGraphic.updateFace(face);
if (flag == 0) {
eye_tracking(face);
}
}
private void eye_tracking(Face face) {
float leftEye = face.getIsLeftEyeOpenProbability();
float rightEye = face.getIsRightEyeOpenProbability();
if (leftEye > THRESHOLD && rightEye > THRESHOLD) {
state_i = 0;
firstTime = true;
firstTimeSMS = true;
Log.i(TAG, "onUpdate: Open Eyes Detected");
} else {
if (state_i == 0) {
lastLowToHighState = SystemClock.elapsedRealtime();
lastLowToHighStateSMS = SystemClock.elapsedRealtime();
}
if (firstTime && SystemClock.elapsedRealtime() - lastLowToHighState > alarmTime) {
// Used to prevent the function from continuously firing
lastLowToHighState = SystemClock.elapsedRealtime();
firstTime = false;
alert_box();
flag = 1;
}
if (firstTimeSMS && SystemClock.elapsedRealtime() - lastLowToHighStateSMS > smsTime) {
try {
sendSMS();
} catch (Exception e) {
e.printStackTrace();
}
lastLowToHighStateSMS = SystemClock.elapsedRealtime();
firstTimeSMS = false;
}
numbOfBlinks++;
state_i = 1;
Log.d(TAG, "onUpdate: Closed Eyes Detected");
}
}
EDIT after Alexander M code
@Override
public void onUpdate(FaceDetector.Detections<Face> detectionResults, Face face) {
mOverlay.add(mFaceGraphic);
mFaceGraphic.updateFace(face);
if (flag == 0) {
eye_tracking(face);
}
}
private void onEyesClosed() {
eyesClosed = true;
}
private void onEyesOpened() {
if (eyesClosed) {
numbOfBlinks++;
Log.i(TAG,"numb of blinks"+ numbOfBlinks);
}
eyesClosed = false;
}
private void eye_tracking(Face face) {
float leftEye = face.getIsLeftEyeOpenProbability();
float rightEye = face.getIsRightEyeOpenProbability();
if (leftEye > THRESHOLD && rightEye > THRESHOLD) {
state_i = 0;
firstTime = true;
firstTimeSMS = true;
onEyesOpened();
//Log.i(TAG, "onUpdate: Open Eyes Detected");
} else {
onEyesClosed();
if (state_i == 0) {
lastLowToHighState = SystemClock.elapsedRealtime();
lastLowToHighStateSMS = SystemClock.elapsedRealtime();
}
if (firstTime && SystemClock.elapsedRealtime() - lastLowToHighState > alarmTime) {
// Used to prevent the function from continuously firing
lastLowToHighState = SystemClock.elapsedRealtime();
firstTime = false;
alert_box();
flag = 1;
}
if (firstTimeSMS && SystemClock.elapsedRealtime() - lastLowToHighStateSMS > smsTime) {
try {
sendSMS();
} catch (Exception e) {
e.printStackTrace();
}
lastLowToHighStateSMS = SystemClock.elapsedRealtime();
firstTimeSMS = false;
}
numbOfBlinks++;
state_i = 1;
//Log.d(TAG, "onUpdate: Closed Eyes Detected");
}
}
Upvotes: 0
Views: 49
Reputation: 433
If I understand your code correctly, you already have a way to detect opened and closed eyes. Provided that, I'd recommend you to add two methods to your class: onEyesClosed()
and onEyesOpened()
. Call them from your eye_tracking(Face face)
method (both methods can be called many times in a row, that doesn't matter). Now, let's build "blink detection logic" based on there two methods:
private boolean eyesClosed;
private int numbOfBlinks;
private void onEyesClosed() {
eyesClosed = true;
}
private void onEyesOpened() {
if (eyesClosed) {
numbOfBlinks++;
}
eyesClosed = false;
}
Upvotes: 1