|
| 1 | +from typing import List, Optional, TypeVar |
| 2 | + |
| 3 | +from mindee.documents.base import Document, TypeApiPrediction, clean_out_string |
| 4 | +from mindee.fields.text import TextField |
| 5 | + |
| 6 | + |
| 7 | +class LicensePlateV1(Document): |
| 8 | + """License Plate v1 prediction results.""" |
| 9 | + |
| 10 | + license_plates: List[TextField] |
| 11 | + """List of all license plates found in the image.""" |
| 12 | + |
| 13 | + def __init__( |
| 14 | + self, |
| 15 | + api_prediction=None, |
| 16 | + input_source=None, |
| 17 | + page_n: Optional[int] = None, |
| 18 | + ): |
| 19 | + """ |
| 20 | + License Plate v1 prediction results. |
| 21 | +
|
| 22 | + :param api_prediction: Raw prediction from HTTP response |
| 23 | + :param input_source: Input object |
| 24 | + :param page_n: Page number for multi pages pdf input |
| 25 | + """ |
| 26 | + super().__init__( |
| 27 | + input_source=input_source, |
| 28 | + document_type="license_plate", |
| 29 | + api_prediction=api_prediction, |
| 30 | + page_n=page_n, |
| 31 | + ) |
| 32 | + self._build_from_api_prediction(api_prediction["prediction"], page_n=page_n) |
| 33 | + |
| 34 | + def _build_from_api_prediction( |
| 35 | + self, api_prediction: TypeApiPrediction, page_n: Optional[int] = None |
| 36 | + ) -> None: |
| 37 | + """ |
| 38 | + Build the object from the prediction API JSON. |
| 39 | +
|
| 40 | + :param api_prediction: Raw prediction from HTTP response |
| 41 | + :param page_n: Page number |
| 42 | + """ |
| 43 | + self.license_plates = [ |
| 44 | + TextField(prediction, page_n=page_n) |
| 45 | + for prediction in api_prediction["license_plates"] |
| 46 | + ] |
| 47 | + |
| 48 | + def __str__(self) -> str: |
| 49 | + license_plates = f"\n { ' ' * 15 }".join( |
| 50 | + [str(item) for item in self.license_plates] |
| 51 | + ) |
| 52 | + return clean_out_string( |
| 53 | + "----- EU License Plate V1 -----\n" |
| 54 | + f"Filename: {self.filename or ''}\n" |
| 55 | + f"License Plates: { license_plates }\n" |
| 56 | + "----------------------" |
| 57 | + ) |
| 58 | + |
| 59 | + |
| 60 | +TypeLicensePlateV1 = TypeVar("TypeLicensePlateV1", bound=LicensePlateV1) |
0 commit comments