Age detection of photos using C# in .NET Framework

Age detection is an excellent way to gather user data and tailor advertising subjects. Normally this useful functionality is locked behind AI training and other tedious processes. However, today we will look at how to accomplish this very quickly and easily. Let’s get straight to it.

We begin simply enough by installing our API client using NuGet:

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

Now call the FaceDetectAge function and provide your image file using the following snippet.

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 FaceDetectAgeExample
{
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
{
// Detect the age of people in an image
AgeDetectionResult result = apiInstance.FaceDetectAge(imageFile);
Debug.WriteLine(result);
}
catch (Exception e)
{
Debug.Print("Exception when calling FaceApi.FaceDetectAge: " + e.Message );
}
}
}
}

And that’s it! All done. Let’s take a quick look at an example:

Image for post
Image for post

And here is a response for our image:

{
"Successful": true,
"PeopleWithAge": [
{
"FaceLocation": {
"LeftX": 785,
"TopY": 89,
"RightX": 2005,
"BottomY": 1160
},
"AgeClassificationConfidence": 0.9,
"AgeClass": "25-32",
"Age": 31.739656448364258
}
],
"PeopleIdentified": 1
}

As you can see, this function supports multiple people and provides their location in the photograph, as well as the age information.

Written by

There’s an API for that. Cloudmersive is a leader in Highly Scalable Cloud 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