Automatically Describe an Image in Natural Language in Python

A picture might be worth a thousand words, but you don’t need a thousand words to describe a picture. With the /image/recognize/describe iteration of the Cloudmersive Image API, your application can generate a short sentence describing a photo of your choice. This is really useful for quickly processing, storing & captioning images, and you’ll receive an accompanying ‘confidence score’ to indicate how likely it is that the description is on point. Below, I’ll walk through connecting to this API using Python code from the Cloudmersive API Console page.

You’ll first need to run this command to install the Python SDK:

pip install cloudmersive-image-api-client

Next, copy the below code to begin calling the API. In the second of the two snippets you’ll be prompted to input your Cloudmersive API key:

from __future__ import print_function
import time
import cloudmersive_image_api_client
from cloudmersive_image_api_client.rest import ApiException
from pprint import pprint
# Configure API key authorization: Apikey
configuration = cloudmersive_image_api_client.Configuration()
configuration.api_key['Apikey'] = 'YOUR_API_KEY'

For the final step, copy the remaining code and you’re ready to go:

# create an instance of the API class
api_instance = cloudmersive_image_api_client.RecognizeApi(cloudmersive_image_api_client.ApiClient(configuration))
image_file = '/path/to/inputfile' # file | Image file to perform the operation on. Common file formats such as PNG, JPEG are supported.
try:
# Describe an image in natural language
api_response = api_instance.recognize_describe(image_file)
pprint(api_response)
except ApiException as e:
print("Exception when calling RecognizeApi->recognize_describe: %s\n" % e)

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Cloudmersive

Cloudmersive

There’s an API for that. Cloudmersive is a leader in Highly Scalable Cloud APIs.