Analyze Image Meta-Data
with Amazon Rekognition
+Athena
Amit Agrawal - agramit@[Link]
Solution Architect, AWS
© 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
Workshop Architecture
You can have many cameras taking
pictures in the store, these images S3
can be use for multiple purposes e.g.
facial recognition etc. You will:
1. Produce multiple images
2. Durably ingest and store the
Lambda
images in S3 bucket Rekognition
3. Perform real time meta-data
generation using Rekognition
4. Use Athena to perform ad-hoc
queries Bucket
5. QuickSight to build dashboards.
Athena QuickSight
Objectives for Today
Build an application using Amazon Rekognition and learn how we can
analyze image meta data using Amazon Athena
At the end of the workshop, you will:
1. Understand how to invoke Rekognition APIs
2. Understand how to create DB/table in Athena
3. Understand how to build dashboard in QuickSight by connecting
directly with Athena
Activities
Download Artifacts
Activity Details: [Link]
Sample Image Files: [Link]
Lambda function
code:
[Link]
Activity 1
Create IAM user and S3
buckets
Activity 1: Setting up IAM role and S3 buckets
We are going to:
A. Create an AWS IAM role for Lambda function execution
B. Create S3 bucket for input and output folders for the image
processing
We will use durable S3 service to store the captured images. S3 is an
ideal service to store large number of data set as it provides 11 9s
durability and automatic scalability. This solution will read input images
from S3 bucket and will put meta data in the output folders.
Activity 1-A: Create an AWS IAM Role
You can use AWS IAM to securely control individual and group access to
your AWS resources. You can create and manage user identities ("IAM
users") and grant permissions for those IAM users to access your
resources. To grant permissions, you create policy documents that you
attach to users, groups, or other entities.
In Activity 1-A, you will create an IAM role and attach policies to
allow Lambda function access to Rekogniton Service, S3 bucket
and CloudWatch logs. We will attaching this role when we
configure S3 event later on.
Activity 1-A: Create an AWS IAM Role
1. Go to the AWS IAM console, click “Users”
1. [Link]
1#/roles
2. Choose “Create Role” on the upper left hand side
Activity 1-A: Create an AWS IAM role
3. Select RoleType from
“AWS Service Role”
4. Click on AWS Lambda
“Select” button
Activity 1-A: Create an AWS IAM role
6. Choose “Attach policies”
and then select following
policies
“AmazonS3FullAccess”,
“AWSLambdaExecute” and
”AmazonRekognitionFullAc
cess”
7. Click on “Next Step”
Activity 1-A: Create an AWS IAM role
8. Choose name for the role
for “Role Name” field
9. Provide description for the
role in “Role Description” field
10. Click on “Create Role”
button
Activity 1-B: Create S3 Buckets
Click on Service management
console and select S3.
Click on “Create bucket”
button
Activity 1-B: Create S3 Buckets
1. Provide unique “Bucket Name” and
select region you want to create
bucket in
2. Click on “Next” button
Note: Please ensure that the bucket is created in same region where rest of the application will be.
Bucket name has to be globally unique and should be selected sensibly.
Activity 1-B: Create S3 Buckets
1. In next page leave the default setting
and click on ”Next” button
2. Next page is for setting permissions,
leave the default setting and click on
“Next” button
3. Review the setting and click on
“Create Bucket” button.
Note: Please ensure that the bucket is created in same region where rest of the application will be.
Bucket name has to be globally unique and should be selected sensibly.
Activity 1-B: Create S3 Buckets
1. On main page click on “Create folder”
button
2. Create following folders
1. csv
2. json
Activity 2
Setup Lambda function
Activity 2: Setting up Lambda function to process
Images
We are going to:
A. Create a Lambda function using python 2.7
B. Configure S3 event to invoke Lambda function
Activity 2: Setting up Lambda function
1. Go to the AWS Management
Console
2. From the Services menu on the
AWS Management Console, select
Lambda service
3. Click on “Create a Lambda
function” button
Please make sure Lambda function is created in
same region as S3 bucket.
Activity 2: Setting up Lambda function
1. In the “Select Blueprint” screen, select
“s3-get-object-python” option
Activity 2: Setting up Lambda function
1. In “Configure trigger” screen fill up
following information:
1. Select the bucket name from
the drop down
2. In EventType dropdown select
“Object Created (All)”
3. In Suffix field specify “jpg”
4. Check “Enable Trigger” check
box
5. Click on “Next” button
Activity 2: Setting up Lambda function
1. In “Configure Function” screen fill
up following information:
1. In “Name” field provide name
for Lambda function
2. In “Runtime” dropdown select
Python 2.7
3. Copy lambda function from
downloaded file to “Lambda
function code” section
Ensure that Runtime is selected as Python 2.7
Activity 2: Setting up Lambda function
1. In “Lambda function handler and role”
section:
1. In “Role” dropdown select “Choose
an existing role”
2. In ”Existing Role” dropdown select the
role we created in step 1.
2. Click on “Next” button
3. Next page review the information and
click on “Create function” button
Activity 3
Setup Athena Service
Activity 3: Setup Athena Service
1. Open the AWS Management
Console by clicking Open
Console.
2. From the Services menu on the
AWS Management Console, select
Athena Service
Activity 3: Setup Athena Service
Click on “Add Table” section
1. In Database drop down select “Create
New Database”
2. Provide Database name
3. Provide Table name
4. In “Location of Input of Data Set”
provide the S3 bucket we created as
part of step 2
s3://<region_name>/bucket_name e.g. s3://us-east-1/2017-ai-demo/csv
Activity 3: Setup Athena Service
To save time let’s paste the SQL
statement in the query editor
Activity 3: Setup Athena Service
CREATE EXTERNAL TABLE IF NOT EXISTS <db_name>.<table_name> (
`ImageLocation` string,
`Timestamp` string,
You can download SQL file from following link:
`Gender` string, [Link]
`Smiling` string,
`Beard` string,
`Mustache` string,
`Sharpness` string,
`Brightness` string,
`MouthOpen` string,
`EyesOpen` string,
Activity 3: Setup Athena Service
`LowAgeRange` string,
`HighAgeRange` string,
`Eyeglasses` string,
`Sunglasses` string
ROW FORMAT SERDE
'[Link]'
WITH SERDEPROPERTIES (
'[Link]' = ',',
'[Link]' = ','
Please don’t specify region detail in S3
) LOCATION 's3://<bucket_name>/csv/' bucket name
Activity 3: Setup Athena Service
To run the query select on
Eye icon next to table name
Activity 4
Let’s test this
Activity 4: Testing Sample CSV File:
Let’s test this before we setup
QuickSight dashboard
1. Upload sample images we
Sample Athena table data:
downloaded earlier to S3 bucket.
2. Once files are placed Lambda
function will kick off and place files in
csv and json folder
3. Go to Athena service and run select
query and data should be
populated in the table
Activity 4: Testing
Let’s run some queries with where clause
Select * from <database_name>.<table_name> where gender=’Female’
Activity 5
Setup QuickSight
Activity 5: Setup Amazon QuickSight Dashboard
1. In QuickSight Service menu
Select Athena as data
source
2. Specify Athena DB name in
“Data Source name” field
3. Click on “Create data
source” button
Activity 5: Setup Amazon QuickSight Dashboard
1. Select the table name from the
list
2. Select how you want to query
the data either directly from
the source or load it up in
memory first. For this lab you
can choose either
Activity 5: Setup Amazon QuickSight Dashboard
From designer window you can choose type of graph you
want to build as well data you want to see. This example
shows graph between count of people who are smiling by
gender
Activity 6
Clean up
Activity 6: Clean up
1. Make sure you delete the sample images from
the S3 bucket and then S3 bucket itself
2. Delete the Athena table and database
3. Delete the Lambda function
Let’s Sum It Up
Summary
1. Fully managed and easy-to-use image recognition
service
2. Four primary capabilities
• Object and Scene Detection
• Facial Analysis
• Face Comparison
• Face Recognition
3. Integrated with AWS and AI Services
• Amazon S3
• Lex and Polly
4. Scalable and low cost
Thank you!
Contact: agramit@[Link]