Skip to content

Instantly share code, notes, and snippets.

@QuanSai
Created August 22, 2012 04:25
Show Gist options
  • Save QuanSai/3422273 to your computer and use it in GitHub Desktop.
Save QuanSai/3422273 to your computer and use it in GitHub Desktop.
This is the first part of what I call "DjFaceChop" which will crop faces out of given images. I looked at example code of face tracking all over the net and came up with this. Uses the OpenCV Haar training data to detect faces in the image. The rest is
import cv2
def facechop(image):
facedata = "haarcascade_frontalface_default.xml"
DOWNSCALE = 1
cascade = cv2.CascadeClassifier(facedata)
img = cv2.imread(image)
minisize = (img.shape[1]/DOWNSCALE,img.shape[0]/DOWNSCALE)
miniframe = cv2.resize(img, minisize)
faces = cascade.detectMultiScale(miniframe)
for f in faces:
x, y, w, h = [ v*DOWNSCALE for v in f ]
cv2.rectangle(img, (x,y), (x+w,y+h), (255,255,255))
cv2.imshow(image, img)
return
if __name__ == '__main__':
facechop("beatles.jpg")
while(True):
key = cv2.waitKey(20)
if key in [27, ord('Q'), ord('q')]: # exit on ESC
break
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment