Create a Collection

In this step, you will create a collection to recognize faces using Amazon Rekognition Video.

Preparing images for the collection.

First, save photos of the faces to be recognized in Amazon Rekognition on your PC. It is recommended to register at least 2 or 3 persons for the following steps.

The photos can be either taken with a smartphone or captured from your PC’s camera. Please prepare a PNG or JPEG image file of 15MB or less with the face covering the majority of the image.

The name of the image file should be something like name.jpg (e.g. alice.jpg). The name is used as the image ID when the image is added to the collection.

If you used the sample video in 1-3-b on Cloud9, download images from the following links.

Create S3 buckets and upload images for collection

  • Open Amazon S3 console
  • Select Buckets from the left menu and click the Create bucket button on the right
  • Create a bucket with the following settings
    • Bucket name: kvs-workshop-bucket-date-your-name (e.g., kvs-workshop-bucket-20200101-alice)
    • Region: Same as the region selected in Lab-1
    • Check Block all public access
  • Click on Create bucket
  • Click on a bucket with a name starting with kvs-workshop-bucket- in the list of buckets
  • The following console will appear, select all the image files you have just saved and drag and drop them onto the console
  • Click on Upload

Amazon S3 Bucket

Creating a collection

Next, you will create a collection to recognize faces on Amazon Rekognition.

Run the following commands on the terminal of the PC for operation if you are using Raspberry Pi in Lab 1, or Cloud9 Terminal if you are using AWS Cloud9 in Lab 1.

export COLLECTION_ID=kvs-workshop-collection
aws rekognition create-collection --collection-id $COLLECTION_ID

Confirm the following output and a collection named kvs-workshop-collection is created.

{
    "StatusCode": 200,
    "CollectionArn": "aws:rekognition:your-region:012345678901:collection/kvs-workshop-collection",
    "FaceModelVersion": "5.0"
}

Adding faces to the collection

Register the S3 bucket name you just created in an environment variable. In the following command, replace date-your-name with yours.

export BUCKET_NAME="kvs-workshop-bucket-date-your-name"

Run the following command to add all the images you just uploaded to the S3 bucket to the collection.

for key in $(aws s3 ls s3://$BUCKET_NAME | awk '{print $4}'); do
  name=$(echo $key | sed 's/\.[^\.]*$//')
  echo "index: $key"
  aws rekognition index-faces --collection-id $COLLECTION_ID \
  --image "S3Object={Bucket=$BUCKET_NAME,Name=$key}" \
  --external-image-id $name \
  --max-faces=1
done

Confirm the number of faces registered in the collection.

aws rekognition describe-collection --collection-id $COLLECTION_ID

You will get the following output, and check the value of FaceCount. If you can confirm the value of FaceCount as follows, which shows that there are two faces in the collection.

{
    "FaceCount": 2,
    "FaceModelVersion": "5.0",
    "CollectionARN": "arn:aws:rekognition:your-region:012345678901:collection/kvs-workshop-collection",
    "CreationTimestamp": 1588237022.79
}

That’s it. You created a collection. Next, you will configure a stream processor to recognize the face in the collection you created.