Using IBM Watson Visual Recognition to classify Images

This post is about using IBM Watson Visual Recognition service to classify images based on how we train it with positive and negative examples. Yes, we will train the service and let it classify some input images.


  1. IBM Bluemix Account
  2. Java 8
  3. Watson SDK for Java
  4. Apache Maven

SDK for Java

We need this maven dependency in our pom.xml

Provision a Visual Recognition Service

From our dashboard, click Create.

Click Watson menu option.

Then, choose Visual Recognition.

Provide a Service Name. For our purpose, we may leave the rest of the fields as they are. Then, click Create.

At this point, we successfully provisioned the service. Please note that for free accounts, we are only allowed to provision one instance of this service. Then, proceed to Service credentials.

Take note of the url and api_key. We will use them in our Java codes.

Visual Recognition Tool

We may train our service using the Visual Recognition Tool (https://watson-visual-recognition.ng.bluemix.net/). It allows for the creation of classifiers and upload of associated image files. But for our case, we will create classifiers and upload images via Java codes.

Java Codes

Our Service

We have a class annotated with @Service that wraps the VisualRecognition class from the SDK and simply delegate method calls to appropriate methods.

Training the Service

We are training the service by uploading images of “nasilemak” for it to know what the food is visually. We also upload images not of “nasilemak” for the service to know what the food is not.

Please note that file name format of the zip files. They mean something:

  1. _positive_examples contains images of what “nasilemak” is visually.
  2. negative_examples contains images of that “nasilemak” is not visually.

If we run the codes above, we get this output in the console.

If we go to to the Visual Recognition Tool, we see our new classifier.

Testing the Service

For testing, we only use our own classifiers, e.g., malaysia-food with classifier-id malaysiaxfood_287949534.

The input image file to classify using our service is “nasi-lemak-typical-indonesian-food-bali-27670807.jpg”. Notice, we passed our classifier id as a JSON string to the parameters method.

These codes generate the following output on the console.

Download the codes


Karl San Gabriel

Karl San Gabriel

Java and Enterprise Technologies Expert