Skip to content

Instantly share code, notes, and snippets.

@codePrincess
Created August 25, 2017 07:14
Show Gist options
  • Save codePrincess/2123dda3d453ff7bf4db432afac202a7 to your computer and use it in GitHub Desktop.
Save codePrincess/2123dda3d453ff7bf4db432afac202a7 to your computer and use it in GitHub Desktop.
Azure Function for calling Cognitive Services Face API
#r "Newtonsoft.Json"
using System;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Linq;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
public static TraceWriter logger = null;
public static async Task<object> Run(HttpRequestMessage req, TraceWriter log)
{
string myContent = req.Content.ReadAsStringAsync().Result;
logger = log;
if (myContent == String.Empty) {
logger.Info("no image found");
return req.CreateResponse(HttpStatusCode.BadRequest, new {
error = "Image might be empty"
});
}
string apiKey = "xxx#placeYourKeyHere#xxx";
string csOCRUrl = "https://westeurope.api.cognitive.microsoft.com/face/v1.0/detect?returnFaceLandmarks=true&returnFaceAttributes=age,gender,hair,emotion";
HttpClient client = new HttpClient();
client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", apiKey);
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/octet-stream"));
HttpContent content = req.Content;
content.Headers.ContentType = new MediaTypeWithQualityHeaderValue("application/octet-stream");
HttpResponseMessage result = await client.PostAsync(csOCRUrl, content);
var csData = result.Content.ReadAsStringAsync().Result;
if (csData != null) {
var thelist = JsonConvert.DeserializeObject<List<Dictionary<string,object>>>(csData);
var dictionaries = thelist.First();
logger.Info(dictionaries.ToString());
JObject faceAttributes = (JObject) dictionaries["faceAttributes"] ;
logger.Info(faceAttributes.ToString());
Dictionary<string, object> faceAttDict = faceAttributes.ToObject<Dictionary<string, object>>();
JObject emotion = (JObject) faceAttDict["emotion"];
Dictionary<string, object> emoDict = emotion.ToObject<Dictionary<string, object>>();
logger.Info(emoDict.ToString());
double happy = (double) emotion["happiness"];
double age = (double) faceAttributes["age"];
string gender = (string) faceAttributes["gender"];
if (gender == "male" && age > 40 && happy > 0.5) {
return req.CreateResponse(HttpStatusCode.OK, new {
content_type = "105",
content_description = "It seems like to be - Stephan the Greatest",
code = HttpStatusCode.OK
});
}
if (gender == "female" && happy > 0.5) {
return req.CreateResponse(HttpStatusCode.OK, new {
content_type = "106",
content_description = "You are looking great today",
code = HttpStatusCode.OK
});
}
if (happy > 0.5) {
return req.CreateResponse(HttpStatusCode.OK, new {
content_type = "100",
content_description = "Great to have you here today!",
code = HttpStatusCode.OK
});
} else {
return req.CreateResponse(HttpStatusCode.OK, new {
content_type = "101",
content_description = "Cheer up mate!",
code = HttpStatusCode.OK
});
}
}
else {
return req.CreateResponse(HttpStatusCode.OK, new {
TypeOfId = "Unknown",
code = HttpStatusCode.UnsupportedMediaType
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment