Instantly share code, notes, and snippets.

Embed
What would you like to do?
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