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

Let’s keep this as simple as possible. How about two steps? Here we go:

First, install the API client needed using this command in the package manager console:

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

All that’s left to do is call RecognizeDetectPeople:

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 RecognizeDetectPeopleExample
{
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 RecognizeApi();
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 people, including locations, in an image
ObjectDetectionResult result = apiInstance.RecognizeDetectPeople(imageFile);
Debug.WriteLine(result);
}
catch (Exception e)
{
Debug.Print("Exception when calling RecognizeApi.RecognizeDetectPeople: " + e.Message );
}
}
}
}

Done. See? Pretty easy. Now let’s look at a quick example using this image:

Image for post
Image for post

And our results:

{
"Successful": true,
"Objects": [
{
"ObjectClassName": "person",
"Height": 1376,
"Width": 2117,
"Score": 0.9483813047409058,
"X": 1564,
"Y": 538
},
{
"ObjectClassName": "person",
"Height": 1317,
"Width": 761,
"Score": 0.8933420777320862,
"X": 8,
"Y": 413
},
{
"ObjectClassName": "person",
"Height": 1362,
"Width": 1089,
"Score": 0.8351640105247498,
"X": 679,
"Y": 474
},
{
"ObjectClassName": "person",
"Height": 1444,
"Width": 1810,
"Score": 0.6968361139297485,
"X": 1004,
"Y": 495
}
],
"ObjectCount": 4
}

We are given dimensions of each person, their locations, and an overall person count.

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