来自Microsoft Emotion Apis的空响应

时间:2017-01-16 08:36:01

标签: android microsoft-cognitive

我总是得到响应字符串" []"在使用Microsoft认知服务Emotion API时,无论何时我通过android中的前置摄像头发送图像。

当我用样本图像检查时,它会提供所需的结果(情绪分析)。

我不知道前置摄像头图像的问题是什么。

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());
    }
}

1 个答案:

答案 0 :(得分:0)

我刚刚找到问题的解决方案,实际上通过相机点击的图像被旋转到90度,这就是为什么Emotion API无法检测图像中的任何面部。当我更正图像旋转时,它完美地工作。