justin2341 commited on
Commit
15b0ec5
·
verified ·
1 Parent(s): f19a916

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +99 -63
app.py CHANGED
@@ -18,7 +18,7 @@ from facebox import FaceBox
18
 
19
  verifyThreshold = 0.67
20
 
21
- maxFaceCount = 1
22
 
23
  licensePath = "license.txt"
24
  license = ""
@@ -90,49 +90,67 @@ def compare_face():
90
  faceBoxes2 = (FaceBox * maxFaceCount)()
91
  faceCount2 = faceDetection(image_np2, image_np2.shape[1], image_np2.shape[0], faceBoxes2, maxFaceCount)
92
 
93
- if faceCount1 == 1 and faceCount2 == 1:
94
- templateExtraction(image_np1, image_np1.shape[1], image_np1.shape[0], faceBoxes1[0])
95
- templateExtraction(image_np2, image_np2.shape[1], image_np2.shape[0], faceBoxes2[0])
96
- similarity = similarityCalculation(faceBoxes1[0].templates, faceBoxes2[0].templates)
97
- if similarity > verifyThreshold:
98
- result = "Same person"
99
- else:
100
- result = "Different person"
101
- elif faceCount1 == 0:
102
- result = "No face1"
103
- elif faceCount2 == 0:
104
- result = "No face2"
105
-
106
- if faceCount1 == 1:
107
  landmark_68 = []
108
  for j in range(68):
109
- landmark_68.append({"x": faceBoxes1[0].landmark_68[j * 2], "y": faceBoxes1[0].landmark_68[j * 2 + 1]})
110
 
111
- face1 = {"x1": faceBoxes1[0].x1, "y1": faceBoxes1[0].y1, "x2": faceBoxes1[0].x2, "y2": faceBoxes1[0].y2,
112
- "yaw": faceBoxes1[0].yaw, "roll": faceBoxes1[0].roll, "pitch": faceBoxes1[0].pitch,
113
- "face_quality": faceBoxes1[0].face_quality, "face_luminance": faceBoxes1[0].face_luminance, "eye_dist": faceBoxes1[0].eye_dist,
114
- "left_eye_closed": faceBoxes1[0].left_eye_closed, "right_eye_closed": faceBoxes1[0].right_eye_closed,
115
- "face_occlusion": faceBoxes1[0].face_occlusion, "mouth_opened": faceBoxes1[0].mouth_opened,
116
  "landmark_68": landmark_68}
 
 
 
 
 
117
 
118
- if faceCount2 == 1:
119
  landmark_68 = []
120
  for j in range(68):
121
- landmark_68.append({"x": faceBoxes2[0].landmark_68[j * 2], "y": faceBoxes2[0].landmark_68[j * 2 + 1]})
122
 
123
- face2 = {"x1": faceBoxes2[0].x1, "y1": faceBoxes2[0].y1, "x2": faceBoxes2[0].x2, "y2": faceBoxes2[0].y2,
124
- "yaw": faceBoxes2[0].yaw, "roll": faceBoxes2[0].roll, "pitch": faceBoxes2[0].pitch,
125
- "face_quality": faceBoxes2[0].face_quality, "face_luminance": faceBoxes2[0].face_luminance, "eye_dist": faceBoxes2[0].eye_dist,
126
- "left_eye_closed": faceBoxes2[0].left_eye_closed, "right_eye_closed": faceBoxes2[0].right_eye_closed,
127
- "face_occlusion": faceBoxes2[0].face_occlusion, "mouth_opened": faceBoxes2[0].mouth_opened,
 
128
  "landmark_68": landmark_68}
 
 
 
 
 
 
 
 
 
 
 
 
 
129
 
130
- response = jsonify({"compare_result": result, "compare_similarity": similarity, "face1": face1, "face2": face2})
 
 
 
 
131
 
132
- response.status_code = 200
133
- response.headers["Content-Type"] = "application/json; charset=utf-8"
134
- return response
 
 
135
 
 
 
 
 
136
  @app.route('/compare_face_base64', methods=['POST'])
137
  def compare_face_base64():
138
  result = "None"
