dc086989 commited on
Commit
16f1995
·
verified ·
1 Parent(s): 0c753c1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -145
app.py CHANGED
@@ -72,122 +72,29 @@ def face_compare(frame1, frame2):
72
  return detections_table + matches_table
73
 
74
  def check_liveness(frame):
75
- url = "https://faceapi.miniai.live/face_liveness_check"
76
- file = {'file': open(frame, 'rb')}
77
-
78
- r = requests.post(url=url, files=file)
79
-
80
- faceCount = None
81
-
82
- response_data = r.json()
83
-
84
- for item in response_data.get('face_state', []):
85
- if 'faceCount' in item:
86
- faceCount = item['faceCount']
87
- break
88
-
89
- faces = None
90
- live_result = []
91
- live_result.append(f"<table><tr><th>FaceID</th><th>Age</th><th>Gender</th><th>Liveness</th></tr>")
92
-
93
- for item in response_data.get('face_state', []):
94
- if item.get('FaceID'):
95
- faceID = item.get('FaceID')
96
- result = item.get('LivenessCheck')
97
- age = item.get('Age')
98
- gender = item.get('Gender')
99
- live_result.append(f"<tr><td>{faceID}</td><td>{age}</td><td>{gender}</td><td>{result}</td></tr>")
100
- live_result.append(f"</table>")
101
- live_result = ''.join(live_result)
102
-
103
- try:
104
- image = Image.open(frame)
105
-
106
- for face in r.json().get('faces'):
107
- x1 = face.get('x1')
108
- y1 = face.get('y1')
109
- x2 = face.get('x2')
110
- y2 = face.get('y2')
111
-
112
- if x1 < 0:
113
- x1 = 0
114
- if y1 < 0:
115
- y1 = 0
116
- if x2 >= image.width:
117
- x2 = image.width - 1
118
- if y2 >= image.height:
119
- y2 = image.height - 1
120
-
121
- face_image = image.crop((x1, y1, x2, y2))
122
- face_image_ratio = face_image.width / float(face_image.height)
123
- resized_w = int(face_image_ratio * 150)
124
- resized_h = 150
125
-
126
- face_image = face_image.resize((int(resized_w), int(resized_h)))
127
-
128
- if faces is None:
129
- faces = face_image
130
- else:
131
- new_image = Image.new('RGB',(faces.width + face_image.width + 10, 150), (80,80,80))
132
-
133
- new_image.paste(faces,(0,0))
134
- new_image.paste(face_image,(faces.width + 10, 0))
135
- faces = new_image.copy()
136
- except:
137
- pass
138
-
139
- return [faces, live_result]
140
-
141
- def face_emotion(frame):
142
- url = "https://faceapi.miniai.live/face_emotion"
143
- file = {'file': open(frame, 'rb')}
144
-
145
- r = requests.post(url=url, files=file)
146
-
147
- emotion_result = []
148
- emotion_result.append(f"<table><tr><td>Emotional Result : </td><td>{r.json().get('emotion_result')}</td></tr>")
149
- emotion_result.append(f"</table>")
150
- emotion_result = ''.join(emotion_result)
151
-
152
- faces = None
153
-
154
- try:
155
- image = Image.open(frame)
156
-
157
- for face in r.json().get('faces'):
158
- x1 = face.get('x1')
159
- y1 = face.get('y1')
160
- x2 = face.get('x2')
161
- y2 = face.get('y2')
162
-
163
- if x1 < 0:
164
- x1 = 0
165
- if y1 < 0:
166
- y1 = 0
167
- if x2 >= image.width:
168
- x2 = image.width - 1
169
- if y2 >= image.height:
170
- y2 = image.height - 1
171
-
172
- face_image = image.crop((x1, y1, x2, y2))
173
- face_image_ratio = face_image.width / float(face_image.height)
174
- resized_w = int(face_image_ratio * 150)
175
- resized_h = 150
176
-
177
- face_image = face_image.resize((int(resized_w), int(resized_h)))
178
-
179
- if faces is None:
180
- faces = face_image
181
- else:
182
- new_image = Image.new('RGB',(faces.width + face_image.width + 10, 150), (80,80,80))
183
-
184
- new_image.paste(faces,(0,0))
185
- new_image.paste(face_image,(faces.width + 10, 0))
186
- faces = new_image.copy()
187
- except:
188
- pass
189
 
190
- return [faces, emotion_result]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
191
 
192
  # APP Interface
193
  with gr.Blocks() as MiniAIdemo:
@@ -251,7 +158,7 @@ with gr.Blocks() as MiniAIdemo:
251
  btn_f_match.click(face_compare, inputs=[im_match_in1, im_match_in2], outputs=txt_compare_out)
252
  with gr.Tab("Face Liveness Detection"):
253
  with gr.Row():
