2017-01-16 1 views
0

Je reçois toujours la chaîne de réponse "[]" en retour lors de l'utilisation de Microsoft Cognitive services Emotion API, chaque fois que j'envoie l'image obtenue par caméra frontale dans android.Null Réponse de Microsoft Emotion Apis

Lorsque je l'ai vérifié avec l'image échantillon, il donne le résultat requis (analyse de l'émotion).

Je ne sais pas quel est le problème avec l'image de la caméra frontale.

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

Répondre

0

Je viens de trouver la solution au problème, en fait l'image cliqué caméra est mis en rotation à 90 degrés, c'est pourquoi API Emotion a été incapable de détecter tout visage dans le image.When je corrige la rotation de l'image, il fonctionne à la perfection.