-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.py
187 lines (153 loc) · 7.88 KB
/
index.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
import face_recognition
from flask import Flask, jsonify, request, redirect
from services import service_extract_face_encoding, service_store_face_encoding, service_fetch_face_ID
import pickle
# database = pickle.loads(open("./database/facial_data.pickle", "rb").read())
# You can change this to any folder on your system
ALLOWED_EXTENSIONS = {'png', 'jpg', 'jpeg', 'gif'}
app = Flask(__name__)
def allowed_file(filename):
return '.' in filename and \
filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
@app.route('/service/checkid', methods=['GET','POST'])
def check_upload():
# Check if a valid image file was uploaded
if request.method == 'POST':
if 'file' not in request.files:
return 300
file = request.files['file']
if file.filename == '':
return 300
if file and allowed_file(file.filename):
faceEncoding = service_extract_face_encoding.extractFaceEncoding(file)
facecount = len(faceEncoding)
faceID = service_fetch_face_ID.fetchFaceID(faceEncoding)
res = {
"number of faces": facecount,
"name":faceID,
}
return jsonify(res)
# If no valid image file was uploaded, show the file upload form:
return '''
<!doctype html>
<title>Face Recognition</title>
<h1>Upload a picture. Let's see if we know it is!</h1>
<form method="POST" enctype="multipart/form-data">
<input type="file" name="file">
<input type="submit" value="Upload">
</form>
<br>
<p><a href="/service/storeid"> Register your own face </a>Done that already?</p>
<p> <a href="/service/checkid"> Click here</a> to check if we know you! </p>
'''
@app.route('/service/storeid', methods=['GET','POST'])
def register_upload():
# Check if a valid image file was uploaded
if request.method == 'POST':
if 'file' not in request.files:
return 300
file = request.files['file']
if file.filename == '':
return 300
if file and allowed_file(file.filename) and request.form.get('id'):
faceEncoding = service_extract_face_encoding.extractFaceEncoding(file)
storeFormat = {'faceID':request.form.get('id'), 'faceEncoding': faceEncoding}
service_store_face_encoding.storeFaceEconding(storeFormat)
return '''
<!doctype html>
<title>Face Register</title>
<h1>That was the hard part!</h1>
<h4>Now let's see if it worked. Upload an image here to check --> <a href="/service/checkid">Photo checker</a></h4>
<h5> You can also upload another face for us to detect</h5> <a href="/service/storeid"> Register your face </a>
'''
# If no valid image file was uploaded, show the file upload form:
return '''
<!doctype html>
<title>Face Register</title>
<h1>Upload a picture to register for Face Recognition!</h1>
<form method="POST" enctype="multipart/form-data">
<input type="file" name="file">
<p>Name</p>
<input type="text" name="id" >
<input type="submit" value="Upload">
</form>
<h4>Already registered? Upload an image here to check --> <a href="/service/checkid">Photo checker</a> </h4>
<p>If you're unsure, try <a href="/">this test first</a></p>
'''
# storeFormat = {'faceID':faceID, 'faceEncoding': faceEncoding}
@app.route('/', methods=['GET', 'POST'])
def upload_image():
# Check if a valid image file was uploaded
if request.method == 'POST':
if 'file' not in request.files:
return redirect(request.url)
file = request.files['file']
if file.filename == '':
return redirect(request.url)
if file and allowed_file(file.filename):
# The image file seems valid! Detect faces and return the result.
return detect_faces_in_image(file)
# If no valid image file was uploaded, show the file upload form:
return '''
<!doctype html>
<title>Is this a picture of Obama?</title>
<h1>Upload a picture and see if it's a picture of Obama!</h1>
<form method="POST" enctype="multipart/form-data">
<input type="file" name="file">
<input type="submit" value="Upload">
</form>
<h4>Are you ready to try it out? <a href="/service/storeid"> Register your face </a> </h4>
<h5>Already registered? Upload an image here to check --> <a href="/service/checkid">Photo checker</a> </h5>
'''
def detect_faces_in_image(file_stream):
# Pre-calculated face encoding of Obama generated with face_recognition.face_encodings(img)
known_face_encoding = [-0.09634063, 0.12095481, -0.00436332, -0.07643753, 0.0080383,
0.01902981, -0.07184699, -0.09383309, 0.18518871, -0.09588896,
0.23951106, 0.0986533 , -0.22114635, -0.1363683 , 0.04405268,
0.11574756, -0.19899382, -0.09597053, -0.11969153, -0.12277931,
0.03416885, -0.00267565, 0.09203379, 0.04713435, -0.12731361,
-0.35371891, -0.0503444 , -0.17841317, -0.00310897, -0.09844551,
-0.06910533, -0.00503746, -0.18466514, -0.09851682, 0.02903969,
-0.02174894, 0.02261871, 0.0032102 , 0.20312519, 0.02999607,
-0.11646006, 0.09432904, 0.02774341, 0.22102901, 0.26725179,
0.06896867, -0.00490024, -0.09441824, 0.11115381, -0.22592428,
0.06230862, 0.16559327, 0.06232892, 0.03458837, 0.09459756,
-0.18777156, 0.00654241, 0.08582542, -0.13578284, 0.0150229 ,
0.00670836, -0.08195844, -0.04346499, 0.03347827, 0.20310158,
0.09987706, -0.12370517, -0.06683611, 0.12704916, -0.02160804,
0.00984683, 0.00766284, -0.18980607, -0.19641446, -0.22800779,
0.09010898, 0.39178532, 0.18818057, -0.20875394, 0.03097027,
-0.21300618, 0.02532415, 0.07938635, 0.01000703, -0.07719778,
-0.12651891, -0.04318593, 0.06219772, 0.09163868, 0.05039065,
-0.04922386, 0.21839413, -0.02394437, 0.06173781, 0.0292527 ,
0.06160797, -0.15553983, -0.02440624, -0.17509389, -0.0630486 ,
0.01428208, -0.03637431, 0.03971229, 0.13983178, -0.23006812,
0.04999552, 0.0108454 , -0.03970895, 0.02501768, 0.08157793,
-0.03224047, -0.04502571, 0.0556995 , -0.24374914, 0.25514284,
0.24795187, 0.04060191, 0.17597422, 0.07966681, 0.01920104,
-0.01194376, -0.02300822, -0.17204897, -0.0596558 , 0.05307484,
0.07417042, 0.07126575, 0.00209804]
# Load the uploaded image file
img = face_recognition.load_image_file(file_stream)
# Get face encodings for any faces in the uploaded image
unknown_face_encodings = face_recognition.face_encodings(img)
facecount= len(unknown_face_encodings)
face_found = False
is_obama = False
if len(unknown_face_encodings) > 0:
face_found = True
# See if the first face in the uploaded image matches the known face of Obama
match_results = face_recognition.compare_faces([known_face_encoding], unknown_face_encodings[0])
if match_results[0]:
is_obama = True
# Return the result as json
result = {
"face_found_in_image": face_found,
"is_picture_of_obama": is_obama,
"number_of_faces": facecount
}
return jsonify(result)
if __name__ == "__main__":
from os import environ
# app.run(host='0.0.0.0', port=5001, debug=True)
app.run(host='0.0.0.0', port=environ.get('PORT', 5555), debug=False)