forked from mindee/doctr
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: Moved doctr.documents to doctr.io (mindee#390)
* refactor: Moved doctr.documents to doctr.io * refactor: Refactored doctr.datasets * refactor: Updated imports * docs: Updated README * fix: Fixed syntax * style: Fixed lint * test: Updated unittests * refactor: Updated import * docs: Updated documentation * refactor: Moved reader types to doctr.utils * feat: Added binary decoding as tensor image * test: Updated unittests * refactor: Refactored API * fix: Fixed import * fix: Fixed API routes * test: Fixed imports * refactor: Removed unused imports * test: Removed unused import * refactor: Removed unused imports * test: Updated unittests * test: Fixed imports
- Loading branch information
Showing
35 changed files
with
427 additions
and
201 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -77,7 +77,7 @@ Supported datasets | |
:caption: Package Reference | ||
|
||
datasets | ||
documents | ||
io | ||
models | ||
transforms | ||
utils |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
from .elements import * | ||
from .reader import * | ||
from .image import * | ||
from .pdf import * | ||
from .html import * |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# Copyright (C) 2021, Mindee. | ||
|
||
# This program is licensed under the Apache License version 2. | ||
# See LICENSE or go to <https://www.apache.org/licenses/LICENSE-2.0.txt> for full license details. | ||
|
||
from weasyprint import HTML | ||
from typing import Any | ||
|
||
__all__ = ['read_html'] | ||
|
||
|
||
def read_html(url: str, **kwargs: Any) -> bytes: | ||
"""Read a PDF file and convert it into an image in numpy format | ||
Example:: | ||
>>> from doctr.documents import read_html | ||
>>> doc = read_html("https://www.yoursite.com") | ||
Args: | ||
url: URL of the target web page | ||
Returns: | ||
decoded PDF file as a bytes stream | ||
""" | ||
|
||
return HTML(url, **kwargs).write_pdf() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
from .base import * | ||
|
||
from doctr.file_utils import is_tf_available, is_torch_available | ||
|
||
if is_tf_available(): | ||
from .tensorflow import * | ||
elif is_torch_available(): | ||
from .pytorch import * # type: ignore[misc] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
# Copyright (C) 2021, Mindee. | ||
|
||
# This program is licensed under the Apache License version 2. | ||
# See LICENSE or go to <https://www.apache.org/licenses/LICENSE-2.0.txt> for full license details. | ||
|
||
from pathlib import Path | ||
from typing import Optional, Tuple | ||
import numpy as np | ||
import cv2 | ||
from doctr.utils.common_types import AbstractFile | ||
|
||
__all__ = ['read_img_as_numpy'] | ||
|
||
|
||
def read_img_as_numpy( | ||
file: AbstractFile, | ||
output_size: Optional[Tuple[int, int]] = None, | ||
rgb_output: bool = True, | ||
) -> np.ndarray: | ||
"""Read an image file into numpy format | ||
Example:: | ||
>>> from doctr.documents import read_img | ||
>>> page = read_img("path/to/your/doc.jpg") | ||
Args: | ||
file: the path to the image file | ||
output_size: the expected output size of each page in format H x W | ||
rgb_output: whether the output ndarray channel order should be RGB instead of BGR. | ||
Returns: | ||
the page decoded as numpy ndarray of shape H x W x 3 | ||
""" | ||
|
||
if isinstance(file, (str, Path)): | ||
if not Path(file).is_file(): | ||
raise FileNotFoundError(f"unable to access {file}") | ||
img = cv2.imread(str(file), cv2.IMREAD_COLOR) | ||
elif isinstance(file, bytes): | ||
file = np.frombuffer(file, np.uint8) | ||
img = cv2.imdecode(file, cv2.IMREAD_COLOR) | ||
else: | ||
raise TypeError("unsupported object type for argument 'file'") | ||
|
||
# Validity check | ||
if img is None: | ||
raise ValueError("unable to read file.") | ||
# Resizing | ||
if isinstance(output_size, tuple): | ||
img = cv2.resize(img, output_size[::-1], interpolation=cv2.INTER_LINEAR) | ||
# Switch the channel order | ||
if rgb_output: | ||
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) | ||
return img |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
# Copyright (C) 2021, Mindee. | ||
|
||
# This program is licensed under the Apache License version 2. | ||
# See LICENSE or go to <https://www.apache.org/licenses/LICENSE-2.0.txt> for full license details. | ||
|
||
import numpy as np | ||
from PIL import Image | ||
from io import BytesIO | ||
import torch | ||
from torchvision.transforms.functional import to_tensor | ||
|
||
from doctr.utils.common_types import AbstractPath | ||
|
||
__all__ = ['read_img_as_tensor', 'decode_img_as_tensor'] | ||
|
||
|
||
def _from_pil_img(pil_img: Image, dtype: torch.dtype = torch.float32) -> torch.Tensor: | ||
|
||
if dtype == torch.float32: | ||
img = to_tensor(pil_img) | ||
else: | ||
img = torch.from_numpy( | ||
np.array(pil_img, np.uint8, copy=True) | ||
) | ||
img = img.view(pil_img.size[1], pil_img.size[0], len(pil_img.getbands())) | ||
# put it from HWC to CHW format | ||
img = img.permute((2, 0, 1)).contiguous() | ||
if dtype == torch.float16: | ||
# Switch to FP16 | ||
img = img.to(dtype=torch.float16).div(255) | ||
|
||
return img | ||
|
||
|
||
def read_img_as_tensor(img_path: AbstractPath, dtype: torch.dtype = torch.float32) -> torch.Tensor: | ||
"""Read an image file as a PyTorch tensor | ||
Args: | ||
img_path: location of the image file | ||
dtype: the desired data type of the output tensor. If it is float-related, values will be divided by 255. | ||
Returns: | ||
decoded image as a tensor | ||
""" | ||
|
||
if dtype not in (torch.uint8, torch.float16, torch.float32): | ||
raise ValueError("insupported value for dtype") | ||
|
||
pil_img = Image.open(img_path, mode='r').convert('RGB') | ||
|
||
return _from_pil_img(pil_img, dtype) | ||
|
||
|
||
def decode_img_as_tensor(img_content: bytes, dtype: torch.dtype = torch.float32) -> torch.Tensor: | ||
"""Read a byte stream as a PyTorch tensor | ||
Args: | ||
img_content: bytes of a decoded image | ||
dtype: the desired data type of the output tensor. If it is float-related, values will be divided by 255. | ||
Returns: | ||
decoded image as a tensor | ||
""" | ||
|
||
if dtype not in (torch.uint8, torch.float16, torch.float32): | ||
raise ValueError("insupported value for dtype") | ||
|
||
pil_img = Image.open(BytesIO(img_content), mode='r').convert('RGB') | ||
|
||
return _from_pil_img(pil_img, dtype) |
Oops, something went wrong.