from scipy.spatial import distance as dist
from imutils.video import FileVideoStream
from imutils.video import VideoStream
from imutils import face_utils
import numpy as np
import argparse
import imutils
import time
import dlib
import cv2

def mouth_aspect_ratio(mouth):
    A = np.linalg.norm(mouth[13] - mouth[19])

    B = np.linalg.norm(mouth[14] - mouth[18])
    C = np.linalg.norm(mouth[15] - mouth[17])

    D = np.linalg.norm(mouth[12] - mouth[16])

    # Compute the mouth aspect ratio
    mar = (A + B + C) / (2 * D)

    return mar




def eye_aspect_ratio(eye):
	# compute the euclidean distances between the two sets of
	# vertical eye landmarks (x, y)-coordinates
	A = dist.euclidean(eye[1], eye[5])
	B = dist.euclidean(eye[2], eye[4])

	# compute the euclidean distance between the horizontal
	# eye landmark (x, y)-coordinates
	C = dist.euclidean(eye[0], eye[3])

	# compute the eye aspect ratio
	ear = (A + B) / (2.0 * C)

	return ear
 
EYE_AR_THRESH = 0.3
MOUTH_AR_THRESH = 0.8
EYE_AR_CONSEC_FRAMES = 3
DOWZY_CONSEC_FRAMES = 40

# initialize the frame YAWN_COUNTERs and the total number of blinks
YAWN_COUNTER = 0
DAWZY_COUNER = 0 
TOTAL = 0

print("[INFO] loading facial landmark predictor...")
detector = dlib.get_frontal_face_detector()
predictor = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat")

(lStart, lEnd) = face_utils.FACIAL_LANDMARKS_IDXS["left_eye"]
(rStart, rEnd) = face_utils.FACIAL_LANDMARKS_IDXS["right_eye"]
(mStart, mEnd) = face_utils.FACIAL_LANDMARKS_IDXS["mouth"]

print(lStart,lEnd)

cap = cv2.VideoCapture(0) 

while True:

	ret,frame = cap.read()
	frame = cv2.resize(frame, (450,450))
	gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

	rects = detector(gray, 0)

	for rect in rects:
		shape = predictor(gray, rect)
		shape = face_utils.shape_to_np(shape)

		leftEye = shape[lStart:lEnd]
		rightEye = shape[rStart:rEnd]
		mouth = shape[mStart:mEnd]
		leftEAR = eye_aspect_ratio(leftEye)
		rightEAR = eye_aspect_ratio(rightEye)
		mouthEAR = mouth_aspect_ratio(mouth)

		print(mouthEAR)
		cv2.putText(frame, "mouth: {:.2f}".format(mouthEAR), (300, 30),
			cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 255), 2)
 
		ear = (leftEAR + rightEAR) / 2.0

		leftEyeHull = cv2.convexHull(leftEye)
		rightEyeHull = cv2.convexHull(rightEye)
		mouthHull = cv2.convexHull(mouth)
		cv2.drawContours(frame, [leftEyeHull], -1, (0, 255, 0), 1)
		cv2.drawContours(frame, [rightEyeHull], -1, (0, 255, 0), 1)
		cv2.drawContours(frame, [mouthHull], -1, (0, 255, 0), 1)
		# check to see if the eye aspect ratio is below the blink
		# threshold, and if so, increment the blink frame YAWN_COUNTER
		if ear < EYE_AR_THRESH and mouthEAR>=MOUTH_AR_THRESH:
			YAWN_COUNTER += 1
		elif ear < EYE_AR_THRESH:
			DAWZY_COUNER += 1
			YAWN_COUNTER = 0
		else:
			# if the eyes were closed for a sufficient number of
			# then increment the total number of blink
			# reset the eye frame YAWN_COUNTER
			YAWN_COUNTER = 0
			DAWZY_COUNER = 0


		if YAWN_COUNTER >= EYE_AR_CONSEC_FRAMES:
			cv2.putText(frame, "Yawning ", (150, 300),
				cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 255), 2)
		if DAWZY_COUNER >= DOWZY_CONSEC_FRAMES:
			cv2.putText(frame, "DAWZY ", (150, 250),
				cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 255), 2)
			
		# draw the total number of blinks on the frame along with
		# the computed eye aspect ratio for the frame
		cv2.putText(frame, "EYES: {:.2f}".format(ear), (10, 30),
			cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 255), 2)
		# cv2.putText(frame, "EAR: {:.2f}".format(ear), (300, 30),
		# 	cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 255), 2)
 
	# show the frame
	cv2.imshow("Frame", frame)
	key = cv2.waitKey(1) & 0xFF
 
	# if the `q` key was pressed, break from the loop
	if key == ord("q"):
		break
cv2.waitKey(0)
cv2.destroyAllWindows()


