import cv2
import sys
import playsound

face_cascade = cv2.CascadeClassifier('cascades/haarcascade_frontalface_default.xml')

# capture video using cv2
video_capture = cv2.VideoCapture(0)

while True:
    # capture frame by frame, i.e, one by one 
    ret, frame = video_capture.read()
    
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    
    # for each face on the projected on the frame
    faces = face_cascade.detectMultiScale(
        gray,
        scaleFactor = 1.1,
        minNeighbors = 5, 
        # minSize(35, 35)
    )
    
    # loop through the video faces for detection
    for (x, y, w, h) in faces:
        point1 = x+w
        point2 = y+h
        frame_color = (50, 50, 200)
        
        rectangleBox = cv2.rectangle(frame, (x, y), (point1, point2), frame_color, 2)
        cv2.imshow('video', frame)
        if faces.any():
            playsound.playsound('openDoorAlert.mp3', True)
            
            if len(faces) > 1:
                print("There are " + str(len(faces)) + " peoples at the gate")
            else:
                print("There is " + str(len(faces)) + " person at the gate")
        else:
            pass
        if cv2.waitKey(1) & 0xFF == ord('q'):
            sys.exit()
        

[((55, 124), 'cv2.CascadeClassifier', 'cv2.CascadeClassifier', (['"""cascades/haarcascade_frontalface_default.xml"""'], {}), "('cascades/haarcascade_frontalface_default.xml')\n", (76, 124), False, 'import cv2\n'), ((168, 187), 'cv2.VideoCapture', 'cv2.VideoCapture', (['(0)'], {}), '(0)\n', (184, 187), False, 'import cv2\n'), ((302, 341), 'cv2.cvtColor', 'cv2.cvtColor', (['frame', 'cv2.COLOR_BGR2GRAY'], {}), '(frame, cv2.COLOR_BGR2GRAY)\n', (314, 341), False, 'import cv2\n'), ((735, 797), 'cv2.rectangle', 'cv2.rectangle', (['frame', '(x, y)', '(point1, point2)', 'frame_color', '(2)'], {}), '(frame, (x, y), (point1, point2), frame_color, 2)\n', (748, 797), False, 'import cv2\n'), ((806, 832), 'cv2.imshow', 'cv2.imshow', (['"""video"""', 'frame'], {}), "('video', frame)\n", (816, 832), False, 'import cv2\n'), ((869, 915), 'playsound.playsound', 'playsound.playsound', (['"""openDoorAlert.mp3"""', '(True)'], {}), "('openDoorAlert.mp3', True)\n", (888, 915), False, 'import playsound\n'), ((1223, 1233), 'sys.exit', 'sys.exit', ([], {}), '()\n', (1231, 1233), False, 'import sys\n'), ((1176, 1190), 'cv2.waitKey', 'cv2.waitKey', (['(1)'], {}), '(1)\n', (1187, 1190), False, 'import cv2\n')]