DEV Community


Posted on • Updated on

AWS Cloud Quest: Image and Video Analysis Study Note

This is the DIY challenge of the Image and Video Analysis in AWS Cloud Quest.

Image description

DIY Steps:

  1. Update and Deploy the Lambda code
The code is developed using reference from


import json
import logging
import boto3

# It is good practice to use proper logging.
# Here we are using the logging module of python.

logger = logging.getLogger()

# Declare the label that is used to identify
# if the image contains the label

LABEL = 'spaceship'

# Using boto3 S3 Client
s3_client = boto3.client('s3')

def lambda_handler(event, context):
    This code gets the S3 attributes from the trigger event,
    then invokes the rekognition api to detect labels.
    If the label matches the one present on the LABELS list,
    response is written in the S3 bucket with "Status":"Label Found",
    else the response is written in the S3 bucket with "Status":"Label Not Found".
    bucket = event['Records'][0]['s3']['bucket']['name']
    image = event['Records'][0]['s3']['object']['key']
    output_key = 'output/rekognition_response.json'
    response = {'Status': 'Not Found', 'body': []}

    # Using rekognition boto3 client.

    rekognition_client = boto3.client('rekognition')

    # Identify label

        response_rekognition = rekognition_client.detect_labels(   # You are calling detect_labels API 
            Image={                                                # to analyzing Images Stored in an Amazon S3 Bucket
                'S3Object': {
                    'Bucket': bucket,
                    'Name': image
            MinConfidence=70                                        # MinConfidence specifies the minimum confidence  
        )                                                           # level for the labels to return.

    # The below code section tries to find all labels in the detect_labels 
    # list. If the label is found in the list, the response JSON file is updated with 
    # "Status":"Success! <label_name> found" then updates the response body with all 
    # detected labels. If the label is not found, the response JSON file is updated with 
    # "Status":"Failed, <label_name> not found" then updates the response body with all 
    # detected labels.

        detected_labels = [] # Declaring empty label lists.

        if response_rekognition['Labels']:
            for label in response_rekognition['Labels']:

            if LABEL in detected_labels:
                response['Status'] = f"Success! {LABEL} found"
                response['Status'] = f"Failed! {LABEL} Not found"

    except Exception as error:

# Finally the file will be written in the S3 bucket output folder.
      Body=json.dumps(response, indent=4)

    return response

You can use below code to create test event to test
the Lambda function.
    "Records": [
                "s3": {
                    "bucket": {
                    "name": "<Your_bucket_name>"
                    "object": {
                    "key": "input/ovni.png"

# You can visit
# to get more sample codes.
Enter fullscreen mode Exit fullscreen mode

Top comments (0)