Each of the examples below is a small independent code snippet within 10 lines that could work by copy and paste to a python source code file. By playing with them, you should be getting started with Clarifai API. For more information about the API, check the API Reference.
.. toctree:: tutorial-predict.rst tutorial-feedback.rst
from clarifai.rest import ClarifaiApp
app = ClarifaiApp()
app.inputs.create_image_from_url(url='https://samples.clarifai.com/puppy.jpeg', concepts=['my puppy'])
app.inputs.create_image_from_url(url='https://samples.clarifai.com/wedding.jpg', not_concepts=['my puppy'])
Note
This assumes you follow through the tutorial and finished the "Upload Images" Otherwise you may not be able to create the model.
model = app.models.create(model_id="puppy", concepts=["my puppy"])
Note
This assumes you follow through the tutorial and finished the "Upload Images" and "Create a Model" to create a model. Otherwise you may not be able to train the model.
model.train()
Note
This assumes you follow through the tutorial and finished the "Upload Images", "Create a Model", and "Train the Model". Otherwise you may not be able to make predictions with the model.
from clarifai.rest import ClarifaiApp
app = ClarifaiApp()
model = app.models.get('puppy')
model.predict_by_url('https://samples.clarifai.com/metro-north.jpg')
from clarifai.rest import Image as ClImage
# make an image with an url
img = ClImage(url='https://samples.clarifai.com/dog1.jpeg')
# make an image with a filename
img = ClImage(filename='/tmp/user/dog.jpg')
# allow duplicate url
img = ClImage(url='https://samples.clarifai.com/dog1.jpeg', allow_dup_url=True)
# make an image with concepts
img = ClImage(url='https://samples.clarifai.com/dog1.jpeg', \
concepts=['cat', 'animal'])
# make an image with metadata
img = ClImage(url='https://samples.clarifai.com/dog1.jpeg', \
concepts=['cat', 'animal'], \
metadata={'id':123,
'city':'New York'
})
If you have a large amount of images, you may not want to upload them one by one by calling app.inputs.create_image_from_url('https://samples.clarifai.com/dog1.jpeg')
Instead you may want to use the bulk import API.
Note
The max number images per batch is 128. If you have more than 128 images to upload, you may want to chunk them into 128 or less, and bulk import them batch by batch.
In order to use this, you have to instantiate Image() objects from various sources.
from clarifai.rest import ClarifaiApp
from clarifai.rest import Image as ClImage
# assume there are 100 urls in the list
images = []
for url in urls:
img = ClImage(url=url)
images.append(img)
app.inputs.bulk_create_images(images)
Note
This assumes you follow through the tutorial and finished the "Upload Images" Otherwise you may not be able to search
from clarifai.rest import ClarifaiApp
app = ClarifaiApp()
app.inputs.search_by_annotated_concepts(concept='my puppy')
app.inputs.search_by_predicted_concepts(concept='dog')
app.inputs.search_by_image(url='https://samples.clarifai.com/dog1.jpeg')
app.inputs.search_by_metadata(metadata={'key':'value'})