Created
July 21, 2017 09:46
-
-
Save prombouts/6b8e27940278126606e6a69b23742f91 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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