Skip to content

Commit

Permalink
Fixed bugs in lmabda_function and added logging.
Browse files Browse the repository at this point in the history
  • Loading branch information
RobRoseKnows committed Sep 26, 2018
1 parent b9242a7 commit 5b715c6
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 8 deletions.
29 changes: 22 additions & 7 deletions blurFaces/lambda_function.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
# Adapted from: https://github.com/awslabs/serverless-application-model/blob/master/examples/apps/rekognition-python/lambda_function.py
logger = logging.getLogger(__name__)
logger.setLevel(logging.WARNING)
logger.debug('Loading function...')

import boto3
import json
import urllib
import os
import io

logger.debug('Loading in pillow...')

from PIL import Image, ImageDraw

print('Loading function')
logger.debug('Loading in boto3 functions.')

rekognition = boto3.client('rekognition')
s3 = boto3.client('s3')
Expand All @@ -17,15 +23,22 @@


def detect_faces(bucket, key):
logger.info("Calling rekognition with s3://{}/{}".format(bucket, key))
response = rekognition.detect_faces(Image={"S3Object": {"Bucket": bucket, "Name": key}})
logger.info("Called rekognition with s3://{}/{}".format(bucket, key))
return response

def download_file(bucket, key):
s3.download_file(bucket, key, '/tmp/img')
return '/tmp/img'
name = '/tmp/img'
logger.info("Downloading s3://{}/{} to {}".format(bucket, key, name))
s3.download_file(bucket, key, name)
logger.info("Downloaded image to {}".format(name))
return name

def upload_file(bucket, key, local_path):
logger.info("Uploading {} to s3://{}/{}".format(local_path, bucket, key))
response = s3.upload_file(local_path, bucket, key)
logger.info("Uploaded {} to s3://{}/{}".format(local_path, bucket, key))
return response

# --------------- PIL Methods -------------------
Expand All @@ -35,6 +48,8 @@ def load_image(img_location):
img = Image.open(io.BytesIO(f.read()))
img_format = img.format

logger.info("Found image with format {}".format(img_format))

return img, img_format

def draw_rectangles(img, faces):
Expand All @@ -56,6 +71,7 @@ def draw_rectangles(img, faces):

def save_image(img, img_format, location):
img.save(location, img_format)
logger.info("Saved image of format {} to {}".format(img_format, location))
return location

# --------------- Main handler ------------------
Expand All @@ -69,7 +85,7 @@ def lambda_handler(event, context):

# 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'))
key = urllib.parse.unquote_plus(event['Records'][0]['s3']['object']['key'], encoding='utf-8')
try:
# Calls rekognition DetectFaces API to detect faces in S3 object
response = detect_faces(bucket, key)
Expand All @@ -90,7 +106,6 @@ def lambda_handler(event, context):
return upload_file(os.getenv('OUTPUT_BUCKET'), key, saved_to)

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.")
logger.exception("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
2 changes: 1 addition & 1 deletion blurFaces/template.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ Resources:
Runtime: python3.6
CodeUri: s3://robrose-serverless-repo/dist/blurFaces.zip
Description: An Amazon S3 trigger that uses rekognition APIs to detect faces
MemorySize: 256
MemorySize: 128
Timeout: 60
Policies:
- S3ReadPolicy:
Expand Down

0 comments on commit 5b715c6

Please sign in to comment.