|
| 1 | +--- |
| 2 | +title: Driver License OCR Python |
| 3 | +category: 622b805aaec68102ea7fcbc2 |
| 4 | +slug: python-driver-license-ocr |
| 5 | +parentDoc: 609808f773b0b90051d839de |
| 6 | +--- |
| 7 | +The Python OCR SDK supports the [Driver License API](https://platform.mindee.com/mindee/driver_license). |
| 8 | + |
| 9 | +Using the [sample below](https://github.com/mindee/client-lib-test-data/blob/main/products/driver_license/default_sample.jpg), we are going to illustrate how to extract the data that we want using the OCR SDK. |
| 10 | + |
| 11 | + |
| 12 | +# Quick-Start |
| 13 | +```py |
| 14 | +from mindee import Client, product, AsyncPredictResponse |
| 15 | + |
| 16 | +# Init a new client |
| 17 | +mindee_client = Client(api_key="my-api-key") |
| 18 | + |
| 19 | +# Load a file from disk |
| 20 | +input_doc = mindee_client.source_from_path("/path/to/the/file.ext") |
| 21 | + |
| 22 | +# Load a file from disk and enqueue it. |
| 23 | +result: AsyncPredictResponse = mindee_client.enqueue_and_parse( |
| 24 | + product.DriverLicenseV1, |
| 25 | + input_doc, |
| 26 | +) |
| 27 | + |
| 28 | +# Print a brief summary of the parsed data |
| 29 | +print(result.document) |
| 30 | + |
| 31 | +``` |
| 32 | + |
| 33 | +**Output (RST):** |
| 34 | +```rst |
| 35 | +######## |
| 36 | +Document |
| 37 | +######## |
| 38 | +:Mindee ID: fbdeae38-ada3-43ac-aa58-e01a3d47e474 |
| 39 | +:Filename: default_sample.jpg |
| 40 | +
|
| 41 | +Inference |
| 42 | +######### |
| 43 | +:Product: mindee/driver_license v1.0 |
| 44 | +:Rotation applied: Yes |
| 45 | +
|
| 46 | +Prediction |
| 47 | +========== |
| 48 | +:Country Code: USA |
| 49 | +:State: AZ |
| 50 | +:ID: D12345678 |
| 51 | +:Category: D |
| 52 | +:Last Name: Sample |
| 53 | +:First Name: Jelani |
| 54 | +:Date of Birth: 1957-02-01 |
| 55 | +:Place of Birth: |
| 56 | +:Expiry Date: 2018-02-01 |
| 57 | +:Issued Date: 2013-01-10 |
| 58 | +:Issuing Authority: |
| 59 | +:MRZ: |
| 60 | +:DD Number: DD1234567890123456 |
| 61 | +``` |
| 62 | + |
| 63 | +# Field Types |
| 64 | +## Standard Fields |
| 65 | +These fields are generic and used in several products. |
| 66 | + |
| 67 | +### BaseField |
| 68 | +Each prediction object contains a set of fields that inherit from the generic `BaseField` class. |
| 69 | +A typical `BaseField` object will have the following attributes: |
| 70 | + |
| 71 | +* **value** (`Union[float, str]`): corresponds to the field value. Can be `None` if no value was extracted. |
| 72 | +* **confidence** (`float`): the confidence score of the field prediction. |
| 73 | +* **bounding_box** (`[Point, Point, Point, Point]`): contains exactly 4 relative vertices (points) coordinates of a right rectangle containing the field in the document. |
| 74 | +* **polygon** (`List[Point]`): contains the relative vertices coordinates (`Point`) of a polygon containing the field in the image. |
| 75 | +* **page_id** (`int`): the ID of the page, always `None` when at document-level. |
| 76 | +* **reconstructed** (`bool`): indicates whether an object was reconstructed (not extracted as the API gave it). |
| 77 | + |
| 78 | +> **Note:** A `Point` simply refers to a List of two numbers (`[float, float]`). |
| 79 | +
|
| 80 | + |
| 81 | +Aside from the previous attributes, all basic fields have access to a custom `__str__` method that can be used to print their value as a string. |
| 82 | + |
| 83 | +### DateField |
| 84 | +Aside from the basic `BaseField` attributes, the date field `DateField` also implements the following: |
| 85 | + |
| 86 | +* **date_object** (`Date`): an accessible representation of the value as a python object. Can be `None`. |
| 87 | + |
| 88 | +### StringField |
| 89 | +The text field `StringField` only has one constraint: its **value** is an `Optional[str]`. |
| 90 | + |
| 91 | +# Attributes |
| 92 | +The following fields are extracted for Driver License V1: |
| 93 | + |
| 94 | +## Category |
| 95 | +**category** ([StringField](#stringfield)): The category or class of the driver license. |
| 96 | + |
| 97 | +```py |
| 98 | +print(result.document.inference.prediction.category.value) |
| 99 | +``` |
| 100 | + |
| 101 | +## Country Code |
| 102 | +**country_code** ([StringField](#stringfield)): The alpha-3 ISO 3166 code of the country where the driver license was issued. |
| 103 | + |
| 104 | +```py |
| 105 | +print(result.document.inference.prediction.country_code.value) |
| 106 | +``` |
| 107 | + |
| 108 | +## Date of Birth |
| 109 | +**date_of_birth** ([DateField](#datefield)): The date of birth of the driver license holder. |
| 110 | + |
| 111 | +```py |
| 112 | +print(result.document.inference.prediction.date_of_birth.value) |
| 113 | +``` |
| 114 | + |
| 115 | +## DD Number |
| 116 | +**dd_number** ([StringField](#stringfield)): The DD number of the driver license. |
| 117 | + |
| 118 | +```py |
| 119 | +print(result.document.inference.prediction.dd_number.value) |
| 120 | +``` |
| 121 | + |
| 122 | +## Expiry Date |
| 123 | +**expiry_date** ([DateField](#datefield)): The expiry date of the driver license. |
| 124 | + |
| 125 | +```py |
| 126 | +print(result.document.inference.prediction.expiry_date.value) |
| 127 | +``` |
| 128 | + |
| 129 | +## First Name |
| 130 | +**first_name** ([StringField](#stringfield)): The first name of the driver license holder. |
| 131 | + |
| 132 | +```py |
| 133 | +print(result.document.inference.prediction.first_name.value) |
| 134 | +``` |
| 135 | + |
| 136 | +## ID |
| 137 | +**id** ([StringField](#stringfield)): The unique identifier of the driver license. |
| 138 | + |
| 139 | +```py |
| 140 | +print(result.document.inference.prediction.id.value) |
| 141 | +``` |
| 142 | + |
| 143 | +## Issued Date |
| 144 | +**issued_date** ([DateField](#datefield)): The date when the driver license was issued. |
| 145 | + |
| 146 | +```py |
| 147 | +print(result.document.inference.prediction.issued_date.value) |
| 148 | +``` |
| 149 | + |
| 150 | +## Issuing Authority |
| 151 | +**issuing_authority** ([StringField](#stringfield)): The authority that issued the driver license. |
| 152 | + |
| 153 | +```py |
| 154 | +print(result.document.inference.prediction.issuing_authority.value) |
| 155 | +``` |
| 156 | + |
| 157 | +## Last Name |
| 158 | +**last_name** ([StringField](#stringfield)): The last name of the driver license holder. |
| 159 | + |
| 160 | +```py |
| 161 | +print(result.document.inference.prediction.last_name.value) |
| 162 | +``` |
| 163 | + |
| 164 | +## MRZ |
| 165 | +**mrz** ([StringField](#stringfield)): The Machine Readable Zone (MRZ) of the driver license. |
| 166 | + |
| 167 | +```py |
| 168 | +print(result.document.inference.prediction.mrz.value) |
| 169 | +``` |
| 170 | + |
| 171 | +## Place of Birth |
| 172 | +**place_of_birth** ([StringField](#stringfield)): The place of birth of the driver license holder. |
| 173 | + |
| 174 | +```py |
| 175 | +print(result.document.inference.prediction.place_of_birth.value) |
| 176 | +``` |
| 177 | + |
| 178 | +## State |
| 179 | +**state** ([StringField](#stringfield)): Second part of the ISO 3166-2 code, consisting of two letters indicating the US State. |
| 180 | + |
| 181 | +```py |
| 182 | +print(result.document.inference.prediction.state.value) |
| 183 | +``` |
| 184 | + |
| 185 | +# Questions? |
| 186 | +[Join our Slack](https://join.slack.com/t/mindee-community/shared_invite/zt-2d0ds7dtz-DPAF81ZqTy20chsYpQBW5g) |
0 commit comments