【问题标题】:Null Response from Microsoft Emotion Apis来自 Microsoft Emotion API 的空响应
【发布时间】:2017-03-31 14:13:35
【问题描述】:

每当我发送通过 android 中的前置摄像头获取的图像时,在使用 Microsoft 认知服务 Emotion API 时,我总是收到响应字符串“[]”作为回报。

当我用示例图像检查它时,它给出了所需的结果(情绪分析)。

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

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

【问题讨论】:

    标签: android microsoft-cognitive


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 2020-05-20
      • 2017-06-07
      • 1970-01-01
      • 1970-01-01
      • 2021-03-24
      • 2023-03-12
      • 2018-05-12
      • 2023-04-07
      • 1970-01-01
      相关资源
      最近更新 更多