Skip to content

The program can detect if the person is sleepy or not using the Dlib library. If someone is closing his eyes so the program will calculate the aspect ratio of eyes and give the conclusion that the person fell asleep at the specified time

Notifications You must be signed in to change notification settings

Michael-BJ/Blink-Detection-Dlib

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 

Repository files navigation

How it's works

  1. 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
  1. 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()
  1. Make the program to target or detect the face
detector = dlib.get_frontal_face_detector() 
  1. Insert the libary that we need
predictor = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat") 
  1. 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
  1. Calculate the ratio of the eye using the coordinates/indexes that have been obtained
EAR = (A + B) / (2.0 * C)`
  1. 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
  1. Change the BGR to the grayscale
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
  1. Detect the face in the grayscale image
faces = detector(gray)
  1. Convert the coordinates/indexes of facial landmark to a numpy
point = predictor(gray, face)
points = face_utils.shape_to_np(point)
  1. Convert the coordinates of the right and left eyes using numpy array
leftEye = points[lStart:lEnd]
rightEye = points[rStart:rEnd]
  1. Calculate the eye aspect ratio
leftEAR = calculate_EAR(leftEye)
rightEAR = calculate_EAR(rightEye)
EAR = (leftEAR + rightEAR) / 2.0

Demo

Clik the picture to see the Video Watch the video

About

The program can detect if the person is sleepy or not using the Dlib library. If someone is closing his eyes so the program will calculate the aspect ratio of eyes and give the conclusion that the person fell asleep at the specified time

Topics

Resources

Stars

Watchers

Forks

Languages