是否可以同时调用 2 API 并显示组合的答案字符串?

Is it possible to call 2 API's and displaying a combined answer string at the same time?

由于 await 和 async 函数,我无法做到这一点。 我想要一个应用程序,可以实时分析一张脸,并显示他脸上的矩形,上面应该写着性别、年龄、情绪、情绪信心。 所以我想同时使用 Face API 和 Emotion API。 谢谢

假设您使用的是 C# SDK,您可以等待这两个任务完成。代码会像这样:

static bool SameFace(Microsoft.ProjectOxford.Face.Contract.FaceRectangle r1,
        Microsoft.ProjectOxford.Common.Rectangle r2)
{
    // Fuzzy match of rectangles...
    return Math.Abs((r1.Top + r1.Height / 2) - (r2.Top + r2.Height / 2)) < 3 && 
        Math.Abs((r1.Left + r1.Width / 2) - (r2.Left + r2.Width / 2)) < 3;
}

void Test(string imageUrl)
{
    var faceClient = new FaceServiceClient(FACE_API_KEY);
    var emotionClient = new EmotionServiceClient(EMOTION_API_KEY);

    var faceTask = faceClient.DetectAsync(imageUrl, false, false, new FaceAttributeType[] { FaceAttributeType.Age, FaceAttributeType.Gender });
    var emotionTask = emotionClient.RecognizeAsync(imageUrl);

    Task.WaitAll(faceTask, emotionTask);

    var people = from face in faceTask.Result
                 from emotion in emotionTask.Result
                 where SameFace(face.FaceRectangle, emotion.FaceRectangle)
                 select new {
                     face.FaceAttributes.Gender,
                     face.FaceAttributes.Age,
                     emotion.Scores
                 };

    // Do something with 'people'
}

棘手的部分是这两个 API 没有相同的矩形类型,并且给出的值略有不同,因此是模糊匹配。

重复。

请参阅该线程以获取更多信息。