File size: 10,358 Bytes
d5edc80 624d33d 18f962c 624d33d f4e78ae 624d33d bfeff6a 624d33d d5edc80 624d33d f4e78ae 624d33d f4e78ae d5edc80 624d33d bbef06d 624d33d 98360ec 624d33d bbef06d b7a1380 bbef06d 624d33d a8cc23e 624d33d a8cc23e bbef06d 624d33d a8cc23e bbef06d f8ecdb2 624d33d bbef06d 624d33d a8cc23e 624d33d bbef06d b7a1380 bbef06d 624d33d bbef06d a8cc23e cfd326f 4b33b81 cfd326f 624d33d a8cc23e 996b930 cc6b22e cfd326f bbef06d cfd326f 996b930 7ac9541 996b930 f8ecdb2 624d33d 996b930 bbef06d 996b930 624d33d d5edc80 72aa1b8 f4e78ae d5edc80 f4e78ae be13c6e f4e78ae 4197e3c f4e78ae 4197e3c 3b0ebb5 4197e3c 092bebf 4197e3c 092bebf 6688ec2 4197e3c f9b2418 092bebf 4197e3c c0c3779 0df2db4 033c4e4 6688ec2 3b0ebb5 d5edc80 0b0573b 2456460 d5edc80 624d33d 7c272be be13c6e 624d33d 0c182f7 624d33d ef2362d 0b0573b 470a5ce 72aa1b8 470a5ce 72aa1b8 ef2362d 72aa1b8 58b8dbf 72aa1b8 5b85c12 624d33d | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 | 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_np = np.array(img)
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')
print(birth_date)
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' or nationality=='IMD' or country=='IMD':
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:
# print("I am in 1st except")
birth_date=lst[x][1][4]#datetime.strptime(lst[x][1][4], '%y%m%d').strftime('%d/%m/%Y')
expiry_date=lst[x][1][5]#datetime.strptime(lst[x][1][5], '%y%m%d').strftime('%d/%m/%Y')
# print(birth_date)
issue_date=expiry_date
document_type=lst[x][1][7]
document_number=lst[x][1][8]
# print(document_number)
temp_lst.append([surname,name,country,birth_date,expiry_date,document_number,issue_date])
#temp_lst.append([surname,name,country,nationality,birth_date,expiry_date,document_type,document_number])
final_lst.append(temp_lst)
else:
temp_lst=[]
surname=lst[x][0]
name=lst[x][1]
country=lst[x][2]
nationality=lst[x][3]
try:
birth_date=datetime.strptime(lst[x][4], '%y%m%d').strftime('%d/%m/%Y')
# print(birth_date)
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=='IMD' or country=='IMD' :
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]#datetime.strptime(lst[x][4], '%y%m%d').strftime('%d/%m/%Y')
expiry_date=lst[x][5]#datetime.strptime(lst[x][5], '%y%m%d').strftime('%d/%m/%Y')
issue_date=expiry_date
# print("I am in 2nd except")
#print(birth_date)
# print(datetime.strptime(lst[x][4], '%y%m%d').strftime('%d/%m/%Y'))
# print(datetime.strptime(lst[x][4], '%y%m%d'))
document_type=lst[x][7]
document_number=lst[x][8]
# print(document_number)
if document_type=="P" or document_type=="p":# or document_type=="PM" or document_type=="PO":
temp_lst.append([surname,name,country,birth_date,expiry_date,document_number,issue_date])
print(temp_lst)
else:
print(lst)
temp_lst.append("The attached document is not a Passport")
#temp_lst.append([surname,name,country,nationality,birth_date,expiry_date,document_type,document_number])
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']
#columns = ['family_name','given_name','country','nationality','date_of_birth','expiration','document_type','passport_number']
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() # <-- Important!
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")
# mrzscanner.initLicense("DLS2eyJoYW5kc2hha2VDb2RlIjoiMjAwMDAxLTE2NDk4Mjk3OTI2MzUiLCJvcmdhbml6YXRpb25JRCI6IjIwMDAwMSIsInNlc3Npb25QYXNzd29yZCI6IndTcGR6Vm05WDJrcEQ5YUoifQ==")
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() # <-- Important!
mrzscanner.initLicense("DLS2eyJoYW5kc2hha2VDb2RlIjoiMTAxNjIyNjAzLVRYbEVaWE5yZEc5d1VISnZhbDlrYkhJIiwib3JnYW5pemF0aW9uSUQiOiIxMDE2MjI2MDMiLCJjaGVja0NvZGUiOjE4ODA3MzYyNzN9")
# mrzscanner.initLicense("DLS2eyJoYW5kc2hha2VDb2RlIjoiMjAwMDAxLTE2NDk4Mjk3OTI2MzUiLCJvcmdhbml6YXRpb25JRCI6IjIwMDAwMSIsInNlc3Npb25QYXNzd29yZCI6IndTcGR6Vm05WDJrcEQ5YUoifQ==")
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):
# set license.
mrzscanner.initLicense("DLS2eyJoYW5kc2hha2VDb2RlIjoiMTAxNjIyNjAzLVRYbEVaWE5yZEc5d1VISnZhbDlrYkhJIiwib3JnYW5pemF0aW9uSUQiOiIxMDE2MjI2MDMiLCJjaGVja0NvZGUiOjE4ODA3MzYyNzN9")
#mrzscanner.initLicense("DLS2eyJoYW5kc2hha2VDb2RlIjoiMjAwMDAxLTE2NDk4Mjk3OTI2MzUiLCJvcmdhbml6YXRpb25JRCI6IjIwMDAwMSIsInNlc3Npb25QYXNzd29yZCI6IndTcGR6Vm05WDJrcEQ5YUoifQ==")
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")
#io = gr.Interface(lambda x: "Hello, " + x + "!", "textbox", "textbox")
#io = gr.Interface(fn=main, inputs="image", outputs="json")
app = gr.mount_gradio_app(app, io,"/",gradio_api_url="http://localhost:7860/")
#gradio_app = gr.routes.App.create_app(io)
#app = gr.mount_gradio_app(app, block, "/", gradio_api_url="http://localhost:7860/")
#app.mount("/img", StaticFiles(directory="images", html=True), name="images")
uvicorn.run(app, host="0.0.0.0", port=7860)
#app.mount(CUSTOM_PATH, gradio_app)
#io.launch()
|