From d3174793d26cbef88333494fd0a7bc998fcb2d82 Mon Sep 17 00:00:00 2001 From: John Ramey Date: Tue, 16 Aug 2016 11:24:08 -0500 Subject: [PATCH] Updated face-detection example --- cloudvision/handler.py | 33 +------------------- examples/face_detection.py | 62 +++++++++++++++++++++++++++++++------- requirements.txt | 1 + 3 files changed, 53 insertions(+), 43 deletions(-) diff --git a/cloudvision/handler.py b/cloudvision/handler.py index 1888187..dfc0062 100644 --- a/cloudvision/handler.py +++ b/cloudvision/handler.py @@ -1,4 +1,3 @@ -import argparse import logging import os import sys @@ -12,8 +11,7 @@ def lambda_handler(event, context): """AWS Lambda Handler for API Gateway input""" post_args = event.get("body", {}) - image_url = post_args.get("image_url", - "https://raw.githubusercontent.com/ramhiser/serverless-cloud-vision/master/examples/images/ramhiser-and-son.jpg") + image_url = post_args["image_url"] detect_type = post_args.get("detect_type", "FACE_DETECTION") max_results = post_args.get("max_results", 4) @@ -27,32 +25,3 @@ def lambda_handler(event, context): return json_return -if __name__ == '__main__': - parser = argparse.ArgumentParser( - description='Detects faces in the given image.' - ) - parser.add_argument( - '-i', '--image_url', - help='The image URL to send to Google Cloud Vision API ', - required=True - ) - parser.add_argument( - '-d', '--detect_type', - help='detection type to perform. Default: %(default)s', - default="FACE_DETECTION" - ) - parser.add_argument( - '-m', '--max_results', - help='the max number of entities to detect. Default: %(default)s', - default=4, - type=int - ) - - args = parser.parse_args() - - detection_results = detect_image(args.image_url, - args.detect_type, - args.max_results) - - print detection_results - diff --git a/examples/face_detection.py b/examples/face_detection.py index 952db00..5f6c633 100644 --- a/examples/face_detection.py +++ b/examples/face_detection.py @@ -1,9 +1,11 @@ -from PIL import Image -from PIL import ImageDraw +import argparse +import json import urllib2 +import requests import cStringIO -from lambda_cloudvision import detect_image +from PIL import Image +from PIL import ImageDraw def highlight_faces(image_url, faces, output_filename): @@ -17,8 +19,8 @@ def highlight_faces(image_url, faces, output_filename): faces have polygons drawn around them. """ img = urllib2.urlopen(image_url) - if img.headers.maintype != 'image': - raise TypeError('Invalid filetype given') + if img.headers.maintype != "image": + raise TypeError("Invalid filetype given") # Source: http://stackoverflow.com/a/7391991/234233 img_file = cStringIO.StringIO(img.read()) @@ -27,14 +29,52 @@ def highlight_faces(image_url, faces, output_filename): draw = ImageDraw.Draw(im) for face in faces["responses"][0]["faceAnnotations"]: - box = [(v.get('x', 0.0), v.get('y', 0.0)) for v in - face['boundingPoly']['vertices']] - draw.line(box + [box[0]], width=5, fill='#00ff00') + box = [(v.get("x", 0.0), v.get("y", 0.0)) for v in + face["boundingPoly"]["vertices"]] + draw.line(box + [box[0]], width=5, fill="#00ff00") del draw im.save(output_filename) + if __name__ == '__main__': - image_url = "https://raw.githubusercontent.com/ramhiser/aws-lambda-cloud-vision/master/images/highlighted-faces.jpg" - faces = detect_image(image_url, detect_type="FACE_DETECTION") - highlight_faces(image_url, faces, "images/highlighted-faces.jpg") + parser = argparse.ArgumentParser( + description="Detects faces in the given image." + ) + parser.add_argument( + "-i", "--image_url", + help="The image URL to send to Google Cloud Vision API ", + required=True + ) + parser.add_argument( + "-m", "--max_results", + help="the max number of entities to detect. Default: %(default)s", + default=4, + type=int + ) + parser.add_argument( + "-e", "--endpoint", + help="The API Gateway endpoint to use", + required=True + ) + parser.add_argument( + "-o", "--output", + help="The filename of the output image. Default: %(default)s", + default="images/highlighted-faces.jpg" + ) + + args = parser.parse_args() + + post_params = { + "image_url": args.image_url, + "detect_type": "FACE_DETECTION", + "max_results": args.max_results + } + + # Lazy and used requests in addition to urllib2 + r = requests.post(args.endpoint, + data=json.dumps(post_params), + headers={'content-type': 'application/json'}) + detection_results = r.json() + + highlight_faces(args.image_url, detection_results, args.output) diff --git a/requirements.txt b/requirements.txt index 6657722..c848447 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,4 @@ google-api-python-client==1.5.1 oauth2client==3.0.0 Pillow==3.3.0 +requests==2.11.0