- First import the libaray that we need
import cv2
import numpy as np
import dlib
from playsound import playsound
from imutils import face_utils
from scipy.spatial import distance as dist
- Connect the webcam to the program
cap = cv2.VideoCapture(0)
while True:
_, frame = cap.read()
cv2.imshow('Blink detection', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
- Make the program to target or detect the face
detector = dlib.get_frontal_face_detector()
- Insert the libary that we need
predictor = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat")
- Calculate the coordinates/indexes for the vertical eye and horizontal eye (x,y)
def calculate_EAR(eye):
A = dist.euclidean(eye[1], eye[5]) # for horizontal
B = dist.euclidean(eye[2], eye[4]) # for horizontal
C = dist.euclidean(eye[0], eye[3]) # for vertikal
- Calculate the ratio of the eye using the coordinates/indexes that have been obtained
EAR = (A + B) / (2.0 * C)`
- Find the index of right and left eye
(lStart, lEnd) = face_utils.FACIAL_LANDMARKS_IDXS["left_eye"]
(rStart, rEnd) = face_utils.FACIAL_LANDMARKS_IDXS["right_eye"]
8.Determine the eye aspect ratio for sleepy and the number of frames the person has closed their eye
counter = 0
eyes_ear = 0.2
eyes_per_frame = 48
- Change the BGR to the grayscale
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
- Detect the face in the grayscale image
faces = detector(gray)
- Convert the coordinates/indexes of facial landmark to a numpy
point = predictor(gray, face)
points = face_utils.shape_to_np(point)
- Convert the coordinates of the right and left eyes using numpy array
leftEye = points[lStart:lEnd]
rightEye = points[rStart:rEnd]
- Calculate the eye aspect ratio
leftEAR = calculate_EAR(leftEye)
rightEAR = calculate_EAR(rightEye)
EAR = (leftEAR + rightEAR) / 2.0