face.comのapiつかってみた
http://developers.face.com/
画像から顔情報を検出・認識してくれるface.comのapiを使って、笑い男で顔のマスキング的なことをやってみた。まあおなじみですね。
適当に使ってみたのはこの画像。
ライブラリはPILとface.comのpythonクライアント(http://github.com/Kami/python-face-client)を使用。
笑い男の画像はそこらから拾ってきたやつ。
from face_client import face_client from PIL import Image import urllib2 from cStringIO import StringIO url = IMAGE_URL image = Image.open(StringIO(urllib2.urlopen(url).read())) x_size, y_size = image.size client = face_client.FaceClient(API_KEY, API_SECRET) response = client.faces_detect(url) mask_image = Image.open(LAUGHING_MAN) for photo in response['photos']: for tag in photo['tags']: #顔の縦横サイズを取得 w = x_size * float(tag['width']) / 100 h = y_size * float(tag['height']) / 100 #ちょっと補正 w *= 1.3 h *= 1.3 #顔の中心の座標を取得 c_x = x_size * float(tag['center']['x']) / 100 c_x -= w / 2 c_y = y_size * float(tag['center']['y']) / 100 c_y -= h / 2 tmp = mask_image.resize((w, h), Image.ANTIALIAS) try: image.paste(tmp, (c_x, c_y), mask = tmp) except Exception, e: print e image.save('hoge.jpg')