dc086989's picture
Update app.py
cd03f3c verified
import gradio as gr
import requests
from PIL import Image
def check_id(frame):
url = "https://api.miniai.live/api/check_id"
files = {'image': open(frame, 'rb')}
r = requests.post(url=url, files=files)
html = None
images = None
mrz = None
status = r.json().get('Status')
table_value = ""
if r.json().get('MRZ') is not None:
mrz = r.json().get('MRZ')
for key, value in r.json().items():
if key == 'Status' or key == 'Images' or key == 'MRZ' or key == 'Position':
continue
mrz_value = ''
if mrz is not None and mrz.get(key) is not None:
mrz_value = mrz[key]
del mrz[key]
row_value = ("<tr>"
"<td>{key}</td>"
"<td>{value}</td>"
"<td>{mrz_value}</td>"
"</tr>".format(key=key, value=value, mrz_value=mrz_value))
table_value = table_value + row_value
if mrz is not None:
for key, value in mrz.items():
if key == 'MRZ':
value = value.replace('<', '&lt;')
value = value.replace(',', '<p>')
row_value = ("<tr>"
"<td>{key}</td>"
"<td>{value}</td>"
"<td>{mrz_value}</td>"
"</tr>".format(key=key, value='', mrz_value=value))
table_value = table_value + row_value
html = ("<table>"
"<tr>"
"<th style=""width:20%"">Field</th>"
"<th style=""width:40%"">Value</th>"
"<th style=""width:40%"">MRZ</th>"
"</tr>"
"{table_value}"
"</table>".format(table_value=table_value))
table_value = ""
for key, value in r.json().items():
if key == 'Images':
for details_key, details_value in value.items():
for image_key, image_value in details_value.items():
if image_key == 'image':
row_value = ("<tr>"
"<td>{key}</td>"
"<td><img src=""data:image/png;base64,{base64_image} width = '200' height= '100' /></td>"
"</tr>".format(key=details_key, base64_image=image_value))
table_value = table_value + row_value
images = ("<table>"
"<tr>"
"<th>Field</th>"
"<th>Image</th>"
"</tr>"
"{table_value}"
"</table>".format(table_value=table_value))
return [html, images]
def check_bank(frame):
url = "https://api.miniai.live/api/check_credit"
files = {'image': open(frame, 'rb')}
r = requests.post(url=url, files=files)
html = None
images = None
status = r.json().get('Status')
table_value = ""
for key, value in r.json().items():
if key == 'Status' or key == 'Images' or key == 'Position':
continue
row_value = ("<tr>"
"<td>{key}</td>"
"<td>{value}</td>"
"</tr>".format(key=key, value=value))
table_value = table_value + row_value
html = ("<table>"
"<tr>"
"<th style=""width:20%"">Field</th>"
"<th style=""width:40%"">Value</th>"
"</tr>"
"{table_value}"
"</table>".format(table_value=table_value))
table_value = ""
for key, value in r.json().items():
if key == 'Images':
for details_key, details_value in value.items():
for image_key, image_value in details_value.items():
if image_key == 'image':
row_value = ("<tr>"
"<td>{key}</td>"
"<td><img src=""data:image/png;base64,{base64_image} width = '200' height= '100' /></td>"
"</tr>".format(key=details_key, base64_image=image_value))
table_value = table_value + row_value
images = ("<table>"
"<tr>"
"<th>Field</th>"
"<th>Image</th>"
"</tr>"
"{table_value}"
"</table>".format(table_value=table_value))
return [html, images]
def check_mrz(frame):
url = "https://api.miniai.live/api/check_mrz"
files = {'image': open(frame, 'rb')}
r = requests.post(url=url, files=files)
html = None
images = None
mrz = None
status = r.json().get('Status')
table_value = ""
if r.json().get('MRZ') is not None:
mrz = r.json().get('MRZ')
for key, value in r.json().items():
if key == 'Status' or key == 'Images' or key == 'MRZ' or key == 'Position':
continue
mrz_value = ''
if mrz is not None and mrz.get(key) is not None:
mrz_value = mrz[key]
del mrz[key]
row_value = ("<tr>"
"<td>{key}</td>"
"<td>{mrz_value}</td>"
"</tr>".format(key=key, mrz_value=mrz_value))
table_value = table_value + row_value
if mrz is not None:
for key, value in mrz.items():
if key == 'MRZ':
value = value.replace('<', '&lt;')
value = value.replace(',', '<p>')
row_value = ("<tr>"
"<td>{key}</td>"
"<td>{mrz_value}</td>"
"</tr>".format(key=key, mrz_value=value))
table_value = table_value + row_value
html = ("<table>"
"<tr>"
"<th style=""width:20%"">Field</th>"
"<th style=""width:40%"">MRZ</th>"
"</tr>"
"{table_value}"
"</table>".format(table_value=table_value))
table_value = ""
for key, value in r.json().items():
if key == 'Images':
for details_key, details_value in value.items():
for image_key, image_value in details_value.items():
if image_key == 'image':
row_value = ("<tr>"
"<td>{key}</td>"
"<td><img src=""data:image/png;base64,{base64_image} width = '200' height= '100' /></td>"
"</tr>".format(key=details_key, base64_image=image_value))
table_value = table_value + row_value
images = ("<table>"
"<tr>"
"<th>Field</th>"
"<th>Image</th>"
"</tr>"
"{table_value}"
"</table>".format(table_value=table_value))
return [html, images]
# APP Interface
with gr.Blocks() as MiniAIdemo:
gr.Markdown(
"""
<a href="https://miniai.live" style="display: flex; align-items: center;">
<img src="https://miniai.live/wp-content/uploads/2024/02/logo_name-1-768x426-1.png" style="width: 18%; margin-right: 15px;"/>
<div>
<p style="font-size: 50px; font-weight: bold; margin-right: 20px;">ID Document Recognition WebAPI Demo</p>
</div>
</a>
<br/>
<div style="display: flex; justify-content: center; align-items: center;">
<table style="text-align: center;">
<tr>
<td style="text-align: center; vertical-align: middle;"><a href="https://github.com/MiniAiLive"><img src="https://miniai.live/wp-content/uploads/2024/10/new_git-1-300x67.png" style="height: 50px; margin-right: 5px;" title="GITHUB"/></a></td>
<td style="text-align: center; vertical-align: middle;"><a href="https://huggingface.co/MiniAiLive"><img src="https://miniai.live/wp-content/uploads/2024/10/new_hugging-1-300x67.png" style="height: 50px; margin-right: 5px;" title="HuggingFace"/></a></td>
<td style="text-align: center; vertical-align: middle;"><a href="https://demo.miniai.live"><img src="https://miniai.live/wp-content/uploads/2024/10/new_gradio-300x67.png" style="height: 50px; margin-right: 5px;" title="Gradio"/></a></td>
</tr>
<tr>
<td style="text-align: center; vertical-align: middle;"><a href="https://docs.miniai.live/"><img src="https://miniai.live/wp-content/uploads/2024/10/a-300x70.png" style="height: 50px; margin-right: 5px;" title="Documentation"/></a></td>
<td style="text-align: center; vertical-align: middle;"><a href="https://www.youtube.com/@miniailive"><img src="https://miniai.live/wp-content/uploads/2024/10/Untitled-1-300x70.png" style="height: 50px; margin-right: 5px;" title="Youtube"/></a></td>
<td style="text-align: center; vertical-align: middle;"><a href="https://play.google.com/store/apps/dev?id=5831076207730531667"><img src="https://miniai.live/wp-content/uploads/2024/10/googleplay-300x62.png" style="height: 50px; margin-right: 5px;" title="Google Play"/></a></td>
</tr>
</table>
</div>
<br/>
"""
)
with gr.Tabs():
with gr.Tab("ID Document Recognition"):
with gr.Row():
with gr.Column(scale=3):
im_id_in = gr.Image(type='filepath', height=300)
gr.Examples(
[
"images/id/demo1.jpg",
"images/id/demo2.png",
"images/id/demo3.png",
],
inputs=im_id_in
)
btn_f_id = gr.Button("Check Document Details", variant='primary')
with gr.Column(scale=5):
table_id_out = gr.HTML()
with gr.Column(scale=2):
im_id_out = gr.HTML()
btn_f_id.click(check_id, inputs=im_id_in, outputs=[table_id_out, im_id_out])
with gr.Tab("Bank/Credit Recognition"):
with gr.Row():
with gr.Column(scale=3):
im_bank_in = gr.Image(type='filepath', height=300)
gr.Examples(
[
"images/bank/demo1.jpg",
"images/bank/demo2.png",
"images/bank/demo3.png",
],
inputs=im_bank_in
)
btn_f_bank = gr.Button("Check Document Details", variant='primary')
with gr.Column(scale=5):
table_bank_out = gr.HTML()
with gr.Column(scale=2):
im_bank_out = gr.HTML()
btn_f_bank.click(check_bank, inputs=im_bank_in, outputs=[table_bank_out, im_bank_out])
with gr.Tab("MRZ/Barcode Recognition"):
with gr.Row():
with gr.Column(scale=3):
im_mrz_in = gr.Image(type='filepath', height=300)
gr.Examples(
[
"images/mrz_barcode/demo1.png",
"images/mrz_barcode/demo2.png",
],
inputs=im_mrz_in
)
btn_f_mrz = gr.Button("Check Document Details", variant='primary')
with gr.Column(scale=5):
table_mrz_out = gr.HTML()
with gr.Column(scale=2):
im_mrz_out = gr.HTML()
btn_f_mrz.click(check_mrz, inputs=im_mrz_in, outputs=[table_mrz_out, im_mrz_out])
gr.HTML('<a href="https://visitorbadge.io/status?path=https%3A%2F%2Fhuggingface.co%2Fspaces%2FMiniAiLive%2FID-Document-Recognition-Demo"><img src="https://api.visitorbadge.io/api/combined?path=https%3A%2F%2Fhuggingface.co%2Fspaces%2FMiniAiLive%2FID-Document-Recognition-Demo&label=VISITORS&labelColor=%2337d67a&countColor=%23ff8a65&style=plastic&labelStyle=none" /></a>')
if __name__ == "__main__":
MiniAIdemo.launch()