254
- with gr.Column(scale=1):
255
  im_liveness_in = gr.Image(type='filepath', height=300)
256
  gr.Examples(
257
  [
@@ -267,36 +174,9 @@ with gr.Blocks() as MiniAIdemo:
267
  inputs=im_liveness_in
268
  )
269
  btn_f_liveness = gr.Button("Check Liveness!", variant='primary')
270
- with gr.Blocks():
271
- with gr.Row():
272
- with gr.Column():
273
- im_liveness_out = gr.Image(label="Croped Face", type="pil", scale=1)
274
- with gr.Column():
275
- livness_result_output = gr.HTML()
276
- btn_f_liveness.click(check_liveness, inputs=im_liveness_in, outputs=[im_liveness_out, livness_result_output])
277
- with gr.Tab("Face Emotional Recognition"):
278
- with gr.Row():
279
  with gr.Column():
280
- im_emotion_in = gr.Image(type='filepath', height=300)
281
- gr.Examples(
282
- [
283
- "images/emotion/1.jpg",
284
- "images/emotion/2.jpg",
285
- "images/emotion/3.jpg",
286
- "images/emotion/4.jpg",
287
- "images/emotion/5.jpg",
288
- "images/emotion/6.jpg",
289
- ],
290
- inputs=im_emotion_in
291
- )
292
- btn_f_emotion = gr.Button("Check Emotion!", variant='primary')
293
- with gr.Blocks():
294
- with gr.Row():
295
- with gr.Column():
296
- im_emotion_out = gr.Image(label="Result Image", type="pil", scale=1)
297
- with gr.Column():
298
- txt_emotion_out = gr.HTML()
299
- btn_f_emotion.click(face_emotion, inputs=im_emotion_in, outputs=[im_emotion_out, txt_emotion_out])
300
  gr.HTML('<a href="https://visitorbadge.io/status?path=https%3A%2F%2Fhuggingface.co%2Fspaces%2FMiniAiLive%2FFaceRecognition-LivenessDetection-Demo"><img src="https://api.visitorbadge.io/api/combined?path=https%3A%2F%2Fhuggingface.co%2Fspaces%2FMiniAiLive%2FFaceRecognition-LivenessDetection-Demo&label=VISITORS&labelColor=%2337d67a&countColor=%23ff8a65&style=plastic&labelStyle=none" /></a>')
301
  if __name__ == "__main__":
302
  MiniAIdemo.launch()
 
72
  return detections_table + matches_table
73
 
74
  def check_liveness(frame):
75
+ url = "https://facelive.miniai.live/api/check_livenes"
76
+ files = {'image': open(frame, 'rb')}
77
+ r = requests.post(url=url, files=files)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
78
 
79
+ html = None
80
+ table_value = ""
81
+
82
+ for key, value in r.json().items():
83
+ row_value = ("<tr>"
84
+ "<td>{key}</td>"
85
+ "<td>{value}</td>"
86
+ "</tr>".format(key=key, value=value))
87
+ table_value = table_value + row_value
88
+
89
+ html = ("<table>"
90
+ "<tr>"
91
+ "<th style=""width:30%"">Field</th>"
92
+ "<th style=""width:50%"">Value</th>"
93
+ "</tr>"
94
+ "{table_value}"
95
+ "</table>".format(table_value=table_value))
96
+
97
+ return html
98
 
99
  # APP Interface
100
  with gr.Blocks() as MiniAIdemo:
 
158
  btn_f_match.click(face_compare, inputs=[im_match_in1, im_match_in2], outputs=txt_compare_out)
159
  with gr.Tab("Face Liveness Detection"):
160
  with gr.Row():
161
+ with gr.Column():
162
  im_liveness_in = gr.Image(type='filepath', height=300)
163
  gr.Examples(
164
  [
 
174
  inputs=im_liveness_in
175
  )
176
  btn_f_liveness = gr.Button("Check Liveness!", variant='primary')
 
 
 
 
 
 
 
 
 
177
  with gr.Column():
178
+ livness_result_output = gr.HTML()
179
+ btn_f_liveness.click(check_liveness, inputs=im_liveness_in, outputs=livness_result_output)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
180
  gr.HTML('<a href="https://visitorbadge.io/status?path=https%3A%2F%2Fhuggingface.co%2Fspaces%2FMiniAiLive%2FFaceRecognition-LivenessDetection-Demo"><img src="https://api.visitorbadge.io/api/combined?path=https%3A%2F%2Fhuggingface.co%2Fspaces%2FMiniAiLive%2FFaceRecognition-LivenessDetection-Demo&label=VISITORS&labelColor=%2337d67a&countColor=%23ff8a65&style=plastic&labelStyle=none" /></a>')
181
  if __name__ == "__main__":
182
  MiniAIdemo.launch()