#!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()

[((218, 274), 'cv2.CascadeClassifier', 'cv2.CascadeClassifier', (['"""haarcascade_frontalface_alt.xml"""'], {}), "('haarcascade_frontalface_alt.xml')\n", (239, 274), False, 'import cv2\n'), ((292, 336), 'cv2.CascadeClassifier', 'cv2.CascadeClassifier', (['"""haarcascade_eye.xml"""'], {}), "('haarcascade_eye.xml')\n", (313, 336), False, 'import cv2\n'), ((397, 416), 'cv2.VideoCapture', 'cv2.VideoCapture', (['(0)'], {}), '(0)\n', (413, 416), False, 'import cv2\n'), ((1148, 1171), 'cv2.destroyAllWindows', 'cv2.destroyAllWindows', ([], {}), '()\n', (1169, 1171), False, 'import cv2\n'), ((540, 579), 'cv2.cvtColor', 'cv2.cvtColor', (['frame', 'cv2.COLOR_BGR2GRAY'], {}), '(frame, cv2.COLOR_BGR2GRAY)\n', (552, 579), False, 'import cv2\n'), ((595, 617), 'cv2.equalizeHist', 'cv2.equalizeHist', (['gray'], {}), '(gray)\n', (611, 617), False, 'import cv2\n'), ((1008, 1034), 'cv2.imshow', 'cv2.imshow', (['"""Video"""', 'frame'], {}), "('Video', frame)\n", (1018, 1034), False, 'import cv2\n'), ((924, 994), 'cv2.rectangle', 'cv2.rectangle', (['roi_color', '(ex, ey)', '(ex + ew, ey + eh)', '(0, 0, 255)', '(2)'], {}), '(roi_color, (ex, ey), (ex + ew, ey + eh), (0, 0, 255), 2)\n', (937, 994), False, 'import cv2\n'), ((1053, 1067), 'cv2.waitKey', 'cv2.waitKey', (['(1)'], {}), '(1)\n', (1064, 1067), False, 'import cv2\n')]