#!python3
# eye_detection.py - detect eyes using webcam
# tutorial: https://www.roytuts.com/real-time-eye-detection-in-webcam-using-python-3/

import cv2
import math
import numpy as np


def main():

    faceCascade = cv2.CascadeClassifier("haarcascade_frontalface_alt.xml")
    eyeCascade = cv2.CascadeClassifier("haarcascade_eye.xml")

    # grab the reference to the webcam
    # try:
    vs = cv2.VideoCapture(0)
    print(vs)

    while True:
        ret, frame = vs.read()

        if frame is None:
            break

        gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
        gray = cv2.equalizeHist(gray)

        faces = faceCascade.detectMultiScale(frame)

        for (x, y, w, h) in faces:
            roi_gray = gray[y : y + h, x : x + w]
            roi_color = frame[y : y + h, x : x + w]
            eyes = eyeCascade.detectMultiScale(roi_gray)
            for (ex, ey, ew, eh) in eyes:
                cv2.rectangle(roi_color, (ex, ey), (ex + ew, ey + eh), (0, 0, 255), 2)

            cv2.imshow("Video", frame)
            key = cv2.waitKey(1) & 0xFF

            if key == ord("q") or key == 27:
                break

    cv2.destroyAllWindows()


if __name__ == "__main__":
    main()

[((12, 18, 12, 74), 'cv2.CascadeClassifier', 'cv2.CascadeClassifier', ({(12, 40, 12, 73): '"""haarcascade_frontalface_alt.xml"""'}, {}), "('haarcascade_frontalface_alt.xml')", False, 'import cv2\n'), ((13, 17, 13, 61), 'cv2.CascadeClassifier', 'cv2.CascadeClassifier', ({(13, 39, 13, 60): '"""haarcascade_eye.xml"""'}, {}), "('haarcascade_eye.xml')", False, 'import cv2\n'), ((17, 9, 17, 28), 'cv2.VideoCapture', 'cv2.VideoCapture', ({(17, 26, 17, 27): '0'}, {}), '(0)', False, 'import cv2\n'), ((44, 4, 44, 27), 'cv2.destroyAllWindows', 'cv2.destroyAllWindows', ({}, {}), '()', False, 'import cv2\n'), ((26, 15, 26, 54), 'cv2.cvtColor', 'cv2.cvtColor', ({(26, 28, 26, 33): 'frame', (26, 35, 26, 53): 'cv2.COLOR_BGR2GRAY'}, {}), '(frame, cv2.COLOR_BGR2GRAY)', False, 'import cv2\n'), ((27, 15, 27, 37), 'cv2.equalizeHist', 'cv2.equalizeHist', ({(27, 32, 27, 36): 'gray'}, {}), '(gray)', False, 'import cv2\n'), ((38, 12, 38, 38), 'cv2.imshow', 'cv2.imshow', ({(38, 23, 38, 30): '"""Video"""', (38, 32, 38, 37): 'frame'}, {}), "('Video', frame)", False, 'import cv2\n'), ((36, 16, 36, 86), 'cv2.rectangle', 'cv2.rectangle', ({(36, 30, 36, 39): 'roi_color', (36, 41, 36, 49): '(ex, ey)', (36, 51, 36, 69): '(ex + ew, ey + eh)', (36, 71, 36, 82): '(0, 0, 255)', (36, 84, 36, 85): '(2)'}, {}), '(roi_color, (ex, ey), (ex + ew, ey + eh), (0, 0, 255), 2)', False, 'import cv2\n'), ((39, 18, 39, 32), 'cv2.waitKey', 'cv2.waitKey', ({(39, 30, 39, 31): '(1)'}, {}), '(1)', False, 'import cv2\n')]