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

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!

Image for post
Image for post

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