|
| 1 | +import argparse |
| 2 | +import docscanner |
| 3 | +import sys |
| 4 | +import numpy as np |
| 5 | + |
| 6 | +from mrz.checker.td1 import TD1CodeChecker |
| 7 | +from mrz.checker.td2 import TD2CodeChecker |
| 8 | +from mrz.checker.td3 import TD3CodeChecker |
| 9 | +from mrz.checker.mrva import MRVACodeChecker |
| 10 | +from mrz.checker.mrvb import MRVBCodeChecker |
| 11 | + |
| 12 | +def check(lines): |
| 13 | + try: |
| 14 | + td1_check = TD1CodeChecker(lines) |
| 15 | + if bool(td1_check): |
| 16 | + return "TD1", td1_check.fields() |
| 17 | + except Exception as err: |
| 18 | + pass |
| 19 | + |
| 20 | + try: |
| 21 | + td2_check = TD2CodeChecker(lines) |
| 22 | + if bool(td2_check): |
| 23 | + return "TD2", td2_check.fields() |
| 24 | + except Exception as err: |
| 25 | + pass |
| 26 | + |
| 27 | + try: |
| 28 | + td3_check = TD3CodeChecker(lines) |
| 29 | + if bool(td3_check): |
| 30 | + return "TD3", td3_check.fields() |
| 31 | + except Exception as err: |
| 32 | + pass |
| 33 | + |
| 34 | + try: |
| 35 | + mrva_check = MRVACodeChecker(lines) |
| 36 | + if bool(mrva_check): |
| 37 | + return "MRVA", mrva_check.fields() |
| 38 | + except Exception as err: |
| 39 | + pass |
| 40 | + |
| 41 | + try: |
| 42 | + mrvb_check = MRVBCodeChecker(lines) |
| 43 | + if bool(mrvb_check): |
| 44 | + return "MRVB", mrvb_check.fields() |
| 45 | + except Exception as err: |
| 46 | + pass |
| 47 | + |
| 48 | + return 'No valid MRZ information found' |
| 49 | + |
| 50 | +def scandocument(): |
| 51 | + """ |
| 52 | + Command-line script for recognize MRZ info from a given image |
| 53 | + """ |
| 54 | + parser = argparse.ArgumentParser(description='Scan MRZ info from a given image') |
| 55 | + parser.add_argument('filename') |
| 56 | + parser.add_argument('-u', '--ui', default=False, type=bool, help='Whether to show the image') |
| 57 | + parser.add_argument('-l', '--license', default='', type=str, help='Set a valid license key') |
| 58 | + args = parser.parse_args() |
| 59 | + # print(args) |
| 60 | + try: |
| 61 | + filename = args.filename |
| 62 | + license = args.license |
| 63 | + ui = args.ui |
| 64 | + |
| 65 | + # set license |
| 66 | + if license == '': |
| 67 | + docscanner.initLicense("DLS2eyJoYW5kc2hha2VDb2RlIjoiMjAwMDAxLTE2NDk4Mjk3OTI2MzUiLCJvcmdhbml6YXRpb25JRCI6IjIwMDAwMSIsInNlc3Npb25QYXNzd29yZCI6IndTcGR6Vm05WDJrcEQ5YUoifQ==") |
| 68 | + else: |
| 69 | + docscanner.initLicense(license) |
| 70 | + |
| 71 | + # initialize mrz scanner |
| 72 | + scanner = docscanner.createInstance() |
| 73 | + |
| 74 | + # load MRZ model |
| 75 | + scanner.loadModel(docscanner.get_model_path()) |
| 76 | + |
| 77 | + s = "" |
| 78 | + if ui: |
| 79 | + import cv2 |
| 80 | + image = cv2.imread(filename) |
| 81 | + results = scanner.decodeMat(image) |
| 82 | + for result in results: |
| 83 | + print(result.text) |
| 84 | + s += result.text + '\n' |
| 85 | + x1 = result.x1 |
| 86 | + y1 = result.y1 |
| 87 | + x2 = result.x2 |
| 88 | + y2 = result.y2 |
| 89 | + x3 = result.x3 |
| 90 | + y3 = result.y3 |
| 91 | + x4 = result.x4 |
| 92 | + y4 = result.y4 |
| 93 | + |
| 94 | + cv2.drawContours(image, [np.int0([(x1, y1), (x2, y2), (x3, y3), (x4, y4)])], 0, (0, 255, 0), 2) |
| 95 | + # cv2.putText(image, result.text, (x1, y1), cv2.FONT_HERSHEY_SIMPLEX, 0.4, (0, 0, 255), 2) |
| 96 | + |
| 97 | + print(check(s[:-1])) |
| 98 | + |
| 99 | + cv2.imshow("image", image) |
| 100 | + cv2.waitKey(0) |
| 101 | + else: |
| 102 | + results = scanner.decodeFile(filename) |
| 103 | + for result in results: |
| 104 | + print(result.text) |
| 105 | + s += result.text + '\n' |
| 106 | + |
| 107 | + print(check(s[:-1])) |
| 108 | + |
| 109 | + |
| 110 | + except Exception as err: |
| 111 | + print(err) |
| 112 | + sys.exit(1) |
0 commit comments