Created
August 26, 2016 13:19
-
-
Save robo8080/a785f19f8020e030a29ac6894babd5ce to your computer and use it in GitHub Desktop.
FaceDetect.py
This file contains hidden or 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
from PIL import Image, ImageDraw | |
import photos | |
from objc_util import * | |
CIImage, CIDetector = map(ObjCClass, ['CIImage', 'CIDetector']) | |
#imagebuffer = photos.capture_image() | |
imagebuffer = photos.pick_image() | |
imagebuffer.save('.temp.jpg') | |
data = NSData.dataWithContentsOfFile_('.temp.jpg') | |
ci_img = CIImage.imageWithData_(data) | |
opt = {'CIDetectorAccuracy': 'CIDetectorAccuracyHigh'} | |
d = CIDetector.detectorOfType_context_options_('CIDetectorTypeFace', None, opt) | |
face = d.featuresInImage_(ci_img) | |
#print(face) | |
if face.count() > 0: | |
f = face.firstObject() | |
b = f.bounds() | |
w=b.size.width | |
h=b.size.height | |
drawbuffer = ImageDraw.Draw(imagebuffer) | |
drawbuffer.rectangle((b.origin.x,imagebuffer.size[1]-(b.origin.y+h), b.origin.x+w,imagebuffer.size[1]-b.origin.y), outline='white') | |
del drawbuffer | |
else: | |
print('No face detected.') | |
imagebuffer.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment