Using Face API in C#

by Ginktage | Tuesday, Aug 2, 2011 | 12 views

Face.com 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 Face.com API allows the third party developers to create Applications that use the Face.com’s Face Recognition technology.

The Face.com officially supports the following Client Libraries

  • PHP Client Library
  • http://api.face.com/lib/api_client.js

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

  • http://api.face.com/lib/FaceRestAPI.cs

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

The Face.com 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 http://developers.face.com/account/

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>();
str.Add(userid);

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();
xml.LoadXml(objFaces.rawData);

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();
xml.LoadXml(objFaces.rawData);
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();
xml.LoadXml(objFaces.rawData);
XmlNodeList xnList1 = xml.SelectNodes(@"response/photos/photo/tags/tag/attributes/gender");
foreach (XmlNode xn in xnList1)
{
   lblGender.Text =lblGender.Text+ xn["value"].InnerText;
}

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

  • Tagger-Widget
  • Tools
  • Poster Yourself
  • CelebrityFindr
If you enjoyed this post, please consider leaving a comment or subscribing to the RSS feed to have future articles delivered to your feed reader.
Like it? Share it!

3 Comments so far. Feel free to join this conversation.

  1. Teodora R. MS July 5, 2014 at 8:43 pm - Reply

    Thank you for this article! This was amazing! But as Mario Fraiss has said, Face.com is no longer useful. Do you know something similar to Face.com for mood detection with a library for c#?

  2. Mario Fraiß November 16, 2012 at 11:45 pm - Reply

    Hi, great article, thx for that! Unfortunately Face.com is now a part of facebook and so no longer useful :(

    Greetings from Austria
    Mario

Leave A Response