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 = ("" "{key}" "{value}" "{mrz_value}" "".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('<', '<') value = value.replace(',', '

') row_value = ("" "{key}" "{value}" "{mrz_value}" "".format(key=key, value='', mrz_value=value)) table_value = table_value + row_value html = ("" "" "" "" "" "" "{table_value}" "
FieldValueMRZ
".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 = ("" "{key}" "" "".format(key=details_key, base64_image=image_value)) table_value = table_value + row_value images = ("" "" "" "" "" "{table_value}" "
FieldImage
".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 = ("" "{key}" "{value}" "".format(key=key, value=value)) table_value = table_value + row_value html = ("" "" "" "" "" "{table_value}" "
FieldValue
".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 = ("" "{key}" "" "".format(key=details_key, base64_image=image_value)) table_value = table_value + row_value images = ("" "" "" "" "" "{table_value}" "
FieldImage
".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 = ("" "{key}" "{mrz_value}" "".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('<', '<') value = value.replace(',', '

') row_value = ("" "{key}" "{mrz_value}" "".format(key=key, mrz_value=value)) table_value = table_value + row_value html = ("" "" "" "" "" "{table_value}" "
FieldMRZ
".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 = ("" "{key}" "" "".format(key=details_key, base64_image=image_value)) table_value = table_value + row_value images = ("" "" "" "" "" "{table_value}" "
FieldImage
".format(table_value=table_value)) return [html, images] # APP Interface with gr.Blocks() as MiniAIdemo: gr.Markdown( """

ID Document Recognition WebAPI Demo



""" ) 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('') if __name__ == "__main__": MiniAIdemo.launch()