Skip to content

Commit

Permalink
Updated face-detection example
Browse files Browse the repository at this point in the history
  • Loading branch information
ramhiser committed Aug 16, 2016
1 parent 630e903 commit d317479
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 43 deletions.
33 changes: 1 addition & 32 deletions cloudvision/handler.py
@@ -1,4 +1,3 @@
import argparse
import logging
import os
import sys
Expand All @@ -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)

Expand All @@ -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

62 changes: 51 additions & 11 deletions 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):
Expand All @@ -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())
Expand All @@ -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)
1 change: 1 addition & 0 deletions 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

0 comments on commit d317479

Please sign in to comment.