Python – Eye blink detection project

In this tutorial you will learn about detecting a blink of human eye with the feature mappers knows as haar cascades. Here in the project, we will use the python language along with the OpenCV library for the algorithm execution and image processing respectively. The haar cascades we are going to use in the project are pretrained and stored along with the OpenCV library as haarcascade_frontalface_default.xml and haarcascade_eye_tree_eyeglasses.xml files. The project develops a basic understanding of the systems such as driver drowsiness detection, eye blink locks, eye detection, face detection and also the haar cascades usage with the OpenCV library.

About Haar Cascades:
Haar feature-based cascade classifiers is an effective object detection method proposed by Paul Viola and Michael Jones in their paper, “Rapid Object Detection using a Boosted Cascade of Simple Features” in 2001. It is a machine learning-based approach where a cascade function is trained from a lot of positive and negative images. Here positive images are the samples which contain the target object and negative are the ones which don’t. It takes a lot of positive and negative samples to train the classifier.

Haar features , source – OpenCV docs

Now, we extract the features from the given input image with the haar features shown in the above image. They are just like convolutional kernels. Each feature is a single value obtained by subtracting the sum of pixels under the white rectangle from the sum of pixels under the black rectangle.

Application of haarcascades, source – OpenCV Docs

The Excessive calculation:
With all the possible sizes of the classifiers the features are calculated, but the amount of computation it takes to calculate the features, a 24×24 window results over 160000 features. Also for each feature calculation, the sum of the pixels is also needed. To make it computationally less expensive the creators of haar cascades introduced the integral image, which means however large your image, it reduces the calculations for a given pixel to an operation involving just four pixels.

The false features
Now among the features that are calculated, most of the features are false and irrelevant. Now the window which is applied to a region of the image may see a different region which seems with the same features to the window but is not in reality. So, there is a need to remove the false features which were done by the AdaBoost which helped select the best features out of 160000+ features. Adaboost short form of Adaptive Boosting is a Machine learning algorithm which was used for this sole task.

Algorithm :

  1. The frame is captured and converted to grayscale.
  2. Bilateral Filtering is applied to remove impurities.
  3. Face is detected with the haarcascade.
  4. The ROI (Region Of Image) of Face is fed to eye detection part of algorithm.
  5. Eyes are detected and resulting list is passed to if-else contruct.
  6. If the length of list is more than two, means that the eyes are there.
  7. Else the program is marked to be eye blinked and restarted.

The haarcascade_frontalface_default.xml and haarcascade_eye_tree_eyeglasses.xml are the xml files stored in the same directory as the python script.





#All the imports go here
import numpy as np
import cv2
#Initializing the face and eye cascade classifiers from xml files
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
eye_cascade = cv2.CascadeClassifier('haarcascade_eye_tree_eyeglasses.xml')
#Variable store execution state
first_read = True
#Starting the video capture
cap = cv2.VideoCapture(0)
ret,img =
    ret,img =
    #Coverting the recorded image to grayscale
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    #Applying filter to remove impurities
    gray = cv2.bilateralFilter(gray,5,1,1)
    #Detecting the face for region of image to be fed to eye classifier
    faces = face_cascade.detectMultiScale(gray, 1.3, 5,minSize=(200,200))
        for (x,y,w,h) in faces:
            img = cv2.rectangle(img,(x,y),(x+w,y+h),(0,255,0),2)
            #roi_face is face which is input to eye classifier
            roi_face = gray[y:y+h,x:x+w]
            roi_face_clr = img[y:y+h,x:x+w]
            eyes = eye_cascade.detectMultiScale(roi_face,1.3,5,minSize=(50,50))
            #Examining the length of eyes object for eyes
                #Check if program is running for detection 
                    "Eye detected press s to begin"
                    cv2.FONT_HERSHEY_PLAIN, 3,
                    "Eyes open!", (70,70), 
                    cv2.FONT_HERSHEY_PLAIN, 2,
                    #To ensure if the eyes are present before starting
                    "No eyes detected", (70,70),
                    cv2.FONT_HERSHEY_PLAIN, 3,
                    #This will print on console and restart the algorithm
                    print("Blink detected--------------")
        "No face detected",(100,100),
        cv2.FONT_HERSHEY_PLAIN, 3
    #Controlling the algorithm with keys
    a = cv2.waitKey(1)
    elif(a==ord('s') and first_read):
        #This will start the detection
        first_read = False


Sample Output:

Sample run of above code

The Source code and the cascade classifiers can be found here.

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.

My Personal Notes arrow_drop_up

A computer Vision and Machine Learning enthusiast who want to contribute to the society in best possible ways, painting the globe white

If you like GeeksforGeeks and would like to contribute, you can also write an article using or mail your article to See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.