Skip to content

Instantly share code, notes, and snippets.

@robo8080
Created August 27, 2016 12:33
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save robo8080/c0dd46174c724139e6e3c2dce8ee68f4 to your computer and use it in GitHub Desktop.
Save robo8080/c0dd46174c724139e6e3c2dce8ee68f4 to your computer and use it in GitHub Desktop.
FaceDetect2.py
#!python2
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)
faces = d.featuresInImage_(ci_img)
#print face
if faces.count() > 0:
drawbuffer = ImageDraw.Draw(imagebuffer)
for face in faces:
b = face.bounds()
w=b.size.width
h=b.size.height
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