Skip to content

Instantly share code, notes, and snippets.

@devStepsize
Last active April 30, 2017 17:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save devStepsize/b76fa99db3c1962be7372f08e71a158c to your computer and use it in GitHub Desktop.
Save devStepsize/b76fa99db3c1962be7372f08e71a158c to your computer and use it in GitHub Desktop.
Local image tagging with Clarifai's Python client
"""
Tagging a few local images using the Clarifai API and the clarifai-python
client.
"""
from os.path import expanduser
from clarifai.client import ClarifaiApi
directory = expanduser('~/pictures/clarifai/')
pictures = ['burning_basmatty.jpg', 'concert.jpg']
clarifai_api = ClarifaiApi()
results = clarifai_api.tag_images(
[open('%s%s' % (directory, p), 'rb') for p in pictures]
)
# Uncomment the below to print the whole JSON returned
# from pprint import pprint
# pprint(result)
for i, result in enumerate(results['results']):
tags = result['result']['tag']
print '\nTags for %s (Tag - Probability)' % pictures[i]
for j in range(len(tags['classes'])):
print '%s - %0.4f' % (tags['classes'][j], tags['probs'][j])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment