I am a newbie and I tried to figure out how could I call Sighthound API to analyze an image that I have on my local drive. I tried it but it gave me an error saying that I had a wrong encoded image as follow:
- Code: Select all
[color=#FF4000]{"error":"ERROR_MEDIA_DATA","reason":"Wrong encoded image: /Users/user//Documents/men.jpg","reasonCode":40005}[/color]
Please help if you happen to know what's the problem is. Many Thanks!!
Below is my code
[code]
/code]
import java.io.IOException;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import javax.json.Json;
import javax.json.JsonObject;
import javax.json.JsonReader;
import javax.json.*;
public class UrlSample {
// https://www.sighthound.com/products/cloud
private static final String api = "https://dev.sighthoundapi.com/v1/detections?type=face,person&faceOption=landmark,gender";
static String YourAPIKey = "xxxxxx";
private static final String accessToken = YourAPIKey;
private static String imageUrl = "/Users/user//Documents/men.jpg";
// private static String imageUrl = "https://vgy.me/ZPkPaX.jpg";
//private static String imageUrl = "https://www.chiaramail.com/confirm_reg_files/20160313_142257.jpg";
public static void main(String[] args) throws IOException {
JsonObject jsonImage = Json.createObjectBuilder().add("image", imageUrl).build();
URL apiURL = new URL(api);
HttpURLConnection connection = (HttpURLConnection) apiURL.openConnection();
connection.setRequestProperty("Content-Type", "application/json");
connection.setRequestProperty("X-Access-Token", accessToken);
connection.setRequestMethod("POST");
connection.setDoInput(true);
connection.setDoOutput(true);
byte[] body = jsonImage.toString().getBytes();
connection.setFixedLengthStreamingMode(body.length);
OutputStream os = connection.getOutputStream();
os.write(body);
os.flush();
int httpCode = connection.getResponseCode();
if ( httpCode == 200 ){
JsonReader jReader = Json.createReader(connection.getInputStream());
JsonObject jsonBody = jReader.readObject();
System.out.println(jsonBody);
} else {
JsonReader jReader = Json.createReader(connection.getErrorStream());
JsonObject jsonError = jReader.readObject();
System.out.println(jsonError);
}
}
}
