What's new

Python Eye Blink Counting Detection in OpenCV Python [FREE SOURCE CODE]

unGuardable

Forum Veteran
Elite
Joined
Aug 8, 2015
Posts
508
Solutions
1
Reaction
7,044
Points
933

Eye Blink Counting Detection OpenCV Python With Source Code​

This 2022 Eye Blink Counting Detection OpenCV Python was developed using Python OpenCV, Python OpenCV Project 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.

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 you don’t know what would be the Python IDE to use, I have here a list of the You do not have permission to view the full content of this post. Log in or register now. that will suit you. I also have here You do not have permission to view the full content of this post. Log in or register now..

To start executing Eye Blink Counting Detection OpenCV Python With Source Code, make sure that you have installed You do not have permission to view the full content of this post. Log in or register now. 3.9 and PyCharm on your computer.

Complete Codes​

Python:
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:
            count+=1
        else:
            if count>=3:
                total+=1

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

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
Database:None
Type:Deep Learning
Developer:IT SOURCECODE
Updates:0

Download 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 You do not have permission to view the full content of this post. Log in or register now..

You do not have permission to view the full content of this post. Log in or register now.

Output

You do not have permission to view the full content of this post. Log in or register now.

Summary

In this article post, 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.
 
Last edited:
Back
Top