-
Notifications
You must be signed in to change notification settings - Fork 37
/
app.py
275 lines (225 loc) · 10 KB
/
app.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
import sys
sys.path.append('.')
import os
import numpy as np
import base64
import io
from PIL import Image
from flask import Flask, request, jsonify
from facesdk import getMachineCode
from facesdk import setActivation
from facesdk import faceDetection
from facesdk import initSDK
from facebox import FaceBox
livenessThreshold = 0.7
yawThreshold = 10
pitchThreshold = 10
rollThreshold = 10
occlusionThreshold = 0.9
eyeClosureThreshold = 0.8
mouthOpeningThreshold = 0.5
borderRate = 0.05
smallFaceThreshold = 100
lowQualityThreshold = 0.3
hightQualityThreshold = 0.7
luminanceDarkThreshold = 50
luminanceLightThreshold = 200
maxFaceCount = 10
licensePath = "license.txt"
license = ""
machineCode = getMachineCode()
print("machineCode: ", machineCode.decode('utf-8'))
try:
with open(licensePath, 'r') as file:
license = file.read()
except IOError as exc:
print("failed to open license.txt: ", exc.errno)
print("license: ", license)
ret = setActivation(license.encode('utf-8'))
print("activation: ", ret)
ret = initSDK("data".encode('utf-8'))
print("init: ", ret)
app = Flask(__name__)
@app.route('/check_liveness', methods=['POST'])
def check_liveness():
faces = []
isNotFront = None
isOcclusion = None
isEyeClosure = None
isMouthOpening = None
isBoundary = None
isSmall = None
quality = None
luminance = None
livenessScore = None
file = request.files['file']
try:
image = Image.open(file)
except:
result = "Failed to open file"
faceState = {"is_not_front": isNotFront, "is_occluded": isOcclusion, "eye_closed": isEyeClosure, "mouth_opened": isMouthOpening,
"is_boundary_face": isBoundary, "is_small": isSmall, "quality": quality, "luminance": luminance, "result": result, "liveness_score": livenessScore}
response = jsonify({"face_state": faceState, "faces": faces})
response.status_code = 200
response.headers["Content-Type"] = "application/json; charset=utf-8"
return response
image_np = np.asarray(image)
faceBoxes = (FaceBox * maxFaceCount)()
faceCount = faceDetection(image_np, image_np.shape[1], image_np.shape[0], faceBoxes, maxFaceCount)
for i in range(faceCount):
landmark_68 = []
for j in range(68):
landmark_68.append({"x": faceBoxes[i].landmark_68[j * 2], "y": faceBoxes[i].landmark_68[j * 2 + 1]})
faces.append({"x1": faceBoxes[i].x1, "y1": faceBoxes[i].y1, "x2": faceBoxes[i].x2, "y2": faceBoxes[i].y2,
"liveness": faceBoxes[i].liveness,
"yaw": faceBoxes[i].yaw, "roll": faceBoxes[i].roll, "pitch": faceBoxes[i].pitch,
"face_quality": faceBoxes[i].face_quality, "face_luminance": faceBoxes[i].face_luminance, "eye_dist": faceBoxes[i].eye_dist,
"left_eye_closed": faceBoxes[i].left_eye_closed, "right_eye_closed": faceBoxes[i].right_eye_closed,
"face_occlusion": faceBoxes[i].face_occlusion, "mouth_opened": faceBoxes[i].mouth_opened,
"landmark_68": landmark_68})
result = ""
if faceCount == 0:
result = "No face"
elif faceCount > 1:
result = "Multiple face"
else:
livenessScore = faceBoxes[0].liveness
if livenessScore > livenessThreshold:
result = "Real"
else:
result = "Spoof"
isNotFront = True
isOcclusion = False
isEyeClosure = False
isMouthOpening = False
isBoundary = False
isSmall = False
quality = "Low"
luminance = "Dark"
if abs(faceBoxes[0].yaw) < yawThreshold and abs(faceBoxes[0].roll) < rollThreshold and abs(faceBoxes[0].pitch) < pitchThreshold:
isNotFront = False
if faceBoxes[0].face_occlusion > occlusionThreshold:
isOcclusion = True
if faceBoxes[0].left_eye_closed > eyeClosureThreshold or faceBoxes[0].right_eye_closed > eyeClosureThreshold:
isEyeClosure = True
if faceBoxes[0].mouth_opened > mouthOpeningThreshold:
isMouthOpening = True
if (faceBoxes[0].x1 < image_np.shape[1] * borderRate or
faceBoxes[0].y1 < image_np.shape[0] * borderRate or
faceBoxes[0].x1 > image_np.shape[1] - image_np.shape[1] * borderRate or
faceBoxes[0].x1 > image_np.shape[0] - image_np.shape[0] * borderRate):
isBoundary = True
if faceBoxes[0].eye_dist < smallFaceThreshold:
isSmall = True
if faceBoxes[0].face_quality < lowQualityThreshold:
quality = "Low"
elif faceBoxes[0].face_quality < hightQualityThreshold:
quality = "Medium"
else:
quality = "High"
if faceBoxes[0].face_luminance < luminanceDarkThreshold:
luminance = "Dark"
elif faceBoxes[0].face_luminance < luminanceLightThreshold:
luminance = "Normal"
else:
luminance = "Light"
faceState = {"is_not_front": isNotFront, "is_occluded": isOcclusion, "eye_closed": isEyeClosure, "mouth_opened": isMouthOpening,
"is_boundary_face": isBoundary, "is_small": isSmall, "quality": quality, "luminance": luminance, "result": result, "liveness_score": livenessScore}
response = jsonify({"face_state": faceState, "faces": faces})
response.status_code = 200
response.headers["Content-Type"] = "application/json; charset=utf-8"
return response
@app.route('/check_liveness_base64', methods=['POST'])
def check_liveness_base64():
faces = []
isNotFront = None
isOcclusion = None
isEyeClosure = None
isMouthOpening = None
isBoundary = None
isSmall = None
quality = None
luminance = None
livenessScore = None
content = request.get_json()
try:
imageBase64 = content['base64']
image_data = base64.b64decode(imageBase64)
image = Image.open(io.BytesIO(image_data))
except:
result = "Failed to open file"
faceState = {"is_not_front": isNotFront, "is_occluded": isOcclusion, "eye_closed": isEyeClosure, "mouth_opened": isMouthOpening,
"is_boundary_face": isBoundary, "is_small": isSmall, "quality": quality, "luminance": luminance, "result": result, "liveness_score": livenessScore}
response = jsonify({"face_state": faceState, "faces": faces})
response.status_code = 200
response.headers["Content-Type"] = "application/json; charset=utf-8"
return response
image_np = np.asarray(image)
faceBoxes = (FaceBox * maxFaceCount)()
faceCount = faceDetection(image_np, image_np.shape[1], image_np.shape[0], faceBoxes, maxFaceCount)
for i in range(faceCount):
landmark_68 = []
for j in range(68):
landmark_68.append({"x": faceBoxes[i].landmark_68[j * 2], "y": faceBoxes[i].landmark_68[j * 2 + 1]})
faces.append({"x1": faceBoxes[i].x1, "y1": faceBoxes[i].y1, "x2": faceBoxes[i].x2, "y2": faceBoxes[i].y2,
"liveness": faceBoxes[i].liveness,
"yaw": faceBoxes[i].yaw, "roll": faceBoxes[i].roll, "pitch": faceBoxes[i].pitch,
"face_quality": faceBoxes[i].face_quality, "face_luminance": faceBoxes[i].face_luminance, "eye_dist": faceBoxes[i].eye_dist,
"left_eye_closed": faceBoxes[i].left_eye_closed, "right_eye_closed": faceBoxes[i].right_eye_closed,
"face_occlusion": faceBoxes[i].face_occlusion, "mouth_opened": faceBoxes[i].mouth_opened,
"landmark_68": landmark_68})
result = ""
if faceCount == 0:
result = "No face"
elif faceCount > 1:
result = "Multiple face"
else:
livenessScore = faceBoxes[0].liveness
if livenessScore > livenessThreshold:
result = "Real"
else:
result = "Spoof"
isNotFront = True
isOcclusion = False
isEyeClosure = False
isMouthOpening = False
isBoundary = False
isSmall = False
quality = "Low"
luminance = "Dark"
if abs(faceBoxes[0].yaw) < yawThreshold and abs(faceBoxes[0].roll) < rollThreshold and abs(faceBoxes[0].pitch) < pitchThreshold:
isNotFront = False
if faceBoxes[0].face_occlusion > occlusionThreshold:
isOcclusion = True
if faceBoxes[0].left_eye_closed > eyeClosureThreshold or faceBoxes[0].right_eye_closed > eyeClosureThreshold:
isEyeClosure = True
if faceBoxes[0].mouth_opened > mouthOpeningThreshold:
isMouthOpening = True
if (faceBoxes[0].x1 < image_np.shape[1] * borderRate or
faceBoxes[0].y1 < image_np.shape[0] * borderRate or
faceBoxes[0].x1 > image_np.shape[1] - image_np.shape[1] * borderRate or
faceBoxes[0].x1 > image_np.shape[0] - image_np.shape[0] * borderRate):
isBoundary = True
if faceBoxes[0].eye_dist < smallFaceThreshold:
isSmall = True
if faceBoxes[0].face_quality < lowQualityThreshold:
quality = "Low"
elif faceBoxes[0].face_quality < hightQualityThreshold:
quality = "Medium"
else:
quality = "High"
if faceBoxes[0].face_luminance < luminanceDarkThreshold:
luminance = "Dark"
elif faceBoxes[0].face_luminance < luminanceLightThreshold:
luminance = "Normal"
else:
luminance = "Light"
faceState = {"is_not_front": isNotFront, "is_occluded": isOcclusion, "eye_closed": isEyeClosure, "mouth_opened": isMouthOpening,
"is_boundary_face": isBoundary, "is_small": isSmall, "quality": quality, "luminance": luminance, "result": result, "liveness_score": livenessScore}
response = jsonify({"face_state": faceState, "faces": faces})
response.status_code = 200
response.headers["Content-Type"] = "application/json; charset=utf-8"
return response
if __name__ == '__main__':
port = int(os.environ.get("PORT", 8080))
app.run(host='0.0.0.0', port=port)