Skip to content

Instantly share code, notes, and snippets.

@jfversluis
Created August 15, 2016 07:28
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 jfversluis/b1f9b804ddb8565741af35bcb49c1643 to your computer and use it in GitHub Desktop.
Save jfversluis/b1f9b804ddb8565741af35bcb49c1643 to your computer and use it in GitHub Desktop.
Code to receive an image file on the Bot Framework and send it to the Emotion API
var imageCount = activity.Attachments.Count(a => a.ContentType.Contains("image"));
if (imageCount > 0)
{
// Notify user that we got something
var message = imageCount == 1 ? $"You've sent 1 image" : $"You've sent {imageCount} images";
var imageReply = activity.CreateReply(message);
await connector.Conversations.ReplyToActivityAsync(imageReply);
}
foreach (var attachment in activity.Attachments.Where(a => a.ContentType.Contains("image")).Take(1))
{
var imageBytes = ConvertContentToByteArray(attachment.ContentUrl);
#error TODO: Add your Emotion API key here
var emotionClient = new EmotionServiceClient("b436fxxxxxxxxxxxxxxcb30");
var emotionResults = await emotionClient.RecognizeAsync(new MemoryStream(imageBytes));
var aggregatedResults = new Dictionary<string, float>();
foreach (var e in emotionResults)
{
foreach (var s in e.Scores.ToRankedList())
{
if (!aggregatedResults.ContainsKey(s.Key))
aggregatedResults.Add(s.Key, s.Value);
else
aggregatedResults[s.Key] += s.Value;
}
}
var emotionsMessage = $"I recognize {emotionResults.Count()} people, in general their emotion seems to be: {aggregatedResults.OrderByDescending(r => r.Value).First().Key}";
var emotionsReply = activity.CreateReply(emotionsMessage);
await connector.Conversations.ReplyToActivityAsync(emotionsReply);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment