Skip to content

Instantly share code, notes, and snippets.

@SeptiyanAndika
Created July 7, 2018 08:58
Show Gist options
  • Save SeptiyanAndika/08f35a4beb135ed39d27c45a6fd65145 to your computer and use it in GitHub Desktop.
Save SeptiyanAndika/08f35a4beb135ed39d27c45a6fd65145 to your computer and use it in GitHub Desktop.
from __future__ import print_function
import boto3
from decimal import Decimal
import json
import urllib
print('Loading function')
rekognition = boto3.client('rekognition')
# --------------- Helper Functions to call Rekognition APIs ------------------
def detect_faces(bucket, key):
response = rekognition.detect_faces(Image={"S3Object": {"Bucket": bucket, "Name": key}})
return response
def detect_text(bucket,key):
response = rekognition.detect_text(Image={"S3Object": {"Bucket": bucket, "Name": key}})
return response
def detect_labels(bucket, key):
response = rekognition.detect_labels(Image={"S3Object": {"Bucket": bucket, "Name": key}})
# Sample code to write response to DynamoDB table 'MyTable' with 'PK' as Primary Key.
# Note: role used for executing this Lambda function should have write access to the table.
#table = boto3.resource('dynamodb').Table('MyTable')
#labels = [{'Confidence': Decimal(str(label_prediction['Confidence'])), 'Name': label_prediction['Name']} for label_prediction in response['Labels']]
#table.put_item(Item={'PK': key, 'Labels': labels})
return response
def index_faces(bucket, key):
# Note: Collection has to be created upfront. Use CreateCollection API to create a collecion.
#rekognition.create_collection(CollectionId='BLUEPRINT_COLLECTION')
response = rekognition.index_faces(Image={"S3Object": {"Bucket": bucket, "Name": key}}, CollectionId="BLUEPRINT_COLLECTION")
return response
def peta(x):
if 'a' in x:
return 'a'
elif 'b' in x:
return 'b'
elif 'c' in x:
return 'c'
return '0'
def process_response(response):
data_bersih=map(lambda x: x['DetectedText'].lower(), filter(lambda x: x['Type'] == 'LINE', response['TextDetections']))[3:]
return map(peta, data_bersih)
# --------------- Main handler ------------------
def lambda_handler(event, context):
'''Demonstrates S3 trigger that uses
Rekognition APIs to detect faces, labels and index faces in S3 Object.
'''
#print("Received event: " + json.dumps(event, indent=2))
# Get the object from the event
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_text(bucket, key)
response = process_response(response)
# Calls rekognition DetectLabels API to detect labels in S3 object
#response = detect_labels(bucket, 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)
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment