from __future__ import print_function
import boto3
from decimal import Decimal
import json
import urllib
print('Loading function')
rekognition = boto3.client('rekognition')
client = boto3.client('sns')
def detect_labels(bucket, key):
response = rekognition.detect_labels(Image={"S3Object": {"Bucket": bucket, "Name": key}})
return response
# --------------- Main handler ------------------
def lambda_handler(event, context):
bucket = event['Records'][0]['s3']['bucket']['name']
key = urllib.unquote_plus(event['Records'][0]['s3']['object']['key'].encode('utf8'))
try:
# Calls rekognition DetectFaces API to detect faces in S3 object
#response = detect_faces(bucket, key)
# Calls rekognition DetectLabels API to detect labels in S3 object
response = detect_labels(bucket, key)
tosend=""
for Label in response["Labels"]:
#print(Label["Name"] + Label["Confidence"])
if (Label["Name"] in ("Human","Person")):
print('Name of item detected {0}-level of accuracy{1}%'.format(Label["Name"],Label["Confidence"]))
#tosend+='Name of item detected |==> {0} with a Level of accurancy |==> {1}%'.format(Label["Name"],Label["Confidence"])
#tosend+="Unknown face detected " + s3.genereate_tosend( 'get_object',Params = {'Bucket': bucket_name, 'Key': file_key})
generate_presigned_url = s3.genereate_presigned_url( 'get_object',Params = {'Bucket': bucket_name, 'Key': file_key})
# Calls rekognition IndexFaces API to detect faces in S3 object and index faces into specified collection
#response = index_faces(bucket, key)
# Print response to console.
print(response)
message = client.publish(TargetArn='arn:aws:sns:us-east-1:112589004333:Inform-me', Message=generate_presigned_url, Subject='Intelligent Security System')
return response
except Exception as e:
print(e)
print("Error processing object {} from bucket {}. ".format(key, bucket) +
"Make sure your object and bucket exist and your bucket is in the same region as this function.")
raise e