Cloud Training (Online)

Using Face API in C# provides an face recognition API that offers the developers and publishers to automatically detect and recognise faces in photos using robust , free REST API .

This was one of the API that we used during the Yahoo OpenHack India 2011 for our App called “Face the Music” . The application developed captures the human emotion (facial expression) via the webcam and plays the music based on the emotion. The emotion can be happy, frustrated, sad, surprised, scary, dreamy etc.

The API allows the third party developers to create Applications that use the’s Face Recognition technology.

The officially supports the following Client Libraries

  • PHP Client Library
  •  C#

Interestingly , there are community Client Libraries too which includes C# which was developed by Daren Willman

To use this C# Client Library is also easier . Just include the .cs file to your project .

The C# API has the classes FaceRestAPI and FaceRestAPI.FaceAPI which can be used to retreive the attributes of the picture .

Make sure that your register and get your API Keys

Once you get your API Key , you can then include the following code to get the attributes

FaceRestAPI obj = new FaceRestAPI(apikey,secretkey, "", true, "xml", "", "");
string userid = "" // registeredemailID;
List<string> str = new List<string>();

List<string> urls = new List<string>();
urls.Add(UrlFile); // http location of the picture
FaceRestAPI.FaceAPI objFaces = obj.faces_detect(urls, string.Empty, null, null);

The faces_detect will return the FaceAPI Object which includes a property rawdata .This is the Data for whcih the format was supplied for . In the above example , we use xml , so the FaceAPI.RawData will contain the XML string . It is also possible to get the data in Json format .

Just Load the Data in to the XmlDocument Object and you can retreive all the necessary properties .

To retreive the mood of the person , retreive the attribute “mood” from the XML string

XmlDocument xml = new XmlDocument();

XmlNodeList xnList = xml.SelectNodes(@"response/photos/photo/tags/tag/attributes/mood");
foreach (XmlNode xn in xnList)
   mood = xn["value"].InnerText;
   confidence = xn["confidence"].InnerText;

To check if the person in the picture is wearing the Glasses , use the Glasses node

XmlDocument xml = new XmlDocument();
XmlNodeList xnList3 = xml.SelectNodes(@"response/photos/photo/tags/tag/attributes/glasses");
foreach (XmlNode xn in xnList3)
   lblGlasses.Text = lblGlasses.Text + xn["value"].InnerText;

Similarly , to find the gender , use the gender tag of the XML data

XmlDocument xml = new XmlDocument();
XmlNodeList xnList1 = xml.SelectNodes(@"response/photos/photo/tags/tag/attributes/gender");
foreach (XmlNode xn in xnList1)
   lblGender.Text =lblGender.Text+ xn["value"].InnerText;

The also includes some samples and examples which can be found in the below location

  • Tagger-Widget
  • Tools
  • Poster Yourself
  • CelebrityFindr