import cv2
import numpy as np

cap = cv2.VideoCapture(0)
count = 0
condition = False
while condition == False:
    ret, frame = cap.read()
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    blurred = cv2.GaussianBlur(gray, (3, 3), 0)
    height, width = blurred.shape
    face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
    faces = face_cascade.detectMultiScale(blurred, scaleFactor=1.3, minNeighbors=5, minSize=(30, 30))

    for (x, y, w, h) in faces:
        c_x = int(x + w/2)
        c_y = int(y + h/2)
        center = (c_x, c_y)
        top = (c_x, int(c_y-h/2*1.4))
        bottom = (c_x, int(c_y+h/2*1.1))
        left = (int(c_x - w / 2 * 0.8), c_y)
        right = (int(c_x + w / 2 * 0.8), c_y)

        c_x2 = int(( int(c_x - w / 2 * 0.8) + int(c_x + w / 2 * 0.8) )/2)
        c_y2 = int(( int(c_y-h/2*1.4) + int(c_y+h/2*1.1) )/2)
        '''
        cv2.circle(frame, center, 10, (255, 255, 255), 5)       #center
        cv2.circle(frame, top, 10, (255, 255, 255), 5)          # top
        cv2.circle(frame, bottom, 10, (255, 255, 255), 5)       # bottom
        cv2.circle(frame, left, 10, (255, 255, 255), 5)       # left
        cv2.circle(frame, right, 10, (255, 255, 255), 5)       # right
        
        cv2.circle(frame, (x, y), 10, (255, 255, 255), 5)       # top left
        cv2.circle(frame, (x+w, y), 10, (255, 255, 255), 5)     # top right
        cv2.circle(frame, (x, y+h), 10, (255, 255, 255), 5)     # bottom left
        cv2.circle(frame, (x+h, y+h), 10, (255, 255, 255), 5)   # bottom right
        '''
        center_ellipse = (c_x2, c_y2)
        axes = (int(w/2* 0.9), int(h/2*1.4))  # Major axis, Minor axis
        angle = 0  # Rotation angle
        color = (0, 255, 0)  # Color in BGR format
        thickness = 2

        # Draw the ellipse on the image
        #cv2.ellipse(frame, center_ellipse, axes, angle, 0, 360, color, thickness)

        mask = np.zeros((height, width), dtype=np.uint8)
        cv2.ellipse(mask, center_ellipse, axes, angle, 0, 360, 255, -1)
        result = cv2.bitwise_and(frame, frame, mask=mask)

    cv2.imshow('Head Detection', result)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

cap.release()
cv2.destroyAllWindows()

