import boto3
import json
def detect_faces(photo):
client=boto3.client('rekognition')
with open(photo, 'rb') as image:
response = client.detect_faces(Image={'Bytes': image.read()},Attributes=['ALL'])
print('Detected faces for ' + photo)
for faceDetail in response['FaceDetails']:
print('The detected face is between ' + str(faceDetail['AgeRange']['Low'])
+ ' and ' + str(faceDetail['AgeRange']['High']) + ' years old')
print('Gender ' + str(faceDetail['Gender']['Value']))
return len(response['FaceDetails'])
def main():
photo='test.jpg'
face_count=detect_faces(photo)
print("Faces detected: " + str(face_count))
if __name__ == "__main__":
main()
>python detect_faces.py
Detected faces for test.jpg
The detected face is between 17 and 29 years old
Gender Male
The detected face is between 12 and 22 years old
Gender Female
Faces detected: 2