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!
4 Answers
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.

- 321
- 1
- 2
-
Thanks @Rich Edwards. I have another question, is v3 available? because i tried to use it but my Android Studio is unable to find it on the web. And yes ClassifyImagesOptions is undefined in both v2 & v3. – Algor7 May 18 '16 at 20:07
-
I m using both these dependencies.. 'com.ibm.watson.developer_cloud:java-wrapper:1.0.3' & 'com.ibm.watson.developer_cloud:java-sdk:3.0.0-RC1' – Algor7 May 18 '16 at 20:19
-
Also unable to use "VisualRecognition.VERSION_DATE_2016_05_19". I am using "VisualRecognition.VERSION_DATE_2015_12_02" instead. – Algor7 May 18 '16 at 20:25
-
@Algor7 The service is not there yet. it will be release tomorrow. – German Attanasio May 19 '16 at 02:35
-
Could you please tell me why am i getting UnauthorizedException while i am using the right credentials(not one we use for login to Bluemix)? Do you guys remove the support to previous version(v2) when you launch the newer one(v3)? – Algor7 May 19 '16 at 19:16
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
andpassword
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);
}
}

- 22,217
- 7
- 47
- 63
-
Thanks @German Attanasio it is really helpful. I am selecting images from default Photo app and i am receiving image uri in onActivityResult(). How can i change this image uri into File object so that i can pass it as a pramater in service.classify(FileObject).execute(). – Algor7 May 19 '16 at 09:39
-
I am using this File file = new File(imageUri.getPath()); result = service.classify(file).execute(); but getting error on this line. – Algor7 May 19 '16 at 09:54
-
It is giving me "java.lang.IllegalArgumentException: image cannot be null or not be found" error – Algor7 May 19 '16 at 10:05
-
Try to send an InputStream instead of a file otherwise the application will need to have permission to access that file – German Attanasio May 19 '16 at 13:49
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>

- 751
- 4
- 29
-
Thankx @Leo for your answer. but i am getting error on importing ClassifyImagesOptions class.. I am using both dependencies.. 'com.ibm.watson.developer_cloud:java-wrapper:1.0.3' & 'com.ibm.watson.developer_cloud:java-sdk:3.0.0-RC1'. Any idea to solve this? – Algor7 May 18 '16 at 20:21
-
Also unable to use "VisualRecognition.VERSION_DATE_2016_05_19". I am using "VisualRecognition.VERSION_DATE_2015_12_02" instead. – Algor7 May 18 '16 at 20:25
-
I am getting "com.ibm.watson.developer_cloud.service.exception.UnauthorizedException: Unauthorized: Access is denied due to invalid credentials" error now. But i am sure i am using the right credentials(not the one that i use to login). – Algor7 May 19 '16 at 18:45
-
double click in the service in bluemix console and look for the credentials for that service on the left – Leo May 19 '16 at 20:44
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);

- 51
- 7