Skip to content

Instantly share code, notes, and snippets.

@MarcScott
Last active February 22, 2017 11:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MarcScott/2116aae340afef8bdccdd035d4e20c8b to your computer and use it in GitHub Desktop.
Save MarcScott/2116aae340afef8bdccdd035d4e20c8b to your computer and use it in GitHub Desktop.
from clarifai import rest
from clarifai.rest import ClarifaiApp
from clarifai.rest import Image as ClImage
from PIL import Image as PilImage
CLARIFAI_APP_ID = "some_long_string"
CLARIFAI_APP_SECRET = "some_long_string"
##Grab the image
the_image = 'path/to/image.jpg'
smiley = '/path/to/overlay.jpg'
app = ClarifaiApp(CLARIFAI_APP_ID, CLARIFAI_APP_SECRET)
##face recognition model
model = app.models.get('a403429f2ddf4b49b307e318f00e528b')
image = ClImage(filename=the_image)
data = model.predict([image])
##find the bounding box
box = data['outputs'][0]['data']['regions'][1]['region_info']['bounding_box']
back = PilImage.open(the_image)
back_w, back_h = back.size
fore = PilImage.open(smiley)
smiley_width = (box['right_col'] - box['left_col']) * back_w
smiley_height = (box['bottom_row'] - box['top_row']) * back_h
maxsize = (smiley_width, smiley_height)
fore.thumbnail(maxsize, PilImage.ANTIALIAS)
#fore.save('temp.png')
#temp = PilImage.open('temp.png')
back.paste(fore, (round(box['left_col'] * back_w), round(box['top_row'] * back_h)), fore)
back.save('out.png')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment