Created
April 18, 2018 16:05
-
-
Save russau/b5f0fd08f6794cfa83895db9b58a192d to your computer and use it in GitHub Desktop.
Python AWS Rekognition: detect_faces and detect_text
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pprint | |
import boto3 | |
from PIL import Image, ImageDraw | |
rek = boto3.client('rekognition') | |
with open('TestingZone/MonaLisa.jpg', 'rb') as f: | |
image_bytes = f.read() | |
########################################## | |
response = rek.detect_labels( | |
Image={ | |
'Bytes': image_bytes | |
}) | |
pprint.pprint(response) | |
########################################## | |
response = rek.detect_faces( | |
Image={ | |
'Bytes': image_bytes | |
}, | |
Attributes=['ALL']) | |
pprint.pprint(response) | |
########################################## | |
src = Image.open('TestingZone/MonaLisa.jpg') | |
draw1 = ImageDraw.Draw(src) | |
width, height = src.size | |
img = Image.new("RGB", src.size) | |
draw = ImageDraw.Draw(img) | |
img.paste(src, (0, 0)) | |
for face in response["FaceDetails"]: | |
for point in face["Landmarks"]: | |
x = point["X"] * width | |
y = point["Y"] * height | |
r = 5 | |
draw.ellipse((x-r, y-r, x+r, y+r), fill="red") | |
img.save('TestingZone/MonaLisa-rek.jpg') | |
########################################## | |
with open('TestingZone/pike-st.jpg', 'rb') as f: | |
image_bytes = f.read() | |
response = rek.detect_text( | |
Image={ | |
'Bytes': image_bytes | |
}) | |
pprint.pprint(response) | |
src = Image.open('TestingZone/pike-st.jpg') | |
draw1 = ImageDraw.Draw(src) | |
width, height = src.size | |
img = Image.new("RGB", src.size) | |
draw = ImageDraw.Draw(img) | |
img.paste(src, (0, 0)) | |
for text in response["TextDetections"]: | |
points = [(point['X']*width, point['Y']*height) for point in text["Geometry"]["Polygon"]] | |
points.append(points[0]) | |
draw.line(points, fill="red", width=9) | |
img.save('TestingZone/pike-st-rek.jpg') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment