python-doctr 1.0.1__py3-none-any.whl → 1.2.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- doctr/cli/__init__.py +0 -0
- doctr/cli/main.py +669 -0
- doctr/datasets/__init__.py +2 -0
- doctr/datasets/coco_text.py +4 -2
- doctr/datasets/cord.py +2 -1
- doctr/datasets/datasets/base.py +12 -24
- doctr/datasets/datasets/pytorch.py +14 -4
- doctr/datasets/detection.py +14 -3
- doctr/datasets/doc_artefacts.py +2 -1
- doctr/datasets/funsd.py +2 -1
- doctr/datasets/generator/base.py +121 -37
- doctr/datasets/generator/pytorch.py +5 -6
- doctr/datasets/ic03.py +9 -7
- doctr/datasets/ic13.py +4 -2
- doctr/datasets/iiit5k.py +2 -1
- doctr/datasets/iiithws.py +4 -2
- doctr/datasets/imgur5k.py +4 -2
- doctr/datasets/layout.py +129 -0
- doctr/datasets/mjsynth.py +4 -2
- doctr/datasets/ocr.py +2 -1
- doctr/datasets/orientation.py +2 -1
- doctr/datasets/recognition.py +5 -4
- doctr/datasets/sroie.py +2 -1
- doctr/datasets/svhn.py +2 -1
- doctr/datasets/svt.py +10 -8
- doctr/datasets/synthtext.py +2 -1
- doctr/datasets/table_structure.py +97 -0
- doctr/datasets/utils.py +104 -21
- doctr/datasets/vocabs.py +320 -148
- doctr/datasets/wildreceipt.py +4 -2
- doctr/file_utils.py +3 -1
- doctr/io/__init__.py +3 -0
- doctr/io/elements.py +381 -271
- doctr/io/exporters.py +1915 -0
- doctr/io/figures.py +313 -0
- doctr/io/image/base.py +54 -7
- doctr/io/image/pytorch.py +13 -5
- doctr/io/pdf.py +6 -5
- doctr/io/reader.py +268 -18
- doctr/io/sources.py +636 -0
- doctr/models/__init__.py +3 -0
- doctr/models/_utils.py +206 -13
- doctr/models/builder.py +590 -137
- doctr/models/classification/__init__.py +3 -0
- doctr/models/classification/hgnetv2/__init__.py +1 -0
- doctr/models/classification/hgnetv2/pytorch.py +352 -0
- doctr/models/classification/magc_resnet/pytorch.py +1 -1
- doctr/models/classification/mobilenet/pytorch.py +6 -6
- doctr/models/classification/predictor/pytorch.py +34 -2
- doctr/models/classification/resnet/pytorch.py +5 -5
- doctr/models/classification/starnet/__init__.py +1 -0
- doctr/models/classification/starnet/pytorch.py +202 -0
- doctr/models/classification/textnet/pytorch.py +6 -5
- doctr/models/classification/vgg/pytorch.py +3 -2
- doctr/models/classification/vip/layers/pytorch.py +4 -4
- doctr/models/classification/vip/pytorch.py +4 -4
- doctr/models/classification/vit/pytorch.py +3 -3
- doctr/models/classification/vit_det/__init__.py +1 -0
- doctr/models/classification/vit_det/layers/__init__.py +1 -0
- doctr/models/classification/vit_det/layers/pytorch.py +215 -0
- doctr/models/classification/vit_det/pytorch.py +346 -0
- doctr/models/classification/zoo.py +14 -2
- doctr/models/detection/_utils/base.py +23 -7
- doctr/models/detection/core.py +32 -18
- doctr/models/detection/differentiable_binarization/base.py +23 -9
- doctr/models/detection/differentiable_binarization/pytorch.py +10 -4
- doctr/models/detection/fast/base.py +22 -6
- doctr/models/detection/fast/pytorch.py +24 -11
- doctr/models/detection/linknet/base.py +22 -6
- doctr/models/detection/linknet/pytorch.py +10 -3
- doctr/models/detection/predictor/pytorch.py +43 -2
- doctr/models/detection/zoo.py +6 -2
- doctr/models/factory/hub.py +59 -41
- doctr/models/formula/__init__.py +2 -0
- doctr/models/formula/formulanet/__init__.py +2 -0
- doctr/models/formula/formulanet/base.py +70 -0
- doctr/models/formula/formulanet/pytorch.py +587 -0
- doctr/models/formula/predictor/__init__.py +1 -0
- doctr/models/formula/predictor/pytorch.py +132 -0
- doctr/models/formula/zoo.py +102 -0
- doctr/models/kie_predictor/base.py +23 -1
- doctr/models/kie_predictor/pytorch.py +87 -15
- doctr/models/layout/__init__.py +2 -0
- doctr/models/layout/lw_detr/__init__.py +1 -0
- doctr/models/layout/lw_detr/base.py +309 -0
- doctr/models/layout/lw_detr/layers/__init__.py +1 -0
- doctr/models/layout/lw_detr/layers/pytorch.py +785 -0
- doctr/models/layout/lw_detr/pytorch.py +895 -0
- doctr/models/layout/predictor/__init__.py +1 -0
- doctr/models/layout/predictor/pytorch.py +114 -0
- doctr/models/layout/zoo.py +94 -0
- doctr/models/modules/layers/pytorch.py +139 -3
- doctr/models/modules/transformer/pytorch.py +151 -31
- doctr/models/predictor/base.py +415 -17
- doctr/models/predictor/pytorch.py +109 -12
- doctr/models/preprocessor/pytorch.py +82 -25
- doctr/models/reading_order/__init__.py +1 -0
- doctr/models/reading_order/base.py +1127 -0
- doctr/models/recognition/core.py +14 -1
- doctr/models/recognition/crnn/pytorch.py +40 -16
- doctr/models/recognition/master/base.py +4 -1
- doctr/models/recognition/master/pytorch.py +84 -22
- doctr/models/recognition/parseq/base.py +4 -1
- doctr/models/recognition/parseq/pytorch.py +126 -25
- doctr/models/recognition/predictor/_utils.py +131 -14
- doctr/models/recognition/predictor/pytorch.py +68 -12
- doctr/models/recognition/sar/pytorch.py +21 -7
- doctr/models/recognition/utils.py +80 -50
- doctr/models/recognition/viptr/pytorch.py +38 -14
- doctr/models/recognition/vitstr/base.py +4 -1
- doctr/models/recognition/vitstr/pytorch.py +11 -7
- doctr/models/recognition/zoo.py +36 -9
- doctr/models/table_structure/__init__.py +2 -0
- doctr/models/table_structure/grid.py +430 -0
- doctr/models/table_structure/predictor/__init__.py +1 -0
- doctr/models/table_structure/predictor/pytorch.py +231 -0
- doctr/models/table_structure/tablecenternet/__init__.py +2 -0
- doctr/models/table_structure/tablecenternet/base.py +675 -0
- doctr/models/table_structure/tablecenternet/pytorch.py +525 -0
- doctr/models/table_structure/zoo.py +87 -0
- doctr/models/utils/pytorch.py +381 -25
- doctr/models/zoo.py +141 -4
- doctr/transforms/functional/base.py +6 -0
- doctr/transforms/functional/pytorch.py +134 -6
- doctr/transforms/modules/base.py +261 -82
- doctr/transforms/modules/pytorch.py +396 -76
- doctr/utils/common_types.py +21 -1
- doctr/utils/data.py +24 -11
- doctr/utils/fonts.py +64 -17
- doctr/utils/geometry.py +167 -28
- doctr/utils/metrics.py +481 -19
- doctr/utils/multithreading.py +127 -4
- doctr/utils/reconstitution.py +696 -132
- doctr/utils/visualization.py +73 -10
- doctr/version.py +1 -1
- {python_doctr-1.0.1.dist-info → python_doctr-1.2.0.dist-info}/METADATA +119 -249
- python_doctr-1.2.0.dist-info/RECORD +191 -0
- {python_doctr-1.0.1.dist-info → python_doctr-1.2.0.dist-info}/WHEEL +1 -1
- python_doctr-1.2.0.dist-info/entry_points.txt +2 -0
- python_doctr-1.0.1.dist-info/RECORD +0 -149
- {python_doctr-1.0.1.dist-info → python_doctr-1.2.0.dist-info}/licenses/LICENSE +0 -0
- {python_doctr-1.0.1.dist-info → python_doctr-1.2.0.dist-info}/top_level.txt +0 -0
- {python_doctr-1.0.1.dist-info → python_doctr-1.2.0.dist-info}/zip-safe +0 -0
doctr/utils/visualization.py
CHANGED
|
@@ -42,7 +42,7 @@ def rect_patch(
|
|
|
42
42
|
Returns:
|
|
43
43
|
a rectangular Patch
|
|
44
44
|
"""
|
|
45
|
-
if len(geometry) != 2 or any(not isinstance(elt, tuple) or len(elt) != 2 for elt in geometry):
|
|
45
|
+
if len(geometry) != 2 or any(not isinstance(elt, (tuple, list)) or len(elt) != 2 for elt in geometry):
|
|
46
46
|
raise ValueError("invalid geometry format")
|
|
47
47
|
|
|
48
48
|
# Unpack
|
|
@@ -96,11 +96,13 @@ def polygon_patch(
|
|
|
96
96
|
|
|
97
97
|
# Unpack
|
|
98
98
|
height, width = page_dimensions
|
|
99
|
-
|
|
100
|
-
|
|
99
|
+
if preserve_aspect_ratio:
|
|
100
|
+
width = height = max(height, width)
|
|
101
|
+
# Scale a copy, so that the caller's geometry (e.g. the one of a word) stays relative
|
|
102
|
+
abs_geometry = geometry * (width, height)
|
|
101
103
|
|
|
102
104
|
return patches.Polygon(
|
|
103
|
-
|
|
105
|
+
abs_geometry,
|
|
104
106
|
fill=fill,
|
|
105
107
|
linewidth=linewidth,
|
|
106
108
|
edgecolor=(*color, alpha),
|
|
@@ -124,7 +126,8 @@ def create_obj_patch(
|
|
|
124
126
|
Returns:
|
|
125
127
|
a matplotlib Patch
|
|
126
128
|
"""
|
|
127
|
-
|
|
129
|
+
# A page export read back from a JSON file stores the geometries as lists instead of tuples
|
|
130
|
+
if isinstance(geometry, (tuple, list)):
|
|
128
131
|
if len(geometry) == 2: # straight word BB (2 pts)
|
|
129
132
|
return rect_patch(geometry, page_dimensions, **kwargs)
|
|
130
133
|
elif len(geometry) == 4: # rotated word BB (4 pts)
|
|
@@ -144,11 +147,12 @@ def get_colors(num_colors: int) -> list[tuple[float, float, float]]:
|
|
|
144
147
|
colors: list of generated colors
|
|
145
148
|
"""
|
|
146
149
|
colors = []
|
|
147
|
-
|
|
148
|
-
|
|
150
|
+
# Integer steps give exactly num_colors hues (a float step can add one more, close to 360°, i.e. red again)
|
|
151
|
+
for i in range(num_colors):
|
|
152
|
+
hue = i / num_colors
|
|
149
153
|
lightness = (50 + np.random.rand() * 10) / 100.0
|
|
150
154
|
saturation = (90 + np.random.rand() * 10) / 100.0
|
|
151
|
-
colors.append(colorsys.hls_to_rgb(hue, lightness, saturation))
|
|
155
|
+
colors.append(colorsys.hls_to_rgb(hue, lightness, saturation))
|
|
152
156
|
return colors
|
|
153
157
|
|
|
154
158
|
|
|
@@ -157,6 +161,7 @@ def visualize_page(
|
|
|
157
161
|
image: np.ndarray,
|
|
158
162
|
words_only: bool = True,
|
|
159
163
|
display_artefacts: bool = True,
|
|
164
|
+
display_layout: bool = True,
|
|
160
165
|
scale: float = 10,
|
|
161
166
|
interactive: bool = True,
|
|
162
167
|
add_labels: bool = True,
|
|
@@ -179,6 +184,7 @@ def visualize_page(
|
|
|
179
184
|
image: np array of the page, needs to have the same shape than page['dimensions']
|
|
180
185
|
words_only: whether only words should be displayed
|
|
181
186
|
display_artefacts: whether artefacts should be displayed
|
|
187
|
+
display_layout: whether detected layout regions should be displayed
|
|
182
188
|
scale: figsize of the largest windows side
|
|
183
189
|
interactive: whether the plot should be interactive
|
|
184
190
|
add_labels: for static plot, adds text labels on top of bounding box
|
|
@@ -199,6 +205,33 @@ def visualize_page(
|
|
|
199
205
|
if interactive:
|
|
200
206
|
artists: list[patches.Patch] = [] # instantiate an empty list of patches (to be drawn on the page)
|
|
201
207
|
|
|
208
|
+
# Draw layout regions first so text boxes are overlaid on top of them
|
|
209
|
+
if display_layout and page.get("layout"):
|
|
210
|
+
region_classes = sorted({region["type"] for region in page["layout"]})
|
|
211
|
+
layout_colors = {cls: color for color, cls in zip(get_colors(max(len(region_classes), 1)), region_classes)}
|
|
212
|
+
for region in page["layout"]:
|
|
213
|
+
rect = create_obj_patch(
|
|
214
|
+
region["geometry"],
|
|
215
|
+
page["dimensions"],
|
|
216
|
+
label=f"{region['type']} (confidence: {region['confidence']:.2%})",
|
|
217
|
+
color=layout_colors[region["type"]],
|
|
218
|
+
linewidth=2,
|
|
219
|
+
fill=False,
|
|
220
|
+
**kwargs,
|
|
221
|
+
)
|
|
222
|
+
ax.add_patch(rect)
|
|
223
|
+
if interactive:
|
|
224
|
+
artists.append(rect)
|
|
225
|
+
elif add_labels and len(region["geometry"]) == 2:
|
|
226
|
+
ax.text(
|
|
227
|
+
int(page["dimensions"][1] * region["geometry"][0][0]),
|
|
228
|
+
int(page["dimensions"][0] * region["geometry"][0][1]),
|
|
229
|
+
region["type"],
|
|
230
|
+
size=9,
|
|
231
|
+
alpha=0.7,
|
|
232
|
+
color=layout_colors[region["type"]],
|
|
233
|
+
)
|
|
234
|
+
|
|
202
235
|
for block in page["blocks"]:
|
|
203
236
|
if not words_only:
|
|
204
237
|
rect = create_obj_patch(
|
|
@@ -281,6 +314,7 @@ def visualize_kie_page(
|
|
|
281
314
|
image: np.ndarray,
|
|
282
315
|
words_only: bool = False,
|
|
283
316
|
display_artefacts: bool = True,
|
|
317
|
+
display_layout: bool = True,
|
|
284
318
|
scale: float = 10,
|
|
285
319
|
interactive: bool = True,
|
|
286
320
|
add_labels: bool = True,
|
|
@@ -303,6 +337,7 @@ def visualize_kie_page(
|
|
|
303
337
|
image: np array of the page, needs to have the same shape than page['dimensions']
|
|
304
338
|
words_only: whether only words should be displayed
|
|
305
339
|
display_artefacts: whether artefacts should be displayed
|
|
340
|
+
display_layout: whether detected layout regions should be displayed
|
|
306
341
|
scale: figsize of the largest windows side
|
|
307
342
|
interactive: whether the plot should be interactive
|
|
308
343
|
add_labels: for static plot, adds text labels on top of bounding box
|
|
@@ -323,6 +358,23 @@ def visualize_kie_page(
|
|
|
323
358
|
if interactive:
|
|
324
359
|
artists: list[patches.Patch] = [] # instantiate an empty list of patches (to be drawn on the page)
|
|
325
360
|
|
|
361
|
+
if display_layout and page.get("layout"):
|
|
362
|
+
region_classes = sorted({region["type"] for region in page["layout"]})
|
|
363
|
+
layout_colors = {cls: color for color, cls in zip(get_colors(max(len(region_classes), 1)), region_classes)}
|
|
364
|
+
for region in page["layout"]:
|
|
365
|
+
rect = create_obj_patch(
|
|
366
|
+
region["geometry"],
|
|
367
|
+
page["dimensions"],
|
|
368
|
+
label=f"{region['type']} (confidence: {region['confidence']:.2%})",
|
|
369
|
+
color=layout_colors[region["type"]],
|
|
370
|
+
linewidth=2,
|
|
371
|
+
fill=False,
|
|
372
|
+
**kwargs,
|
|
373
|
+
)
|
|
374
|
+
ax.add_patch(rect)
|
|
375
|
+
if interactive:
|
|
376
|
+
artists.append(rect)
|
|
377
|
+
|
|
326
378
|
colors = {k: color for color, k in zip(get_colors(len(page["predictions"])), page["predictions"])}
|
|
327
379
|
for key, value in page["predictions"].items():
|
|
328
380
|
for prediction in value:
|
|
@@ -340,6 +392,16 @@ def visualize_kie_page(
|
|
|
340
392
|
if interactive:
|
|
341
393
|
# add patch to cursor's artists
|
|
342
394
|
artists.append(rect)
|
|
395
|
+
elif add_labels and len(prediction["geometry"]) == 2:
|
|
396
|
+
# We draw only if boxes are in straight format
|
|
397
|
+
ax.text(
|
|
398
|
+
int(page["dimensions"][1] * prediction["geometry"][0][0]),
|
|
399
|
+
int(page["dimensions"][0] * prediction["geometry"][0][1]),
|
|
400
|
+
prediction["value"],
|
|
401
|
+
size=10,
|
|
402
|
+
alpha=0.5,
|
|
403
|
+
color=colors[key],
|
|
404
|
+
)
|
|
343
405
|
|
|
344
406
|
if interactive:
|
|
345
407
|
import mplcursors
|
|
@@ -352,13 +414,14 @@ def visualize_kie_page(
|
|
|
352
414
|
|
|
353
415
|
|
|
354
416
|
def draw_boxes(boxes: np.ndarray, image: np.ndarray, color: tuple[int, int, int] | None = None, **kwargs) -> None:
|
|
355
|
-
"""Draw an array of relative straight boxes on an image
|
|
417
|
+
"""Draw an array of relative straight boxes on an image.
|
|
356
418
|
|
|
357
419
|
Args:
|
|
358
|
-
boxes: array of relative boxes, of shape (*, 4)
|
|
420
|
+
boxes: array of relative boxes, of shape `(*, 4)`
|
|
359
421
|
image: np array, float32 or uint8
|
|
360
422
|
color: color to use for bounding box edges
|
|
361
423
|
**kwargs: keyword arguments from `matplotlib.pyplot.plot`
|
|
424
|
+
|
|
362
425
|
"""
|
|
363
426
|
h, w = image.shape[:2]
|
|
364
427
|
# Convert boxes to absolute coords
|
doctr/version.py
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
__version__ = '
|
|
1
|
+
__version__ = '1.2.0'
|