Skip to content

Instantly share code, notes, and snippets.

@abhishekkrthakur
Last active August 29, 2015 14:05
Show Gist options
  • Save abhishekkrthakur/3d10ee5b298f3e3953ea to your computer and use it in GitHub Desktop.
Save abhishekkrthakur/3d10ee5b298f3e3953ea to your computer and use it in GitHub Desktop.
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment