在 C# 中显示 Project Oxford 的 Emotion API 结果

Displaying Project Oxford's Emotion API result in C#

我无法显示 Emotion API 返回的结果。结果以 Emotion[] 的形式返回。代码如下

    private async void button2_Click(object sender, EventArgs e)
    {
        try
        {
            pictureBox2.Image = (Bitmap)pictureBox1.Image.Clone();
            String s = System.Windows.Forms.Application.StartupPath + "\" + "emotion.jpg";
            pictureBox2.Image.Save(s);

            string imageFilePath = s;// System.Windows.Forms.Application.StartupPath + "\" + "testing.jpg"; 
            Uri fileUri = new Uri(imageFilePath);

            BitmapImage bitmapSource = new BitmapImage();
            bitmapSource.BeginInit();
            bitmapSource.CacheOption = BitmapCacheOption.None;
            bitmapSource.UriSource = fileUri;
            bitmapSource.EndInit();

         //   _emotionDetectionUserControl.ImageUri = fileUri;
           // _emotionDetectionUserControl.Image = bitmapSource;

            System.Windows.MessageBox.Show("Detecting...");

            ***Emotion[] emotionResult*** = await UploadAndDetectEmotions(imageFilePath);

            System.Windows.MessageBox.Show("Detection Done");

        }
        catch (Exception ex)
        {
            System.Windows.MessageBox.Show(ex.ToString());
        }
    }

我需要从各种情绪的结果中找出最主要的情绪。

我去了API reference。它 returns JSON 像这样:

[
  {
    "faceRectangle": {
      "left": 68,
      "top": 97,
      "width": 64,
      "height": 97
    },
    "scores": {
      "anger": 0.00300731952,
      "contempt": 5.14648448E-08,
      "disgust": 9.180124E-06,
      "fear": 0.0001912825,
      "happiness": 0.9875571,
      "neutral": 0.0009861537,
      "sadness": 1.889955E-05,
      "surprise": 0.008229999
    }
  }
]

我将其粘贴到 http://json2csharp.com/ 中,它为我生成了一些 classes。 (我将根 class 重命名为 Emotion 并将 scores class 替换为 IDictionary<string, double>。那是因为你不只是想要 属性 对于每种情绪。您需要一个可以排序的集合以找到最高的情绪。(IDictionary<string, double> 是最容易将 json 反序列化为的集合。)

public class FaceRectangle
{
    public int left { get; set; }
    public int top { get; set; }
    public int width { get; set; }
    public int height { get; set; }
}

public class Emotion
{
    public FaceRectangle faceRectangle { get; set; }
    public IDictionary<string, double> scores { get; set; }
}

然后我写了一个单元测试并粘贴到微软API页面的JSON中,看看我是否可以反序列化它。我添加了 Newtsonsoft.Json Nuget package 并写了这个:

[TestClass]
public class DeserializeEmotion
{
    [TestMethod]
    public void DeserializeEmotions()
    {
        var emotions = JsonConvert.DeserializeObject<Emotion[]>(JSON);
        var scores = emotions[0].scores;
        var highestScore = scores.Values.OrderByDescending(score => score).First();
        //probably a more elegant way to do this.
        var highestEmotion = scores.Keys.First(key => scores[key] == highestScore);
        Assert.AreEqual("happiness", highestEmotion);
    }

    private const string JSON =
        "[{'faceRectangle': {'left': 68,'top': 97,'width': 64,'height': 97},'scores': {'anger': 0.00300731952,'contempt': 5.14648448E-08,'disgust': 9.180124E-06,'fear': 0.0001912825,'happiness': 0.9875571,'neutral': 0.0009861537,'sadness': 1.889955E-05,'surprise': 0.008229999}}]";

}

测试通过,就这样。您有一个包含分数的 Dictionary<string,double>,因此您既可以显示它们,又可以找到得分最高的情绪。