Eye Blink Counting Detection OpenCV Python With Source Code

This Eye Blink Counting Detection OpenCV Python was developed using Python OpenCV with Source Code.

We are going to build upon this knowledge and develop a computer vision application that is capable of detecting and counting blinks in video streams using facial landmarks and OpenCV.

An Eye Blink Detection OpenCV Python focused solely on using the eye aspect ratio as a quantitative metric to determine if a person has blinked in a video stream.

However, due to noise in a video stream, subpar facial landmark detections, or fast changes in viewing angle, a simple threshold on the eye aspect ratio could produce a false-positive detection, reporting that a blink had taken place when in reality the person had not blinked.

What is OpenCV?

OpenCV is short for Open Source Computer Vision. Intuitively by the name, it is an open-source Computer Vision and Machine Learning library.

This library is capable of processing real-time images and videos while also boasting analytical capabilities. It supports the Deep Learning frameworks.

In this Python OpenCV Project also includes a downloadable Python Project With Source Code for free, just find the downloadable source code below and click to start downloading.

By the way, if you are new to Python programming and don’t know what Python IDE to use, I have here a list of Best Python IDE for Windows, Linux, Mac OS that will suit you. I also have here How to Download and Install Latest Version of Python on Windows.

To start executing this project, make sure that you have installed Python 3.9 and PyCharm in your computer.

Time needed: 5 minutes

These are the steps on how to run Eye Blink Counting Detection OpenCV Python With Source Code

  • Step 1: Download the given source code below.

    First, download the given source code below and unzip the source code.
    eye blink count OpenCV download source code

  • Step 2: Import the project to your PyCharm IDE.

    Next, import the source code you’ve download to your PyCharm IDE.
    eye blink count OpenCV open project

  • Step 3: Run the project.

    last, run the project with the command “py main.py”
    eye blink count OpenCV run project

Installed Libraries

import cv2 
import dlib 
from scipy.spatial import distance
from imutils import face_utils

Complete Source Code

import cv2 
import dlib 
from scipy.spatial import distance
from imutils import face_utils

cap = cv2.VideoCapture(0)

detector = dlib.get_frontal_face_detector()
predictor = dlib.shape_predictor('shape_predictor_68_face_landmarks.dat')

def eye_aspect_ratio(eye):
	A = distance.euclidean(eye[1], eye[5])
	B = distance.euclidean(eye[2], eye[4])

	C = distance.euclidean(eye[0], eye[3])
	eye = (A + B) / (2.0 * C)

	return eye

count = 0
total = 0

while True:
    success,img = cap.read()
    imgGray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
    faces = detector(imgGray)

    for face in faces:
        landmarks = predictor(imgGray,face)

        landmarks = face_utils.shape_to_np(landmarks)
        leftEye = landmarks[42:48]
        rightEye = landmarks[36:42]

        leftEye = eye_aspect_ratio(leftEye)
        rightEye = eye_aspect_ratio(rightEye)

        eye = (leftEye + rightEye) / 2.0

        if eye<0.3:
            if count>=3:

    cv2.putText(img, "Blink Count: {}".format(total), (10, 30),cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 255), 2)
    if cv2.waitKey(1) & 0xff==ord('q'):


Eye Blink Count Detection OpenCV Python Output

Eye Blink Counting Detection OpenCV Python: Project Information

Project Name:Eye Blink Counting Detection OpenCV Python
Language/s Used:Python OpenCV
Python version (Recommended):3.8
Type:Deep Learning
Eye Blink Counting Detection OpenCV Python – Project Information

Download the Source Code below

Anyway, if you want to level up your programming knowledge, especially Python OpenCV, try this new article I’ve made for you Best OpenCV Projects With Source Code For Beginners.


In this article, I demonstrated how to execute a blink detector using OpenCV, Python, and dlib.

The first step in building a blink detector is to perform facial landmark detection to localize the eyes in a given frame from a video stream.

Once we have the facial landmarks for both eyes, we compute the eye aspect ratio for each eye, which gives us a singular value, relating the distances between the vertical eye landmark points to the distances between the horizontal landmark points.

Once we have the eye aspect ratio, we can threshold it to determine if a person is blinking — the eye aspect ratio will remain approximately constant when the eyes are open and then will rapidly approach zero during a blink, then increase again as the eye opens.


If you have any questions or suggestions about Eye Blink Counting Detection OpenCV Python With Source Code, please feel free to leave a comment below.

Leave a Comment