Skip to content

Instantly share code, notes, and snippets.

@hariby
Last active September 22, 2018 17:15
Show Gist options
  • Save hariby/8076766025f4fde2772d518a8198a26b to your computer and use it in GitHub Desktop.
Save hariby/8076766025f4fde2772d518a8198a26b to your computer and use it in GitHub Desktop.
import io, boto3
import numpy as np
from PIL import Image
from botocore.exceptions import ClientError
REGION = 'us-east-2'
CollectionId = '<CollectionID>'
def get_name(frame):
rekognition = boto3.client('rekognition', region_name=REGION)
image = Image.fromarray(np.uint8(frame[:,:,::-1]))
stream = io.BytesIO()
image.save(stream,format="JPEG")
image_binary = stream.getvalue()
try:
response = rekognition.search_faces_by_image(
CollectionId=CollectionId,
Image={'Bytes':image_binary}
)
return response['FaceMatches'][0]['Face']['FaceId']
except ClientError as err:
code = err.response['Error']['Code']
if code == 'InvalidParameterException':
return 'NOFACE'
elif code == 'AccessDeniedException':
return 'DENIED'
elif code == 'InvalidImageFormatException':
return 'ILLFORMED'
else:
return 'ERR'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment