import cv2
import numpy as np
from matplotlib import pyplot as plt

eye_cascade = cv2.CascadeClassifier('haarcascade_eye.xml')

def eyeDetection(videoPath):
    while True:
        cap = cv2.VideoCapture(videoPath)
        ret, frame = cap.read()
        
        if not ret:
            break

        gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
        eyes = eye_cascade.detectMultiScale(gray, 1.05, 8)
        
        leftEye = np.array([])
        rightEye = np.array([])
        
        if len(eyes) == 2:
            (x1, y1, w1, h1) = eyes[0]
            (x2, y2, w2, h2) = eyes[1]
            
            centerX1 = x1 + w1 // 2
            centerX2 = x2 + w2 // 2
            centerY1 = y1 + h1 // 2
            centerY2 = y2 + h2 // 2
            
            centerBetweenEyes = (centerX1 + centerX2) // 2
            
            if centerX1 <= centerBetweenEyes:
                leftEye = [centerX1, centerY1]
                rightEye = [centerX2, centerY2]
            else:
                leftEye = [centerX2, centerY2]
                rightEye = [centerX1, centerY1]
         
        # cv2.imshow('Frame', frame)
        # cv2.imwrite("SakmarovaFace.png", frame)
         
        # if cv2.waitKey(0) & 0xFF == ord('n'):
        #     continue

        if leftEye and rightEye:
            break
        
        
        cap.release()
        cv2.destroyAllWindows()
        
    return leftEye, rightEye