import cv2 scaling = 10 webcam = cv2.VideoCapture(0) haar = cv2.CascadeClassifier("/usr/local/Cellar/opencv/2.4.8.2/share/OpenCV/lbpcascades/lbpcascade_frontalface.xml") if webcam.isOpened(): # try to get the first frame rval, frame = webcam.read() else: rval = False while rval: minisize = (frame.shape[1]/scaling,frame.shape[0]/scaling) miniframe = cv2.resize(frame, minisize) faces = haar.detectMultiScale(miniframe) for f in faces: x, y, w, h = [ v*scaling for v in f ] cv2.rectangle(frame, (x,y), (x+w,y+h), (0,0,255)) crop_img = frame[y:y+h, x:x+w] cv2.imwrite('face.png', crop_img) cv2.imshow("preview", frame) rval, frame = webcam.read() key = cv2.waitKey(20) if key in [27, ord('Q'), ord('q')]: break