IBM Watson Visual Recognition in Java

2019-05-30 15:40发布

问题:

I want to use IBM Watson Visual Recognition for my android app and want to call APIs in JAVA but i don't find any example or any reference to the list of methods in JAVA to use this service. You can see the JAVA examples are missing here. Please help me to find few suitable examples or any reference to these methods. Please also tell me what is bluemix platform and is it necessary to use it in order to use IBM Watson Visual Recognition? Thanks in Advance!

回答1:

Look at the Java SDK, and in particular the Visual Recognition example, which mimics the use case from the demo (node source code/training images for that here).

I am a developer evangelist for IBM Watson Developer Cloud.



回答2:

You need to:

  • Install the Java-SDK 3.3.0
  • Create an instance of the Visual Recognition service in Bluemix.
  • Update the snippet below with the username and password you get when you create the service in Bluemix.

Code:

public class VisualRecognitionExample {

  public static void main(String[] args) {
    VisualRecognition service = new VisualRecognition("2016-05-20");
    service.setUsernameAndPassword("<username>", "<password>");

    System.out.println("Classify using all the classifiers");
    options = new ClassifyImagesOptions.Builder()
      .images(new File("car.png"))
      .build();
    result = service.classify(options).execute();
    System.out.println(result);    
  }
}


回答3:

Check this tutorial (https://developer.ibm.com/recipes/tutorials/estimate-a-childs-age-based-on-photos-using-watson-visual-recognition/).

It uses an outdated version of the Watson Java SDK (https://github.com/watson-developer-cloud/java-sdk) so the code may has changed a little, but it's basically that.

In order to use Visual Recognition, you can use a regular bluemix account, so you can use the Watson Visual Recognition API

update

use this POM

<dependencies>
    <dependency>
        <groupId>com.ibm.watson.developer_cloud</groupId>
        <artifactId>java-sdk</artifactId>
        <version>3.0.0-RC1</version>
    </dependency>
    <dependency>
        <groupId>commons-io</groupId>
        <artifactId>commons-io</artifactId>
        <version>2.5</version>
    </dependency>
</dependencies>


回答4:

I checked with curl first and found solution with java you can use following code:

Used:OkClient3 jar

OkHttpClient client = new OkHttpClient();
            File file = new File(String.valueOf(path));
            RequestBody formBody = new MultipartBody.Builder()
                    .setType(MultipartBody.FORM)
                    .addFormDataPart("image_file", "images.jpeg", RequestBody.create(MediaType.parse("image/jpeg"), file))
                    .build();
            Request request = new Request.Builder().url(new URL("https://gateway-a.watsonplatform.net/visual-recognition/api/v3/collections/{classifier_id}/find_similar?limit=100&api_key=YOUR_API&version=2016-05-20")).post(formBody).build();
            Response response = client.newCall(request).execute(); 
            if (!response.isSuccessful())
                throw new Exception("Unexpected code " + response);
            System.out.println(response.message());
            jsonString = response.body().string().toString();
           System.out.println(jsonString);