How to generate an English text description of an image in Java

Cloudmersive
2 min readAug 5, 2020

Today we will be harnessing the power of Deep Learning AI to create text descriptions of images using Java. While it would normally require over a week of machine learning time with a heavy-duty GPU to set up, today’s method will skip that, since we already have an AI trained specifically for the task and ready to go.

First, our repositories reference must be added to our Maven file.

<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>

Next comes a dependency for the Cloudmersive API client.

<dependencies>
<dependency>
<groupId>com.github.Cloudmersive</groupId>
<artifactId>Cloudmersive.APIClient.Java</artifactId>
<version>v3.54</version>
</dependency>
</dependencies>

Now we can go forward with our function call:

// Import classes://import com.cloudmersive.client.invoker.ApiClient;//import com.cloudmersive.client.invoker.ApiException;//import com.cloudmersive.client.invoker.Configuration;//import com.cloudmersive.client.invoker.auth.*;//import com.cloudmersive.client.RecognizeApi;ApiClient defaultClient = Configuration.getDefaultApiClient();// Configure API key authorization: ApikeyApiKeyAuth Apikey = (ApiKeyAuth) defaultClient.getAuthentication("Apikey");Apikey.setApiKey("YOUR API KEY");// Uncomment the following line to set a prefix for the API key, e.g. "Token" (defaults to null)//Apikey.setApiKeyPrefix("Token");RecognizeApi apiInstance = new RecognizeApi();File imageFile = new File("/path/to/file.txt"); // File | Image file to perform the operation on.  Common file formats such as PNG, JPEG are supported.try {ImageDescriptionResponse result = apiInstance.recognizeDescribe(imageFile);System.out.println(result);} catch (ApiException e) {System.err.println("Exception when calling RecognizeApi#recognizeDescribe");e.printStackTrace();}

And that’s all there is to it!

--

--

Cloudmersive

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