Detect scenes using Amazon Rekognition

Amazon Rekognition makes it easy to add image and video analysis to your applications using proven, highly scalable, deep learning technology that requires no machine learning expertise to use. With Amazon Rekognition, you can identify objects, people, text, scenes, and activities in images and videos, as well as detect any inappropriate content.

In this second lab, you will use Amazon Rekognition to detect scenes in an image and to generate text file report, that contains information about the image being analyzed.

Rekognition

Save the image above locally as you will use it for testing.

Create project

  1. Create a new .NET Core console application project.

Rekognition

  1. Add AWSSDK.Rekognition Nuget package to the project: Nuget

  2. Add the following import statements to Program.cs:

using System;
using System.IO;
using System.Text;

using System.Threading.Tasks;

using Amazon.Rekognition;
using Amazon.Rekognition.Model;
  1. Replace the Main method in Program.cs with the following async version:
static async Task Main(string[] args)
{
    if (args.Length != 1)
    {
        Console.WriteLine("Please provide picture file name!");

        return;
    }

    var fileName = args[0];

    await DetectScenes(fileName);
}
  1. Add the following DetectScenes method implementation:

Please note that when you initialize AWS SDK’s AmazonRekognitionClient, you need to pass the RegionEndpoint of the region you are making labs in. The code below initializes AmazonRekognitionClient in the EUWest1 region.

static async Task DetectScenes(string fileName)
{
    var rekognitionClient = new AmazonRekognitionClient(Amazon.RegionEndpoint.EUWest1);

    var detectLabelsRequest = new DetectLabelsRequest();

    var rekognitionImage = new Image();

    byte[] data = null;

    using (FileStream fileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read))
    {
        data = new byte[fileStream.Length];
        fileStream.Read(data, 0, (int)fileStream.Length);
    }

    rekognitionImage.Bytes = new MemoryStream(data);

    detectLabelsRequest.Image = rekognitionImage;

    var detectLabelsResponse = await rekognitionClient.DetectLabelsAsync(detectLabelsRequest);

    if (detectLabelsResponse.Labels.Count > 0)
    {
        var content = new StringBuilder();
        content.AppendLine("Amazon Rekognition detects the objects and scenes in the image and returns them along with a percent confidence score for each object and scene");
        content.AppendLine("The following objects and scenes were detected:");

        foreach (var item in detectLabelsResponse.Labels)
        {
            content.AppendLine(item.Name + " with the confidence " + Convert.ToInt32(item.Confidence) + "%");
        }

        string outputFileName = fileName.Replace(Path.GetExtension(fileName), "_report.txt");
        File.WriteAllText(outputFileName, content.ToString());
    }
    else
    {
        Console.WriteLine("No objects have been detected!");
    }

}

The code above does the following:

  • Creates an instance of the AWS SDK’s AmazonRekognitionClient, initialized to the EUWest1 region, and an instance of the DetectLabelsRequest class for use with the DetectLabels API.
var rekognitionClient = new AmazonRekognitionClient(Amazon.RegionEndpoint.EUWest1);

var detectLabelsRequest = new DetectLabelsRequest();
  • Create an Amazon Rekognition image, load input image, convert it to byte array and add that to the Rekognition image that you created in the previous step.
  • Once the preparation work for this faces detection process is ready, call DetectLabelsAsync method of the AmazonRekognitionClient to complete it.
var detectLabelsResponse = await rekognitionClient.DetectLabelsAsync(detectLabelsRequest);
  • The detectLabelsResponse variable is of type DetectLabelsResponse. This type has a property containing a list of Label objects. The code iterates over this collection to retrieve the properties of each Label object to create text file that includes all Label objects and their properties as part of the output report.

Run application

Now you can build the application and run it by passing the path to the sample image:

Rekognition.exe c:\projects\002.png

Check the folder with original image, you should see another new file with the name 002_report.txt with the following report:

Amazon Rekognition detects the objects and scenes in the image and returns them along with a percent confidence score for each object and scene
The following objects and scenes were detected:
Asphalt with the confidence 100%
Tarmac with the confidence 100%
Road with the confidence 100%
Transportation with the confidence 99%
Automobile with the confidence 99%
Vehicle with the confidence 99%
Car with the confidence 99%
Human with the confidence 99%
Pedestrian with the confidence 99%
Person with the confidence 99%
Skateboard with the confidence 95%
Sport with the confidence 95%
Sports with the confidence 95%
Wheel with the confidence 90%
Machine with the confidence 90%
Zebra Crossing with the confidence 89%
Building with the confidence 85%
Town with the confidence 85%
Urban with the confidence 85%
Street with the confidence 85%
City with the confidence 85%
Path with the confidence 80%
Neighborhood with the confidence 74%
Intersection with the confidence 68%
Parking Lot with the confidence 55%
Parking with the confidence 55%
Downtown with the confidence 55%