watson-developer-cloud / java-sdk

:1st_place_medal: Java SDK to use the IBM Watson services.
http://watson-developer-cloud.github.io/java-sdk/
Apache License 2.0
590 stars 533 forks source link

[VR-v1] Add Support for ByteArray? #126

Closed RobWills closed 8 years ago

RobWills commented 8 years ago

How easy would it be to provide a recognize method for a bytearray instead of a file? I am unable to store a file on the Bluemix file system but have a bytearray of a file that I uploaded locally.

germanattanasio commented 8 years ago

can't you create a temporary file? What if we provide a recognize(InputStream stream) so instead of ByteArray we use an InputStream

jsstylos commented 8 years ago

+1 for an InputStream

RobWills commented 8 years ago

No I can’t seem to create a temporary file from within my Bluemix java session. I think this is by design.

I am trying to upload files locally but can’t save them as a java.io.File and instead have them as byte[].

I don’t think I can create a FileInputStream as there is no file to read from but I could create a ByteArrayInputStream OK so I assume that would be fine.

Rob

germanattanasio commented 8 years ago

@RobWills where are you getting the image? from a url?

Did you try to create a temporary file in Bluemix?

For example:

File image = File.createTempFile("picture", ".jpg"); 
byte[] bytes = // your image as byte array
FileOutputStream stream = new FileOutputStream(image);
try {
    stream.write(bytes);
} finally {
    stream.close();
}
RobWills commented 8 years ago

No, I have a server side component that uploads via my browser.

That can provide me with a java.io.File or a byte[].

I was simply using a File when developing locally in Eclipse with Tomcat but run into trouble when deploying to Bluemix. So the file exists locally behind my firewall so is inaccessible to the server side java code.

I think InputStream would work well (better than byte[] actually) and even though I actually wanted to store a File so that I could display it, the need to recognize a file without having to store it on the web server would be a good enhancement.

pplesky commented 8 years ago

@RobWills I am using this class to recognize an image: https://github.com/mstahv/watson-java-api/blob/master/watson-api/src/main/java/org/watson/visualrecognition/VisualRecognitionService.java

RobWills commented 8 years ago

Ah yes. I had seen that a few days ago. It looks like it has implemented something like what I want though it would be better in the base package ideally. Thanks very much for reminding me about that. Rob

From: Patrik Plesky [mailto:notifications@github.com] Sent: 30 November 2015 13:18 To: watson-developer-cloud/java-sdk Cc: Robin Wills Subject: Re: [java-sdk] Visual Recognition of bytearray? (#126)

@RobWillshttps://github.com/RobWills I am using this class to recognize an image: https://github.com/mstahv/watson-java-api/blob/master/watson-api/src/main/java/org/watson/visualrecognition/VisualRecognitionService.java

— Reply to this email directly or view it on GitHubhttps://github.com/watson-developer-cloud/java-sdk/issues/126#issuecomment-160624591.


No virus found in this message. Checked by AVG - www.avg.comhttp://www.avg.com Version: 2016.0.7227 / Virus Database: 4477/11090 - Release Date: 11/29/15

germanattanasio commented 8 years ago

I've added a recognize method that takes an InputStream.

byte[] bytes = // your image as byte array
InputStream is = new ByteArrayInputStream(bytes);

final LabelSet labelSet = new LabelSet();
labelSet.withLabelGroup("Animal").withLabelGroup("Food");

VisualRecognition service = new VisualRecognition();
VisualRecognitionImages labels = service.recognize("mypicture.jpg", is, labelSet);

This will be available in our next release, probably this Friday.

RobWills commented 8 years ago

German, As before very impressive turn around. Could I be really cheeky and ask for the code you wrote so I can extend the class temporarily? I’ve already missed my deadline and really want to get this sorted. Of course I will test accordingly. Rob

germanattanasio commented 8 years ago

@RobWills we usually push changes to the dev branch and only change master with releases.

You can always get the latest stable changes by switching to the dev branch and building the SNAPSHOT jar.

In this case, I added a utility class to send an InputStream in the request body.

You need two classes:

RobWills commented 8 years ago

German, Once again thank you for all your help. I decided simply to extend those two classes to include your enhancement for the time being as I am not too familiar with git / maven etc. It is working fine locally but for some reason not in Bluemix. I’m going to try and take a look at the logs. Rob

RobWills commented 8 years ago

log.txt

I've attached my log file. When running locally, it works fine. When deployed to Bluemix as war file, the call to recognize returned JSON but without the labels array (i.e. just the image_id and image_name elements). No crashes or anything. It just doesn't return the interesting bit!

I'm happy to do further testing if it will help you. Rob

germanattanasio commented 8 years ago

That's normal, if they image can't be classified it doesn't return labels. Check that you are calling recognize without sending labels so that the service runs all the classifiers

RobWills commented 8 years ago

German,

The same image gets classified when I run locally on Tomcat (the classifications are at the bottom of the log file I attached).

I did also try adding a label that was one of the classifications locally but no returned labels this way either.

It definitely doesn’t seem to be working ☹

Rob

germanattanasio commented 8 years ago

@RobWills we released the v2 of visual recognition which supports training your own classifier. I will be adding support for that in the SDK next week.