'''This script detects if a person is drowsy or not,using dlib and eye aspect ratio
calculations. Uses webcam video feed as input.'''

#Import necessary libraries
from scipy.spatial import distance
from imutils import face_utils
import numpy as np
import pygame #For playing sound
import time
import dlib
import cv2

#Initialize Pygame and load music
pygame.mixer.init()
pygame.mixer.music.load('audio/alert.wav')

#Minimum threshold of eye aspect ratio below which alarm is triggerd
EYE_ASPECT_RATIO_THRESHOLD = 0.2

#Minimum consecutive frames for which eye ratio is below threshold for alarm to be triggered
EYE_ASPECT_RATIO_CONSEC_FRAMES = 10

#COunts no. of consecutuve frames below threshold value
COUNTER = 0

#ALERT time : After this time alert will be stop even the eyes are closed
ALERT_CONSEC_FRAMES = 50
ALERT_TIME = 0

#Load face cascade which will be used to draw a rectangle around detected faces.
face_cascade = cv2.CascadeClassifier("haarcascades/haarcascade_frontalface_default.xml")

#This function calculates and return eye aspect ratio
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])

    ear = (A+B) / (2*C)
    return ear

#Load face detector and predictor, uses dlib shape predictor file
detector = dlib.get_frontal_face_detector()
predictor = dlib.shape_predictor('shape_predictor_68_face_landmarks.dat')

#Extract indexes of facial landmarks for the left and right eye
(lStart, lEnd) = face_utils.FACIAL_LANDMARKS_IDXS['left_eye']
(rStart, rEnd) = face_utils.FACIAL_LANDMARKS_IDXS['right_eye']

#Start webcam video capture
video_capture = cv2.VideoCapture(0)

#Give some time for camera to initialize(not required)
time.sleep(2)

#Grid Setting
box = 40
old_face_x = 0
old_face_y = 0
old_face_h = 0
old_face_w = 0

while(True):
    #Read each frame and flip it, and convert to grayscale
    ret, frame = video_capture.read()
    frame = cv2.flip(frame,1)
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

    #Detect facial points through detector function
    faces = detector(gray, 0)

    #Detect faces through haarcascade_frontalface_default.xml
    face_rectangle = face_cascade.detectMultiScale(gray, 1.3, 5)

    face_x = 0
    face_y = 0
    #Draw rectangle around each face detected
    for (x,y,w,h) in face_rectangle:
        face_x = x
        face_y = y
        face_w = w
        face_h = h

    # New Larger Rectangle
    if old_face_x < face_x - box or old_face_y < face_y - box or old_face_x+old_face_w > face_x+face_w + box or old_face_y+old_face_h > face_y+face_h + box:
        old_face_x = face_x
        old_face_w = face_w
        old_face_y = face_y
        old_face_h = face_h

    # Face rectangle is drawn from old values
    if old_face_x != 0 and old_face_y != 0:
        overlay = frame.copy()
        #Frame
        #cv2.rectangle(overlay,(old_face_x-box,old_face_y-box),(old_face_x+old_face_w+box,old_face_y+old_face_h+box),(0,0,0),2)
        #Rectangle
        cv2.rectangle(overlay,(old_face_x-box,old_face_y-box),(old_face_x+old_face_w+box,old_face_y+old_face_h+box),(0,0,0),-1)
        cv2.addWeighted(overlay,0.5,frame,0.5,0,frame)

    #Driver warning text position
    text_pos_x = old_face_x -box + 5;
    text_pos_y = old_face_y -box + 20;

    #Detect facial points
    for face in faces:

        shape = predictor(gray, face)
        shape = face_utils.shape_to_np(shape)

        #Get array of coordinates of leftEye and rightEye
        leftEye = shape[lStart:lEnd]
        rightEye = shape[rStart:rEnd]

        #Calculate aspect ratio of both eyes
        leftEyeAspectRatio = eye_aspect_ratio(leftEye)
        rightEyeAspectRatio = eye_aspect_ratio(rightEye)

        eyeAspectRatio = (leftEyeAspectRatio + rightEyeAspectRatio) / 2

        #Use hull to remove convex contour discrepencies and draw eye shape around eyes
        leftEyeHull = cv2.convexHull(leftEye)
        rightEyeHull = cv2.convexHull(rightEye)
        cv2.drawContours(frame, [leftEyeHull], -1, (255, 255, 255), 1)
        cv2.drawContours(frame, [rightEyeHull], -1, (255, 255, 255), 1)

        #Detect if eye aspect ratio is less than threshold
        if(eyeAspectRatio < EYE_ASPECT_RATIO_THRESHOLD):
            #print(COUNTER)
            COUNTER += 1
            #If no. of frames is greater than threshold frames,
            if COUNTER >= EYE_ASPECT_RATIO_CONSEC_FRAMES:
                pygame.mixer.music.play(-1)
                cv2.putText(frame, "DRIVER ASLEEP!", (text_pos_x,text_pos_y), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0,0,255), 2)
                # Stop Alert after sometime
                ALERT_TIME += 1
                if ALERT_TIME == ALERT_CONSEC_FRAMES :
                    pygame.mixer.music.stop()
                    ALERT_TIME = 0
                    COUNTER = 0
            else:
                cv2.putText(frame, "DRIVER AWAKE", (text_pos_x,text_pos_y), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0,255,0), 1)

        else:
            cv2.putText(frame, "DRIVER AWAKE", (text_pos_x,text_pos_y), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0,255,0), 1)
            pygame.mixer.music.stop()
            COUNTER = 0

    #Show video feed
    cv2.imshow('Video', frame)
    if(cv2.waitKey(1) & 0xFF == ord('q')):
        break

#Finally when video capture is over, release the video capture and destroyAllWindows
video_capture.release()
cv2.destroyAllWindows()
