I essentially copied the sample Detection API Java code that shows how to upload a photo and get back results showing gender. This works fine as long as the image is referenced by a URL, but if instead I send the image inline, after reading it from disk and Base64 encoding it, although I get a HTML return code of 200, the JSON data returned has no objects data.
In other words, the following works fine and returns the expected gender:
private static final String api = "https://dev.sighthoundapi.com/v1/detections?type=face,person&faceOption=gender";
private static String imageURL = "https://www.chiaramail.com/confirm_reg_files/20160313_142257.jpg";
JSONObject imageObj = new JSONObject();
try {
imageObj.put("image", imageURL);
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 = imageObj.toString().getBytes();
connection.setFixedLengthStreamingMode(body.length);
OutputStream os = connection.getOutputStream();
os.write(body);
os.flush();
int httpCode = connection.getResponseCode();
String json = "";
JSONObject jObj;
if ( httpCode == 200 ){
InputStream is = connection.getInputStream();
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "n");
}
is.close();
json = sb.toString();
} catch (Exception e) {
Log.e("Buffer Error", "Error converting result: " + e.toString());
}
try {
jObj = new JSONObject(json);
return jObj;
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data: " + e.toString());
}
} else {
Log.e("JSON Parser", "Error: " + connection.getErrorStream());
}
} catch (Exception ex) {
Log.e("JSON Parser", "Error: " + ex);
}
but the following returns '[]' for objects:
JSONObject imageObj = new JSONObject();
try {
Bitmap bm = BitmapFactory.decodeFile(fileUri.getPath());
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.JPEG, 100, baos); //bm is the bitmap object
byte[] b = baos.toByteArray();
String encodedImage = Base64.encodeToString(b, Base64.NO_WRAP);
imageObj.put("image", encodedImage);
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 = imageObj.toString().getBytes();
connection.setFixedLengthStreamingMode(body.length);
OutputStream os = connection.getOutputStream();
os.write(body);
os.flush();
int httpCode = connection.getResponseCode();
String json = "";
JSONObject jObj;
if ( httpCode == 200 ){
InputStream is = connection.getInputStream();
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "n");
}
is.close();
json = sb.toString();
} catch (Exception e) {
Log.e("Buffer Error", "Error converting result: " + e.toString());
}
try {
jObj = new JSONObject(json);
return jObj;
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data: " + e.toString());
}
} else {
Log.e("JSON Parser", "Error: " + connection.getErrorStream());
}
} catch (Exception ex) {
Log.e("JSON Parser", "Error: " + ex);
}
Any idea what I'm doing wrong?
