python-doctr 1.1.0__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/main.py +186 -23
- doctr/datasets/detection.py +12 -2
- doctr/datasets/generator/base.py +118 -35
- doctr/datasets/generator/pytorch.py +1 -1
- doctr/datasets/ic03.py +6 -5
- doctr/datasets/layout.py +7 -1
- doctr/datasets/recognition.py +3 -3
- doctr/datasets/svt.py +7 -6
- doctr/datasets/utils.py +100 -26
- doctr/datasets/vocabs.py +320 -150
- doctr/io/__init__.py +2 -0
- doctr/io/elements.py +138 -20
- doctr/io/exporters.py +848 -208
- doctr/io/figures.py +313 -0
- doctr/io/image/base.py +54 -8
- doctr/io/image/pytorch.py +14 -8
- doctr/io/pdf.py +6 -5
- doctr/io/reader.py +268 -18
- doctr/io/sources.py +636 -0
- doctr/models/__init__.py +1 -0
- doctr/models/_utils.py +150 -5
- doctr/models/builder.py +316 -334
- doctr/models/classification/__init__.py +1 -0
- doctr/models/classification/hgnetv2/__init__.py +1 -0
- doctr/models/classification/hgnetv2/pytorch.py +352 -0
- doctr/models/classification/predictor/pytorch.py +28 -0
- doctr/models/classification/zoo.py +11 -2
- doctr/models/detection/core.py +1 -2
- doctr/models/detection/differentiable_binarization/base.py +17 -5
- doctr/models/detection/differentiable_binarization/pytorch.py +7 -1
- doctr/models/detection/fast/base.py +17 -5
- doctr/models/detection/fast/pytorch.py +21 -8
- doctr/models/detection/linknet/base.py +17 -5
- doctr/models/detection/linknet/pytorch.py +7 -0
- doctr/models/detection/predictor/pytorch.py +40 -0
- doctr/models/detection/zoo.py +4 -0
- doctr/models/factory/hub.py +5 -2
- 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 +7 -0
- doctr/models/kie_predictor/pytorch.py +47 -6
- doctr/models/layout/lw_detr/pytorch.py +150 -93
- doctr/models/layout/predictor/pytorch.py +32 -0
- doctr/models/layout/zoo.py +4 -0
- doctr/models/modules/transformer/pytorch.py +151 -31
- doctr/models/predictor/base.py +283 -7
- doctr/models/predictor/pytorch.py +53 -98
- doctr/models/preprocessor/pytorch.py +13 -11
- doctr/models/reading_order/base.py +720 -260
- doctr/models/recognition/core.py +14 -1
- doctr/models/recognition/crnn/pytorch.py +37 -11
- doctr/models/recognition/master/base.py +4 -1
- doctr/models/recognition/master/pytorch.py +83 -20
- doctr/models/recognition/parseq/base.py +4 -1
- doctr/models/recognition/parseq/pytorch.py +123 -22
- doctr/models/recognition/predictor/_utils.py +131 -14
- doctr/models/recognition/predictor/pytorch.py +67 -11
- doctr/models/recognition/sar/pytorch.py +20 -6
- doctr/models/recognition/utils.py +75 -43
- doctr/models/recognition/viptr/pytorch.py +37 -11
- doctr/models/recognition/vitstr/base.py +4 -1
- doctr/models/recognition/vitstr/pytorch.py +7 -3
- doctr/models/recognition/zoo.py +31 -3
- doctr/models/table_structure/grid.py +430 -0
- doctr/models/table_structure/predictor/pytorch.py +152 -1
- doctr/models/table_structure/tablecenternet/base.py +206 -16
- doctr/models/table_structure/tablecenternet/pytorch.py +8 -5
- doctr/models/table_structure/zoo.py +5 -0
- doctr/models/utils/pytorch.py +73 -16
- doctr/models/zoo.py +53 -4
- doctr/transforms/functional/base.py +6 -0
- doctr/transforms/functional/pytorch.py +134 -6
- doctr/transforms/modules/base.py +17 -3
- doctr/transforms/modules/pytorch.py +185 -4
- doctr/utils/data.py +20 -9
- doctr/utils/fonts.py +17 -9
- doctr/utils/geometry.py +36 -14
- doctr/utils/metrics.py +143 -13
- doctr/utils/multithreading.py +122 -2
- doctr/utils/visualization.py +11 -7
- doctr/version.py +1 -1
- {python_doctr-1.1.0.dist-info → python_doctr-1.2.0.dist-info}/METADATA +54 -224
- {python_doctr-1.1.0.dist-info → python_doctr-1.2.0.dist-info}/RECORD +93 -81
- {python_doctr-1.1.0.dist-info → python_doctr-1.2.0.dist-info}/WHEEL +1 -1
- {python_doctr-1.1.0.dist-info → python_doctr-1.2.0.dist-info}/entry_points.txt +0 -0
- {python_doctr-1.1.0.dist-info → python_doctr-1.2.0.dist-info}/licenses/LICENSE +0 -0
- {python_doctr-1.1.0.dist-info → python_doctr-1.2.0.dist-info}/top_level.txt +0 -0
- {python_doctr-1.1.0.dist-info → python_doctr-1.2.0.dist-info}/zip-safe +0 -0
doctr/io/reader.py
CHANGED
|
@@ -3,8 +3,10 @@
|
|
|
3
3
|
# This program is licensed under the Apache License 2.0.
|
|
4
4
|
# See LICENSE or go to <https://opensource.org/licenses/Apache-2.0> for full license details.
|
|
5
5
|
|
|
6
|
-
|
|
6
|
+
import logging
|
|
7
|
+
from collections.abc import Callable, Sequence
|
|
7
8
|
from pathlib import Path
|
|
9
|
+
from typing import Any, Literal, TypeAlias, overload
|
|
8
10
|
|
|
9
11
|
import numpy as np
|
|
10
12
|
|
|
@@ -12,33 +14,117 @@ from doctr.file_utils import requires_package
|
|
|
12
14
|
from doctr.utils.common_types import AbstractFile
|
|
13
15
|
|
|
14
16
|
from .html import read_html
|
|
15
|
-
from .
|
|
16
|
-
|
|
17
|
+
from .sources import (
|
|
18
|
+
_PAGE_ERRORS,
|
|
19
|
+
IMAGE_EXTENSIONS,
|
|
20
|
+
ConcatSource,
|
|
21
|
+
ImageSource,
|
|
22
|
+
PageDecodeError,
|
|
23
|
+
PageSource,
|
|
24
|
+
PDFSource,
|
|
25
|
+
_file_name,
|
|
26
|
+
_is_pdf,
|
|
27
|
+
_list_files,
|
|
28
|
+
_UnreadablePage,
|
|
29
|
+
)
|
|
17
30
|
|
|
18
31
|
__all__ = ["DocumentFile"]
|
|
19
32
|
|
|
33
|
+
logger = logging.getLogger(__name__)
|
|
34
|
+
|
|
35
|
+
# What to do with a file that cannot be read (missing, not an image, a corrupted PDF...): raise the error, skip it
|
|
36
|
+
# with a warning, or skip it and call a function with the file and the error
|
|
37
|
+
OnFileError: TypeAlias = Literal["raise", "skip"] | Callable[[AbstractFile, Exception], Any]
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
def _skip_file(on_error: OnFileError, file: AbstractFile, error: Exception) -> None:
|
|
41
|
+
"""Report a file that cannot be read, and is left out of the document"""
|
|
42
|
+
if callable(on_error):
|
|
43
|
+
on_error(file, error)
|
|
44
|
+
else:
|
|
45
|
+
logger.warning(f"Skipping {_file_name(file)}, which cannot be read: {error}")
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
def _file_at(doc: ConcatSource, origins: list[AbstractFile | None], idx: int) -> AbstractFile:
|
|
49
|
+
"""The input of a page of a concatenation built by `DocumentFile.from_files`
|
|
50
|
+
|
|
51
|
+
Args:
|
|
52
|
+
doc: the concatenation
|
|
53
|
+
origins: the input of each source of `doc` (a path, bytes or a URL), or None for a source of images
|
|
54
|
+
idx: the page index in the concatenation
|
|
55
|
+
|
|
56
|
+
Returns:
|
|
57
|
+
the input the page comes from, e.g. the URL of a rendered web page rather than its PDF content
|
|
58
|
+
"""
|
|
59
|
+
source_idx, page_idx = doc.locate(idx)
|
|
60
|
+
source = doc.sources[source_idx]
|
|
61
|
+
return source.files[page_idx] if isinstance(source, ImageSource) else origins[source_idx] # type: ignore[return-value]
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
def _materialize(source: PageSource) -> list[np.ndarray]:
|
|
65
|
+
"""Decode every page of a source into memory"""
|
|
66
|
+
try:
|
|
67
|
+
return list(source)
|
|
68
|
+
finally:
|
|
69
|
+
source.close()
|
|
70
|
+
|
|
20
71
|
|
|
21
72
|
class DocumentFile:
|
|
22
|
-
"""Read a document from multiple extensions
|
|
73
|
+
"""Read a document from multiple extensions
|
|
74
|
+
|
|
75
|
+
Every reader decodes all the pages into a list of numpy arrays by default. Pass `lazy=True` to get a
|
|
76
|
+
:class:`~doctr.io.PageSource` instead: it behaves like that list, but a page is only decoded when it is
|
|
77
|
+
accessed. Predictors process page sources chunk by chunk, so that very long documents (1000+ pages) or
|
|
78
|
+
huge collections of images can be passed in a single call with a bounded memory footprint.
|
|
79
|
+
"""
|
|
80
|
+
|
|
81
|
+
@overload
|
|
82
|
+
@classmethod
|
|
83
|
+
def from_pdf(cls, file: AbstractFile, lazy: Literal[False] = ..., **kwargs: Any) -> list[np.ndarray]: ...
|
|
23
84
|
|
|
85
|
+
@overload
|
|
24
86
|
@classmethod
|
|
25
|
-
def from_pdf(cls, file: AbstractFile, **kwargs) ->
|
|
87
|
+
def from_pdf(cls, file: AbstractFile, lazy: Literal[True], **kwargs: Any) -> PDFSource: ...
|
|
88
|
+
|
|
89
|
+
@overload
|
|
90
|
+
@classmethod
|
|
91
|
+
def from_pdf(cls, file: AbstractFile, lazy: bool, **kwargs: Any) -> list[np.ndarray] | PDFSource: ...
|
|
92
|
+
|
|
93
|
+
@classmethod
|
|
94
|
+
def from_pdf(cls, file: AbstractFile, lazy: bool = False, **kwargs: Any) -> list[np.ndarray] | PDFSource:
|
|
26
95
|
"""Read a PDF file
|
|
27
96
|
|
|
28
97
|
>>> from doctr.io import DocumentFile
|
|
29
98
|
>>> doc = DocumentFile.from_pdf("path/to/your/doc.pdf")
|
|
99
|
+
>>> # Pages are rendered on demand: suited for very long documents
|
|
100
|
+
>>> doc = DocumentFile.from_pdf("path/to/your/long_doc.pdf", lazy=True)
|
|
30
101
|
|
|
31
102
|
Args:
|
|
32
103
|
file: the path to the PDF file or a binary stream
|
|
33
|
-
|
|
104
|
+
lazy: if True, return a :class:`~doctr.io.PDFSource` rendering the pages on demand
|
|
105
|
+
**kwargs: additional parameters to :class:`~doctr.io.PDFSource` (`scale`, `rgb_mode`, `password`)
|
|
106
|
+
and :meth:`pypdfium2.PdfPage.render`
|
|
34
107
|
|
|
35
108
|
Returns:
|
|
36
|
-
the list of pages decoded as numpy ndarray of shape H x W x 3
|
|
109
|
+
the list of pages decoded as numpy ndarray of shape H x W x 3, or a lazy source if `lazy` is True
|
|
37
110
|
"""
|
|
38
|
-
|
|
111
|
+
source = PDFSource(file, **kwargs)
|
|
112
|
+
return source if lazy else _materialize(source)
|
|
39
113
|
|
|
114
|
+
@overload
|
|
40
115
|
@classmethod
|
|
41
|
-
def from_url(cls, url: str, **kwargs) -> list[np.ndarray]:
|
|
116
|
+
def from_url(cls, url: str, lazy: Literal[False] = ..., **kwargs: Any) -> list[np.ndarray]: ...
|
|
117
|
+
|
|
118
|
+
@overload
|
|
119
|
+
@classmethod
|
|
120
|
+
def from_url(cls, url: str, lazy: Literal[True], **kwargs: Any) -> PDFSource: ...
|
|
121
|
+
|
|
122
|
+
@overload
|
|
123
|
+
@classmethod
|
|
124
|
+
def from_url(cls, url: str, lazy: bool, **kwargs: Any) -> list[np.ndarray] | PDFSource: ...
|
|
125
|
+
|
|
126
|
+
@classmethod
|
|
127
|
+
def from_url(cls, url: str, lazy: bool = False, **kwargs: Any) -> list[np.ndarray] | PDFSource:
|
|
42
128
|
"""Interpret a web page as a PDF document
|
|
43
129
|
|
|
44
130
|
>>> from doctr.io import DocumentFile
|
|
@@ -46,10 +132,11 @@ class DocumentFile:
|
|
|
46
132
|
|
|
47
133
|
Args:
|
|
48
134
|
url: the URL of the target web page
|
|
49
|
-
|
|
135
|
+
lazy: if True, return a :class:`~doctr.io.PDFSource` rendering the pages on demand
|
|
136
|
+
**kwargs: additional parameters to :class:`~doctr.io.PDFSource` and :meth:`pypdfium2.PdfPage.render`
|
|
50
137
|
|
|
51
138
|
Returns:
|
|
52
|
-
the list of pages decoded as numpy ndarray of shape H x W x 3
|
|
139
|
+
the list of pages decoded as numpy ndarray of shape H x W x 3, or a lazy source if `lazy` is True
|
|
53
140
|
"""
|
|
54
141
|
requires_package(
|
|
55
142
|
"weasyprint",
|
|
@@ -57,23 +144,186 @@ class DocumentFile:
|
|
|
57
144
|
+ "Installation instructions: https://doc.courtbouillon.org/weasyprint/stable/first_steps.html#installation",
|
|
58
145
|
)
|
|
59
146
|
pdf_stream = read_html(url)
|
|
60
|
-
|
|
147
|
+
source = PDFSource(pdf_stream, **kwargs)
|
|
148
|
+
return source if lazy else _materialize(source)
|
|
61
149
|
|
|
150
|
+
@overload
|
|
62
151
|
@classmethod
|
|
63
|
-
def from_images(
|
|
152
|
+
def from_images(
|
|
153
|
+
cls, files: Sequence[AbstractFile] | AbstractFile, lazy: Literal[False] = ..., **kwargs: Any
|
|
154
|
+
) -> list[np.ndarray]: ...
|
|
155
|
+
|
|
156
|
+
@overload
|
|
157
|
+
@classmethod
|
|
158
|
+
def from_images(
|
|
159
|
+
cls, files: Sequence[AbstractFile] | AbstractFile, lazy: Literal[True], **kwargs: Any
|
|
160
|
+
) -> ImageSource: ...
|
|
161
|
+
|
|
162
|
+
@overload
|
|
163
|
+
@classmethod
|
|
164
|
+
def from_images(
|
|
165
|
+
cls, files: Sequence[AbstractFile] | AbstractFile, lazy: bool, **kwargs: Any
|
|
166
|
+
) -> list[np.ndarray] | ImageSource: ...
|
|
167
|
+
|
|
168
|
+
@classmethod
|
|
169
|
+
def from_images(
|
|
170
|
+
cls, files: Sequence[AbstractFile] | AbstractFile, lazy: bool = False, **kwargs: Any
|
|
171
|
+
) -> list[np.ndarray] | ImageSource:
|
|
64
172
|
"""Read an image file (or a collection of image files) and convert it into an image in numpy format
|
|
65
173
|
|
|
66
174
|
>>> from doctr.io import DocumentFile
|
|
67
175
|
>>> pages = DocumentFile.from_images(["path/to/your/page1.png", "path/to/your/page2.png"])
|
|
176
|
+
>>> # Every image of a folder (sorted by name), decoded on demand
|
|
177
|
+
>>> pages = DocumentFile.from_images("path/to/your/folder", lazy=True)
|
|
68
178
|
|
|
69
179
|
Args:
|
|
70
|
-
files: the path to
|
|
180
|
+
files: the path to an image file, to a directory of images, or an image as bytes, or a collection of those
|
|
181
|
+
lazy: if True, return a :class:`~doctr.io.ImageSource` decoding the images on demand
|
|
71
182
|
**kwargs: additional parameters to :meth:`doctr.io.image.read_img_as_numpy`
|
|
72
183
|
|
|
73
184
|
Returns:
|
|
74
|
-
the list of pages decoded as numpy ndarray of shape H x W x 3
|
|
185
|
+
the list of pages decoded as numpy ndarray of shape H x W x 3, or a lazy source if `lazy` is True
|
|
75
186
|
"""
|
|
76
|
-
|
|
77
|
-
|
|
187
|
+
source = ImageSource(files, **kwargs)
|
|
188
|
+
return source if lazy else _materialize(source)
|
|
189
|
+
|
|
190
|
+
@overload
|
|
191
|
+
@classmethod
|
|
192
|
+
def from_files(
|
|
193
|
+
cls,
|
|
194
|
+
files: Sequence[AbstractFile] | AbstractFile,
|
|
195
|
+
lazy: Literal[False] = ...,
|
|
196
|
+
pdf_kwargs: dict[str, Any] | None = ...,
|
|
197
|
+
image_kwargs: dict[str, Any] | None = ...,
|
|
198
|
+
on_error: OnFileError = ...,
|
|
199
|
+
) -> list[np.ndarray]: ...
|
|
200
|
+
|
|
201
|
+
@overload
|
|
202
|
+
@classmethod
|
|
203
|
+
def from_files(
|
|
204
|
+
cls,
|
|
205
|
+
files: Sequence[AbstractFile] | AbstractFile,
|
|
206
|
+
lazy: Literal[True],
|
|
207
|
+
pdf_kwargs: dict[str, Any] | None = ...,
|
|
208
|
+
image_kwargs: dict[str, Any] | None = ...,
|
|
209
|
+
on_error: OnFileError = ...,
|
|
210
|
+
) -> ConcatSource: ...
|
|
211
|
+
|
|
212
|
+
@overload
|
|
213
|
+
@classmethod
|
|
214
|
+
def from_files(
|
|
215
|
+
cls,
|
|
216
|
+
files: Sequence[AbstractFile] | AbstractFile,
|
|
217
|
+
lazy: bool,
|
|
218
|
+
pdf_kwargs: dict[str, Any] | None = ...,
|
|
219
|
+
image_kwargs: dict[str, Any] | None = ...,
|
|
220
|
+
on_error: OnFileError = ...,
|
|
221
|
+
) -> list[np.ndarray] | ConcatSource: ...
|
|
222
|
+
|
|
223
|
+
@classmethod
|
|
224
|
+
def from_files(
|
|
225
|
+
cls,
|
|
226
|
+
files: Sequence[AbstractFile] | AbstractFile,
|
|
227
|
+
lazy: bool = False,
|
|
228
|
+
pdf_kwargs: dict[str, Any] | None = None,
|
|
229
|
+
image_kwargs: dict[str, Any] | None = None,
|
|
230
|
+
on_error: OnFileError = "raise",
|
|
231
|
+
) -> list[np.ndarray] | ConcatSource:
|
|
232
|
+
"""Read any mix of PDFs, images, directories and URLs as a single document, in the given order
|
|
233
|
+
|
|
234
|
+
>>> from doctr.io import DocumentFile
|
|
235
|
+
>>> doc = DocumentFile.from_files(["cover.png", "report.pdf", "path/to/scans/"], lazy=True)
|
|
236
|
+
>>> source_idx, page_idx = doc.locate(42) # the source (in `doc.sources`) of the 43rd page
|
|
237
|
+
|
|
238
|
+
Args:
|
|
239
|
+
files: a path (to a PDF, an image or a directory), a URL, a PDF or an image as bytes,
|
|
240
|
+
or a collection of those. Directories are expanded (non-recursively) to the PDFs and images they
|
|
241
|
+
contain, sorted by name.
|
|
242
|
+
lazy: if True, return a :class:`~doctr.io.ConcatSource` decoding the pages on demand
|
|
243
|
+
pdf_kwargs: keyword arguments of :class:`~doctr.io.PDFSource`, applied to every PDF and URL
|
|
244
|
+
image_kwargs: keyword arguments of :meth:`doctr.io.image.read_img_as_numpy`, applied to every image
|
|
245
|
+
on_error: what to do with a file that cannot be read (missing, not an image, a corrupted PDF, an
|
|
246
|
+
unreachable URL...): "raise" (default) the error, "skip" the file with a warning, or a callable to
|
|
247
|
+
skip it and call with the file (or URL) and the error. Images are checked from their header: a
|
|
248
|
+
corrupted content is only detected when the image is decoded, i.e. here if `lazy` is False, and by
|
|
249
|
+
the predictor otherwise (cf. the `on_error` argument of
|
|
250
|
+
:meth:`~doctr.models.predictor.OCRPredictor.stream`).
|
|
251
|
+
|
|
252
|
+
Returns:
|
|
253
|
+
the list of pages decoded as numpy ndarray of shape H x W x 3, or a lazy source if `lazy` is True
|
|
254
|
+
"""
|
|
255
|
+
if not (on_error in ("raise", "skip") or callable(on_error)):
|
|
256
|
+
raise ValueError(f"`on_error` must be 'raise', 'skip' or a callable, got {on_error!r}")
|
|
257
|
+
pdf_kwargs = pdf_kwargs or {}
|
|
258
|
+
image_kwargs = image_kwargs or {}
|
|
259
|
+
|
|
260
|
+
def _readable(file: AbstractFile, open_file: Callable[[], Any]) -> Any:
|
|
261
|
+
# The opened file, or None if it cannot be read and errors are skipped
|
|
262
|
+
try:
|
|
263
|
+
return open_file()
|
|
264
|
+
except _PAGE_ERRORS as e:
|
|
265
|
+
if on_error == "raise":
|
|
266
|
+
raise
|
|
267
|
+
_skip_file(on_error, file, e)
|
|
268
|
+
return None
|
|
269
|
+
|
|
270
|
+
inputs: list[AbstractFile] = []
|
|
271
|
+
for file in [files] if isinstance(files, (str, Path, bytes)) else files:
|
|
272
|
+
if isinstance(file, (str, Path)) and Path(file).is_dir():
|
|
273
|
+
inputs.extend(_list_files(Path(file), (".pdf", *IMAGE_EXTENSIONS)))
|
|
274
|
+
else:
|
|
275
|
+
inputs.append(file)
|
|
276
|
+
|
|
277
|
+
def _open_url(url: str) -> PDFSource:
|
|
278
|
+
try:
|
|
279
|
+
return cls.from_url(url, lazy=True, **pdf_kwargs)
|
|
280
|
+
except (PageDecodeError, OSError) as e:
|
|
281
|
+
# An unreachable page (`weasyprint.urls.URLFetchingError` is an `OSError`), or one rendered to an
|
|
282
|
+
# invalid PDF: a file that cannot be read, named by its URL rather than by its PDF content
|
|
283
|
+
raise PageDecodeError(f"unable to read the web page {url}: {e}") from e
|
|
284
|
+
|
|
285
|
+
sources: list[PageSource] = []
|
|
286
|
+
# The input of each source (None for a source of images, which knows its files), to name the faulty input of
|
|
287
|
+
# a page that cannot be decoded
|
|
288
|
+
origins: list[AbstractFile | None] = []
|
|
289
|
+
images: list[AbstractFile] = []
|
|
290
|
+
|
|
291
|
+
def _add_source(source: PageSource | None, origin: AbstractFile | None) -> None:
|
|
292
|
+
if source is not None:
|
|
293
|
+
sources.append(source)
|
|
294
|
+
origins.append(origin)
|
|
295
|
+
|
|
296
|
+
def _flush_images() -> None:
|
|
297
|
+
# Consecutive images are grouped into a single source
|
|
298
|
+
if images:
|
|
299
|
+
_add_source(ImageSource(list(images), **image_kwargs), None)
|
|
300
|
+
images.clear()
|
|
301
|
+
|
|
302
|
+
for file in inputs:
|
|
303
|
+
if isinstance(file, str) and file.lower().startswith(("http://", "https://")):
|
|
304
|
+
_flush_images()
|
|
305
|
+
_add_source(_readable(file, lambda: _open_url(file)), file)
|
|
306
|
+
elif isinstance(file, (str, Path, bytes)) and _is_pdf(file):
|
|
307
|
+
_flush_images()
|
|
308
|
+
_add_source(_readable(file, lambda: PDFSource(file, **pdf_kwargs)), file)
|
|
309
|
+
elif on_error == "raise" or _readable(file, lambda: ImageSource([file], **image_kwargs)) is not None:
|
|
310
|
+
# When skipping errors, every image is checked on its own, so that one bad file only skips itself
|
|
311
|
+
images.append(file)
|
|
312
|
+
_flush_images()
|
|
78
313
|
|
|
79
|
-
|
|
314
|
+
source = ConcatSource(sources)
|
|
315
|
+
if lazy:
|
|
316
|
+
return source
|
|
317
|
+
if on_error == "raise":
|
|
318
|
+
return _materialize(source)
|
|
319
|
+
# Decode every page, skipping those that cannot be read
|
|
320
|
+
pages: list[np.ndarray] = []
|
|
321
|
+
try:
|
|
322
|
+
for idx, page in enumerate(source._iter_indices(range(len(source)), skip_errors=True)):
|
|
323
|
+
if isinstance(page, _UnreadablePage):
|
|
324
|
+
_skip_file(on_error, _file_at(source, origins, idx), page.error)
|
|
325
|
+
else:
|
|
326
|
+
pages.append(page)
|
|
327
|
+
finally:
|
|
328
|
+
source.close()
|
|
329
|
+
return pages
|