|
| 1 | +import os |
| 2 | + |
| 3 | +from regula.documentreader.webclient import * |
| 4 | +from regula.documentreader.webclient.ext.models.recognition_request import LicenseRequest, EncryptedRCLRequest |
| 5 | + |
| 6 | +host = os.getenv("API_BASE_PATH", "https://api.regulaforensics.com") |
| 7 | +regula_license = os.getenv("TEST_LICENSE", None) # optional, used here only for smoke test purposes |
| 8 | + |
| 9 | +# read optional local license file |
| 10 | +if os.path.isfile('regula.license') and os.access('regula.license', os.R_OK): |
| 11 | + with open("regula.license", "rb") as f: |
| 12 | + print("Found local license file. Using it for performing request...") |
| 13 | + regula_license = f.read() |
| 14 | + |
| 15 | +with open("license.txt", "r") as f: |
| 16 | + license_ = f.read() |
| 17 | + |
| 18 | +with open("encrypted-rcl.txt", "r") as f: |
| 19 | + encrypted_rcl = f.read() |
| 20 | + |
| 21 | +with DocumentReaderApi(host) as api: |
| 22 | + api.license = regula_license |
| 23 | + |
| 24 | + params = ProcessParams( |
| 25 | + scenario=Scenario.FULL_PROCESS, |
| 26 | + result_type_output=[ |
| 27 | + # actual results |
| 28 | + Result.STATUS, Result.AUTHENTICITY, Result.TEXT, Result.IMAGES, |
| 29 | + Result.DOCUMENT_TYPE, Result.DOCUMENT_TYPE_CANDIDATES, Result.IMAGE_QUALITY, |
| 30 | + Result.DOCUMENT_POSITION, |
| 31 | + # legacy results |
| 32 | + Result.MRZ_TEXT, Result.VISUAL_TEXT, Result.BARCODE_TEXT, Result.RFID_TEXT, |
| 33 | + Result.VISUAL_GRAPHICS, Result.BARCODE_GRAPHICS, Result.RFID_GRAPHICS, |
| 34 | + Result.LEXICAL_ANALYSIS |
| 35 | + ] |
| 36 | + ) |
| 37 | + request = RecognitionRequest( |
| 38 | + process_params=params, |
| 39 | + container_list=ContainerList([ |
| 40 | + LicenseRequest(license_), |
| 41 | + EncryptedRCLRequest(encrypted_rcl) |
| 42 | + ]) |
| 43 | + ) |
| 44 | + response = api.process(request) |
| 45 | + |
| 46 | + # status examples |
| 47 | + response_status = response.status |
| 48 | + doc_overall_status = "valid" if response_status.overall_status == CheckResult.OK else "not valid" |
| 49 | + |
| 50 | + # text fields example |
| 51 | + doc_number_field = response.text.get_field(TextFieldType.DOCUMENT_NUMBER) |
| 52 | + doc_number_field_by_name = response.text.get_field_by_name("Document Number") |
| 53 | + |
| 54 | + doc_number_mrz = doc_number_field.get_value() |
| 55 | + doc_number_visual = doc_number_field.get_value(Source.VISUAL) |
| 56 | + doc_number_visual_validity = doc_number_field.source_validity(Source.VISUAL) |
| 57 | + doc_number_mrz_validity = doc_number_field.source_validity(Source.MRZ) |
| 58 | + doc_number_mrz_visual_matching = doc_number_field.cross_source_comparison(Source.MRZ, Source.VISUAL) |
| 59 | + |
| 60 | + doc_authenticity = response.authenticity() |
| 61 | + |
| 62 | + doc_ir_b900 = doc_authenticity.ir_b900_checks \ |
| 63 | + if doc_authenticity is not None else None |
| 64 | + # if FULL_PROCESS then auth is None |
| 65 | + |
| 66 | + doc_ir_b900_blank = doc_ir_b900.checks_by_element(SecurityFeatureType.BLANK) \ |
| 67 | + if doc_authenticity is not None else None |
| 68 | + |
| 69 | + doc_image_pattern = doc_authenticity.image_pattern_checks \ |
| 70 | + if doc_authenticity is not None else None |
| 71 | + |
| 72 | + doc_image_pattern_blank = doc_image_pattern.checks_by_element(SecurityFeatureType.BLANK) \ |
| 73 | + if doc_authenticity is not None else None |
| 74 | + |
| 75 | + # images fields example |
| 76 | + document_image = response.images.get_field(GraphicFieldType.DOCUMENT_FRONT).get_value() |
| 77 | + portrait_from_visual = response.images.get_field(GraphicFieldType.PORTRAIT).get_value(Source.VISUAL) |
| 78 | + with open('portrait.jpg', 'wb') as f: |
| 79 | + f.write(portrait_from_visual) |
| 80 | + with open('document-image.jpg', 'wb') as f: |
| 81 | + f.write(document_image) |
| 82 | + |
| 83 | + print(""" |
| 84 | + --------------------------------------------------------------------------- |
| 85 | + Web API version: {ping_version} |
| 86 | + --------------------------------------------------------------------------- |
| 87 | + Document Overall Status: {doc_overall_status} |
| 88 | + Document Number Visual: {doc_number_visual} |
| 89 | + Document Number MRZ: {doc_number_mrz} |
| 90 | + Validity Of Document Number Visual: {doc_number_visual_validity} |
| 91 | + Validity Of Document Number MRZ: {doc_number_mrz_validity} |
| 92 | + MRZ-Visual values comparison: {doc_number_mrz_visual_matching} |
| 93 | + --------------------------------------------------------------------------- |
| 94 | + """.format( |
| 95 | + ping_version=api.ping().version, |
| 96 | + doc_overall_status=doc_overall_status, doc_number_visual=doc_number_visual, |
| 97 | + doc_number_mrz=doc_number_mrz, doc_number_visual_validity=doc_number_mrz_validity, |
| 98 | + doc_number_mrz_validity=doc_number_mrz_validity, doc_number_mrz_visual_matching=doc_number_mrz_visual_matching, |
| 99 | + )) |
0 commit comments