Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Microsoft Cognitive Services Face and Emotion API demo
namespace FaceRecognitionDemo
{
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Threading.Tasks;
using Microsoft.ProjectOxford.Emotion;
using Microsoft.ProjectOxford.Emotion.Contract;
using Microsoft.ProjectOxford.Face;
using Microsoft.ProjectOxford.Face.Contract;
using ImageProcessor;
using ImageProcessor.Imaging.Formats;
internal class Program
{
/* Private constants */
private const string DropboxPublicFolderUrl = "https://dl.dropboxusercontent.com/u/xxxxxxx/";
private const string DropboxPublicFolderPath = @"C:\Dropbox\Public\";
private const string EmotionApiKey = "05e8853bfe6546c9ac5865e00541761d";
private const string FaceApiKey = "220817a07db44ad9ac5ba21d9b31bbd4";
private const string FaceImageFilename = "face-image.jpg";
private const string OriginalImageUrl = "http://bit.ly/295NCb1";
/* Private static methods */
private static void CreateFaceImage(string imageUrl, Face face)
{
using (var webClient = new WebClient())
{
using (Stream stream = webClient.OpenRead(imageUrl))
{
if (stream == null)
{
throw new Exception("Failed to open image URL for reading");
}
using (var imageFactory = new ImageFactory())
{
Rectangle faceRectangle = new Rectangle(face.FaceRectangle.Left, face.FaceRectangle.Top, face.FaceRectangle.Width, face.FaceRectangle.Height);
imageFactory.Load(stream).Crop(faceRectangle).Format(new JpegFormat()).Save(Path.Combine(DropboxPublicFolderPath, FaceImageFilename));
}
}
}
}
private static async Task<Face> ExtractFace(string imageUrl)
{
IFaceServiceClient faceServiceClient = new FaceServiceClient(FaceApiKey);
IEnumerable<FaceAttributeType> requestedFaceAttributes = new List<FaceAttributeType>
{
FaceAttributeType.Age,
FaceAttributeType.Gender,
FaceAttributeType.HeadPose,
FaceAttributeType.Smile,
FaceAttributeType.FacialHair,
FaceAttributeType.Glasses
};
Face[] faces = await faceServiceClient.DetectAsync(imageUrl, true, true, requestedFaceAttributes);
return faces.Single();
}
private static async Task<Emotion> IdentifyEmotion(string imageUrl)
{
EmotionServiceClient emotionServiceClient = new EmotionServiceClient(EmotionApiKey);
Emotion[] emotions = await emotionServiceClient.RecognizeAsync(imageUrl);
return emotions.Single();
}
private static void Main()
{
Face face = ExtractFace(OriginalImageUrl).Result;
Console.WriteLine($"Age: {face.FaceAttributes.Age}");
Console.WriteLine($"Gender: {face.FaceAttributes.Gender.ToLower()}");
Console.WriteLine($"Smiling: {(face.FaceAttributes.Smile >= 0.5 ? "yes" : "no")}");
Console.WriteLine($"Beard: {(face.FaceAttributes.FacialHair.Beard >= 0.5 ? "yes" : "no")}");
Console.WriteLine($"Moustache: {(face.FaceAttributes.FacialHair.Moustache >= 0.5 ? "yes" : "no")}");
Console.WriteLine($"Sideburns: {(face.FaceAttributes.FacialHair.Sideburns >= 0.5 ? "yes" : "no")}");
Console.Write("Glasses: ");
switch (face.FaceAttributes.Glasses)
{
case Glasses.ReadingGlasses:
Console.WriteLine("reading");
break;
case Glasses.Sunglasses:
Console.WriteLine("sunglasses");
break;
case Glasses.SwimmingGoggles:
Console.WriteLine("swimming goggles");
break;
default:
Console.WriteLine("none");
break;
}
CreateFaceImage(OriginalImageUrl, face);
Emotion emotion = IdentifyEmotion($"{DropboxPublicFolderUrl}{FaceImageFilename}").Result;
Console.WriteLine($"Anger: {emotion.Scores.Anger}");
Console.WriteLine($"Contempt: {emotion.Scores.Contempt}");
Console.WriteLine($"Disgust: {emotion.Scores.Disgust}");
Console.WriteLine($"Fear: {emotion.Scores.Fear}");
Console.WriteLine($"Happiness: {emotion.Scores.Happiness}");
Console.WriteLine($"Neutral: {emotion.Scores.Neutral}");
Console.WriteLine($"Sadness: {emotion.Scores.Sadness}");
Console.WriteLine($"Surprise: {emotion.Scores.Surprise}");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment