import numpy as np
import cv2
from PIL import ImageGrab

face_cascade = cv2.CascadeClassifier('data/haarcascade_frontalface_alt2.xml')
cap = cv2.VideoCapture(0)
count = 0

while(True):map(m)
    img = ImageGrab.grab()
    img_np = np.array(img)
    ret, frame = cap.read()
    gray = cv2.cvtColor(img_np, cv2.COLOR_BGR2GRAY)
    faces = face_cascade.detectMultiScale(frame, scaleFactor = 1.5, minNeighbors = 5,minSize=(40,40))
    for (i,(x,y,w,h)) in enumerate (faces):
        print(x,y,w,h)
        cv2.rectangle(frame,(x,y),(x+w,y+h),(255,0,0),2)
        cv2.putText(frame, "member #{}".format(i + 1), (x, y - 10),
                    cv2.FONT_HERSHEY_SIMPLEX, 0.55, (0, 0, 255), 2)
        roi_gray = frame[y:y+h, x:x+w]
        roi_color = frame[y:y+h, x:x+w]
    cv2.imshow('frame',frame)
    if cv2.waitKey(20) & 0xFF == ord('q'):
        for (i,(x,y,w,h)) in enumerate (faces):
            cv2.rectangle(frame,(x,y),(x+w,y+h),(255,0,0),2)
            cv2.imwrite('/Users/aryn/Documents/ML(Python)/OpenCV'+str(count)+'.jpg',roi_color)
            count = count +1

cap.release()
cv2.destroyAllWindows()
