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

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.

Image for post
Image for post

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.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store