- install aws cli from https://docs.aws.amazon.com/cli/latest/userguide/getting-started-install.html
-
aws configure
- AWS -> Security credentials -> Access Keys -> Create
- Key ID
- Access Key
- Def region name:
eu-west-1
-
aws rekognition create-collection --collection-id COLLECTION_NAME --region eu-west-1
- create a collection in aws rekognition
-
aws dynamodb create-table --table-name COLLECTION_NAME --attribute-definitions AttributeName=RekognitionId,AttributeType=S --key-schema AttributeName=RekognitionId,KeyType=HASH --provisioned-throughput ReadCapacityUnits=1,WriteCapacityUnits=1 --region eu-west-1
- create a table in dynamoDb to store faceprint-id and key
-
aws s3 mb s3://UNIQUE_BUCKET_NAME
- bucket to store images
-
IAM Role for the lambda function
- IAM -> Roles ->
- trusted entity type: AWS Service
- service: Lambda
- next -> name role -> create
- edit policy: roles list find role -> click on it... -> create inline policy - json tab
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"logs:CreateLogGroup",
"logs:CreateLogStream",
"logs:PutLogEvents"
],
"Resource": "arn:aws:logs:*:*:*"
},
{
"Effect": "Allow",
"Action": [
"s3:GetObject"
],
"Resource": [
"arn:aws:s3:::BUCKET_NAME/*"
]
},
{
"Effect": "Allow",
"Action": [
"dynamodb:PutItem"
],
"Resource": [
"[DYNAMOdb_ARN_FROM_CONSOLE_NOT_LOG]"
]
},
{
"Effect": "Allow",
"Action": [
"rekognition:IndexFaces"
],
"Resource": "*"
}
]
}
- Write Lambda func
- search and create lambda func
- choose appropriate role
- in triggers -> select S3 bucket -> select on-create
from __future__ import print_function
import boto3
from decimal import Decimal
import json
import urllib
print('Loading function')
dynamodb = boto3.client('dynamodb')
s3 = boto3.client('s3')
rekognition = boto3.client('rekognition')
# --------------- Helper Functions ------------------
def index_faces(bucket, key):
response = rekognition.index_faces(
Image={"S3Object":
{"Bucket": bucket,
"Name": key}},
CollectionId="COLLECTION_NAME")
return response
def update_index(tableName,faceId, fullName):
response = dynamodb.put_item(
TableName=tableName,
Item={
'RekognitionId': {'S': faceId},
'FullName': {'S': fullName}
}
)
# --------------- Main handler ------------------
def lambda_handler(event, context):
# Get the object from the event
bucket = event['Records'][0]['s3']['bucket']['name']
print("Records: ",event['Records'])
key = event['Records'][0]['s3']['object']['key']
print("Key: ",key)
# key = key.encode()
# key = urllib.parse.unquote_plus(key)
try:
# Calls Amazon Rekognition IndexFaces API to detect faces in S3 object
# to index faces into specified collection
response = index_faces(bucket, key)
# Commit faceId and full name object metadata to DynamoDB
if response['ResponseMetadata']['HTTPStatusCode'] == 200:
faceId = response['FaceRecords'][0]['Face']['FaceId']
ret = s3.head_object(Bucket=bucket,Key=key)
personFullName = ret['Metadata']['fullname']
update_index('COLLECTION_NAME',faceId,personFullName)
# Print response to console
print(response)
return response
except Exception as e:
print(e)
print("Error processing object {} from bucket {}. ".format(key, bucket))
raise e