Before you go, check out these stories!

0
Hackernoon logoImage Analysis using AWS Rekognition via AWS CLI by@snehalnarkar9

Image Analysis using AWS Rekognition via AWS CLI

Author profile picture

@snehalnarkar9Snehal Narkar

Software Engineer at www.udgama.com

In this blog, I am going to show you how we can use rekognition for image analysis using AWS CLI. So you can test the image analysis on your local machine. We will be going to perform label detection and object detection for an image so basically we are performing image analysis in this blog by using AWS CLI.

How will be the flow for Image Analysis:

In My Image analysis using AWS Rekognition via Lambda function blog, I already explained what is Rekognition and the benefits and use cases of Rekognition. So here will directly start with the implementation part.

For Image analysis, we are using the following services of AWS.

  • IAM
  • S3
  • Rekognition[This service we are using via AWS CLI]

So The flow for image analysis will be:

  • First We are going to read an image from the S3 bucket via AWS CLI command.
  • Once you read and image from an S3 bucket then by using rekognition service will read that image via calling rekognition API and in response, rekognition API will return labels.

Step 1: Creating a User by using IAM:

  • Login inside your AWS account using your credentials.
  • Go to the AWS Management console.
  • Search for the IAM service and enter.
  • On the left-hand side, you can see the users [inside the access management] click on the users.
  • Click on the Add user button.
  • Give the user name of your choice[for eg demouser].
  • In the Select AWS access type select both the access type[Programmatic access as well as AWS Management Console access].
  • For the console password select a custom password and type the password of your choice.
  • Require password reset keep a check by default and click on Next: Permission button.
  • In the Set permissions select Attach existing policies directly. And from policy choose Administrator access option and click on Next: Tags button.
  • Add Tags part is optional so directly click on Next: Review button.
  • Check all your details and click on Create: user button.
  • Once you clicked on create: user button you will get one Download.csv file please download that file. In that file only you will get access_key and secret_key of the user that you will be going to use in a further step.
  • Finally, click on the close button. Your user is ready now.

Step 2: Create an S3 bucket to store images:

  • Go to the AWS Management console.
  • Search for the S3 service and enter.
  • Click on the Create bucket button.
  • give any unique name to your bucket[for eg rekognition].
  • Keep all default settings as it is and click on the Create bucket button.
  • Once your bucket is created click on your bucket name. In that click on the upload button and drag and drop any image that you want and click on the upload button directly. Once the image is uploaded you can see the image as follows:

For the image analysis, I uploaded the following image.

Step 3: Set Up the AWS CLI

  • By running this command you will get to know whether python and pip are present on your pc or not. if not then plz first install it.
pip3 --version
python --version 
Or
python3 --version
  • Install the AWS CLI with Pip by using the following command
pip3 install awscli --upgrade --user
OR
sudo apt-get install awscli
  • Verify that the AWS CLI installed correctly.
aws --version
  • Configure the AWS CLI
aws configure
  • In this command, you need to provide the access_key and secret_key that you downloaded while creating the user.and also provide the region name that you are using [for e.g I am using ‘us-east-1’].
  • Finally to perform the image analysis type the following command:
aws rekognition detect-labels --image "S3Object={Bucket=buket_name,Name=image_name}" --region region_name
Note: In place of the bucket name provide s3 bucket name that you created in step 2 and for image name give the image name that is present inside the s3 bucket. In place of region give your region name for e.g[us-east-1]
  • Once you run the above command you will get the response of image analysis based on the image that you uploaded in s3.
{
    "Labels": [
        {
            "Parents": [],
            "Instances": [],
            "Name": "Human",
            "Confidence": 98.7396011352539
        },
        {
            "Parents": [],
            "Instances": [
                {
                    "BoundingBox": {
                        "Left": 0.4745420217514038,
                        "Top": 0.12450992316007614,
                        "Height": 0.8302116990089417,
                        "Width": 0.42319080233573914
                    },
                    "Confidence": 98.7396011352539
                }
            ],
            "Name": "Person",
            "Confidence": 98.7396011352539
        },
        {
            "Parents": [
                {
                    "Name": "Person"
                }
            ],
            "Instances": [],
            "Name": "Sitting",
            "Confidence": 98.73041534423828
        },
        {
            "Parents": [
                {
                    "Name": "Person"
                }
            ],
            "Instances": [],
            "Name": "Female",
            "Confidence": 97.1566162109375
        },
        {
            "Parents": [
                {
                    "Name": "Person"
                }
            ],
            "Instances": [],
            "Name": "Dating",
            "Confidence": 90.18348693847656
        },
        {
            "Parents": [
                {
                    "Name": "Female"
                },
                {
                    "Name": "Person"
                }
            ],
            "Instances": [],
            "Name": "Woman",
            "Confidence": 85.65811157226562
        },
        {
            "Parents": [
                {
                    "Name": "Person"
                }
            ],
            "Instances": [],
            "Name": "Child",
            "Confidence": 82.99960327148438
        },
        {
            "Parents": [
                {
                    "Name": "Person"
                }
            ],
            "Instances": [],
            "Name": "Teen",
            "Confidence": 82.99960327148438
        },
        {
            "Parents": [
                {
                    "Name": "Female"
                },
                {
                    "Name": "Person"
                }
            ],
            "Instances": [],
            "Name": "Girl",
            "Confidence": 82.99960327148438
        },
        {
            "Parents": [
                {
                    "Name": "Teen"
                },
                {
                    "Name": "Girl"
                },
                {
                    "Name": "Female"
                },
                {
                    "Name": "Woman"
                },
                {
                    "Name": "Kid"
                },
                {
                    "Name": "Person"
                }
            ],
            "Instances": [],
            "Name": "Blonde",
            "Confidence": 82.99960327148438
        },
        {
            "Parents": [
                {
                    "Name": "Person"
                }
            ],
            "Instances": [],
            "Name": "Kid",
            "Confidence": 82.99960327148438
        },
        {
            "Parents": [
                {
                    "Name": "Electronics"
                },
                {
                    "Name": "Computer"
                }
            ],
            "Instances": [],
            "Name": "Pc",
            "Confidence": 71.96940612792969
        },
        {
            "Parents": [
                {
                    "Name": "Electronics"
                }
            ],
            "Instances": [],
            "Name": "Computer",
            "Confidence": 71.96940612792969
        },
        {
            "Parents": [],
            "Instances": [],
            "Name": "Electronics",
            "Confidence": 71.96940612792969
        },
        {
            "Parents": [
                {
                    "Name": "Electronics"
                },
                {
                    "Name": "Pc"
                },
                {
                    "Name": "Computer"
                }
            ],
            "Instances": [],
            "Name": "Laptop",
            "Confidence": 55.87724685668945
        }
    ],
    "LabelModelVersion": "2.0"
}

In response, you will get a list of labels and a corresponding numeric confidence index. You can compare the image that I have mentioned above with the response. In my image, one lady is reading a book and also some electronic objects are present in the image. So in response, you can see different labels are shown related to images like a person, female, and computer.

In this way, you can try image analysis on different images based on your use case.

Enjoy Coding 😃

Tags

Join Hacker Noon

Create your free account to unlock your custom reading experience.