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.
Files changed (143) hide show
  1. doctr/cli/__init__.py +0 -0
  2. doctr/cli/main.py +669 -0
  3. doctr/datasets/__init__.py +2 -0
  4. doctr/datasets/coco_text.py +4 -2
  5. doctr/datasets/cord.py +2 -1
  6. doctr/datasets/datasets/base.py +12 -24
  7. doctr/datasets/datasets/pytorch.py +14 -4
  8. doctr/datasets/detection.py +14 -3
  9. doctr/datasets/doc_artefacts.py +2 -1
  10. doctr/datasets/funsd.py +2 -1
  11. doctr/datasets/generator/base.py +121 -37
  12. doctr/datasets/generator/pytorch.py +5 -6
  13. doctr/datasets/ic03.py +9 -7
  14. doctr/datasets/ic13.py +4 -2
  15. doctr/datasets/iiit5k.py +2 -1
  16. doctr/datasets/iiithws.py +4 -2
  17. doctr/datasets/imgur5k.py +4 -2
  18. doctr/datasets/layout.py +129 -0
  19. doctr/datasets/mjsynth.py +4 -2
  20. doctr/datasets/ocr.py +2 -1
  21. doctr/datasets/orientation.py +2 -1
  22. doctr/datasets/recognition.py +5 -4
  23. doctr/datasets/sroie.py +2 -1
  24. doctr/datasets/svhn.py +2 -1
  25. doctr/datasets/svt.py +10 -8
  26. doctr/datasets/synthtext.py +2 -1
  27. doctr/datasets/table_structure.py +97 -0
  28. doctr/datasets/utils.py +104 -21
  29. doctr/datasets/vocabs.py +320 -148
  30. doctr/datasets/wildreceipt.py +4 -2
  31. doctr/file_utils.py +3 -1
  32. doctr/io/__init__.py +3 -0
  33. doctr/io/elements.py +381 -271
  34. doctr/io/exporters.py +1915 -0
  35. doctr/io/figures.py +313 -0
  36. doctr/io/image/base.py +54 -7
  37. doctr/io/image/pytorch.py +13 -5
  38. doctr/io/pdf.py +6 -5
  39. doctr/io/reader.py +268 -18
  40. doctr/io/sources.py +636 -0
  41. doctr/models/__init__.py +3 -0
  42. doctr/models/_utils.py +206 -13
  43. doctr/models/builder.py +590 -137
  44. doctr/models/classification/__init__.py +3 -0
  45. doctr/models/classification/hgnetv2/__init__.py +1 -0
  46. doctr/models/classification/hgnetv2/pytorch.py +352 -0
  47. doctr/models/classification/magc_resnet/pytorch.py +1 -1
  48. doctr/models/classification/mobilenet/pytorch.py +6 -6
  49. doctr/models/classification/predictor/pytorch.py +34 -2
  50. doctr/models/classification/resnet/pytorch.py +5 -5
  51. doctr/models/classification/starnet/__init__.py +1 -0
  52. doctr/models/classification/starnet/pytorch.py +202 -0
  53. doctr/models/classification/textnet/pytorch.py +6 -5
  54. doctr/models/classification/vgg/pytorch.py +3 -2
  55. doctr/models/classification/vip/layers/pytorch.py +4 -4
  56. doctr/models/classification/vip/pytorch.py +4 -4
  57. doctr/models/classification/vit/pytorch.py +3 -3
  58. doctr/models/classification/vit_det/__init__.py +1 -0
  59. doctr/models/classification/vit_det/layers/__init__.py +1 -0
  60. doctr/models/classification/vit_det/layers/pytorch.py +215 -0
  61. doctr/models/classification/vit_det/pytorch.py +346 -0
  62. doctr/models/classification/zoo.py +14 -2
  63. doctr/models/detection/_utils/base.py +23 -7
  64. doctr/models/detection/core.py +32 -18
  65. doctr/models/detection/differentiable_binarization/base.py +23 -9
  66. doctr/models/detection/differentiable_binarization/pytorch.py +10 -4
  67. doctr/models/detection/fast/base.py +22 -6
  68. doctr/models/detection/fast/pytorch.py +24 -11
  69. doctr/models/detection/linknet/base.py +22 -6
  70. doctr/models/detection/linknet/pytorch.py +10 -3
  71. doctr/models/detection/predictor/pytorch.py +43 -2
  72. doctr/models/detection/zoo.py +6 -2
  73. doctr/models/factory/hub.py +59 -41
  74. doctr/models/formula/__init__.py +2 -0
  75. doctr/models/formula/formulanet/__init__.py +2 -0
  76. doctr/models/formula/formulanet/base.py +70 -0
  77. doctr/models/formula/formulanet/pytorch.py +587 -0
  78. doctr/models/formula/predictor/__init__.py +1 -0
  79. doctr/models/formula/predictor/pytorch.py +132 -0
  80. doctr/models/formula/zoo.py +102 -0
  81. doctr/models/kie_predictor/base.py +23 -1
  82. doctr/models/kie_predictor/pytorch.py +87 -15
  83. doctr/models/layout/__init__.py +2 -0
  84. doctr/models/layout/lw_detr/__init__.py +1 -0
  85. doctr/models/layout/lw_detr/base.py +309 -0
  86. doctr/models/layout/lw_detr/layers/__init__.py +1 -0
  87. doctr/models/layout/lw_detr/layers/pytorch.py +785 -0
  88. doctr/models/layout/lw_detr/pytorch.py +895 -0
  89. doctr/models/layout/predictor/__init__.py +1 -0
  90. doctr/models/layout/predictor/pytorch.py +114 -0
  91. doctr/models/layout/zoo.py +94 -0
  92. doctr/models/modules/layers/pytorch.py +139 -3
  93. doctr/models/modules/transformer/pytorch.py +151 -31
  94. doctr/models/predictor/base.py +415 -17
  95. doctr/models/predictor/pytorch.py +109 -12
  96. doctr/models/preprocessor/pytorch.py +82 -25
  97. doctr/models/reading_order/__init__.py +1 -0
  98. doctr/models/reading_order/base.py +1127 -0
  99. doctr/models/recognition/core.py +14 -1
  100. doctr/models/recognition/crnn/pytorch.py +40 -16
  101. doctr/models/recognition/master/base.py +4 -1
  102. doctr/models/recognition/master/pytorch.py +84 -22
  103. doctr/models/recognition/parseq/base.py +4 -1
  104. doctr/models/recognition/parseq/pytorch.py +126 -25
  105. doctr/models/recognition/predictor/_utils.py +131 -14
  106. doctr/models/recognition/predictor/pytorch.py +68 -12
  107. doctr/models/recognition/sar/pytorch.py +21 -7
  108. doctr/models/recognition/utils.py +80 -50
  109. doctr/models/recognition/viptr/pytorch.py +38 -14
  110. doctr/models/recognition/vitstr/base.py +4 -1
  111. doctr/models/recognition/vitstr/pytorch.py +11 -7
  112. doctr/models/recognition/zoo.py +36 -9
  113. doctr/models/table_structure/__init__.py +2 -0
  114. doctr/models/table_structure/grid.py +430 -0
  115. doctr/models/table_structure/predictor/__init__.py +1 -0
  116. doctr/models/table_structure/predictor/pytorch.py +231 -0
  117. doctr/models/table_structure/tablecenternet/__init__.py +2 -0
  118. doctr/models/table_structure/tablecenternet/base.py +675 -0
  119. doctr/models/table_structure/tablecenternet/pytorch.py +525 -0
  120. doctr/models/table_structure/zoo.py +87 -0
  121. doctr/models/utils/pytorch.py +381 -25
  122. doctr/models/zoo.py +141 -4
  123. doctr/transforms/functional/base.py +6 -0
  124. doctr/transforms/functional/pytorch.py +134 -6
  125. doctr/transforms/modules/base.py +261 -82
  126. doctr/transforms/modules/pytorch.py +396 -76
  127. doctr/utils/common_types.py +21 -1
  128. doctr/utils/data.py +24 -11
  129. doctr/utils/fonts.py +64 -17
  130. doctr/utils/geometry.py +167 -28
  131. doctr/utils/metrics.py +481 -19
  132. doctr/utils/multithreading.py +127 -4
  133. doctr/utils/reconstitution.py +696 -132
  134. doctr/utils/visualization.py +73 -10
  135. doctr/version.py +1 -1
  136. {python_doctr-1.0.1.dist-info → python_doctr-1.2.0.dist-info}/METADATA +119 -249
  137. python_doctr-1.2.0.dist-info/RECORD +191 -0
  138. {python_doctr-1.0.1.dist-info → python_doctr-1.2.0.dist-info}/WHEEL +1 -1
  139. python_doctr-1.2.0.dist-info/entry_points.txt +2 -0
  140. python_doctr-1.0.1.dist-info/RECORD +0 -149
  141. {python_doctr-1.0.1.dist-info → python_doctr-1.2.0.dist-info}/licenses/LICENSE +0 -0
  142. {python_doctr-1.0.1.dist-info → python_doctr-1.2.0.dist-info}/top_level.txt +0 -0
  143. {python_doctr-1.0.1.dist-info → python_doctr-1.2.0.dist-info}/zip-safe +0 -0
@@ -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
- geometry[:, 0] = geometry[:, 0] * (max(width, height) if preserve_aspect_ratio else width)
100
- geometry[:, 1] = geometry[:, 1] * (max(width, height) if preserve_aspect_ratio else height)
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
- geometry,
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
- if isinstance(geometry, tuple):
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
- for i in np.arange(0.0, 360.0, 360.0 / num_colors):
148
- hue = i / 360.0
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)) # type: ignore[arg-type]
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__ = 'v1.0.1'
1
+ __version__ = '1.2.0'