-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmrz_scanner.py
120 lines (110 loc) · 6.24 KB
/
mrz_scanner.py
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
import sys
from dynamsoft_capture_vision_bundle import *
import os
class MRZResult:
def __init__(self, item: ParsedResultItem):
self.doc_type = item.get_code_type()
self.raw_text=[]
self.doc_id = None
self.surname = None
self.given_name = None
self.nationality = None
self.issuer = None
self.gender = None
self.date_of_birth = None
self.date_of_expiry = None
if self.doc_type == "MRTD_TD3_PASSPORT":
if item.get_field_value("passportNumber") != None and item.get_field_validation_status("passportNumber") != EnumValidationStatus.VS_FAILED:
self.doc_id = item.get_field_value("passportNumber")
elif item.get_field_value("documentNumber") != None and item.get_field_validation_status("documentNumber") != EnumValidationStatus.VS_FAILED:
self.doc_id = item.get_field_value("documentNumber")
line = item.get_field_value("line1")
if line is not None:
if item.get_field_validation_status("line1") == EnumValidationStatus.VS_FAILED:
line += ", Validation Failed"
self.raw_text.append(line)
line = item.get_field_value("line2")
if line is not None:
if item.get_field_validation_status("line2") == EnumValidationStatus.VS_FAILED:
line += ", Validation Failed"
self.raw_text.append(line)
line = item.get_field_value("line3")
if line is not None:
if item.get_field_validation_status("line3") == EnumValidationStatus.VS_FAILED:
line += ", Validation Failed"
self.raw_text.append(line)
if item.get_field_value("nationality") != None and item.get_field_validation_status("nationality") != EnumValidationStatus.VS_FAILED:
self.nationality = item.get_field_value("nationality")
if item.get_field_value("issuingState") != None and item.get_field_validation_status("issuingState") != EnumValidationStatus.VS_FAILED:
self.issuer = item.get_field_value("issuingState")
if item.get_field_value("dateOfBirth") != None and item.get_field_validation_status("dateOfBirth") != EnumValidationStatus.VS_FAILED:
self.date_of_birth = item.get_field_value("dateOfBirth")
if item.get_field_value("dateOfExpiry") != None and item.get_field_validation_status("dateOfExpiry") != EnumValidationStatus.VS_FAILED:
self.date_of_expiry = item.get_field_value("dateOfExpiry")
if item.get_field_value("sex") != None and item.get_field_validation_status("sex") != EnumValidationStatus.VS_FAILED:
self.gender = item.get_field_value("sex")
if item.get_field_value("primaryIdentifier") != None and item.get_field_validation_status("primaryIdentifier") != EnumValidationStatus.VS_FAILED:
self.surname = item.get_field_value("primaryIdentifier")
if item.get_field_value("secondaryIdentifier") != None and item.get_field_validation_status("secondaryIdentifier") != EnumValidationStatus.VS_FAILED:
self.given_name = item.get_field_value("secondaryIdentifier")
def to_string(self):
msg = (f"Raw Text:\n")
for index, line in enumerate(self.raw_text):
msg += (f"\tLine {index + 1}: {line}\n")
msg+=(f"Parsed Information:\n"
f"\tDocumentType: {self.doc_type or ''}\n"
f"\tDocumentID: {self.doc_id or ''}\n"
f"\tSurname: {self.surname or ''}\n"
f"\tGivenName: {self.given_name or ''}\n"
f"\tNationality: {self.nationality or ''}\n"
f"\tIssuingCountryorOrganization: {self.issuer or ''}\n"
f"\tGender: {self.gender or ''}\n"
f"\tDateofBirth(YYMMDD): {self.date_of_birth or ''}\n"
f"\tExpirationDate(YYMMDD): {self.date_of_expiry or ''}\n")
return msg
def print_results(result: ParsedResult) -> None:
tag = result.get_original_image_tag()
if isinstance(tag, FileImageTag):
print("File:", tag.get_file_path())
if result.get_error_code() != EnumErrorCode.EC_OK:
print("Error:", result.get_error_string())
else:
items = result.get_items()
print("Parsed", len(items), "MRZ Zones.")
for item in items:
mrz_result = MRZResult(item)
print(mrz_result.to_string())
if __name__ == '__main__':
print("**********************************************************")
print("Welcome to Dynamsoft Capture Vision - MRZ Sample")
print("**********************************************************")
# Initialize license.
# You can request and extend a trial license from https://www.dynamsoft.com/customer/license/trialLicense?product=dcv&utm_source=samples&package=python
# The string 'DLS2eyJvcmdhbml6YXRpb25JRCI6IjIwMDAwMSJ9' here is a free public trial license. Note that network connection is required for this license to work.
error_code, error_message = LicenseManager.init_license("DLS2eyJvcmdhbml6YXRpb25JRCI6IjIwMDAwMSJ9")
if error_code != EnumErrorCode.EC_OK and error_code != EnumErrorCode.EC_LICENSE_CACHE_USED:
print("License initialization failed: ErrorCode:", error_code, ", ErrorString:", error_message)
else:
cvr_instance = CaptureVisionRouter()
while (True):
image_path = input(
">> Input your image full path:\n"
">> 'Enter' for sample image or 'Q'/'q' to quit\n"
).strip('\'"')
if image_path.lower() == "q":
sys.exit(0)
if image_path == "":
image_path = "../images/passport-sample.jpg"
if not os.path.exists(image_path):
print("The image path does not exist.")
continue
result = cvr_instance.capture(image_path, "ReadPassportAndId")
if result.get_error_code() != EnumErrorCode.EC_OK:
print("Error:", result.get_error_code(), result.get_error_string())
else:
parsed_result = result.get_parsed_result()
if parsed_result is None or len(parsed_result.get_items()) == 0:
print("No parsed results.")
else:
print_results(parsed_result)
input("Press Enter to quit...")