gray laptop computer showing html codes in shallow focus photography

Using Face API in C#

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
  •  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 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

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

    3 Comments

  1. Mario Fraiß
    November 16, 2012
    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

  2. Teodora R. MS
    July 5, 2014
    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#?

Leave A Reply

Your email address will not be published. Required fields are marked *

You May Also Like

C# Compiler Error CS0442 – ‘Property’: abstract properties cannot have private accessors Reason for the Error You’ll get this error...
This is a really simple one . Below is a simple example of an enum called “Designation” defined with the...
This blog post explain the usage of the Checked Block in .NET and how you can use them in Visual...