Skip to content

Commit 50e4396

Browse files
committed
📝 add documentation for OCR extraction
1 parent 5d9db6b commit 50e4396

File tree

2 files changed

+41
-10
lines changed

2 files changed

+41
-10
lines changed

docs/predictions/standard/field_types.rst

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,3 +64,32 @@ Position
6464
.. autoclass:: mindee.fields.position.PositionField
6565
:members:
6666
:inherited-members:
67+
68+
OCR Extraction
69+
==============
70+
71+
OCR
72+
---
73+
.. autoclass:: mindee.fields.ocr.Ocr
74+
:members:
75+
76+
MVisionV1
77+
---------
78+
.. autoclass:: mindee.fields.ocr.MVisionV1
79+
:members:
80+
81+
OcrPage
82+
-------
83+
.. autoclass:: mindee.fields.ocr.OcrPage
84+
:members:
85+
86+
OcrLine
87+
-------
88+
.. autoclass:: mindee.fields.ocr.OcrLine
89+
:members:
90+
91+
OcrWord
92+
-------
93+
.. autoclass:: mindee.fields.ocr.OcrWord
94+
:members:
95+
:inherited-members:

mindee/fields/ocr.py

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,33 @@
11
from typing import List, Optional
22

33
from mindee.documents.base import TypeApiPrediction
4+
from mindee.fields.base import FieldPositionMixin
45
from mindee.geometry import (
5-
Polygon,
66
get_centroid,
77
get_min_max_x,
88
get_min_max_y,
99
is_point_in_polygon_y,
10-
polygon_from_prediction,
1110
)
1211

1312

14-
class Word:
13+
class OcrWord(FieldPositionMixin):
1514
"""A single word."""
1615

1716
confidence: float
18-
polygon: Polygon
17+
"""The confidence score."""
1918
text: str
19+
"""The extracted text."""
2020

2121
def __init__(self, prediction: TypeApiPrediction):
2222
self.confidence = prediction["confidence"]
23-
self.polygon = polygon_from_prediction(prediction["polygon"])
2423
self.text = prediction["text"]
24+
self._set_position(prediction)
2525

2626
def __str__(self) -> str:
2727
return self.text
2828

2929

30-
class OcrLine(List[Word]):
30+
class OcrLine(List[OcrWord]):
3131
"""A list of words which are on the same line."""
3232

3333
def sort_on_x(self) -> None:
@@ -41,18 +41,18 @@ def __str__(self) -> str:
4141
class OcrPage:
4242
"""OCR extraction for a single page."""
4343

44-
all_words: List[Word]
44+
all_words: List[OcrWord]
4545
"""All the words on the page, in semi-random order."""
4646
_lines: List[OcrLine]
4747

4848
def __init__(self, prediction: TypeApiPrediction):
4949
self.all_words = [
50-
Word(word_prediction) for word_prediction in prediction["all_words"]
50+
OcrWord(word_prediction) for word_prediction in prediction["all_words"]
5151
]
5252
self._lines = []
5353

5454
@staticmethod
55-
def _are_words_on_same_line(current_word: Word, next_word: Word) -> bool:
55+
def _are_words_on_same_line(current_word: OcrWord, next_word: OcrWord) -> bool:
5656
"""Determine if two words are on the same line."""
5757
current_in_next = is_point_in_polygon_y(
5858
get_centroid(current_word.polygon),
@@ -66,7 +66,7 @@ def _are_words_on_same_line(current_word: Word, next_word: Word) -> bool:
6666

6767
def _to_lines(self) -> List[OcrLine]:
6868
"""Order all the words on the page into lines."""
69-
current: Optional[Word] = None
69+
current: Optional[OcrWord] = None
7070
indexes: List[int] = []
7171
lines: List[OcrLine] = []
7272

@@ -110,6 +110,7 @@ class MVisionV1:
110110
"""Mindee Vision V1."""
111111

112112
pages: List[OcrPage]
113+
"""List of pages."""
113114

114115
def __init__(self, prediction: TypeApiPrediction):
115116
self.pages = [
@@ -124,6 +125,7 @@ class Ocr:
124125
"""OCR extraction from the entire document."""
125126

126127
mvision_v1: MVisionV1
128+
"""Mindee Vision v1 results."""
127129

128130
def __init__(self, prediction: TypeApiPrediction):
129131
self.mvision_v1 = MVisionV1(prediction["mvision-v1"])

0 commit comments

Comments
 (0)