Update app.py
Browse files
app.py
CHANGED
@@ -34,8 +34,8 @@ css = """
|
|
34 |
|
35 |
.markdown-fail-container {
|
36 |
background-color: #FFF1F0;
|
37 |
-
padding:
|
38 |
-
margin:
|
39 |
border-radius: 1px;
|
40 |
border: 2px solid red;
|
41 |
text-align: center;
|
@@ -82,8 +82,8 @@ def json_to_html_table(data, image_keys):
|
|
82 |
|
83 |
def check_liveness(frame):
|
84 |
if frame is None:
|
85 |
-
liveness_result = f"""<div class="markdown-fail-container"><p style="text-align: center; font-size: 20px; color: red;">Liveness Check Failed
|
86 |
-
return liveness_result
|
87 |
|
88 |
img_bytes = io.BytesIO()
|
89 |
Image.open(frame).save(img_bytes, format="JPEG")
|
@@ -97,14 +97,14 @@ def check_liveness(frame):
|
|
97 |
|
98 |
result = requests.post(url=url, files=files, headers=headers)
|
99 |
except:
|
100 |
-
liveness_result = f"""<div class="markdown-fail-container"><p style="text-align: center; font-size: 20px; color: red;">Liveness Check Failed
|
101 |
-
return liveness_result
|
102 |
|
103 |
if result.ok:
|
104 |
json_result = result.json()
|
105 |
if json_result.get("resultCode") == "Error":
|
106 |
-
liveness_result = f"""<div class="markdown-fail-container"><p style="text-align: center; font-size: 20px; color: red;">Liveness Check Failed
|
107 |
-
return liveness_result
|
108 |
|
109 |
process_results = json_result.get("result")
|
110 |
status = process_results.get("status")
|
@@ -118,14 +118,15 @@ def check_liveness(frame):
|
|
118 |
if screenReply < screenReplayThreshold or portraitReplace < portraitReplaceThreshold or printedCopy < printedCopyThreshold:
|
119 |
liveness_result = f"""<div class="markdown-fail-container"><p style="text-align: center; font-size: 20px; color: red;">Liveness Check: SPOOF</p></div>"""
|
120 |
|
|
|
121 |
# Update json_result with the modified process_results
|
122 |
-
return liveness_result
|
123 |
|
124 |
-
liveness_result = f"""<div class="markdown-fail-container"><p style="text-align: center; font-size: 20px; color: red;">Liveness Check Failed
|
125 |
-
return liveness_result
|
126 |
else:
|
127 |
-
liveness_result = f"""<div class="markdown-fail-container"><p style="text-align: center; font-size: 20px; color: red;">Liveness Check Failed
|
128 |
-
return liveness_result
|
129 |
|
130 |
def idcard_recognition(frame1, frame2):
|
131 |
url = "https://recognito-iddocumentrecognition.p.rapidapi.com/api/read_idcard"
|
@@ -138,9 +139,8 @@ def idcard_recognition(frame1, frame2):
|
|
138 |
elif frame1 is None and frame2 is not None:
|
139 |
files = {'image': open(frame2, 'rb')}
|
140 |
else:
|
141 |
-
return ['', None, None
|
142 |
|
143 |
-
liveness_result = check_liveness(frame1)
|
144 |
headers = {"X-RapidAPI-Key": os.environ.get("API_KEY")}
|
145 |
|
146 |
r = requests.post(url=url, files=files, headers=headers)
|
@@ -194,7 +194,7 @@ def idcard_recognition(frame1, frame2):
|
|
194 |
|
195 |
result = json_to_html_table(result_table_dict, {'portrait'})
|
196 |
json_result = json.dumps(rawValues, indent=6)
|
197 |
-
return [result, json_result, images
|
198 |
|
199 |
def launch_demo():
|
200 |
with gr.Blocks(css=css) as demo:
|
@@ -231,38 +231,54 @@ def launch_demo():
|
|
231 |
"""
|
232 |
)
|
233 |
|
234 |
-
with gr.
|
235 |
-
with gr.
|
236 |
-
with gr.Row():
|
237 |
-
with gr.Column(scale=3):
|
238 |
-
id_image_input1 = gr.Image(type='filepath', label='Front', elem_classes="example-image")
|
239 |
-
with gr.Column(scale=3):
|
240 |
-
id_image_input2 = gr.Image(type='filepath', label='Back', elem_classes="example-image")
|
241 |
-
|
242 |
with gr.Row():
|
243 |
-
|
244 |
-
|
245 |
-
|
246 |
-
|
247 |
-
|
248 |
-
|
249 |
-
|
250 |
-
|
251 |
-
|
252 |
-
|
253 |
-
|
254 |
-
|
255 |
-
|
256 |
-
|
257 |
-
|
258 |
-
|
259 |
-
|
260 |
-
|
261 |
-
with gr.
|
262 |
-
|
|
|
263 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
264 |
|
265 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
266 |
|
267 |
gr.HTML('<a href="https://visitorbadge.io/status?path=https%3A%2F%2Fhuggingface.co%2Fspaces%2Frecognito%2FID-Document-Verification"><img src="https://api.visitorbadge.io/api/combined?path=https%3A%2F%2Fhuggingface.co%2Fspaces%2Frecognito%2FID-Document-Verification&labelColor=%2337d67a&countColor=%23263759&style=flat" /></a>')
|
268 |
|
@@ -270,3 +286,4 @@ def launch_demo():
|
|
270 |
|
271 |
if __name__ == '__main__':
|
272 |
launch_demo()
|
|
|
|
34 |
|
35 |
.markdown-fail-container {
|
36 |
background-color: #FFF1F0;
|
37 |
+
padding: 20px;
|
38 |
+
margin: 20px;
|
39 |
border-radius: 1px;
|
40 |
border: 2px solid red;
|
41 |
text-align: center;
|
|
|
82 |
|
83 |
def check_liveness(frame):
|
84 |
if frame is None:
|
85 |
+
liveness_result = f"""<div class="markdown-fail-container"><p style="text-align: center; font-size: 20px; color: red;">Liveness Check Failed</p></div>"""
|
86 |
+
return [liveness_result, {"status": "error", "result": "select image file!"}]
|
87 |
|
88 |
img_bytes = io.BytesIO()
|
89 |
Image.open(frame).save(img_bytes, format="JPEG")
|
|
|
97 |
|
98 |
result = requests.post(url=url, files=files, headers=headers)
|
99 |
except:
|
100 |
+
liveness_result = f"""<div class="markdown-fail-container"><p style="text-align: center; font-size: 20px; color: red;">Liveness Check Failed</p></div>"""
|
101 |
+
return [liveness_result, {"status": "error", "result": "failed to open file!"}]
|
102 |
|
103 |
if result.ok:
|
104 |
json_result = result.json()
|
105 |
if json_result.get("resultCode") == "Error":
|
106 |
+
liveness_result = f"""<div class="markdown-fail-container"><p style="text-align: center; font-size: 20px; color: red;">Liveness Check Failed</p></div>"""
|
107 |
+
return [liveness_result, {"status": "error", "result": "server error!"}]
|
108 |
|
109 |
process_results = json_result.get("result")
|
110 |
status = process_results.get("status")
|
|
|
118 |
if screenReply < screenReplayThreshold or portraitReplace < portraitReplaceThreshold or printedCopy < printedCopyThreshold:
|
119 |
liveness_result = f"""<div class="markdown-fail-container"><p style="text-align: center; font-size: 20px; color: red;">Liveness Check: SPOOF</p></div>"""
|
120 |
|
121 |
+
json_output = {"status": "ok", "data": {"screenreplay_integrity_score": screenReply, "portraitreplace_integrity_score": portraitReplace, "printedcutout_integrity_score": printedCopy}}
|
122 |
# Update json_result with the modified process_results
|
123 |
+
return [liveness_result, json_output]
|
124 |
|
125 |
+
liveness_result = f"""<div class="markdown-fail-container"><p style="text-align: center; font-size: 20px; color: red;">Liveness Check Failed</p></div>"""
|
126 |
+
return [liveness_result, {"status": "error", "result": "document not found!"}]
|
127 |
else:
|
128 |
+
liveness_result = f"""<div class="markdown-fail-container"><p style="text-align: center; font-size: 20px; color: red;">Liveness Check Failed</p></div>"""
|
129 |
+
return [liveness_result, {"status": "error", "result": f"{result.text}"}]
|
130 |
|
131 |
def idcard_recognition(frame1, frame2):
|
132 |
url = "https://recognito-iddocumentrecognition.p.rapidapi.com/api/read_idcard"
|
|
|
139 |
elif frame1 is None and frame2 is not None:
|
140 |
files = {'image': open(frame2, 'rb')}
|
141 |
else:
|
142 |
+
return ['', None, None]
|
143 |
|
|
|
144 |
headers = {"X-RapidAPI-Key": os.environ.get("API_KEY")}
|
145 |
|
146 |
r = requests.post(url=url, files=files, headers=headers)
|
|
|
194 |
|
195 |
result = json_to_html_table(result_table_dict, {'portrait'})
|
196 |
json_result = json.dumps(rawValues, indent=6)
|
197 |
+
return [result, json_result, images]
|
198 |
|
199 |
def launch_demo():
|
200 |
with gr.Blocks(css=css) as demo:
|
|
|
231 |
"""
|
232 |
)
|
233 |
|
234 |
+
with gr.Tabs():
|
235 |
+
with gr.Tab("ID Document Recognition"):
|
|
|
|
|
|
|
|
|
|
|
|
|
236 |
with gr.Row():
|
237 |
+
with gr.Column(scale=6):
|
238 |
+
with gr.Row():
|
239 |
+
with gr.Column(scale=3):
|
240 |
+
id_image_input1 = gr.Image(type='filepath', label='Front', elem_classes="example-image")
|
241 |
+
with gr.Column(scale=3):
|
242 |
+
id_image_input2 = gr.Image(type='filepath', label='Back', elem_classes="example-image")
|
243 |
+
|
244 |
+
with gr.Row():
|
245 |
+
id_examples = gr.Examples(
|
246 |
+
examples=[['examples/1_f.png', 'examples/1_b.png'],
|
247 |
+
['examples/2_f.png', 'examples/2_b.png'],
|
248 |
+
['examples/3_f.png', 'examples/3_b.png'],
|
249 |
+
['examples/4.png', None]],
|
250 |
+
inputs=[id_image_input1, id_image_input2],
|
251 |
+
outputs=None,
|
252 |
+
fn=idcard_recognition
|
253 |
+
)
|
254 |
+
|
255 |
+
with gr.Blocks():
|
256 |
+
with gr.Column(scale=4, min_width=400, elem_classes="block-background"):
|
257 |
+
id_recognition_button = gr.Button("ID Card Recognition", variant="primary", size="lg")
|
258 |
|
259 |
+
with gr.Tab("Key Fields"):
|
260 |
+
id_result_output = gr.HTML()
|
261 |
+
with gr.Tab("Raw JSON"):
|
262 |
+
json_result_output = gr.JSON()
|
263 |
+
with gr.Tab("Images"):
|
264 |
+
image_result_output = gr.HTML()
|
265 |
+
|
266 |
+
id_recognition_button.click(idcard_recognition, inputs=[id_image_input1, id_image_input2], outputs=[id_result_output, json_result_output, image_result_output])
|
267 |
|
268 |
+
with gr.Tab("ID Document Liveness Detection"):
|
269 |
+
with gr.Row():
|
270 |
+
with gr.Column(scale=1):
|
271 |
+
id_image_input = gr.Image(label="Image", type='filepath', elem_classes="example-image")
|
272 |
+
gr.Examples(examples=['examples/1_f.png', 'examples/2_f.png', 'examples/3_f.png', 'examples/4.png'], inputs=id_image_input)
|
273 |
+
|
274 |
+
with gr.Blocks():
|
275 |
+
with gr.Column(scale=1, elem_classes="block-background"):
|
276 |
+
check_liveness_button = gr.Button("Check Document Liveness", variant="primary", size="lg")
|
277 |
+
|
278 |
+
liveness_result = gr.Markdown("")
|
279 |
+
json_output = gr.JSON()
|
280 |
+
|
281 |
+
check_liveness_button.click(check_liveness, inputs=id_image_input, outputs=[liveness_result, json_output])
|
282 |
|
283 |
gr.HTML('<a href="https://visitorbadge.io/status?path=https%3A%2F%2Fhuggingface.co%2Fspaces%2Frecognito%2FID-Document-Verification"><img src="https://api.visitorbadge.io/api/combined?path=https%3A%2F%2Fhuggingface.co%2Fspaces%2Frecognito%2FID-Document-Verification&labelColor=%2337d67a&countColor=%23263759&style=flat" /></a>')
|
284 |
|
|
|
286 |
|
287 |
if __name__ == '__main__':
|
288 |
launch_demo()
|
289 |
+
|