@@ -175,48 +193,66 @@ def compare_face_base64():
175
  faceBoxes2 = (FaceBox * maxFaceCount)()
176
  faceCount2 = faceDetection(image_np2, image_np2.shape[1], image_np2.shape[0], faceBoxes2, maxFaceCount)
177
 
178
- if faceCount1 == 1 and faceCount2 == 1:
179
- templateExtraction(image_np1, image_np1.shape[1], image_np1.shape[0], faceBoxes1[0])
180
- templateExtraction(image_np2, image_np2.shape[1], image_np2.shape[0], faceBoxes2[0])
181
- similarity = similarityCalculation(faceBoxes1[0].templates, faceBoxes2[0].templates)
182
- if similarity > verifyThreshold:
183
- result = "Same person"
184
- else:
185
- result = "Different person"
186
- elif faceCount1 == 0:
187
- result = "No face1"
188
- elif faceCount2 == 0:
189
- result = "No face2"
190
-
191
- if faceCount1 == 1:
192
  landmark_68 = []
193
  for j in range(68):
194
- landmark_68.append({"x": faceBoxes1[0].landmark_68[j * 2], "y": faceBoxes1[0].landmark_68[j * 2 + 1]})
195
 
196
- face1 = {"x1": faceBoxes1[0].x1, "y1": faceBoxes1[0].y1, "x2": faceBoxes1[0].x2, "y2": faceBoxes1[0].y2,
197
- "yaw": faceBoxes1[0].yaw, "roll": faceBoxes1[0].roll, "pitch": faceBoxes1[0].pitch,
198
- "face_quality": faceBoxes1[0].face_quality, "face_luminance": faceBoxes1[0].face_luminance, "eye_dist": faceBoxes1[0].eye_dist,
199
- "left_eye_closed": faceBoxes1[0].left_eye_closed, "right_eye_closed": faceBoxes1[0].right_eye_closed,
200
- "face_occlusion": faceBoxes1[0].face_occlusion, "mouth_opened": faceBoxes1[0].mouth_opened,
201
  "landmark_68": landmark_68}
 
 
 
 
 
202
 
203
- if faceCount2 == 1:
204
  landmark_68 = []
205
  for j in range(68):
206
- landmark_68.append({"x": faceBoxes2[0].landmark_68[j * 2], "y": faceBoxes2[0].landmark_68[j * 2 + 1]})
 
207
 
208
- face2 = {"x1": faceBoxes2[0].x1, "y1": faceBoxes2[0].y1, "x2": faceBoxes2[0].x2, "y2": faceBoxes2[0].y2,
209
- "yaw": faceBoxes2[0].yaw, "roll": faceBoxes2[0].roll, "pitch": faceBoxes2[0].pitch,
210
- "face_quality": faceBoxes2[0].face_quality, "face_luminance": faceBoxes2[0].face_luminance, "eye_dist": faceBoxes2[0].eye_dist,
211
- "left_eye_closed": faceBoxes2[0].left_eye_closed, "right_eye_closed": faceBoxes2[0].right_eye_closed,
212
- "face_occlusion": faceBoxes2[0].face_occlusion, "mouth_opened": faceBoxes2[0].mouth_opened,
213
  "landmark_68": landmark_68}
 
 
 
 
 
 
 
 
 
 
 
214
 
215
- response = jsonify({"compare_result": result, "compare_similarity": similarity, "face1": face1, "face2": face2})
 
 
 
 
 
 
216
 
217
- response.status_code = 200
218
- response.headers["Content-Type"] = "application/json; charset=utf-8"
219
- return response
 
 
 
 
 
 
220
 
221
  if __name__ == '__main__':
222
  port = int(os.environ.get("PORT", 8080))
 
18
 
19
  verifyThreshold = 0.67
20
 
21
+ maxFaceCount = 8
22
 
23
  licensePath = "license.txt"
24
  license = ""
 
90
  faceBoxes2 = (FaceBox * maxFaceCount)()
91
  faceCount2 = faceDetection(image_np2, image_np2.shape[1], image_np2.shape[0], faceBoxes2, maxFaceCount)
92
 
93
+ faces1_result = []
94
+ for i in range(faceCount1):
95
+ templateExtraction(image_np1, image_np1.shape[1], image_np1.shape[0], faceBoxes1[i])
96
+
 
 
 
 
 
 
 
 
 
 
97
  landmark_68 = []
98
  for j in range(68):
99
+ landmark_68.append({"x": faceBoxes1[i].landmark_68[j * 2], "y": faceBoxes1[i].landmark_68[j * 2 + 1]})
100
 
101
+ face = {"x1": faceBoxes1[i].x1, "y1": faceBoxes1[i].y1, "x2": faceBoxes1[i].x2, "y2": faceBoxes1[i].y2,
102
+ "yaw": faceBoxes1[i].yaw, "roll": faceBoxes1[i].roll, "pitch": faceBoxes1[i].pitch,
103
+ "face_quality": faceBoxes1[i].face_quality, "face_luminance": faceBoxes1[i].face_luminance, "eye_dist": faceBoxes1[i].eye_dist,
104
+ "left_eye_closed": faceBoxes1[i].left_eye_closed, "right_eye_closed": faceBoxes1[i].right_eye_closed,
105
+ "face_occlusion": faceBoxes1[i].face_occlusion, "mouth_opened": faceBoxes1[i].mouth_opened,
106
  "landmark_68": landmark_68}
107
+
108
+ faces1_result.append(face)
109
+
110
+ for i in range(faceCount2):
111
+ templateExtraction(image_np2, image_np2.shape[1], image_np2.shape[0], faceBoxes2[i])
112
 
 
113
  landmark_68 = []
114
  for j in range(68):
115
+ landmark_68.append({"x": faceBoxes2[i].landmark_68[j * 2], "y": faceBoxes2[i].landmark_68[j * 2 + 1]})
116
 
117
+
118
+ face = {"x1": faceBoxes2[i].x1, "y1": faceBoxes2[i].y1, "x2": faceBoxes2[i].x2, "y2": faceBoxes2[i].y2,
119
+ "yaw": faceBoxes2[i].yaw, "roll": faceBoxes2[i].roll, "pitch": faceBoxes2[i].pitch,
120
+ "face_quality": faceBoxes2[i].face_quality, "face_luminance": faceBoxes2[i].face_luminance, "eye_dist": faceBoxes2[i].eye_dist,
121
+ "left_eye_closed": faceBoxes2[i].left_eye_closed, "right_eye_closed": faceBoxes2[i].right_eye_closed,
122
+ "face_occlusion": faceBoxes2[i].face_occlusion, "mouth_opened": faceBoxes2[i].mouth_opened,
123
  "landmark_68": landmark_68}
124
+
125
+ faces2_result.append(face)
126
+
127
+
128
+ if faceCount1 > 0 and faceCount2 > 0:
129
+ results = []
130
+ for i in range(faceCount1):
131
+ for j in range(faceCount2):
132
+ similarity = similarityCalculation(faceBoxes1[i].templates, faceBoxes2[j].templates)
133
+ match_result = {"face1": i, "face2": j, "similarity": similarity}
134
+ results.append(match_result)
135
+
136
+ response = jsonify({"resultCode": "Ok", "faces1": faces1_result, "faces2": faces2_result, "results": results})
137
 
138
+ response.status_code = 200
139
+ response.headers["Content-Type"] = "application/json; charset=utf-8"
140
+ return response
141
+ elif faceNum1 == 0:
142
+ response = jsonify({"resultCode": "No face1", "faces1": faces1, "faces2": faces2})
143
 
144
+ response.status_code = 200
145
+ response.headers["Content-Type"] = "application/json; charset=utf-8"
146
+ return response
147
+ elif faceNum2 == 0:
148
+ response = jsonify({"resultCode": "No face2", "faces1": faces1, "faces2": faces2})
149
 
150
+ response.status_code = 200
151
+ response.headers["Content-Type"] = "application/json; charset=utf-8"
152
+ return response
153
+
154
  @app.route('/compare_face_base64', methods=['POST'])
155
  def compare_face_base64():
156
  result = "None"
 
193
  faceBoxes2 = (FaceBox * maxFaceCount)()
194
  faceCount2 = faceDetection(image_np2, image_np2.shape[1], image_np2.shape[0], faceBoxes2, maxFaceCount)
195
 
196
+ faces1_result = []
197
+ for i in range(faceCount1):
198
+ templateExtraction(image_np1, image_np1.shape[1], image_np1.shape[0], faceBoxes1[i])
199
+
 
 
 
 
 
 
 
 
 
 
200
  landmark_68 = []
201
  for j in range(68):
202
+ landmark_68.append({"x": faceBoxes1[i].landmark_68[j * 2], "y": faceBoxes1[i].landmark_68[j * 2 + 1]})
203
 
204
+ face = {"x1": faceBoxes1[i].x1, "y1": faceBoxes1[i].y1, "x2": faceBoxes1[i].x2, "y2": faceBoxes1[i].y2,
205
+ "yaw": faceBoxes1[i].yaw, "roll": faceBoxes1[i].roll, "pitch": faceBoxes1[i].pitch,
206
+ "face_quality": faceBoxes1[i].face_quality, "face_luminance": faceBoxes1[i].face_luminance, "eye_dist": faceBoxes1[i].eye_dist,
207
+ "left_eye_closed": faceBoxes1[i].left_eye_closed, "right_eye_closed": faceBoxes1[i].right_eye_closed,
208
+ "face_occlusion": faceBoxes1[i].face_occlusion, "mouth_opened": faceBoxes1[i].mouth_opened,
209
  "landmark_68": landmark_68}
210
+
211
+ faces1_result.append(face)
212
+
213
+ for i in range(faceCount2):
214
+ templateExtraction(image_np2, image_np2.shape[1], image_np2.shape[0], faceBoxes2[i])
215
 
 
216
  landmark_68 = []
217
  for j in range(68):
218
+ landmark_68.append({"x": faceBoxes2[i].landmark_68[j * 2], "y": faceBoxes2[i].landmark_68[j * 2 + 1]})
219
+
220
 
221
+ face = {"x1": faceBoxes2[i].x1, "y1": faceBoxes2[i].y1, "x2": faceBoxes2[i].x2, "y2": faceBoxes2[i].y2,
222
+ "yaw": faceBoxes2[i].yaw, "roll": faceBoxes2[i].roll, "pitch": faceBoxes2[i].pitch,
223
+ "face_quality": faceBoxes2[i].face_quality, "face_luminance": faceBoxes2[i].face_luminance, "eye_dist": faceBoxes2[i].eye_dist,
224
+ "left_eye_closed": faceBoxes2[i].left_eye_closed, "right_eye_closed": faceBoxes2[i].right_eye_closed,
225
+ "face_occlusion": faceBoxes2[i].face_occlusion, "mouth_opened": faceBoxes2[i].mouth_opened,
226
  "landmark_68": landmark_68}
227
+
228
+ faces2_result.append(face)
229
+
230
+
231
+ if faceCount1 > 0 and faceCount2 > 0:
232
+ results = []
233
+ for i in range(faceCount1):
234
+ for j in range(faceCount2):
235
+ similarity = similarityCalculation(faceBoxes1[i].templates, faceBoxes2[j].templates)
236
+ match_result = {"face1": i, "face2": j, "similarity": similarity}
237
+ results.append(match_result)
238
 
239
+ response = jsonify({"resultCode": "Ok", "faces1": faces1_result, "faces2": faces2_result, "results": results})
240
+
241
+ response.status_code = 200
242
+ response.headers["Content-Type"] = "application/json; charset=utf-8"
243
+ return response
244
+ elif faceNum1 == 0:
245
+ response = jsonify({"resultCode": "No face1", "faces1": faces1, "faces2": faces2})
246
 
247
+ response.status_code = 200
248
+ response.headers["Content-Type"] = "application/json; charset=utf-8"
249
+ return response
250
+ elif faceNum2 == 0:
251
+ response = jsonify({"resultCode": "No face2", "faces1": faces1, "faces2": faces2})
252
+
253
+ response.status_code = 200
254
+ response.headers["Content-Type"] = "application/json; charset=utf-8"
255
+ return response
256
 
257
  if __name__ == '__main__':
258
  port = int(os.environ.get("PORT", 8080))