Eye blink detection with OpenCV, Python, and dlib
In this article, we are going to see how to detect eye blink using OpenCV, Python, and dlib. This is a fairly simple task and it requires you to have a basic understanding of OpenCV and how to implement face landmark detection programs using OpenCV and dlib, since we’ll be using that as the base for today’s project.
Step 1: Installing all required packages
So we’ll install all our dependencies in this step. We’re going to use OpenCV for computer vision, dlib library for facial recognition, and also the imutils package to use some functions that will help us convert the landmarks to NumPy array and make it easy for us to use, so let’s install these first.
pip install opencv-python numpy dlib imutils
Step 2: Initialize and read from the webcam
Step 3: Facial Landmark Detection using dlib
Note: The facial landmark detector included in the dlib library is an implementation of the One Millisecond Face Alignment with an Ensemble of Regression Trees paper by Kazemi and Sullivan (2014).
Facial landmarks are the key attributes of a face in an image like eyes, eyebrows, nose, mouth, and Jaw. Since Steps 1 – 3 is not the primary focus of this article so we won’t go in-depth, but instead, I’ll write comments on the code for easy understanding.
Here is the code basic code for facial landmark detection, that we’ll be using later for eye blink detection.
Now the question arises, how are we going to use these landmarks for eye detection.
We saw that we can extract any facial structure from the 68 Facial Landmarks that we detected. So, we’ll extract the landmarks of the eyes i.e 6 (x,y) coordinates for each eye, for any given face in an image. And then we’ll calculate the EAR for these landmarks.
Eye Aspect Ratio (EAR)
This method is very simple, efficient, and doesn’t require anything like image processing. Basically, this ratio gives us a certain relation between the horizontal and vertical measurements of the eye. This is the equation to calculate the EAR using the six parameters of the eye :
We can use the given function to calculate the EAR :
What’s so magical about this EAR?
This is the most important part, when you calculate the EAR of an eye, it remains constant when the eye is open but it suddenly drops when the eye is blinked. Below, I have shown a graph to show it’s working:
As you can see in the image the overall value of EAR was constant throughout except at one point i.e when the eye is blinked, making it one of the most simple and most efficient ways of detecting an eye blink.
Since we have two EAR for each eye respectively we’ll take the average of both the EAR for the right eye and the EAR for the left eye and then check if it is lower than a certain threshold ( we’ll create a variable to set its value) and this threshold might vary a bit, for me it worked with 0.4 or 0.5 but in some cases, it works with 0.25 or 0.3 as well. It depends on the FPS of your video or webcam.
Next: We’ll keep the count of the frames when the EAR is lower than the threshold and if the count is 3 (or 5 depending on the fps) frames then we’ll consider a blink detected.
Below is the full implementation
If you’re using a different video or if you’re using a webcam you’re FPS is going to be different, so might wanna trying changing the values of the variables we defined, although they work fine in most cases.