This article was published as a part of the Data Science Blogathon.
In this article, we will learn how to make a real-time blink detector application using computer vision. Also, we will be using more libraries and mathematics to build such an application while going through a complete pipeline and code-by-code analysis.
1) Driver drowsiness detection: As the name suggests, this application is very useful in building real-world applications like detecting whether the driver is sleepy or not while detecting the eye moment or blink.
2) Iris tracking: This is another use case while we can also track the iris movement for building an AR kind of application.
3) Virtual gaming: We are in the age of virtual reality evolution, and by far, mostly VR-powered games are either hand or body movement driven, but we can also build eye movement driven games.
Let’s Start by Importing the Required Libraries
from scipy.spatial import distance as dist from imutils.video import FileVideoStream from imutils.video import VideoStream from imutils import face_utils import numpy as np import imutils import time import dlib import cv2
The functionality of each library:
def eye_aspect_ratio(eye): A = dist.euclidean(eye[1], eye[5]) B = dist.euclidean(eye[2], eye[4]) C = dist.euclidean(eye[0], eye[3]) ear = (A + B)/ (2.0 * C) return ear
Code breakdown:
1) First, we will get the Euclidean distance between the 2 coordinates of the eyes.
2) While grabbing the coordinates, we will first have the vertical eye landmarks.
3) Then we will have the horizontal eye landmarks using the same algorithm.
4) After grabbing the coordinates, we will calculate the Eye Aspect ratio.
5) Then, at last, we will return the EAR.
Define Constants
EYE_AR_THRESH = 0.3 EYE_AR_CONSEC_FRAMES = 3
Initializing the Variables
COUNTER = 0 TOTAL = 0
print("Loading the dlib's face detector") detector = dlib.get_frontal_face_detector() predictor = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat")
Output:
Loading the dlib's face detector
Get the Index of Facial Landmarks (Eye)
(lStart, lEnd) = face_utils.FACIAL_LANDMARKS_IDXS["left_eye"] (rStart, rEnd) = face_utils.FACIAL_LANDMARKS_IDXS["right_eye"]
Loading the Video/Real-time Streaming
print("Starting the video/live stteaming") vs = FileVideoStream("Video.mp4").start() fileStream = True # vs = VideoStream(src = 0).start() # run this line if you want to run it on webcam. # vs = VideoStream(usePiCamera = True).start() fileStream = False time.sleep(1.0)
Starting the video/live stteaming
Code breakdown:
Main Logic
while True: if fileStream and not vs.more(): break frame = vs.read() frame = imutils.resize(frame, width = 450) gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) rects = detector(gray, 0) for rect in rects: shape = predictor(gray, rect) shape = face_utils.shape_to_np(shape) leftEye = shape[lStart:lEnd] rightEye = shape[rStart:rEnd] leftEAR = eye_aspect_ratio(leftEye) rightEAR = eye_aspect_ratio(rightEye) ear = (leftEAR + rightEAR) / 2.0 leftEyeHull = cv2.convexHull(leftEye) rightEyeHull = cv2.convexHull(rightEye) cv2.drawContours(frame, [leftEyeHull], -1, (0, 255, 0), 1) cv2.drawContours(frame, [rightEyeHull], -1, (0, 255, 0), 1) if ear < EYE_AR_THRESH: COUNTER += 1 else: if COUNTER >= EYE_AR_CONSEC_FRAMES: TOTAL += 1 #reset the eye frame counter COUNTER = 0 cv2.putText(frame, "Blinks:{}".format(TOTAL), (10, 30), cv2.FONT_HERSHEY_COMPLEX, 0.7, (0, 0, 255), 2) cv2.putText(frame, "EAR:{:.2f}".format(ear), (300, 30), cv2.FONT_HERSHEY_COMPLEX, 0.7, (0, 0, 255), 2) cv2.imshow("Frame", frame) key = cv2.waitKey(12) & 0xFF if key == ord("q"): break cv2.destroyAllWindows() vs.stop()
Output:
Code breakdown:
Here’s the repo link to this article. Hope you liked my article on the Blink detection application using computer vision. If you have any opinions or questions, then comment below.
Read on AV Blog about various predictions using Machine Learning.
The media shown in this article is not owned by Analytics Vidhya and are used at the Author’s discretion.