Advertisement
FaisalAhemdBijoy

face_eye_blinking_detection

Sep 22nd, 2022
1,060
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.10 KB | Source Code | 0 0
  1. '''
  2. Face eye blinking using Dlib  and detect left or right eye blinking.
  3. check sleep, drowsy, active
  4. '''
  5.  
  6. import cv2
  7. import dlib
  8. import numpy as np
  9. from imutils import face_utils
  10.  
  11. def eye_aspect_ratio(eye):
  12.     # compute the euclidean distances between the two sets of
  13.     # vertical eye landmarks (x, y)-coordinates
  14.     A = np.linalg.norm(eye[1] - eye[5])
  15.     B = np.linalg.norm(eye[2] - eye[4])
  16.     # compute the euclidean distance between the horizontal
  17.     # eye landmark (x, y)-coordinates
  18.     C = np.linalg.norm(eye[0] - eye[3])
  19.     # compute the eye aspect ratio
  20.     ear = (A + B) / (2.0 * C)
  21.     # return the eye aspect ratio
  22.     return ear
  23. def draw_facial_landmarks(frame, landmarks):
  24.     for (x, y) in landmarks:
  25.         cv2.circle(frame, (x, y), 2, (0, 255, 0), -1)
  26. def eye_blinking_detection(shape_detector, face_detector,blink_threshold=0.3):
  27.  
  28.     cap = cv2.VideoCapture(0)
  29.     while True:
  30.         ret, frame = cap.read()
  31.         if ret == True:
  32.             gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
  33.             faces = face_detector(gray)
  34.             for face in faces:
  35.                 landmarks = shape_detector(gray, face)
  36.                 landmarks = face_utils.shape_to_np(landmarks)
  37.                 left_eye = landmarks[36:42]
  38.                 right_eye = landmarks[42:48]
  39.                 left_eye_hull = cv2.convexHull(left_eye)
  40.                 right_eye_hull = cv2.convexHull(right_eye)
  41.                 cv2.drawContours(frame, [left_eye_hull], -1, (0, 0, 255), 1)
  42.                 cv2.drawContours(frame, [right_eye_hull], -1, (0, 0, 255), 1)
  43.                 left_eye_ratio = eye_aspect_ratio(left_eye)
  44.                 right_eye_ratio = eye_aspect_ratio(right_eye)
  45.  
  46.                 print('left_eye_ratio: ',left_eye_ratio,'\t right_eye_ratio: ',right_eye_ratio)
  47.  
  48.                 draw_facial_landmarks(frame, landmarks)
  49.                
  50.                 # check left and right eye blinking
  51.  
  52.                 if left_eye_ratio < blink_threshold and right_eye_ratio < blink_threshold:
  53.                     cv2.putText(frame, "Both Eye Blinking and sleeping", (10, 90), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 255), 2)
  54.                 elif left_eye_ratio < blink_threshold:
  55.                     cv2.putText(frame, "Left Eye Blinking", (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 255), 2)
  56.                 elif right_eye_ratio < blink_threshold:
  57.                     cv2.putText(frame, "Right Eye Blinking", (10, 60), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 255), 2)
  58.                 else:
  59.                     cv2.putText(frame, "Active", (10, 120), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 255), 2)
  60.             cv2.imshow('frame', frame)
  61.             if cv2.waitKey(1) & 0xFF == ord('q'):
  62.                 break
  63.         else:
  64.             break
  65.     cap.release()
  66.     cv2.destroyAllWindows()
  67.  
  68.  
  69. if __name__ == "__main__":
  70.     face_landmark_path='shape_predictor_68_face_landmarks.dat'
  71.     shape_detector = dlib.shape_predictor(face_landmark_path)
  72.     face_detector = dlib.get_frontal_face_detector()
  73.     blink_threshold=0.2
  74.     eye_blinking_detection(shape_detector, face_detector,blink_threshold)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement