#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Thu Feb 20 14:47:36 2020

@author: yektazahed

note: run this code in the root environment of anaconda and not the virtual
"""

import cv2

#import he cascades
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
eye_cascade = cv2.CascadeClassifier('haarcascade_eye.xml')

#this function detects the face and eyes and draws a rectangle around it
def detect(gray, frame):
    
    faces = face_cascade.detectMultiScale(gray, 1.3, 5)    
    for (x,y,w,h) in faces:
        cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 0, 255), 3)
        roi_color = frame[y:y+h, x:x+w] # I don't get the x and y
        roi_gray = gray[y:y+h, x:x+w]
        eyes = eye_cascade.detectMultiScale(roi_gray, 1.1, 20)
        
        for (x2, y2, w2, h2) in eyes:
            cv2.rectangle(roi_color, (x2, y2), (x2+w2, y2+h2), (0, 225, 0), 3)     #?why not print it on frame?
    return frame

Video_Capture = cv2.VideoCapture(0)

while True:
    _, frame = Video_Capture.read()
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY ) 
    canvas = detect(gray, frame)
    cv2.imshow('Video', canvas)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

Video_Capture.release()
cv2.destroyAllWindows()