|
| 1 | +from typing import List, Optional, TypeVar |
| 2 | + |
| 3 | +from mindee.documents.base import Document, TypeApiPrediction, clean_out_string |
| 4 | +from mindee.fields.date import DateField |
| 5 | +from mindee.fields.text import TextField |
| 6 | + |
| 7 | + |
| 8 | +class IdCardV1(Document): |
| 9 | + """Carte Nationale d'Identité v1 prediction results.""" |
| 10 | + |
| 11 | + document_side: TextField |
| 12 | + """The side of the document which is visible.""" |
| 13 | + id_number: TextField |
| 14 | + """The identification card number.""" |
| 15 | + given_names: List[TextField] |
| 16 | + """The given name(s) of the card holder.""" |
| 17 | + surname: TextField |
| 18 | + """The surname of the card holder.""" |
| 19 | + birth_date: DateField |
| 20 | + """The date of birth of the card holder.""" |
| 21 | + birth_place: TextField |
| 22 | + """The place of birth of the card holder.""" |
| 23 | + expiry_date: DateField |
| 24 | + """The expiry date of the identification card.""" |
| 25 | + authority: TextField |
| 26 | + """The name of the issuing authority.""" |
| 27 | + gender: TextField |
| 28 | + """The gender of the card holder.""" |
| 29 | + mrz1: TextField |
| 30 | + """Machine Readable Zone, first line""" |
| 31 | + mrz2: TextField |
| 32 | + """Machine Readable Zone, second line""" |
| 33 | + |
| 34 | + def __init__( |
| 35 | + self, |
| 36 | + api_prediction=None, |
| 37 | + input_source=None, |
| 38 | + page_n: Optional[int] = None, |
| 39 | + ): |
| 40 | + """ |
| 41 | + Carte Nationale d'Identité v1 prediction results. |
| 42 | +
|
| 43 | + :param api_prediction: Raw prediction from HTTP response |
| 44 | + :param input_source: Input object |
| 45 | + :param page_n: Page number for multi pages pdf input |
| 46 | + """ |
| 47 | + super().__init__( |
| 48 | + input_source=input_source, |
| 49 | + document_type="id_card", |
| 50 | + api_prediction=api_prediction, |
| 51 | + page_n=page_n, |
| 52 | + ) |
| 53 | + self._build_from_api_prediction(api_prediction["prediction"], page_n=page_n) |
| 54 | + |
| 55 | + def _build_from_api_prediction( |
| 56 | + self, api_prediction: TypeApiPrediction, page_n: Optional[int] = None |
| 57 | + ) -> None: |
| 58 | + """ |
| 59 | + Build the object from the prediction API JSON. |
| 60 | +
|
| 61 | + :param api_prediction: Raw prediction from HTTP response |
| 62 | + :param page_n: Page number |
| 63 | + """ |
| 64 | + self.document_side = TextField( |
| 65 | + api_prediction.get("document_side", {}), |
| 66 | + page_n=page_n, |
| 67 | + ) |
| 68 | + self.id_number = TextField( |
| 69 | + api_prediction["id_number"], |
| 70 | + page_n=page_n, |
| 71 | + ) |
| 72 | + self.given_names = [ |
| 73 | + TextField(prediction, page_n=page_n) |
| 74 | + for prediction in api_prediction["given_names"] |
| 75 | + ] |
| 76 | + self.surname = TextField( |
| 77 | + api_prediction["surname"], |
| 78 | + page_n=page_n, |
| 79 | + ) |
| 80 | + self.birth_date = DateField( |
| 81 | + api_prediction["birth_date"], |
| 82 | + page_n=page_n, |
| 83 | + ) |
| 84 | + self.birth_place = TextField( |
| 85 | + api_prediction["birth_place"], |
| 86 | + page_n=page_n, |
| 87 | + ) |
| 88 | + self.expiry_date = DateField( |
| 89 | + api_prediction["expiry_date"], |
| 90 | + page_n=page_n, |
| 91 | + ) |
| 92 | + self.authority = TextField( |
| 93 | + api_prediction["authority"], |
| 94 | + page_n=page_n, |
| 95 | + ) |
| 96 | + self.gender = TextField( |
| 97 | + api_prediction["gender"], |
| 98 | + page_n=page_n, |
| 99 | + ) |
| 100 | + self.mrz1 = TextField( |
| 101 | + api_prediction["mrz1"], |
| 102 | + page_n=page_n, |
| 103 | + ) |
| 104 | + self.mrz2 = TextField( |
| 105 | + api_prediction["mrz2"], |
| 106 | + page_n=page_n, |
| 107 | + ) |
| 108 | + |
| 109 | + def __str__(self) -> str: |
| 110 | + given_names = "\n".join([str(item) for item in self.given_names]) |
| 111 | + return clean_out_string( |
| 112 | + "----- FR Carte Nationale d'Identité V1 -----\n" |
| 113 | + f"Filename: {self.filename or ''}\n" |
| 114 | + f"Document Side: { self.document_side }\n" |
| 115 | + f"Identity Number: { self.id_number }\n" |
| 116 | + f"Given Name(s): { given_names }\n" |
| 117 | + f"Surname: { self.surname }\n" |
| 118 | + f"Date of Birth: { self.birth_date }\n" |
| 119 | + f"Place of Birth: { self.birth_place }\n" |
| 120 | + f"Expiry Date: { self.expiry_date }\n" |
| 121 | + f"Issuing Authority: { self.authority }\n" |
| 122 | + f"Gender: { self.gender }\n" |
| 123 | + f"MRZ Line 1: { self.mrz1 }\n" |
| 124 | + f"MRZ Line 2: { self.mrz2 }\n" |
| 125 | + "----------------------" |
| 126 | + ) |
| 127 | + |
| 128 | + |
| 129 | +TypeIdCardV1 = TypeVar("TypeIdCardV1", bound=IdCardV1) |
0 commit comments