Skip to content

Instantly share code, notes, and snippets.

@prombouts
Created July 21, 2017 09:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save prombouts/6b8e27940278126606e6a69b23742f91 to your computer and use it in GitHub Desktop.
Save prombouts/6b8e27940278126606e6a69b23742f91 to your computer and use it in GitHub Desktop.
public void DoWork()
{
var tmpfile = SaveImageCapture((BitmapSource)player.Source);
var emotions = await MakeRequest(tmpfile);
if (emotions == null ||
!emotions.Any())
{
txtNoFaces.Text = $"Detected faces: none";
return;
}
foreach (var eScores in emotions.Select(e => e.Scores))
{
var data = new Data
{
Date = DateTime.Now,
Anger = eScores.Anger,
Contempt = eScores.Contempt,
Disgust = eScores.Disgust,
Fear = eScores.Fear,
Happiness = eScores.Happiness,
Neutral = eScores.Neutral,
Sadness = eScores.Sadness,
Surprise = eScores.Surprise
};
_sharedViewModel.List.Add(data);
_sharedViewModel.UpdateAverageEmotions(eScores);
}
}
public static string SaveImageCapture(BitmapSource bitmap)
{
var file = Path.GetTempFileName();
var encoder = new JpegBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(bitmap));
encoder.QualityLevel = 80;
// Save Image
var fstream = new FileStream(file, FileMode.Create);
encoder.Save(fstream);
fstream.Close();
TempFiles.Add(file);
return file;
}
private static async Task<Emotion[]> MakeRequest(string image)
{
var emotionServiceClient = new EmotionServiceClient(<INSERT YOUR KEY HERE>);
try
{
using (Stream imageFileStream = File.OpenRead(image))
{
//
// Detect the emotions in the URL
//
return await emotionServiceClient.RecognizeAsync(imageFileStream);
}
}
catch (Exception ex)
{
return null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment