-
Notifications
You must be signed in to change notification settings - Fork 0
/
send_image.py
39 lines (35 loc) · 1.13 KB
/
send_image.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
import boto3
from uuid import uuid4
from os import environ as env
from operator import itemgetter
def main(input_pic, debug=False):
collxn = 'Test_Collection' if debug==True else env['CollectionID']
client = boto3.client('rekognition')
is_match = client.search_faces_by_image(
CollectionId=collxn,
Image={'Bytes':input_pic},
FaceMatchThreshold=60.0
)
ext_id = uuid4().hex if not is_match['FaceMatches'] else max(
[(y['Face']['ExternalImageId'], y['Similarity'])
for y in is_match['FaceMatches']],
key=itemgetter(1)
)[0]
client.index_faces(
CollectionId=collxn,
ExternalImageId=ext_id,
Image={'Bytes':input_pic}
)
return ext_id
if __name__=='__main__':
import os
test_path = env['TestPath']
faces = [
bytearray(open(os.path.join(test_path, x), 'rb').read())
for x in os.listdir(test_path)
]
client = boto3.client('rekognition')
client.delete_collection(CollectionId='Test_Collection')
client.create_collection(CollectionId='Test_Collection')
for face in faces:
print(main(face, debug=True))