Microsoft API的问题

时间:2016-06-21 04:19:33

标签: java android microsoft-cognitive

因此我无法使用微软的Android Emotion API。关于运行Face API,我没有任何问题;我能够得到面部矩形,但我无法让它在情感api上工作。我正在使用内置的Android相机本身拍摄图像。这是我正在使用的代码:

private void detectAndFrame(final Bitmap imageBitmap)
{
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    imageBitmap.compress(Bitmap.CompressFormat.PNG, 100, outputStream);
    ByteArrayInputStream inputStream =
            new ByteArrayInputStream(outputStream.toByteArray());
    AsyncTask<InputStream, String, List<RecognizeResult>> detectTask =
            new AsyncTask<InputStream, String, List<RecognizeResult>>() {
                @Override
                protected List<RecognizeResult> doInBackground(InputStream... params) {
                    try {
                        Log.e("i","Detecting...");
                        faces = faceServiceClient.detect(
                                params[0],
                                true,         // returnFaceId
                                false,        // returnFaceLandmarks
                                null           // returnFaceAttributes: a string like "age, gender"
                        );
                        if (faces == null)
                        {
                            Log.e("i","Detection Finished. Nothing detected");
                            return null;
                        }
                        Log.e("i",
                                String.format("Detection Finished. %d face(s) detected",
                                        faces.length));
                        ImageView imageView = (ImageView)findViewById(R.id.imageView);
                        InputStream stream = params[0];
                        com.microsoft.projectoxford.emotion.contract.FaceRectangle[] rects = new com.microsoft.projectoxford.emotion.contract.FaceRectangle[faces.length];
                        for (int i = 0; i < faces.length; i++) {
                            com.microsoft.projectoxford.face.contract.FaceRectangle rect = faces[i].faceRectangle;
                            rects[i] = new com.microsoft.projectoxford.emotion.contract.FaceRectangle(rect.left, rect.top, rect.width, rect.height);
                        }
                        List<RecognizeResult> result;
                        result =  client.recognizeImage(stream, rects);
                        return result;
                    } catch (Exception e) {
                        Log.e("e", e.getMessage());
                        Log.e("e", "Detection failed");
                        return null;
                    }
                }
                @Override
                protected void onPreExecute() {
                    //TODO: show progress dialog
                }
                @Override
                protected void onProgressUpdate(String... progress) {
                    //TODO: update progress
                }
                @Override
                protected void onPostExecute(List<RecognizeResult> result) {
                    ImageView imageView = (ImageView)findViewById(R.id.imageView);
                    imageView.setImageBitmap(drawFaceRectanglesOnBitmap(imageBitmap, faces));
                    MediaStore.Images.Media.insertImage(getContentResolver(), imageBitmap, "AnImage" ,"Another image");
                    if (result == null) return;
                    for (RecognizeResult res: result) {
                        Scores scores = res.scores;
                        Log.e("Anger: ", ((Double)scores.anger).toString());
                        Log.e("Neutral: ", ((Double)scores.neutral).toString());
                        Log.e("Happy: ", ((Double)scores.happiness).toString());
                    }

                }
            };
    detectTask.execute(inputStream);
}

我不断收到错误帖子请求400,表明JSON或脸部矩形存在某种问题。但我不知道从哪里开始调试这个问题。

1 个答案:

答案 0 :(得分:1)

您已经两次使用该流,因此第二次您已经在流的末尾。因此,您可以重置流,或者只是调用没有矩形的情感API(即跳过对面API的调用。)情感API将为您确定面部矩形。