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
@@ -6,13 +6,18 @@
6
6
 
7
7
  import multiprocessing as mp
8
8
  import os
9
- from collections.abc import Callable, Iterable, Iterator
9
+ import threading
10
+ from collections.abc import Callable, Generator, Iterable, Iterator
11
+ from concurrent.futures import ThreadPoolExecutor
12
+ from itertools import islice
10
13
  from multiprocessing.pool import ThreadPool
11
14
  from typing import Any
12
15
 
16
+ import numpy as np
17
+
13
18
  from doctr.file_utils import ENV_VARS_TRUE_VALUES
14
19
 
15
- __all__ = ["multithread_exec"]
20
+ __all__ = ["multithread_exec", "chunked", "prefetch", "prefetch_chunks"]
16
21
 
17
22
 
18
23
  def multithread_exec(func: Callable[[Any], Any], seq: Iterable[Any], threads: int | None = None) -> Iterator[Any]:
@@ -36,13 +41,131 @@ def multithread_exec(func: Callable[[Any], Any], seq: Iterable[Any], threads: in
36
41
  you might want to disable multiprocessing. To achieve that, set 'DOCTR_MULTIPROCESSING_DISABLE' to 'TRUE'.
37
42
  """
38
43
  threads = threads if isinstance(threads, int) else min(16, mp.cpu_count())
44
+ items = seq if isinstance(seq, (list, tuple)) else list(seq)
45
+ # Never spawn more workers than items - single-item calls skip pool startup entirely
46
+ threads = min(threads, len(items))
39
47
  # Single-thread
40
48
  if threads < 2 or os.environ.get("DOCTR_MULTIPROCESSING_DISABLE", "").upper() in ENV_VARS_TRUE_VALUES:
41
- results = map(func, seq)
49
+ results = map(func, items)
42
50
  # Multi-threading
43
51
  else:
44
52
  with ThreadPool(threads) as tp:
45
53
  # ThreadPool's map function returns a list, but seq could be of a different type
46
54
  # That's why wrapping result in map to return iterator
47
- results = map(lambda x: x, tp.map(func, seq)) # noqa: C417
55
+ results = map(lambda x: x, tp.map(func, items)) # noqa: C417
48
56
  return results
57
+
58
+
59
+ def chunked(seq: Iterable[Any], size: int) -> Iterator[list[Any]]:
60
+ """Split an iterable into consecutive lists of at most `size` elements, consuming it lazily
61
+
62
+ >>> from doctr.utils.multithreading import chunked
63
+ >>> list(chunked(range(5), 2))
64
+ [[0, 1], [2, 3], [4]]
65
+
66
+ Args:
67
+ seq: iterable
68
+ size: the maximum number of elements per chunk
69
+
70
+ Returns:
71
+ iterator of chunks
72
+ """
73
+ if size < 1:
74
+ raise ValueError("the chunk size must be a positive integer")
75
+ iterator = iter(seq)
76
+ while chunk := list(islice(iterator, size)):
77
+ yield chunk
78
+
79
+
80
+ _EXHAUSTED = object()
81
+
82
+
83
+ def prefetch(seq: Iterable[Any], enabled: bool = True) -> Generator[Any, None, None]:
84
+ """Iterate over an iterable while producing its next element in a background thread
85
+
86
+ This overlaps the production of the next element (e.g. decoding the next pages of a document) with the
87
+ processing of the current one (e.g. running the models). At most one element is produced ahead, and the
88
+ iterable is only ever advanced by one thread at a time.
89
+
90
+ >>> from doctr.utils.multithreading import prefetch
91
+ >>> for item in prefetch(expensive_generator()):
92
+ ... process(item)
93
+
94
+ Args:
95
+ seq: iterable
96
+ enabled: whether the elements are produced in a background thread
97
+
98
+ Returns:
99
+ generator over the elements of the iterable: closing it waits for the element being produced, if any
100
+
101
+ Notes:
102
+ Prefetching is disabled when 'DOCTR_MULTIPROCESSING_DISABLE' is set to 'TRUE'.
103
+ """
104
+ if not enabled or os.environ.get("DOCTR_MULTIPROCESSING_DISABLE", "").upper() in ENV_VARS_TRUE_VALUES:
105
+ yield from seq
106
+ return
107
+
108
+ iterator = iter(seq)
109
+ try:
110
+ # On early exit, leaving the context waits for the element being produced, so that the iterable is
111
+ # never closed while it is advanced in the background thread
112
+ with ThreadPoolExecutor(max_workers=1, thread_name_prefix="doctr-prefetch") as executor:
113
+ future = executor.submit(next, iterator, _EXHAUSTED)
114
+ while (item := future.result()) is not _EXHAUSTED:
115
+ future = executor.submit(next, iterator, _EXHAUSTED)
116
+ yield item
117
+ finally:
118
+ close = getattr(iterator, "close", None)
119
+ if callable(close):
120
+ close()
121
+
122
+
123
+ def prefetch_chunks(seq: Iterable[Any], size: int, enabled: bool = True) -> Generator[list[Any], None, None]:
124
+ """Split an iterable into consecutive lists of at most `size` elements, producing the next chunk in a background
125
+ thread while the current one is processed
126
+
127
+ This is how the predictors consume their inputs: a lazy page source (or any iterator) has its next chunk decoded
128
+ ahead, while in-memory inputs (lists, tuples, arrays) have nothing to decode and are only sliced, without a
129
+ background thread.
130
+
131
+ >>> from doctr.utils.multithreading import prefetch_chunks
132
+ >>> for chunk in prefetch_chunks(lazy_source, 32):
133
+ ... process(chunk)
134
+
135
+ Args:
136
+ seq: iterable
137
+ size: the maximum number of elements per chunk
138
+ enabled: whether the next chunk is produced in a background thread (ignored for in-memory inputs)
139
+
140
+ Returns:
141
+ generator of chunks. Closing it (e.g. when the consumer stops early, or on error) stops producing ahead after
142
+ the element in progress rather than completing a whole chunk, and releases the resources held by the
143
+ iteration over `seq` (e.g. an open PDF).
144
+
145
+ Notes:
146
+ Prefetching is disabled when 'DOCTR_MULTIPROCESSING_DISABLE' is set to 'TRUE'.
147
+ """
148
+ in_memory = isinstance(seq, (list, tuple, np.ndarray))
149
+ stop = threading.Event()
150
+
151
+ def _until_stopped() -> Iterator[Any]:
152
+ iterator = iter(seq)
153
+ try:
154
+ for item in iterator:
155
+ yield item
156
+ if stop.is_set():
157
+ return
158
+ finally:
159
+ # Release the resources held by the iteration (e.g. an open PDF) as soon as it is over
160
+ close = getattr(iterator, "close", None)
161
+ if callable(close):
162
+ close()
163
+
164
+ chunks = prefetch(chunked(_until_stopped(), size), enabled=enabled and not in_memory)
165
+ try:
166
+ # Not `yield from`: on early exit, it would close `chunks` (waiting for a whole chunk) before `stop` is set
167
+ for chunk in chunks:
168
+ yield chunk
169
+ finally:
170
+ stop.set()
171
+ chunks.close()