|
| 1 | + |
| 2 | +import sys |
| 3 | +from dynamsoft_capture_vision_bundle import * |
| 4 | +import os |
| 5 | + |
| 6 | +class MRZResult: |
| 7 | + def __init__(self, item: ParsedResultItem): |
| 8 | + self.doc_type = item.get_code_type() |
| 9 | + self.raw_text=[] |
| 10 | + self.doc_id = None |
| 11 | + self.surname = None |
| 12 | + self.given_name = None |
| 13 | + self.nationality = None |
| 14 | + self.issuer = None |
| 15 | + self.gender = None |
| 16 | + self.date_of_birth = None |
| 17 | + self.date_of_expiry = None |
| 18 | + if self.doc_type == "MRTD_TD3_PASSPORT": |
| 19 | + if item.get_field_value("passportNumber") != None and item.get_field_validation_status("passportNumber") != EnumValidationStatus.VS_FAILED: |
| 20 | + self.doc_id = item.get_field_value("passportNumber") |
| 21 | + elif item.get_field_value("documentNumber") != None and item.get_field_validation_status("documentNumber") != EnumValidationStatus.VS_FAILED: |
| 22 | + self.doc_id = item.get_field_value("documentNumber") |
| 23 | + |
| 24 | + line = item.get_field_value("line1") |
| 25 | + if line is not None: |
| 26 | + if item.get_field_validation_status("line1") == EnumValidationStatus.VS_FAILED: |
| 27 | + line += ", Validation Failed" |
| 28 | + self.raw_text.append(line) |
| 29 | + line = item.get_field_value("line2") |
| 30 | + if line is not None: |
| 31 | + if item.get_field_validation_status("line2") == EnumValidationStatus.VS_FAILED: |
| 32 | + line += ", Validation Failed" |
| 33 | + self.raw_text.append(line) |
| 34 | + line = item.get_field_value("line3") |
| 35 | + if line is not None: |
| 36 | + if item.get_field_validation_status("line3") == EnumValidationStatus.VS_FAILED: |
| 37 | + line += ", Validation Failed" |
| 38 | + self.raw_text.append(line) |
| 39 | + |
| 40 | + if item.get_field_value("nationality") != None and item.get_field_validation_status("nationality") != EnumValidationStatus.VS_FAILED: |
| 41 | + self.nationality = item.get_field_value("nationality") |
| 42 | + if item.get_field_value("issuingState") != None and item.get_field_validation_status("issuingState") != EnumValidationStatus.VS_FAILED: |
| 43 | + self.issuer = item.get_field_value("issuingState") |
| 44 | + if item.get_field_value("dateOfBirth") != None and item.get_field_validation_status("dateOfBirth") != EnumValidationStatus.VS_FAILED: |
| 45 | + self.date_of_birth = item.get_field_value("dateOfBirth") |
| 46 | + if item.get_field_value("dateOfExpiry") != None and item.get_field_validation_status("dateOfExpiry") != EnumValidationStatus.VS_FAILED: |
| 47 | + self.date_of_expiry = item.get_field_value("dateOfExpiry") |
| 48 | + if item.get_field_value("sex") != None and item.get_field_validation_status("sex") != EnumValidationStatus.VS_FAILED: |
| 49 | + self.gender = item.get_field_value("sex") |
| 50 | + if item.get_field_value("primaryIdentifier") != None and item.get_field_validation_status("primaryIdentifier") != EnumValidationStatus.VS_FAILED: |
| 51 | + self.surname = item.get_field_value("primaryIdentifier") |
| 52 | + if item.get_field_value("secondaryIdentifier") != None and item.get_field_validation_status("secondaryIdentifier") != EnumValidationStatus.VS_FAILED: |
| 53 | + self.given_name = item.get_field_value("secondaryIdentifier") |
| 54 | + def to_string(self): |
| 55 | + msg = (f"Raw Text:\n") |
| 56 | + for index, line in enumerate(self.raw_text): |
| 57 | + msg += (f"\tLine {index + 1}: {line}\n") |
| 58 | + msg+=(f"Parsed Information:\n" |
| 59 | + f"\tDocumentType: {self.doc_type or ''}\n" |
| 60 | + f"\tDocumentID: {self.doc_id or ''}\n" |
| 61 | + f"\tSurname: {self.surname or ''}\n" |
| 62 | + f"\tGivenName: {self.given_name or ''}\n" |
| 63 | + f"\tNationality: {self.nationality or ''}\n" |
| 64 | + f"\tIssuingCountryorOrganization: {self.issuer or ''}\n" |
| 65 | + f"\tGender: {self.gender or ''}\n" |
| 66 | + f"\tDateofBirth(YYMMDD): {self.date_of_birth or ''}\n" |
| 67 | + f"\tExpirationDate(YYMMDD): {self.date_of_expiry or ''}\n") |
| 68 | + return msg |
| 69 | +def print_results(result: ParsedResult) -> None: |
| 70 | + tag = result.get_original_image_tag() |
| 71 | + if isinstance(tag, FileImageTag): |
| 72 | + print("File:", tag.get_file_path()) |
| 73 | + if result.get_error_code() != EnumErrorCode.EC_OK: |
| 74 | + print("Error:", result.get_error_string()) |
| 75 | + else: |
| 76 | + items = result.get_items() |
| 77 | + print("Parsed", len(items), "MRZ Zones.") |
| 78 | + for item in items: |
| 79 | + mrz_result = MRZResult(item) |
| 80 | + print(mrz_result.to_string()) |
| 81 | + |
| 82 | +if __name__ == '__main__': |
| 83 | + |
| 84 | + print("**********************************************************") |
| 85 | + print("Welcome to Dynamsoft Capture Vision - MRZ Sample") |
| 86 | + print("**********************************************************") |
| 87 | + |
| 88 | + # Initialize license. |
| 89 | + # You can request and extend a trial license from https://www.dynamsoft.com/customer/license/trialLicense?product=dcv&utm_source=samples&package=python |
| 90 | + # The string 'DLS2eyJvcmdhbml6YXRpb25JRCI6IjIwMDAwMSJ9' here is a free public trial license. Note that network connection is required for this license to work. |
| 91 | + error_code, error_message = LicenseManager.init_license("DLS2eyJvcmdhbml6YXRpb25JRCI6IjIwMDAwMSJ9") |
| 92 | + if error_code != EnumErrorCode.EC_OK and error_code != EnumErrorCode.EC_LICENSE_CACHE_USED: |
| 93 | + print("License initialization failed: ErrorCode:", error_code, ", ErrorString:", error_message) |
| 94 | + else: |
| 95 | + cvr_instance = CaptureVisionRouter() |
| 96 | + while (True): |
| 97 | + image_path = input( |
| 98 | + ">> Input your image full path:\n" |
| 99 | + ">> 'Enter' for sample image or 'Q'/'q' to quit\n" |
| 100 | + ).strip('\'"') |
| 101 | + |
| 102 | + if image_path.lower() == "q": |
| 103 | + sys.exit(0) |
| 104 | + |
| 105 | + if image_path == "": |
| 106 | + image_path = "../Images/passport-sample.jpg" |
| 107 | + |
| 108 | + if not os.path.exists(image_path): |
| 109 | + print("The image path does not exist.") |
| 110 | + continue |
| 111 | + result = cvr_instance.capture(image_path, "ReadPassportAndId") |
| 112 | + if result.get_error_code() != EnumErrorCode.EC_OK: |
| 113 | + print("Error:", result.get_error_code(), result.get_error_string()) |
| 114 | + else: |
| 115 | + parsed_result = result.get_parsed_result() |
| 116 | + if parsed_result is None or len(parsed_result.get_items()) == 0: |
| 117 | + print("No parsed results.") |
| 118 | + else: |
| 119 | + print_results(parsed_result) |
| 120 | + input("Press Enter to quit...") |
0 commit comments