├── image1.jpg ├── image2.jpg ├── image3.jpg ├── image4.jpg ├── image5.jpg ├── image6.jpg ├── test ├── elon.jpg ├── my_pic.jpg ├── sundar.jpg ├── billgates.jpg └── testing.py ├── putimages.py ├── README.md ├── policy.json └── lamdafunction.py /image1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/teja156/amazon-rekognition-example/HEAD/image1.jpg -------------------------------------------------------------------------------- /image2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/teja156/amazon-rekognition-example/HEAD/image2.jpg -------------------------------------------------------------------------------- /image3.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/teja156/amazon-rekognition-example/HEAD/image3.jpg -------------------------------------------------------------------------------- /image4.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/teja156/amazon-rekognition-example/HEAD/image4.jpg -------------------------------------------------------------------------------- /image5.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/teja156/amazon-rekognition-example/HEAD/image5.jpg -------------------------------------------------------------------------------- /image6.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/teja156/amazon-rekognition-example/HEAD/image6.jpg -------------------------------------------------------------------------------- /test/elon.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/teja156/amazon-rekognition-example/HEAD/test/elon.jpg -------------------------------------------------------------------------------- /test/my_pic.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/teja156/amazon-rekognition-example/HEAD/test/my_pic.jpg -------------------------------------------------------------------------------- /test/sundar.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/teja156/amazon-rekognition-example/HEAD/test/sundar.jpg -------------------------------------------------------------------------------- /test/billgates.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/teja156/amazon-rekognition-example/HEAD/test/billgates.jpg -------------------------------------------------------------------------------- /putimages.py: -------------------------------------------------------------------------------- 1 | import boto3 2 | 3 | s3 = boto3.resource('s3') 4 | 5 | # Get list of objects for indexing 6 | images=[('image1.jpg','Elon Musk'), 7 | ('image2.jpg','Elon Musk'), 8 | ('image3.jpg','Bill Gates'), 9 | ('image4.jpg','Bill Gates'), 10 | ('image5.jpg','Sundar Pichai'), 11 | ('image6.jpg','Sundar Pichai') 12 | ] 13 | 14 | # Iterate through list to upload objects to S3 15 | for image in images: 16 | file = open(image[0],'rb') 17 | object = s3.Object('famouspersons-images','index/'+ image[0]) 18 | ret = object.put(Body=file, 19 | Metadata={'FullName':image[1]}) 20 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Commands 2 | 3 | Video Link: [https://youtu.be/oHSesteFK5c](https://youtu.be/oHSesteFK5c) 4 | 5 | 6 | - Install aws-shell 7 | ``` 8 | pip install aws-shell 9 | ``` 10 | 11 | - Configure 12 | ``` 13 | aws configure 14 | ``` 15 | 16 | - Create a collection on aws rekognition 17 | ``` 18 | aws rekognition create-collection --collection-id facerecognition_collection --region us-east-1 19 | ``` 20 | 21 | - Create table on DynamoDB 22 | ``` 23 | aws dynamodb create-table --table-name facerecognition \ 24 | --attribute-definitions AttributeName=RekognitionId,AttributeType=S \ 25 | --key-schema AttributeName=RekognitionId,KeyType=HASH \ 26 | --provisioned-throughput ReadCapacityUnits=1,WriteCapacityUnits=1 \ 27 | --region us-east-1 28 | ``` 29 | 30 | - Create S3 bucket 31 | ``` 32 | aws s3 mb s3://bucket-name --region us-east-1 33 | ``` 34 | -------------------------------------------------------------------------------- /policy.json: -------------------------------------------------------------------------------- 1 | { 2 | "Version": "2012-10-17", 3 | "Statement": [ 4 | { 5 | "Effect": "Allow", 6 | "Action": [ 7 | "logs:CreateLogGroup", 8 | "logs:CreateLogStream", 9 | "logs:PutLogEvents" 10 | ], 11 | "Resource": "arn:aws:logs:*:*:*" 12 | }, 13 | { 14 | "Effect": "Allow", 15 | "Action": [ 16 | "s3:GetObject" 17 | ], 18 | "Resource": [ 19 | "arn:aws:s3:::bucket-name/*" 20 | ] 21 | }, 22 | { 23 | "Effect": "Allow", 24 | "Action": [ 25 | "dynamodb:PutItem" 26 | ], 27 | "Resource": [ 28 | "arn:aws:dynamodb:aws-region:account-id:table/family_collection" 29 | ] 30 | }, 31 | { 32 | "Effect": "Allow", 33 | "Action": [ 34 | "rekognition:IndexFaces" 35 | ], 36 | "Resource": "*" 37 | } 38 | ] 39 | } -------------------------------------------------------------------------------- /test/testing.py: -------------------------------------------------------------------------------- 1 | import boto3 2 | import io 3 | from PIL import Image 4 | 5 | rekognition = boto3.client('rekognition', region_name='us-east-1') 6 | dynamodb = boto3.client('dynamodb', region_name='us-east-1') 7 | 8 | image_path = input("Enter path of the image to check: ") 9 | 10 | image = Image.open(image_path) 11 | stream = io.BytesIO() 12 | image.save(stream,format="JPEG") 13 | image_binary = stream.getvalue() 14 | 15 | 16 | response = rekognition.search_faces_by_image( 17 | CollectionId='famouspersons', 18 | Image={'Bytes':image_binary} 19 | ) 20 | 21 | found = False 22 | for match in response['FaceMatches']: 23 | print (match['Face']['FaceId'],match['Face']['Confidence']) 24 | 25 | face = dynamodb.get_item( 26 | TableName='face_recognition', 27 | Key={'RekognitionId': {'S': match['Face']['FaceId']}} 28 | ) 29 | 30 | if 'Item' in face: 31 | print ("Found Person: ",face['Item']['FullName']['S']) 32 | found = True 33 | 34 | if not found: 35 | print("Person cannot be recognized") -------------------------------------------------------------------------------- /lamdafunction.py: -------------------------------------------------------------------------------- 1 | from __future__ import print_function 2 | 3 | import boto3 4 | from decimal import Decimal 5 | import json 6 | import urllib 7 | 8 | print('Loading function') 9 | 10 | dynamodb = boto3.client('dynamodb') 11 | s3 = boto3.client('s3') 12 | rekognition = boto3.client('rekognition') 13 | 14 | 15 | # --------------- Helper Functions ------------------ 16 | 17 | def index_faces(bucket, key): 18 | 19 | response = rekognition.index_faces( 20 | Image={"S3Object": 21 | {"Bucket": bucket, 22 | "Name": key}}, 23 | CollectionId="famouspersons") 24 | return response 25 | 26 | def update_index(tableName,faceId, fullName): 27 | response = dynamodb.put_item( 28 | TableName=tableName, 29 | Item={ 30 | 'RekognitionId': {'S': faceId}, 31 | 'FullName': {'S': fullName} 32 | } 33 | ) 34 | 35 | # --------------- Main handler ------------------ 36 | 37 | def lambda_handler(event, context): 38 | 39 | # Get the object from the event 40 | bucket = event['Records'][0]['s3']['bucket']['name'] 41 | print("Records: ",event['Records']) 42 | key = event['Records'][0]['s3']['object']['key'] 43 | print("Key: ",key) 44 | # key = key.encode() 45 | # key = urllib.parse.unquote_plus(key) 46 | 47 | try: 48 | 49 | # Calls Amazon Rekognition IndexFaces API to detect faces in S3 object 50 | # to index faces into specified collection 51 | 52 | response = index_faces(bucket, key) 53 | 54 | # Commit faceId and full name object metadata to DynamoDB 55 | 56 | if response['ResponseMetadata']['HTTPStatusCode'] == 200: 57 | faceId = response['FaceRecords'][0]['Face']['FaceId'] 58 | 59 | ret = s3.head_object(Bucket=bucket,Key=key) 60 | personFullName = ret['Metadata']['fullname'] 61 | 62 | update_index('face_recognition',faceId,personFullName) 63 | 64 | # Print response to console 65 | print(response) 66 | 67 | return response 68 | except Exception as e: 69 | print(e) 70 | print("Error processing object {} from bucket {}. ".format(key, bucket)) 71 | raise e --------------------------------------------------------------------------------