Skip to content

Instantly share code, notes, and snippets.

@lewcpe
Created November 26, 2017 17:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lewcpe/6ee77409070a9495ca907a73f889e141 to your computer and use it in GitHub Desktop.
Save lewcpe/6ee77409070a9495ca907a73f889e141 to your computer and use it in GitHub Desktop.
Detect Faces with AWS Rekognition
#!/usr/bin/env python3
"""Upload all images and index faces with AWS Rekognition"""
import boto3
import csv
import logging
from os.path import split, splitext
from botocore.exceptions import ClientError
def search_faces(collection, files, outfname):
outfile = open(outfname, 'w')
outcsv = csv.writer(outfile)
client = boto3.client('rekognition')
for fname in files:
logging.info('Searching %s', fname)
mainname = splitext(split(fname)[-1])[0]
image_data = open(fname, 'rb').read()
try:
response = client.search_faces_by_image(
Image={
'Bytes': image_data
},
CollectionId=collection,
MaxFaces=20
)
logging.debug("Resp = %s", response)
except ClientError:
logging.info("No face was found, skip")
continue
for matchdict in response['FaceMatches']:
facedict = matchdict['Face']
name, sourceid = facedict['ExternalImageId'].split('_')
top = facedict['BoundingBox']['Top']
left = facedict['BoundingBox']['Left']
height = facedict['BoundingBox']['Height']
width = facedict['BoundingBox']['Width']
outcsv.writerow([mainname, name, sourceid, top, left, height, width])
outfile.close()
def main():
from glob import glob
images = glob('full_hd.jpg')
images.sort()
search_faces('twice', images, 'twice_likey_test.csv')
if __name__ == "__main__":
logging.basicConfig(level=logging.INFO)
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment