-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapplication.py
133 lines (107 loc) · 4.19 KB
/
application.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
from flask import Flask, render_template, request, session, flash, url_for
import boto3
import base64
import os
from application.forms import *
from flask_login import LoginManager, login_required, login_user, logout_user, UserMixin
application = Flask(__name__, static_url_path='')
admin_pass = os.environ.get('FACIAL_RECOGNITION_ADMIN_PASS', "default")
collection_id = os.environ.get('FACIAL_RECOGNITION_COLLECTION_ID', "arc-face-rec-test")
application.secret_key = os.environ.get('FLASK_APP_SECRET_KEY', str(os.urandom(16)))
aws_region = os.environ.get('FACIAL_RECOGNITION_AWS_REGION', "us-west-2")
rekognition = boto3.client("rekognition", aws_region)
login_manager = LoginManager()
login_manager.init_app(application)
@application.route('/')
@application.route('/index')
def main_page():
return render_template("index.html")
@application.route('/about')
def about_page():
return render_template("about.html")
@application.route('/add_face', methods=['GET', 'POST'])
@login_required
def add_face_page():
# Index new face to collection
form = AddFaceForm()
if request.method == 'POST':
if form.validate_on_submit():
try:
response = rekognition.index_faces(
Image={
"Bytes": base64.b64decode(form.image.data[22:])
},
CollectionId=collection_id,
MaxFaces=1,
ExternalImageId=form.first_name.data + '_' + form.last_name.data
)
if(application.debug):
print(response)
if len(response['UnindexedFaces']) > 0:
flash('Image not usable. Please try another image.', 'error')
else:
flash('Successfully added face.', 'success')
except rekognition.exceptions.InvalidParameterException:
flash('Image not usable. Please try another image.', 'error')
else:
if len(form.image.data) == 0:
flash("Must use a photo.", 'error')
else:
flash('Invalid Form Parameters.', 'error')
return render_template("add_face.html", form=form)
@application.route('/detect', methods=['POST'])
def detect_faces():
# Send image to AWS Rekognition and process result
output = ""
faceImages = str(request.get_data()).split("data:image/png;base64,")
for i in range(1, len(faceImages)):
try:
response = rekognition.search_faces_by_image(
Image={
"Bytes": base64.b64decode(str.encode(faceImages[i]))
},
CollectionId=collection_id
)
if len(response['FaceMatches']) == 0:
output = "Unrecognized Face<br>"
continue
resp = response['FaceMatches'][0]
output += resp['Face']['ExternalImageId'] + \
", Similarity: " + str(resp['Similarity']) + '<br>'
except rekognition.exceptions.InvalidParameterException:
# Catches exception when no faces are detected in the input image
output = "Recognition did not detect face<br>"
return output
class User(UserMixin):
def get_id(self):
self.id = "admin"
return self.id
AdminUser = User()
@login_manager.user_loader
def load_user(user_id):
return AdminUser
@login_manager.unauthorized_handler
def unauthorized():
flash('Must be logged in first.')
return login_page()
@application.route('/login', methods=['GET', 'POST'])
def login_page():
form = LoginForm()
if request.method == 'POST':
if form.validate_on_submit():
if form.username.data == "admin" and form.password.data == admin_pass:
login_user(AdminUser)
return main_page()
else:
flash('Invalid username or password.')
return render_template("login.html", form=form)
else:
flash('Form not valid.')
return render_template("login.html", form=form)
@application.route("/logout")
@login_required
def logout():
logout_user()
return main_page()
if __name__ == '__main__':
application.run(debug=True, host="0.0.0.0")