Search This Blog

Friday, April 14, 2017

How to install and configure IBM Watson JAVA SDK

 

I know Java SDK can be downloaded from the GIT but I had a few problems to configure it properly.

 

Please follow the below steps to configure JAVA SDK for IBM Watson

First download the SDK from the below URL

https://github.com/watson-developer-cloud/java-sdk

Use clone zip or GIT clone or what ever.

 

Now go to java-sdk folder and run ( Assuming you have GRADLE installed)

> gradle eclipse

It will run for a while and will generate the required JARS and dependencies for your master JAVA SDK project.

 

 

image

 

 

After this, go to Eclipse and import the main java-sdk project.

 

 

image

 

Once completed, your workspace will be ready.

 

Lets test one of the visual recognition API.

 

 

Code

===================================

package com.ibm.watson.developer_cloud.visual_recognition.v3;

import java.io.File;

import com.ibm.watson.developer_cloud.visual_recognition.v3.model.ClassifyImagesOptions;
import com.ibm.watson.developer_cloud.visual_recognition.v3.model.VisualClassification;

public class VisualRecognitionExample {
   
      private static final String VERSION_DATE_2016_05_20 = "2016-05-19";
    public static void main(String[] args) {
         
   

   
    VisualRecognition service = new VisualRecognition(VisualRecognition.VERSION_DATE_2016_05_20);
    service.setApiKey("your api key get from blue mix dashboard , create a VR service");


    System.out.println("Classify an image");
    ClassifyImagesOptions options = new ClassifyImagesOptions.Builder()
        .images(new File("src/test/resources/visual_recognition/car.png"))
        .build();
    VisualClassification result = service.classify(options).execute();
    System.out.println(result);
   
}
}

===========================================

 

Result will be returned as

 

{
  "images": [
    {
      "classifiers": [
        {
          "classes": [
            {
              "class": "stock car (racing)",
              "score": 0.959,
              "type_hierarchy": "/vehicle/stock car (racing)"
            },
            {
              "class": "car",
              "score": 0.996
            },
            {
              "class": "vehicle",
              "score": 0.996
            },
            {
              "class": "race car",
              "score": 0.747,
              "type_hierarchy": "/vehicle/race car"
            },
            {
              "class": "gray color",
              "score": 0.937
            }
          ],
          "classifier_id": "default",
          "name": "default"
        }
      ],
      "image": "car.png"
    }
  ],
  "images_processed": 1
}

My image looks like this

image

 

 

Point to note is while importing the java sdk, make sure you import the project and not choose the “open project from file system option in eclipse “

Also you need to exclude parent folder from the build if you do a build all, it might error on build(Remove all sources and do Ctrl+B to build all projects)

image

 

 

Now you can explore other services from workspace itself, keep getting credentials apikey from blue mix dashboard and test all the services.

 

 

Keep Reading !

No comments:

Post a Comment