0
Microsoft認知サービスのEmotion APIを使用しているときに、いつも応答文字列「[]」を取得しています。Microsoft Emotion ApisのNull応答
サンプル画像で確認すると、必要な結果(感情分析)が得られます。
私はフロントカメラの画像で何が問題なのか分かりません。
public void AnalyzeImage(final Bitmap bitmap) {
AsyncTask<InputStream, String, String> asyncTask = new AsyncTask<InputStream, String, String>() {
@Override
protected String doInBackground(InputStream... params) {
ByteArrayOutputStream output = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, output);
ByteArrayInputStream inputStream = new ByteArrayInputStream(output.toByteArray());
Map<String, Object> mapParams = new HashMap<>();
String path = serviceHost + "/recognize";
String uri = getUrl(path, mapParams);
mapParams.clear();
byte[] data = output.toByteArray();
mapParams.put("data", data);
String json="";
try {
json = (String) webInvoke("POST", uri, mapParams, "application/octet-stream", false);
Log.d("RESPONSE", json);
} catch (Exception e) {
e.printStackTrace();
}
return json;
}
@Override
protected void onPreExecute() {
}
@Override
protected void onProgressUpdate(String... progress) {
}
@Override
protected void onPostExecute(String result) {
tvImageEmotion.setText(result);
}
}.execute();
}
public static String getUrl(String path, Map<String, Object> params) {
StringBuffer url = new StringBuffer(path);
boolean start = true;
for (Map.Entry<String, Object> param : params.entrySet()) {
if (start) {
url.append("?");
start = false;
} else {
url.append("&");
}
try {
url.append(param.getKey() + "=" + URLEncoder.encode(param.getValue().toString(), "UTF-8"));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
return url.toString();
}
private Object webInvoke(String method, String url, Map<String, Object> data, String contentType, boolean responseInputStream) throws Exception, Exception {
HttpPost request = null;
request = new HttpPost(url);
boolean isStream = false;
if (contentType != null && !contentType.isEmpty()) {
request.setHeader("Content-Type", contentType);
if (contentType.toLowerCase().contains("octet-stream")) {
isStream = true;
}
} else {
request.setHeader("Content-Type", "application/json");
}
request.setHeader(headerKey, "0e843fb762464d82ae6f486bad99f629");
try {
request.setEntity(new ByteArrayEntity((byte[]) data.get("data")));
HttpResponse response = httpClient.execute(request);
int statusCode = response.getStatusLine().getStatusCode();
if (statusCode == 200) {
if (!responseInputStream) {
return readInput(response.getEntity().getContent());
} else {
return response.getEntity().getContent();
}
} else {
throw new Exception("Error executing POST request! Received error code: " + response.getStatusLine().getStatusCode());
}
} catch (Exception e) {
throw new Exception(e.getMessage());
}
}