2016-04-02 8 views
0

Emotion APIによって返された結果を表示するのに問題があります。結果はEmotion []の形式で返されます。コードは次のとおりですProject OxfordのEmotion APIの結果をC#で表示する

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

と私は、さまざまな感情の結果から最も支配的な感情を見つける必要があります。

答えて

1

私はAPI referenceに行きました。それはこのように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/にそれを貼り付け、それは私のためにいくつかのクラスを生成しました。 (私はルートクラスの名前をEmotionに変更し、scoresクラスを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; } 
} 

それから私は私がNewtsonsoft.Json Nuget packageを追加し、これを書いたユニットテストを書いて、私はそれをデシリアライズことができるかどうかを確認するために、MicrosoftのAPIページからJSONに貼り付け:

[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>が表示されるので、スコアを表示して最高のスコアで感情を見つけることができます。

+0

Emotion APIのサンプルファイル内でこれらのクラス定義を見つけましたが、問題をDictionary またはinbuiltコマンドでもリストに変換できませんでした。私は、Microsoft.ProjectOxford.Emotion.Contractファイルのクラス定義と矛盾することなく回避する必要があると思います。 –

+0

Scottの提案はかなり良いですが、すでにクライアントSDKを使用している場合は、Emotion []を文字列にシリアル化して_other_ Emotion []に戻します。別のオプションは次のようなものです: '' var top = emotions.Select(感情=> { var dict = new Dictionary (); foreach(typeof(スコア)の.GetProperties() ) {dict.Add(property.Name、(フロート)property.GetValue(emotion.Scores));} リターンdict.OrderByDescending(KV => kv.Value).ThenBy(KV => kv.Key ).First(); }); '' – cthrash

+0

@ScottHannen私のプロジェクトは助けてくれてありがとうございます:) –