How to detect eyes in a photo using C# in .NET Framework

Cloudmersive
3 min readOct 2, 2019

--

Facial feature detection is very important for photo filter apps, allowing for better and more detailed filter application. Normally, an AI must be trained on millions of images before this task can be automated. However, today we will look at how to harness a ready-made AI for this purpose using only a few lines of code in less than five minutes. Let’s get started.

To begin, we must install the API client that we need using the Package Manager console:

Install-Package Cloudmersive.APIClient.NET.ImageRecognition -Version 1.3.5

Now call FaceLocateWithLandmarks:

using System;
using System.Diagnostics;
using Cloudmersive.APIClient.NET.ImageRecognition.Api;
using Cloudmersive.APIClient.NET.ImageRecognition.Client;
using Cloudmersive.APIClient.NET.ImageRecognition.Model;
namespace Example
{
public class FaceLocateWithLandmarksExample
{
public void main()
{
// Configure API key authorization: Apikey
Configuration.Default.AddApiKey("Apikey", "YOUR_API_KEY");
// Uncomment below to setup prefix (e.g. Bearer) for API key, if needed
// Configuration.Default.AddApiKeyPrefix("Apikey", "Bearer");
var apiInstance = new FaceApi();
var imageFile = new System.IO.Stream(); // System.IO.Stream | Image file to perform the operation on. Common file formats such as PNG, JPEG are supported.
try
{
// Find faces and face landmarks (eyes, eye brows, nose, mouth) in an image
FaceLocateWithLandmarksResponse result = apiInstance.FaceLocateWithLandmarks(imageFile);
Debug.WriteLine(result);
}
catch (Exception e)
{
Debug.Print("Exception when calling FaceApi.FaceLocateWithLandmarks: " + e.Message );
}
}
}
}

And that’s all there is to it! Let’s take a quick look at an example image.

And our response:

{
"Successful": true,
"Faces": [
{
"LeftX": 187,
"TopY": 435,
"RightX": 633,
"BottomY": 881,
"LeftEyebrow": [
{
"X": 385,
"Y": 479
},
{
"X": 415,
"Y": 463
},
{
"X": 448,
"Y": 461
},
{
"X": 480,
"Y": 472
},
{
"X": 356,
"Y": 552
}
],
"RightEyebrow": [
null,
null,
null,
null,
null
],
"LeftEye": [
{
"X": 278,
"Y": 602
},
{
"X": 296,
"Y": 591
},
{
"X": 323,
"Y": 586
},
{
"X": 304,
"Y": 603
},
{
"X": 287,
"Y": 613
},
{
"X": 408,
"Y": 547
}
],
"RightEye": [
{
"X": 422,
"Y": 535
},
{
"X": 442,
"Y": 528
},
{
"X": 464,
"Y": 526
},
{
"X": 449,
"Y": 540
},
{
"X": 428,
"Y": 545
},
{
"X": 364,
"Y": 765
}
],
"BottomAndSidesOfFace": [
{
"X": 244,
"Y": 697
},
{
"X": 265,
"Y": 733
},
{
"X": 286,
"Y": 770
},
{
"X": 311,
"Y": 803
},
{
"X": 339,
"Y": 830
},
{
"X": 376,
"Y": 849
},
{
"X": 422,
"Y": 854
},
{
"X": 469,
"Y": 843
},
{
"X": 514,
"Y": 820
},
{
"X": 545,
"Y": 789
},
{
"X": 573,
"Y": 754
},
{
"X": 588,
"Y": 713
},
{
"X": 590,
"Y": 666
},
{
"X": 579,
"Y": 617
},
{
"X": 567,
"Y": 568
},
{
"X": 552,
"Y": 523
},
{
"X": 223,
"Y": 588
}
],
"NoseBridge": [
{
"X": 363,
"Y": 577
},
{
"X": 370,
"Y": 602
},
{
"X": 378,
"Y": 626
},
{
"X": 363,
"Y": 674
}
],
"NoseBottom": [
{
"X": 381,
"Y": 672
},
{
"X": 398,
"Y": 668
},
{
"X": 412,
"Y": 656
},
{
"X": 426,
"Y": 644
},
{
"X": 267,
"Y": 616
}
],
"LipsInnerOutline": [
{
"X": 396,
"Y": 727
},
{
"X": 413,
"Y": 719
},
{
"X": 427,
"Y": 713
},
{
"X": 457,
"Y": 713
},
{
"X": 433,
"Y": 717
},
{
"X": 419,
"Y": 724
},
{
"X": 403,
"Y": 731
}
],
"LipsOuterOutline": [
{
"X": 373,
"Y": 730
},
{
"X": 388,
"Y": 704
},
{
"X": 407,
"Y": 701
},
{
"X": 420,
"Y": 691
},
{
"X": 443,
"Y": 694
},
{
"X": 469,
"Y": 712
},
{
"X": 458,
"Y": 730
},
{
"X": 444,
"Y": 742
},
{
"X": 429,
"Y": 750
},
{
"X": 411,
"Y": 757
},
{
"X": 390,
"Y": 766
},
{
"X": 376,
"Y": 757
}
]
}
],
"FaceCount": 1,
"ErrorDetails": null
}

As you can see, numerous points are provided for each feature, allowing you to easily isolate them with a good degree of detail.

--

--

Cloudmersive
Cloudmersive

Written by Cloudmersive

There’s an API for that. Cloudmersive is a leader in Highly Scalable Cloud APIs.

No responses yet