forked from drpventura/PythonRekognitionDemo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathface_detect.py
54 lines (40 loc) · 1.51 KB
/
face_detect.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import boto3
from pprint import pprint
import image_helpers
client = boto3.client('rekognition')
# imgurl = 'http://media.comicbook.com/uploads1/2015/07/fox-comic-con-panel-144933.jpg'
imgurl = 'https://blog.njsnet.co/content/images/2017/02/trumprecognition.png'
imgbytes = image_helpers.get_image_from_url(imgurl)
rekresp = client.detect_faces(Image={'Bytes': imgbytes},
Attributes=['ALL'])
# pprint(rekresp)
numfaces = len(rekresp['FaceDetails'])
print('Found', numfaces, end='')
if numfaces == 1:
print(' face:')
else:
print(' faces:')
for facedeets in rekresp['FaceDetails']:
# construct a printf (almost) style format string for printing the info
fmtstr = '{gender} age {lowage}-{highage},'
# mustache and beard detection
if facedeets['Mustache']['Value'] and facedeets['Beard']['Value']:
fmtstr += ' with beard and mustache,'
elif facedeets['Mustache']['Value']:
fmtstr += ' with mustache,'
elif facedeets['Beard']['Value']:
fmtstr += ' with beard,'
# sunglasses/eyeglasses detection
if facedeets['Sunglasses']['Value']:
fmtstr += ' wearing sunglasses,'
elif facedeets['Eyeglasses']['Value']:
fmtstr += ' wearing glasses,'
fmtstr += ' looks {emotion}'
print(
fmtstr.format(
gender=facedeets['Gender']['Value'],
lowage=facedeets['AgeRange']['Low'],
highage=facedeets['AgeRange']['High'],
emotion=facedeets['Emotions'][0]['Type'].lower()
)
)