Skip to content

DynamoDB

James A Henriquez edited this page Feb 19, 2024 · 15 revisions

Storing Amazon Rekognition information into DynamoDB

Amazon gives instructions for how to store Amazon Rekognition information onto DynamoDB, while it is not live video and instead images I believe some of this information will prove useful for getting our application to work.

Prerequisites

  • You will first need DynamoDB
  • Python SDK (Boto3)
  • S3 Buckets filled with the images
  • DynamoDB database instance to store the data

Lables for images in S3

In the example Amazon gives us, the S3 buckets are filled with images in which Amazon Rekognition will take the images within the buckets and give them labels (for example, "Flower", "Tree", "Train").

We are going to have to connect to this S3 bucket using boto3 (a python software development kit) to retrieve the image and then pass both the bucket and image name to DetectLabels. DetectLabels is one of the many actions Amazon Rekognition is capable of. So, we will take the image, run it through DetectLabels and in return recieve the image name and lable it was given.

import boto3 from io import BytesIO from matplotlib import pyplot as plt from matplotlib import image as mp_img

boto3 = boto3.Session()

def read_image_from_s3(bucket_name, image_name):

`s3 = boto3.resource('s3')`
`bucket = s3.Bucket(name=bucket_name)`
`Object = bucket.Object(image_name)`

`file_name = Object.key`
`file_stream = BytesIO()`
`Object.download_fileobj(file_stream)`
`img = mp_img.imread(file_stream, format="jpeg")`
`plt.imshow(img)`
`plt.show()`

`client = boto3.client('rekognition', region_name="region-name")`
`response = client.detect_labels(Image={'S3Object': {'Bucket': bucket_name, 'Name': image_name}},`
                                `MaxLabels=10)`

`print('Detected labels for ' + image_name)`

`full_labels = response['Labels']`

`return file_name, full_labels`