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
593 stars 532 forks source link

[visual-recognition] Stream error when calling classify #180

Closed kaaron1 closed 8 years ago

kaaron1 commented 8 years ago

I am using the Watson sdk VisualRecognition class and receiving this error about 70% of the time...

[ERROR   ] IOException
expected 1912 bytes but received 2048
[err] java.lang.RuntimeException: java.net.ProtocolException: expected 1912 bytes but received 2048
[err]     at com.ibm.watson.developer_cloud.service.WatsonService.execute(WatsonService.java:124)
[err]     at com.ibm.watson.developer_cloud.service.WatsonService.executeRequest(WatsonService.java:182)
[err]     at com.ibm.watson.developer_cloud.visual_recognition.v2.VisualRecognition.classify(VisualRecognition.java:183)

I could not find a solution on stackoverflow.com or dw. I have an image url, and sending imageUrl.openStream() to the service.classify(fileName, InputStream, VisualClassifier) method. For some reason the bites of the image being streamed are often incorrect.

@germanattanasio: I am posting this here as per your request.

germanattanasio commented 8 years ago

Do you have the image you are using ?

kaaron1 commented 8 years ago

Here are two images I am using that I snagged from the InterConnect 2016 web page.

group group2

 

germanattanasio commented 8 years ago

Can you also include the code you are using to get the image input stream?

kaaron1 commented 8 years ago

So, the images are uploaded to a cloudant DB. The url comes from there. Here is an example.

String imgURLStr = " https://cd9c9031-ef98-4259-b335-d22cc7298aab-bluemix.cloudant.com/sample_nosql_db/1455046131022/g7236.jpg";
String filename = "g7236.jpg";
URL imgURL = new URL(imgURLStr);
service.classify(filename, new BufferedInputStream(imgURL.openStream()), null);
germanattanasio commented 8 years ago

Looks like this is a problem with the way we send the InputStream..

kaaron1 commented 8 years ago

I noticed that in com.ibm.watson.developer_cloud.http.InputStreamRequestBody the contentLength() method is overwritten to use inputstream.available(). It is my understanding that inputstream.available() should not be used as the content length. When I changed this code and returned -1, meaning the length is unknown, the errors I have been receiving stopped.

I will look into making these changes in a forked branch and sending for review.

germanattanasio commented 8 years ago

@kaaron1 you are the man! Please make the Pull request to dev!

kaaron1 commented 8 years ago

Complete. There is a pull request with the change waiting.   Thanks, -Kyle 

@kaaron1 you are the man!Please make the Pull request to dev! —Reply to this email directly or view it on GitHub.

 

germanattanasio commented 8 years ago

It's fixed but not released yet.

germanattanasio commented 8 years ago

release in a few seconds