How to find description/caption of a given image

Microsoft had recently exposed Computer Vision API. This feature returns information about visual content found in an image. Use tagging, descriptions and domain-specific models to identify content and label it with confidence.

This is the Java code to access this API :

package images;
import javax.json.Json;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
public class MicrosoftImageCaption {
	public static void main(String[] args) throws Exception {
		HttpClient httpclient = new DefaultHttpClient();
		try {
			URIBuilder builder = new URIBuilder(
			builder.setParameter("visualFeatures", "Description,Tags");
			builder.setParameter("details", "Celebrities");
			builder.setParameter("language", "en");
			URI uri =;
			HttpPost request = new HttpPost(uri);
			// Request headers - replace this example key with your valid
			// subscription key.
			request.setHeader("Content-Type", "application/json");
			// Request body. Replace the example URL with the URL for the JPEG
			// image of a celebrity.
			StringEntity reqEntity = new StringEntity(
			HttpResponse response = httpclient.execute(request);
			HttpEntity entity = response.getEntity();
			if (entity != null) {
				String resp = EntityUtils.toString(entity);
				String text = Json.createReader(
						new StringReader(resp)).readObject().getJsonObject("description").getJsonArray("captions").getJsonObject(0).getString("text");
		} catch (Exception e) {

You can get your keys by subscribing to Microsoft APIs here : Microsoft API

Uday Ogra

Connect with me at and lets have some healthy discussion :)

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *