-
Notifications
You must be signed in to change notification settings - Fork 1
/
facial_analysis.py
75 lines (48 loc) · 2.05 KB
/
facial_analysis.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import boto3, io, json
from PIL import Image, ImageDraw
import matplotlib.pyplot as plt
def show_faces(photo, r_aws_access_key_id, r_aws_secret_access_key ):
client=boto3.client('rekognition', aws_access_key_id= r_aws_access_key_id , aws_secret_access_key=r_aws_secret_access_key, region_name='us-east-1')
imageFile= photo
image=Image.open(imageFile)
stream = io.BytesIO()
image.save(stream,format=image.format ,dpi=(30, 30))
image_binary = stream.getvalue()
#Call DetectFaces
response = client.detect_faces(Image={'Bytes':image_binary},Attributes=['ALL'] )
imgWidth, imgHeight = image.size
#draw = ImageDraw.Draw(image)
# calculate and display bounding boxes for each detected face
print('Detected faces for ' + imageFile)
'''
for faceDetail in response['FaceDetails']:
box = faceDetail['BoundingBox']
left = imgWidth * box['Left']
top = imgHeight * box['Top']
width = imgWidth * box['Width']
height = imgHeight * box['Height']
points = (
(left,top),
(left + width, top),
(left + width, top + height),
(left , top + height),
(left, top)
)
coordinates.append( points )
draw.line(points, fill='#24f0f0', width=4)
# Alternatively can draw rectangle. However you can't set line width.
#draw.rectangle([left,top, left + width, top + height], outline='#00d400')
'''
'''
fig=plt.imshow(image)
plt.axis('off')
fig.axes.get_xaxis().set_visible(False)
fig.axes.get_yaxis().set_visible(False)
'''
# Serializing json
#json_results = json.dumps(response['FaceDetails'], indent=4)#, sort_keys=True)
# print(json_results)
output_image = imageFile #'tmp_'+ imageFile
plt.savefig('./www/images/' + output_image , bbox_inches='tight', transparent=True, pad_inches=0, dpi=200)
results = [output_image,len(response['FaceDetails']) , response['FaceDetails'] ]#,json_results ]
return results