/clarifai-python

Clarifai API Python Client

Primary LanguagePython

Clarifai Python Client

This Python client provides a simple wrapper around our powerful image recognition API.

The constructor takes your APP_ID and APP_SECRET created in your Clarifai Account. You can also set these variables in your environment as: CLARIFAI_APP_ID CLARIFAI_APP_SECRET

This client uses your APP_ID and APP_SECRET to get an access token. Since this expires every so often, the client is setup to renew the token for you automatically using your credentials so you don't have to worry about it. Additionally, we highly recommend that you have PIL or Pillow installed if you plan to send images from your local machine to our service. The client will automatically determine your allowed limits and resize any images you wish to process automatically before sending. If you do not have PIL or Pillow then you must do this yourself to ensure your API calls are processed without fail.

Installation

pip install git+git://github.com/Clarifai/clarifai-python.git
export CLARIFAI_APP_ID=<an_application_id_from_your_account>
export CLARIFAI_APP_SECRET=<an_application_secret_from_your_account>

Usage

A complete example of using this Python client is as follows. Suppose you want to tag an image on your local drive:

from clarifai.client import ClarifaiApi
clarifai_api = ClarifaiApi() # assumes environment variables are set.
result = clarifai_api.tag_images(open('/path/to/local/image.jpeg', 'rb'))

This will return the tagging result for the given image read off your local storage system (see the Docs for response format). The operations supported by the client can all handle batches of images. Keeping tagging as the running example, this would look like:

result = clarifai_api.tag_images([open('/path/to/local/image.jpeg', 'rb'),
                                  open('/path/to/local/image2.jpeg', 'rb')])

The result will now contain all the results of the tagging for each image in the batch. When sending large batches of images, you must adhere to your application limits for the maximum batch size per request.

If your images live remotely at a public url, you can also use tag_image_urls:

from clarifai.client import ClarifaiApi
clarifai_api = ClarifaiApi()  # assumes environment variables are set.
result = clarifai_api.tag_image_urls('http://www.clarifai.com/img/metro-north.jpg')

If you have multiple urls to tag, you can also call tag_image_urls with an array of urls:

from clarifai.client import ClarifaiApi
clarifai_api = ClarifaiApi()  # assumes environment variables are set.
result = clarifai_api.tag_image_urls(['http://www.clarifai.com/static/img_ours/autotag_examples/metro-north.jpg',
                                      'http://www.clarifai.com/static/img_ours/autotag_examples/dog.jpg'])

The same result format is returned whether provided image bytes or urls.

Please check out the full documentation for our API at developer.clarifai.com.