Update app.py
Browse files
app.py
CHANGED
@@ -5,106 +5,71 @@ import requests
|
|
5 |
from PIL import Image
|
6 |
|
7 |
def face_compare(frame1, frame2):
|
8 |
-
url = "https://
|
9 |
-
files = {'
|
10 |
-
|
11 |
r = requests.post(url=url, files=files)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
"<td>{compare_result}</td>"
|
27 |
-
"</tr>"
|
28 |
-
"<tr>"
|
29 |
-
"<td>Similarity</td>"
|
30 |
-
"<td>{compare_similarity}</td>"
|
31 |
-
"</tr>"
|
32 |
-
"</table>".format(compare_result=compare_result, compare_similarity=compare_similarity))
|
33 |
-
|
34 |
-
try:
|
35 |
-
image1 = Image.open(frame1)
|
36 |
-
image2 = Image.open(frame2)
|
37 |
-
|
38 |
-
face1 = None
|
39 |
-
face2 = None
|
40 |
-
|
41 |
-
if r.json().get('face1') is not None:
|
42 |
-
face = r.json().get('face1')
|
43 |
-
x1 = face.get('x1')
|
44 |
-
y1 = face.get('y1')
|
45 |
-
x2 = face.get('x2')
|
46 |
-
y2 = face.get('y2')
|
47 |
-
|
48 |
-
if x1 < 0:
|
49 |
-
x1 = 0
|
50 |
-
if y1 < 0:
|
51 |
-
y1 = 0
|
52 |
-
if x2 >= image1.width:
|
53 |
-
x2 = image1.width - 1
|
54 |
-
if y2 >= image1.height:
|
55 |
-
y2 = image1.height - 1
|
56 |
-
|
57 |
-
face1 = image1.crop((x1, y1, x2, y2))
|
58 |
-
face_image_ratio = face1.width / float(face1.height)
|
59 |
-
resized_w = int(face_image_ratio * 150)
|
60 |
-
resized_h = 150
|
61 |
-
|
62 |
-
face1 = face1.resize((int(resized_w), int(resized_h)))
|
63 |
-
|
64 |
-
if r.json().get('face2') is not None:
|
65 |
-
face = r.json().get('face2')
|
66 |
-
x1 = face.get('x1')
|
67 |
-
y1 = face.get('y1')
|
68 |
-
x2 = face.get('x2')
|
69 |
-
y2 = face.get('y2')
|
70 |
-
|
71 |
-
if x1 < 0:
|
72 |
-
x1 = 0
|
73 |
-
if y1 < 0:
|
74 |
-
y1 = 0
|
75 |
-
if x2 >= image2.width:
|
76 |
-
x2 = image2.width - 1
|
77 |
-
if y2 >= image2.height:
|
78 |
-
y2 = image2.height - 1
|
79 |
-
|
80 |
-
face2 = image2.crop((x1, y1, x2, y2))
|
81 |
-
face_image_ratio = face2.width / float(face2.height)
|
82 |
-
resized_w = int(face_image_ratio * 150)
|
83 |
-
resized_h = 150
|
84 |
-
|
85 |
-
face2 = face2.resize((int(resized_w), int(resized_h)))
|
86 |
-
|
87 |
-
if face1 is not None and face2 is not None:
|
88 |
-
new_image = Image.new('RGB',(face1.width + face2.width + 10, 150), (80,80,80))
|
89 |
-
|
90 |
-
new_image.paste(face1,(0,0))
|
91 |
-
new_image.paste(face2,(face1.width + 10, 0))
|
92 |
-
faces = new_image.copy()
|
93 |
-
elif face1 is not None and face2 is None:
|
94 |
-
new_image = Image.new('RGB',(face1.width + face1.width + 10, 150), (80,80,80))
|
95 |
-
|
96 |
-
new_image.paste(face1,(0,0))
|
97 |
-
faces = new_image.copy()
|
98 |
-
elif face1 is None and face2 is not None:
|
99 |
-
new_image = Image.new('RGB',(face2.width + face2.width + 10, 150), (80,80,80))
|
100 |
-
|
101 |
-
new_image.paste(face2,(face2.width + 10, 0))
|
102 |
-
faces = new_image.copy()
|
103 |
-
|
104 |
-
except:
|
105 |
-
pass
|
106 |
|
107 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
108 |
|
109 |
def check_liveness(frame):
|
110 |
url = "https://faceapi.miniai.live/face_liveness_check"
|
@@ -281,10 +246,9 @@ with gr.Blocks() as MiniAIdemo:
|
|
281 |
inputs=im_match_in2
|
282 |
)
|
283 |
with gr.Column():
|
284 |
-
im_match_crop = gr.Image(type="pil", height=256)
|
285 |
txt_compare_out = gr.HTML()
|
286 |
btn_f_match = gr.Button("Check Comparing!", variant='primary')
|
287 |
-
btn_f_match.click(face_compare, inputs=[im_match_in1, im_match_in2], outputs=
|
288 |
with gr.Tab("Face Liveness Detection"):
|
289 |
with gr.Row():
|
290 |
with gr.Column(scale=1):
|
|
|
5 |
from PIL import Image
|
6 |
|
7 |
def face_compare(frame1, frame2):
|
8 |
+
url = "https://face.miniai.live/api/face_match"
|
9 |
+
files = {'image1': open(frame1, 'rb'), 'image2': open(frame2, 'rb')}
|
|
|
10 |
r = requests.post(url=url, files=files)
|
11 |
+
response = r.json()
|
12 |
+
|
13 |
+
detections = response.get("detections", [])
|
14 |
+
matches = response.get("match", [])
|
15 |
+
detection_rows = ""
|
16 |
+
match_rows = ""
|
17 |
+
|
18 |
+
# Process detections
|
19 |
+
for detection in detections:
|
20 |
+
face_image = detection.get("face", "")
|
21 |
+
face_img_tag = f"<img src='data:image/png;base64,{face_image}' width='100' />" if face_image else "N/A"
|
22 |
+
first_face_index = detection.get("firstFaceIndex", "N/A")
|
23 |
+
second_face_index = detection.get("secondFaceIndex", "N/A")
|
24 |
+
|
25 |
+
detection_rows += f"""
|
26 |
+
<tr>
|
27 |
+
<td>{first_face_index}</td>
|
28 |
+
<td>{second_face_index}</td>
|
29 |
+
<td>{face_img_tag}</td>
|
30 |
+
</tr>
|
31 |
+
"""
|
32 |
|
33 |
+
# Process matches
|
34 |
+
for match in matches:
|
35 |
+
first_face_index = match.get("firstFaceIndex", "N/A")
|
36 |
+
second_face_index = match.get("secondFaceIndex", "N/A")
|
37 |
+
similarity = match.get("similarity", "N/A")
|
38 |
+
|
39 |
+
match_rows += f"""
|
40 |
+
<tr>
|
41 |
+
<td>{first_face_index}</td>
|
42 |
+
<td>{second_face_index}</td>
|
43 |
+
<td>{similarity:.6f}</td>
|
44 |
+
</tr>
|
45 |
+
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
46 |
|
47 |
+
# Create HTML tables
|
48 |
+
detections_table = f"""
|
49 |
+
<h3>Face Detection</h3>
|
50 |
+
<table border="1" style="border-collapse: collapse; width: 100%;">
|
51 |
+
<tr>
|
52 |
+
<th>First Face Index</th>
|
53 |
+
<th>Second Face Index</th>
|
54 |
+
<th>Face Image</th>
|
55 |
+
</tr>
|
56 |
+
{detection_rows}
|
57 |
+
</table>
|
58 |
+
"""
|
59 |
+
|
60 |
+
matches_table = f"""
|
61 |
+
<h3>Matching Results</h3>
|
62 |
+
<table border="1" style="border-collapse: collapse; width: 100%;">
|
63 |
+
<tr>
|
64 |
+
<th>First Face Index</th>
|
65 |
+
<th>Second Face Index</th>
|
66 |
+
<th>Similarity</th>
|
67 |
+
</tr>
|
68 |
+
{match_rows}
|
69 |
+
</table>
|
70 |
+
"""
|
71 |
+
|
72 |
+
return detections_table + matches_table
|
73 |
|
74 |
def check_liveness(frame):
|
75 |
url = "https://faceapi.miniai.live/face_liveness_check"
|
|
|
246 |
inputs=im_match_in2
|
247 |
)
|
248 |
with gr.Column():
|
|
|
249 |
txt_compare_out = gr.HTML()
|
250 |
btn_f_match = gr.Button("Check Comparing!", variant='primary')
|
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):
|