This repository has been archived by the owner on Apr 18, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathface.py
71 lines (52 loc) · 2.14 KB
/
face.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
import boto3
from botocore.exceptions import ClientError
import base64
from flask import jsonify
ID = '<YOUR_COLLECTION_ID>'
AWS_KEY = '<YOUR_AWS_KEY>'
AWS_SECRET = '<YOUR_AWS_SECRET_KEY>'
def upload_face(name, image):
# --- use this for named profiles in ~/.aws/config file
dev = boto3.session.Session(profile_name='personal-aws')
# --- or use keys
# dev = boto3.session.Session(aws_access_key_id=AWS_KEY, aws_secret_access_key=AWS_SECRET)
dev_client = dev.client('rekognition', region_name='ap-southeast-2')
try:
response = dev_client.index_faces(
CollectionId=ID,
Image={
'Bytes': base64.b64decode(image),
},
ExternalImageId=name,
)
if len(response['FaceRecords']) == 0:
return jsonify({'message': 'No face detected in photo!'})
if response['FaceRecords'][0]['Face']['FaceId'] is not None:
return jsonify({'message': 'Face Uploaded!'})
except ClientError as e:
return jsonify({'message': e.response['Error']['Message']})
def facial_recognition(image):
# -- named profile
dev = boto3.session.Session(profile_name='personal-aws')
# dev = boto3.session.Session(aws_access_key_id=AWS_KEY, aws_secret_access_key=AWS_SECRET)
dev_client = dev.client('rekognition', region_name='ap-southeast-2')
try:
response = dev_client.search_faces_by_image(
CollectionId=ID,
Image={
'Bytes': base64.b64decode(image),
},
MaxFaces=1,
)
if len(response['FaceMatches']) > 1:
return jsonify({'message': 'Too many faces found'})
elif len(response['FaceMatches']) == 0:
return jsonify({'message': 'No face match!'})
elif len(response['FaceMatches']) == 1:
return jsonify({
'message': 'Face Found!',
'id': response['FaceMatches'][0]['Face']['ExternalImageId'],
'confidence': response['FaceMatches'][0]['Face']['Confidence']
})
except ClientError as e:
return jsonify({'message': e.response['Error']['Message']})