|
| 1 | +from typing import List, Optional |
| 2 | + |
| 3 | +from mindee.documents.base import TypeApiPrediction |
| 4 | +from mindee.geometry import ( |
| 5 | + Polygon, |
| 6 | + get_centroid, |
| 7 | + get_min_max_x, |
| 8 | + get_min_max_y, |
| 9 | + is_point_in_polygon_y, |
| 10 | + polygon_from_prediction, |
| 11 | +) |
| 12 | + |
| 13 | + |
| 14 | +class Word: |
| 15 | + """A single word.""" |
| 16 | + |
| 17 | + confidence: float |
| 18 | + polygon: Polygon |
| 19 | + text: str |
| 20 | + |
| 21 | + def __init__(self, prediction: TypeApiPrediction): |
| 22 | + self.confidence = prediction["confidence"] |
| 23 | + self.polygon = polygon_from_prediction(prediction["polygon"]) |
| 24 | + self.text = prediction["text"] |
| 25 | + |
| 26 | + def __str__(self) -> str: |
| 27 | + return self.text |
| 28 | + |
| 29 | + |
| 30 | +class OcrLine(List[Word]): |
| 31 | + """A list of words which are on the same line.""" |
| 32 | + |
| 33 | + def sort_on_x(self) -> None: |
| 34 | + """Sort the words on the line from left to right.""" |
| 35 | + self.sort(key=lambda item: get_min_max_x(item.polygon).min) |
| 36 | + |
| 37 | + def __str__(self) -> str: |
| 38 | + return " ".join([word.text for word in self]) |
| 39 | + |
| 40 | + |
| 41 | +class OcrPage: |
| 42 | + """OCR extraction for a single page.""" |
| 43 | + |
| 44 | + all_words: List[Word] |
| 45 | + """All the words on the page, in semi-random order.""" |
| 46 | + _lines: List[OcrLine] |
| 47 | + |
| 48 | + def __init__(self, prediction: TypeApiPrediction): |
| 49 | + self.all_words = [ |
| 50 | + Word(word_prediction) for word_prediction in prediction["all_words"] |
| 51 | + ] |
| 52 | + self._lines = [] |
| 53 | + |
| 54 | + @staticmethod |
| 55 | + def _are_words_on_same_line(current_word: Word, next_word: Word) -> bool: |
| 56 | + """Determine if two words are on the same line.""" |
| 57 | + current_in_next = is_point_in_polygon_y( |
| 58 | + get_centroid(current_word.polygon), |
| 59 | + next_word.polygon, |
| 60 | + ) |
| 61 | + next_in_current = is_point_in_polygon_y( |
| 62 | + get_centroid(next_word.polygon), current_word.polygon |
| 63 | + ) |
| 64 | + # We need to check both to eliminate any issues due to word order. |
| 65 | + return current_in_next or next_in_current |
| 66 | + |
| 67 | + def _to_lines(self) -> List[OcrLine]: |
| 68 | + """Order all the words on the page into lines.""" |
| 69 | + current: Optional[Word] = None |
| 70 | + indexes: List[int] = [] |
| 71 | + lines: List[OcrLine] = [] |
| 72 | + |
| 73 | + # make sure words are sorted from top to bottom |
| 74 | + self.all_words.sort( |
| 75 | + key=lambda item: get_min_max_y(item.polygon).min, reverse=False |
| 76 | + ) |
| 77 | + |
| 78 | + for _ in self.all_words: |
| 79 | + line: OcrLine = OcrLine() |
| 80 | + for idx, word in enumerate(self.all_words): |
| 81 | + if idx in indexes: |
| 82 | + continue |
| 83 | + if current is None: |
| 84 | + current = word |
| 85 | + indexes.append(idx) |
| 86 | + line = OcrLine() |
| 87 | + line.append(word) |
| 88 | + else: |
| 89 | + if self._are_words_on_same_line(current, word): |
| 90 | + line.append(word) |
| 91 | + indexes.append(idx) |
| 92 | + current = None |
| 93 | + if line: |
| 94 | + line.sort_on_x() |
| 95 | + lines.append(line) |
| 96 | + return lines |
| 97 | + |
| 98 | + @property |
| 99 | + def all_lines(self) -> List[OcrLine]: |
| 100 | + """All the words on the page, ordered in lines.""" |
| 101 | + if not self._lines: |
| 102 | + self._lines = self._to_lines() |
| 103 | + return self._lines |
| 104 | + |
| 105 | + def __str__(self) -> str: |
| 106 | + return "\n".join(str(line) for line in self.all_lines) + "\n" |
| 107 | + |
| 108 | + |
| 109 | +class MVisionV1: |
| 110 | + """Mindee Vision V1.""" |
| 111 | + |
| 112 | + pages: List[OcrPage] |
| 113 | + |
| 114 | + def __init__(self, prediction: TypeApiPrediction): |
| 115 | + self.pages = [ |
| 116 | + OcrPage(page_prediction) for page_prediction in prediction["pages"] |
| 117 | + ] |
| 118 | + |
| 119 | + def __str__(self) -> str: |
| 120 | + return "\n".join([str(page) for page in self.pages]) |
| 121 | + |
| 122 | + |
| 123 | +class Ocr: |
| 124 | + """OCR extraction from the entire document.""" |
| 125 | + |
| 126 | + mvision_v1: MVisionV1 |
| 127 | + |
| 128 | + def __init__(self, prediction: TypeApiPrediction): |
| 129 | + self.mvision_v1 = MVisionV1(prediction["mvision-v1"]) |
| 130 | + |
| 131 | + def __str__(self) -> str: |
| 132 | + return str(self.mvision_v1) |
0 commit comments