2017-01-04 19 views
0

Microsoftコグニティブサービスエモーション(UWP)で使用するために、自分のPCから生の画像として画像を読み込もうとしています。 は、以下の私のコードの一部です:ストリーム画像としてPCから画像を読み込みます

 //Chose Image from PC 
    private async void chosefile_Click(object sender, RoutedEventArgs e) 
    { 


     //Open Dialog 
     FileOpenPicker open = new FileOpenPicker(); 
     open.ViewMode = PickerViewMode.Thumbnail; 
     open.SuggestedStartLocation = PickerLocationId.Desktop; 
     open.FileTypeFilter.Add(".jpg"); 
     open.FileTypeFilter.Add(".jpeg"); 
     open.FileTypeFilter.Add(".gif"); 
     open.FileTypeFilter.Add(".png"); 
     file = await open.PickSingleFileAsync(); 


     if (file != null) 
     {//imagestream is declared as IRandomAccessStream. 

      imagestream = await file.OpenAsync(Windows.Storage.FileAccessMode.Read); 
      var image = new BitmapImage(); 
      image.SetSource(imagestream); 
      imageView.Source = image; 
     } 
     else 
     { 
      // 
     } 
    } 

一部が上記正常に動作しますが、それはPC(ダイアログボックス)から写真を選択し、画像ボックスに表示されます。

private async void analyse_Click(object sender, RoutedEventArgs e) 
    { 
     try 
     { 
      emotionResult = await emotionServiceClient.RecognizeAsync(imagestream.AsStream()); 
     } 
     catch 
     { 
      output.Text = "something is wrong in stream"; 
     } 

     try { 
      if(emotionResult!= null) 
      { 
       Scores score = emotionResult[0].Scores; 
       output.Text = "Your emotions are: \n" + 
        "Happiness: " + score.Happiness + "\n" + 
        "Sadness: " + score.Sadness; 
      } 
     } 
     catch 
     { 
     output.Text = "Something went wrong"; 
     } 
    } 

私は のImageStreamはIRandomAccessStreamとして宣言されている)(エラーがimagestream.AsStreamが原因だと思います。

誰かがその部分を修正する方法を教えてもらえますか、エラーが実際に画像を正しく読み込んでいないためですか?

EDIT: ストリームを使用してストリームの代わりにemotionServiceClientに保存されたファイルを渡す代わりに、これを行うより良い方法がありますか?

+0

キャッチされた例外メッセージは何ですか? –

答えて

0

BitmapImageを作成してストリームの位置を上げているため、emotionServiceClient.RecognizeAsyncに電話をするまでに読み取り位置が最後になってしまうという問題があります。したがって、「巻き戻す」必要があります:

var stream = imagestream.AsStreamForRead(); 
stream.Position = 0; 
emotionResult = await emotionServiceClient.RecognizeAsync(stream); 
0

ファイルをメモリに保持しようとするのではなく、そのパスを保持してパスを使用してその時点でストリームを読み取るのはなぜですか?そこ例で

https://www.microsoft.com/cognitive-services/en-us/Emotion-api/documentation/GetStarted

using (Stream imageFileStream = File.OpenRead(imageFilePath)) 
       { 
        // 
        // Detect the emotions in the URL 
        // 
        emotionResult = await emotionServiceClient.RecognizeAsync(imageFileStream); 
        return emotionResult; 
       } 

オープンファイルダイアログの結果としてimageFilePathをキャプチャします。

+0

こんにちは!私は提案を試み、次のエラーが発生しました: System.InvalidOperationException:UIスレッドで同期操作を実行しないでください。 Task.Runでこのメソッドをラップすることを検討してください。 System.IO.FileStreamの でSystem.IO.WinRTFileSystem.EnsureBackgroundThread() System.IO.WinRTFileSystem.Open(String fullPath、FileModeモード、FileAccessアクセス、FileShare共有、Int32 bufferSize、FileOptionsオプション、FileStream親)で System.IO.FileStream..ctor(文字列パス、FileModeモード、FileAccessアクセス、FileShare共有、Int32 bufferSize、FileOptionsオプション) –

+0

System.IO.FileStream..ctor(文字列パス、FileModeモード、FileAccessアクセス、FileShare共有)で at System.IO.File .OpenRead(String path) 、EmoRec.MainPage。 d__8.MoveNext() –

関連する問題