-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathface_rekognition.py
75 lines (60 loc) · 2.07 KB
/
face_rekognition.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
import json
s3 = boto3.resource('s3')
client = boto3.client('rekognition')
def detect_faces():
detected_faces = client.index_faces(
CollectionId='faces',
DetectionAttributes=['DEFAULT'],
ExternalImageId='TEMPORARY_FILE_TO_COMPARISON',
Image={
'S3Object': {
'Bucket': 'jhmede-fa-images',
'Name': '_analyze.jpg'
}
}
)
return detected_faces
def create_detected_face_id_list(faces):
detected_face_ids = []
for image in range(len(faces['FaceRecords'])):
detected_face_ids.append(faces['FaceRecords'][image]['Face']['FaceId'])
return detected_face_ids
def compare_images(detected_face_ids):
result_comparison = []
for identifier in detected_face_ids:
result_comparison.append(
client.search_faces(
CollectionId='faces',
FaceId=identifier,
FaceMatchThreshold=80,
MaxFaces=10
)
)
return result_comparison
def generate_json_data(comparison_result):
json_data = []
for face_recognized in comparison_result:
if (len(face_recognized.get('FaceMatches'))) >= 1:
person = dict(
name=face_recognized['FaceMatches'][0]['Face']['ExternalImageId'],
similarity=round(face_recognized['FaceMatches'][0]['Similarity'], 2)
)
json_data.append(person)
return json_data
def send_response_file_to_s3_bucket(response):
file = s3.Object('jhmede-fa-site', 'data.json')
file.put(Body=json.dumps(response))
def remove_image_used_by_comparison(face_ids):
client.delete_faces(
CollectionId='faces',
FaceIds=face_ids
)
def main(event, context):
detected_faces = detect_faces()
face_ids = create_detected_face_id_list(detected_faces)
comparison_result = compare_images(face_ids)
data = generate_json_data(comparison_result)
send_response_file_to_s3_bucket(data)
remove_image_used_by_comparison(face_ids)
print(json.dumps(data, indent=4))