2017-01-16 53 views
0

当我使用Microsoft Cognitive服务Emotion API时,只要我通过android中的前置摄像头发送图像,我总是会获得响应字符串“[]”作为回报。来自Microsoft Emotion Apis的无响应

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

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

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

回答

0

我只是找到了解决问题的办法,实际上是图像通过摄像头点击旋转到90度,这也是为什么情绪API无法检测到我纠正了图像旋转它的工作image.When任何脸型完美。