Introduction to Amazon Rekognition

Amazon Rekognition is a highly featured, visual analysis tool that relies on pre-trained machine learning models. Amazon Rekognition enables you to analyze images as well as video without being a machine learning expert. Amazon Rekognition can flag inappropriate content, differentiate fake and real faces, can identify celebrities and can extract text. When analyzing video, Amazon Rekognition can also detect and analyze motion.

Photo by Nicholas Green on Unsplash

The Solution

In this tutorial, we’ll bring you through a simple app that analyzes a picture and counts the number of faces within it.

Remember, for any example solution from AWS with .NET, we focus on the code that exemplifies the problem we are trying to solve. We don’t include logging, input validation, exception handling, etc., and we embed the configuration data within classes instead of using environment variables, configuration files, key/value stores and the like. These items should not be skipped for proper solutions.

Prerequisites

To complete this solution, you will need the .NET CLI which is included in the .NET SDK. In addition, you will need to create an AWS IAM user with programmatic access with the appropriate permissions to interact with Amazon Recognition. In addition, you will need to download the AWS CLI and configure your environment.

Warning: some AWS services may have fees associated with them.

Our Dev Environment

This tutorial was developed using Ubuntu 23.10, .NET 8 SDK and Visual Studio Code 1.86.1. Some commands/constructs may vary across systems.

Create the .NET Recognition Application Project

Our first task is to create the application with the following command:

$ dotnet new console -n CountFaces --use-program-main

Add App Dependencies

Let’s now add the dependencies needed for the AWS SDK.

$ dotnet add package AWSSDK.Core
$ dotnet add package AWSSDK.Rekognition

Developing the Amazon Rekognition App

In the newly created .NET app, let’s open the Program.cs file and change the “Main” function definition to:

static async Task Main(string[] args)

Now, let’s create a few variables to hold some data.

string photoFilePath = "[path to the file to process]";
string photoFileName = "faces.jpg";
string collectionId = "dotnet-test-collection";

Let’s continue to modify the Main function and create a memory stream and read the contents of the photo file into it.

byte[] fileBytes = File.ReadAllBytes(photoFilePath);
MemoryStream fileMemoryStream = new MemoryStream(fileBytes);

The next step is to create the Amazon Rekognition client that will allow us to interact with the Amazon Rekognition service. And, the first thing we’ll do with the client is create a Rekognition collection.

var rekognitionClient = new AmazonRekognitionClient();        

var createCollectionRequest = new CreateCollectionRequest
{
    CollectionId = collectionId
};

CreateCollectionResponse createCollectionResponse = await 
   rekognitionClient.CreateCollectionAsync(createCollectionRequest);

Console.WriteLine("Collection Created with Arn, " + 
   createCollectionResponse.CollectionArn);

With the collection created, we’ll upload the image using an IndexFacesRequest object, passing in the memory stream we created earlier and referencing the collection that we just created.

Image image = new Image
{ 
    Bytes  = fileMemoryStream
};

IndexFacesRequest indexFacesRequest = new IndexFacesRequest
{
    Image = image,
    CollectionId = collectionId,
    ExternalImageId = photoFileName
};

IndexFacesResponse indexFacesResponse = await 
   rekognitionClient.IndexFacesAsync(indexFacesRequest);

To get the number of faces that were indexed by Amazon Rekognition, we can use the FaceRecords.Count property of the IndexFacesResponse object.

int count = indexFacesResponse.FaceRecords.Count;
Console.WriteLine("Face Count: " + count.ToString());

Now that we have completed our goal of counting the faces in the photo, we can delete the collection that we created.

var deleteCollectionRequest = new DeleteCollectionRequest()
{
    CollectionId = collectionId
};

var deleteCollectionResponse = await 
   rekognitionClient.DeleteCollectionAsync(deleteCollectionRequest);

Console.WriteLine("Deleted Collection: " + collectionId);

Testing the .NET Amazon Rekognition App

Now that the code is complete, we can build and run the app with the following commands:

$ dotnet build && dotnet run

When the app finishes running, you should see a message like the following:

Collection Created with Arn, [your Amazon Recognition collection ARN]  
Face Count: 12  
Deleted Collection: dotnet-test-collection

And, that’s it. You have used Amazon Rekognition via the .NET AWS SDK to count the number of faces in a photo.

Summary

We have concluded this tutorial where you have learned how to:

  • Create an Amazon Rekognition collection.
  • Upload a photo to an Amazon Rekognition collection.
  • Index the faces in a photo with Amazon Rekognition.
  • Get a count of the number of faces in a photo that was indexed by Amazon Rekognition.
  • Delete an Amazon Rekognition collection.

Want to know more about the tech in this article?  Checkout these resources:

.NET CLI.NET SDKAWS .NET SDK, Amazon Rekognition