python code
python code
import dlib
import time
detector = dlib.get_frontal_face_detector()
predictor = dlib.shape_predictor('shape_predictor_68_face_landmarks.dat')
# Define the indexes of the facial landmarks for the left and right eye
# Eye aspect ratio threshold and consecutive frame count for detecting drowsiness
COUNTER = 0
ALERT = False
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])
return ear
cap = cv2.VideoCapture(0)
while True:
faces = detector(gray)
left_ear = eye_aspect_ratio(left_eye)
right_ear = eye_aspect_ratio(right_eye)
COUNTER += 1
# Drowsiness detected
if not ALERT:
ALERT = True
print("Drowsiness Detected!")
else:
COUNTER = 0
ALERT = False
if ALERT:
else:
break
cap.release()
cv2.destroyAllWindows()