How to Detect Faces in Images using C# in .NET Framework

Cloudmersive
2 min readSep 30, 2020

Modern advances in software allow for the instant uploading and processing of images. When working with photographs that contain people, it is useful to have a system for identifying the different faces within the image, to help with metadata entry, captioning, and accessibility features. This API will allow you to detect faces within images instantly, with support for common file format such as PNG and JPEG.

Our first step is to run this command to install the SDK:

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

Then, we can call our function, FaceLocate:

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 FaceLocateExample
{
public void main()
{
// Configure API key authorization: Apikey
Configuration.Default.AddApiKey("Apikey", "YOUR_API_KEY");
var apiInstance = new FaceApi();
var imageFile = new System.IO.FileStream("C:\\temp\\inputfile", System.IO.FileMode.Open); // System.IO.Stream | Image file to perform the operation on. Common file formats such as PNG, JPEG are supported.
try
{
// Detect and find faces in an image
FaceLocateResponse result = apiInstance.FaceLocate(imageFile);
Debug.WriteLine(result);
}
catch (Exception e)
{
Debug.Print("Exception when calling FaceApi.FaceLocate: " + e.Message );
}
}
}
}

Now, you can instantly process your images and automatically return the number of faces detected within a photo. You can retrieve your free API Key from the Cloudmersive website. This will give you access to 800 monthly calls across our library of APIs.

--

--

Cloudmersive

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