| from fastapi import FastAPI , File , UploadFile , Form |
| from datetime import datetime |
| from pydantic import BaseModel |
| import mrzscanner |
| import PIL |
| import json |
| import uvicorn |
| from mrz.checker.td1 import TD1CodeChecker |
| from mrz.checker.td2 import TD2CodeChecker |
| from mrz.checker.td3 import TD3CodeChecker |
| from mrz.checker.mrva import MRVACodeChecker |
| from mrz.checker.mrvb import MRVBCodeChecker |
| import cv2 |
| import uuid |
| import numpy as np |
| import gradio as gr |
|
|
| CUSTOM_PATH = "/gradio" |
|
|
| app = FastAPI() |
|
|
| def check(lines): |
| try: |
| td1_check = TD1CodeChecker(lines) |
| if bool(td1_check): |
| return "TD1", td1_check.fields() |
| except Exception as err: |
| pass |
| |
| try: |
| td2_check = TD2CodeChecker(lines) |
| if bool(td2_check): |
| return "TD2", td2_check.fields() |
| except Exception as err: |
| pass |
| |
| try: |
| td3_check = TD3CodeChecker(lines) |
|
|
| if bool(td3_check): |
| return "TD3", td3_check.fields() |
| except Exception as err: |
| pass |
| |
| try: |
| mrva_check = MRVACodeChecker(lines) |
| if bool(mrva_check): |
| return "MRVA", mrva_check.fields() |
| except Exception as err: |
| pass |
| |
| try: |
| mrvb_check = MRVBCodeChecker(lines) |
| if bool(mrvb_check): |
| return "MRVB", mrvb_check.fields() |
| except Exception as err: |
| pass |
| |
| return 'No valid MRZ information found' |
|
|
|
|
|
|
|
|
|
|
| import cv2 |
|
|
| def get_info(scanner,img,file_type): |
|
|
| s="" |
| if file_type=="image": |
| image_np=np.frombuffer(img,dtype=np.uint8) |
| img_np=cv2.imdecode(image_np,cv2.IMREAD_COLOR) |
| elif file_type=="pdf": |
| img_np1 = np.array(img) |
| |
| is_success, im_buf_arr = cv2.imencode(".jpg", img_np1) |
| byte_im = im_buf_arr.tobytes() |
| image_np=np.frombuffer(byte_im,dtype=np.uint8) |
| img_np=cv2.imdecode(image_np,cv2.IMREAD_COLOR) |
|
|
|
|
| |
| |
| else: |
| img_np = np.array(img) |
| |
|
|
|
|
|
|
| results = scanner.decodeMat(img_np) |
|
|
| lst1=[] |
| lst=[] |
| for result in results: |
| s += result.text + '\n' |
| lst1.append(result.text+ '\n') |
|
|
| str1="" |
| str1=str1.join(lst1[-2:]) |
|
|
| if check(str1[:-1]) == "No valid MRZ information found": |
| try: |
| ed=(TD3CodeChecker(str1[:-1],check_expiry=False,compute_warnings=True)) |
| lst.append(ed.fields()) |
| return lst |
| except: |
| return [] |
| else: |
| lst.append(check(str1[:-1])) |
| return lst |
|
|
|
|
|
|
| def extract_result(lst): |
| x=0 |
| final_lst=[] |
| if type(lst[x])==tuple: |
| temp_lst=[] |
| surname=lst[x][1][0] |
| name=lst[x][1][1] |
| country=lst[x][1][2] |
| nationality=lst[x][1][3] |
|
|
| try: |
| birth_date=datetime.strptime(lst[x][1][4], '%y%m%d').strftime('%d/%m/%Y') |
| expiry_date=datetime.strptime(lst[x][1][5], '%y%m%d').strftime('%d/%m/%Y') |
| year=datetime.strptime(lst[x][1][5], '%y%m%d').year |
| month=datetime.strptime(lst[x][1][5], '%y%m%d').month |
| day=datetime.strptime(lst[x][1][5], '%y%m%d').day |
| if nationality=='TWN': |
| year-=5 |
| else: |
| year-=10 |
| |
| if nationality=='IND' or nationality=='CHN' or nationality=='BRA': |
| |
| if str(day)=="31": |
| day=1 |
| month+=1 |
| else: |
| day+=1 |
|
|
| str_day=day |
| str_month=month |
| if len(str(day))==1: |
| str_day="0"+str(day) |
| |
| if len(str(month))==1: |
| str_month="0"+str(month) |
| |
| |
| issue_date=str(str_day)+"/"+str(str_month)+"/"+str(year) |
| except: |
| |
| birth_date=lst[x][1][4] |
| expiry_date=lst[x][1][5] |
| |
|
|
| |
| issue_date=expiry_date |
| |
| document_type=lst[x][1][7] |
| document_number=lst[x][1][8] |
| temp_lst.append([surname,name,country,birth_date,expiry_date,document_number,issue_date]) |
| |
| |
| final_lst.append(temp_lst) |
|
|
| else: |
| temp_lst=[] |
| surname=lst[x][0] |
| name=lst[x][1] |
| country=lst[x][2] |
| nationality=lst[x][3] |
| if country=="IMD": |
| country="IND" |
|
|
| try: |
|
|
| birth_date=datetime.strptime(lst[x][4], '%y%m%d').strftime('%d/%m/%Y') |
| expiry_date=datetime.strptime(lst[x][5], '%y%m%d').strftime('%d/%m/%Y') |
| year=datetime.strptime(lst[x][5], '%y%m%d').year |
| month=datetime.strptime(lst[x][5], '%y%m%d').month |
| day=datetime.strptime(lst[x][5], '%y%m%d').day |
|
|
| if nationality=='TWN': |
| year-=5 |
| else: |
| year-=10 |
|
|
|
|
| if country=='IND' or nationality=='CHN' or nationality=='BRA' or nationality=='IND': |
| |
| if str(day)=="31": |
| day=1 |
| month+=1 |
| else: |
| day+=1 |
| str_day=day |
| str_month=month |
|
|
|
|
| if len(str(day))==1: |
| str_day="0"+str(day) |
| |
| if len(str(month))==1: |
| str_month="0"+str(month) |
| |
| |
| issue_date=str(str_day)+"/"+str(str_month)+"/"+str(year) |
|
|
| |
| |
| except: |
| birth_date=lst[x][4] |
| expiry_date=lst[x][5] |
| issue_date=expiry_date |
|
|
| |
| document_type=lst[x][7] |
| document_number=lst[x][8] |
|
|
| if document_type=="P" or document_type=="p": |
| temp_lst.append([surname,name,country,birth_date,expiry_date,document_number,issue_date]) |
| else: |
| print(lst) |
| temp_lst.append("The attached document is not a Passport") |
| |
| |
| final_lst.append(temp_lst) |
| |
|
|
|
|
|
|
| return temp_lst |
|
|
|
|
| def return_df(final_lst): |
|
|
| if final_lst[0]=="The attached document is not a Passport": |
| return "The attached document is not a Passport" |
| else: |
| columns = ['family_name','given_name','country','date_of_birth','expiration','passport_number','issue_date'] |
| |
| |
| res = {columns[i]: final_lst[0][i] for i in range(len(columns))} |
| json_en=json.dumps(res) |
| |
| return json.loads(json_en) |
|
|
|
|
|
|
| class encodedImage(BaseModel): |
| base64img:str |
|
|
|
|
| @app.post('/') |
| async def _file_upload(img:UploadFile = File(...)): |
|
|
| file_type="" |
| if "pdf" in img.filename: |
| |
| |
| file_type="pdf" |
| import pypdfium2 as pdfium |
| img1 = await img.read() |
| pdf = pdfium.PdfDocument(img1) |
|
|
| for x in range(len(pdf)): |
| page = pdf.get_page(x) |
| pil_image = page.render_to(pdfium.BitmapConv.pil_image,) |
| pil_image.filename = f"{uuid.uuid4()}.jpg" |
| mrzscanner.initLicense("DLS2eyJoYW5kc2hha2VDb2RlIjoiMTAxNjIyNjAzLVRYbEVaWE5yZEc5d1VISnZhbDlrYkhJIiwib3JnYW5pemF0aW9uSUQiOiIxMDE2MjI2MDMiLCJjaGVja0NvZGUiOjE4ODA3MzYyNzN9") |
| |
| |
| scanner = mrzscanner.createInstance() |
| scanner.loadModel(mrzscanner.get_model_path()) |
| lst=get_info(scanner, pil_image,file_type) |
| if lst: |
| break |
|
|
| |
| else: |
| file_type="image" |
| img.filename = f"{uuid.uuid4()}.jpg" |
| |
| contents = await img.read() |
| |
| mrzscanner.initLicense("DLS2eyJoYW5kc2hha2VDb2RlIjoiMTAxNjIyNjAzLVRYbEVaWE5yZEc5d1VISnZhbDlrYkhJIiwib3JnYW5pemF0aW9uSUQiOiIxMDE2MjI2MDMiLCJjaGVja0NvZGUiOjE4ODA3MzYyNzN9") |
| |
| |
| scanner = mrzscanner.createInstance() |
| scanner.loadModel(mrzscanner.get_model_path()) |
| |
| lst=get_info(scanner, contents,file_type) |
|
|
| |
| |
| |
| if lst: |
| |
| final_lst=extract_result(lst) |
| json_file=return_df(final_lst) |
| success=False |
| errorMessage="error" |
| isPassport=True |
| if json_file=="The attached document is not a Passport": |
| success=False |
| errorMessage="The attached document is not a Passport" |
| isPassport=False |
| json_file="" |
| |
| else: |
| success=True |
| errorMessage="None" |
| |
| |
| |
| |
| |
| x = {"success": success,"errorMessage": errorMessage,"isPassport":isPassport, "data": [json_file]} |
| else: |
| x = {"success": False,"errorMessage": "Image Not identified","isPassport":False, "data": []} |
| |
| |
| |
|
|
| |
| |
|
|
| return json.loads(json.dumps(x)) |
|
|
| def func(): |
| return "Parsing International Passports" |
|
|
| def main(img): |
| |
| mrzscanner.initLicense("DLS2eyJoYW5kc2hha2VDb2RlIjoiMTAxNjIyNjAzLVRYbEVaWE5yZEc5d1VISnZhbDlrYkhJIiwib3JnYW5pemF0aW9uSUQiOiIxMDE2MjI2MDMiLCJjaGVja0NvZGUiOjE4ODA3MzYyNzN9") |
| |
| scanner = mrzscanner.createInstance() |
| scanner.loadModel(mrzscanner.get_model_path()) |
| lst=get_info(scanner,img) |
| final_lst=extract_result(lst) |
| json_file=return_df(final_lst) |
| return json_file |
|
|
|
|
| import gradio as gr |
|
|
| io = gr.Interface(func,None,"label") |
|
|
| |
|
|
| |
| app = gr.mount_gradio_app(app, io,"/",gradio_api_url="http://localhost:7860/") |
| |
|
|
|
|
|
|
| |
| |
| uvicorn.run(app, host="0.0.0.0", port=7860) |
|
|
|
|
| |
| |
|
|
|
|
|
|
|
|