Spaces:
Runtime error
Runtime error
Update
Browse files- gradio/demo.py +132 -0
- gradio/examples/1.jpg +0 -0
- gradio/examples/2.jpg +0 -0
- gradio/examples/3.jpg +0 -0
- gradio/examples/4.jpg +0 -0
gradio/demo.py
ADDED
@@ -0,0 +1,132 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import requests
|
3 |
+
import json
|
4 |
+
|
5 |
+
def compare_face(frame1, frame2):
|
6 |
+
url = "http://127.0.0.1:8000/api/compare_face"
|
7 |
+
files = {'file1': open(frame1, 'rb'), 'file2': open(frame2, 'rb')}
|
8 |
+
|
9 |
+
r = requests.post(url=url, files=files)
|
10 |
+
|
11 |
+
html = None
|
12 |
+
faces = None
|
13 |
+
|
14 |
+
compare_result = r.json().get('compare_result')
|
15 |
+
compare_similarity = r.json().get('compare_similarity')
|
16 |
+
|
17 |
+
html = ("<table>"
|
18 |
+
"<tr>"
|
19 |
+
"<th>Face State</th>"
|
20 |
+
"<th>Value</th>"
|
21 |
+
"</tr>"
|
22 |
+
"<tr>"
|
23 |
+
"<td>Result</td>"
|
24 |
+
"<td>{compare_result}</td>"
|
25 |
+
"</tr>"
|
26 |
+
"<tr>"
|
27 |
+
"<td>Similarity</td>"
|
28 |
+
"<td>{compare_similarity}</td>"
|
29 |
+
"</tr>"
|
30 |
+
"</table>".format(compare_result=compare_result, compare_similarity=compare_similarity))
|
31 |
+
|
32 |
+
try:
|
33 |
+
image1 = Image.open(frame1)
|
34 |
+
image2 = Image.open(frame2)
|
35 |
+
|
36 |
+
face1 = None
|
37 |
+
face2 = None
|
38 |
+
|
39 |
+
if r.json().get('face1') is not None:
|
40 |
+
face = r.json().get('face1')
|
41 |
+
x1 = face.get('x1')
|
42 |
+
y1 = face.get('y1')
|
43 |
+
x2 = face.get('x2')
|
44 |
+
y2 = face.get('y2')
|
45 |
+
|
46 |
+
if x1 < 0:
|
47 |
+
x1 = 0
|
48 |
+
if y1 < 0:
|
49 |
+
y1 = 0
|
50 |
+
if x2 >= image1.width:
|
51 |
+
x2 = image1.width - 1
|
52 |
+
if y2 >= image1.height:
|
53 |
+
y2 = image1.height - 1
|
54 |
+
|
55 |
+
face1 = image1.crop((x1, y1, x2, y2))
|
56 |
+
face_image_ratio = face1.width / float(face1.height)
|
57 |
+
resized_w = int(face_image_ratio * 150)
|
58 |
+
resized_h = 150
|
59 |
+
|
60 |
+
face1 = face1.resize((int(resized_w), int(resized_h)))
|
61 |
+
|
62 |
+
if r.json().get('face2') is not None:
|
63 |
+
face = r.json().get('face2')
|
64 |
+
x1 = face.get('x1')
|
65 |
+
y1 = face.get('y1')
|
66 |
+
x2 = face.get('x2')
|
67 |
+
y2 = face.get('y2')
|
68 |
+
|
69 |
+
if x1 < 0:
|
70 |
+
x1 = 0
|
71 |
+
if y1 < 0:
|
72 |
+
y1 = 0
|
73 |
+
if x2 >= image2.width:
|
74 |
+
x2 = image2.width - 1
|
75 |
+
if y2 >= image2.height:
|
76 |
+
y2 = image2.height - 1
|
77 |
+
|
78 |
+
face2 = image2.crop((x1, y1, x2, y2))
|
79 |
+
face_image_ratio = face2.width / float(face2.height)
|
80 |
+
resized_w = int(face_image_ratio * 150)
|
81 |
+
resized_h = 150
|
82 |
+
|
83 |
+
face2 = face2.resize((int(resized_w), int(resized_h)))
|
84 |
+
|
85 |
+
if face1 is not None and face2 is not None:
|
86 |
+
new_image = Image.new('RGB',(face1.width + face2.width + 10, 150), (80,80,80))
|
87 |
+
|
88 |
+
new_image.paste(face1,(0,0))
|
89 |
+
new_image.paste(face2,(face1.width + 10, 0))
|
90 |
+
faces = new_image.copy()
|
91 |
+
elif face1 is not None and face2 is None:
|
92 |
+
new_image = Image.new('RGB',(face1.width + face1.width + 10, 150), (80,80,80))
|
93 |
+
|
94 |
+
new_image.paste(face1,(0,0))
|
95 |
+
faces = new_image.copy()
|
96 |
+
elif face1 is None and face2 is not None:
|
97 |
+
new_image = Image.new('RGB',(face2.width + face2.width + 10, 150), (80,80,80))
|
98 |
+
|
99 |
+
new_image.paste(face2,(face2.width + 10, 0))
|
100 |
+
faces = new_image.copy()
|
101 |
+
|
102 |
+
except:
|
103 |
+
pass
|
104 |
+
|
105 |
+
return [faces, r.json()]
|
106 |
+
|
107 |
+
with gr.Blocks() as demo:
|
108 |
+
gr.Markdown(
|
109 |
+
"""
|
110 |
+
# Face Liveness Detection
|
111 |
+
Get your own Face Liveness Detection Server by duplicating this space.<br/>
|
112 |
+
Contact us at [email protected] for issues and support.<br/>
|
113 |
+
"""
|
114 |
+
)
|
115 |
+
with gr.Row():
|
116 |
+
with gr.Column():
|
117 |
+
compare_face_input1 = gr.Image(type='filepath')
|
118 |
+
gr.Examples(['examples/1.jpg', 'examples/3.jpg'],
|
119 |
+
inputs=compare_face_input1)
|
120 |
+
|
121 |
+
compare_face_input2 = gr.Image(type='filepath')
|
122 |
+
gr.Examples(['examples/2.jpg', 'examples/4.jpg'],
|
123 |
+
inputs=compare_face_input2)
|
124 |
+
|
125 |
+
compare_face_button = gr.Button("Compare Face")
|
126 |
+
with gr.Column():
|
127 |
+
compare_face_output = gr.Image(type="pil").style(height=150)
|
128 |
+
compare_result_output = gr.JSON(label='Result')
|
129 |
+
|
130 |
+
compare_face_button.click(compare_face, inputs=[compare_face_input1, compare_face_input2], outputs=[compare_face_output, compare_result_output])
|
131 |
+
|
132 |
+
demo.launch(server_name="0.0.0.0", server_port=7860)
|
gradio/examples/1.jpg
ADDED
![]() |
gradio/examples/2.jpg
ADDED
![]() |
gradio/examples/3.jpg
ADDED
![]() |
gradio/examples/4.jpg
ADDED
![]() |