import cv2
import numpy as np
from PIL import Image
from PIL import ImageTk
from tkinter import filedialog

face_cascade_name = 'C:/Users/user/anaconda3/envs/Vision/Library/etc/haarcascades/haarcascade_frontalface_alt.xml'
eyes_cascade_name = 'C:/Users/user/anaconda3/envs/Vision/Library/etc/haarcascades/haarcascade_eye_tree_eyeglasses.xml'
file_name = './video/face_01.mp4'
title_name = 'Haar cascade object detection Video'
frame_width=500
cap = cv2.VideoCapture()

def detectAndDisplay(frame) :
    frame_gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    frame_gray = cv2.equalizeHist(frame_gray)
    #--Detect Faces
    faces = face_cascade.detectMultiScale(frame_gray)
    for (x, y, w, h ) in faces :
        center = (x+ w//2, y+h//2)
        frame = cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 244, 0), 4)
        faceROI = frame_gray[y:y+h, x:x+w]

        eyes = eyes_cascade.detectMultiScale(faceROI)
        for (x2, y2,w2, h2) in eyes :
            eye_center = ( x +x2 +w2//2, y+y2+h2//2)
            radius = int(round(w2+h2) *0.25)
            frame = cv2.circle(frame, eye_center, radius, (255, 0, 0), 4)
    cv2.imshow('Capture - Face detection', frame)

face_cascade = cv2.CascadeClassifier()
eyes_cascade = cv2.CascadeClassifier()

if not face_cascade.load(cv2.samples.findFile(face_cascade_name)) :
    print('--(!)Error loading face ascade')
    exit(0)
if not eyes_cascade.load(cv2.samples.findFile(eyes_cascade_name)) :
    print('--(!)Error loading eyes cascade')
    exit(0)


#--2.Read the video stream
cap = cv2.VideoCapture(file_name)
if not cap.isOpened :
    print('--(!)Error opening video capture')
    exit(0)

while True:
    ret, frame = cap.read()
    if frame is None:
        print('--(!) No captured frema -- Break!')
        break

    detectAndDisplay(frame)
    if cv2.waitKey(1) & 0xFF == ord('q') :
        break
    