Created
August 22, 2012 04:25
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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