Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rotate page #488

Merged
merged 66 commits into from
Dec 6, 2021
Merged
Changes from 1 commit
Commits
Show all changes
66 commits
Select commit Hold shift + click to select a range
1af0fc1
feat: integrate image rotation before using predictor
Rob192 Jul 19, 2021
811fd6a
Merge branch 'main' into rotate_page
Rob192 Jul 19, 2021
6f5c183
Merge branch 'main' into rotate_page
Rob192 Sep 21, 2021
c2b18d7
feat: add rotate_document functionality
Rob192 Sep 22, 2021
312179f
fix: remove min_angle from rotate_page
Rob192 Sep 22, 2021
b6bc74f
merge
Rob192 Sep 30, 2021
6fe1bc6
fix: correct models.predictor.tensorflow
Rob192 Sep 30, 2021
a6f2ff1
fix: minor corrections
Rob192 Oct 1, 2021
ccda4d0
feat: Rotate back images and boxes after straightening
Rob192 Oct 4, 2021
7d4ed75
fix: correct typo
Rob192 Oct 4, 2021
a303b23
fix: merge two functions rotate_image
Rob192 Oct 5, 2021
7a78263
fix: do not rotate back pages but only boxes
Rob192 Oct 5, 2021
eb341ac
fix: typos
Rob192 Oct 6, 2021
eeff2d6
fix: add more testing for remap_boxes in cases of boxes with an angle…
Rob192 Oct 6, 2021
16f3489
fix: remove the cropping after rotation of the image
Rob192 Oct 14, 2021
eb063f1
Merge branch 'main' of https://github.com/mindee/doctr
Rob192 Oct 25, 2021
b9ec27e
Merge branch 'main' into rotate_page
Rob192 Oct 25, 2021
f7fcf90
fix: correct model/_utils.py
Rob192 Oct 25, 2021
a04ab4f
Merge branch 'main' of https://github.com/mindee/doctr
Rob192 Oct 28, 2021
8ec9eab
Merge branch 'main' into rotate_page
Rob192 Oct 28, 2021
cf9ab0d
fix: do not use resolve_lines and resolve_boxes as it does not work w…
Rob192 Oct 28, 2021
8a31014
fix: remove expand in geometry.rotate_boxes
Rob192 Oct 29, 2021
e457cf0
fix: reformat code
Rob192 Oct 29, 2021
9975c82
fix: reformat expand from function signature
Rob192 Oct 29, 2021
32d53e4
fix: rename keep_original_size to preserve_aspect_ratio
Rob192 Oct 29, 2021
6821442
fix: vectorize box transformation
Rob192 Oct 29, 2021
988e2d0
Merge branch 'main' into rotate_page
Rob192 Nov 16, 2021
573f13f
fix: minor modifications + remove test_bbox_to_rbbox
Rob192 Nov 16, 2021
290e8ed
fix: add the straighten_pages to the latest codebase
Rob192 Nov 19, 2021
1775ebf
feat: add the straighten_pages to the pytorch predictor
Rob192 Nov 19, 2021
816168c
Merge branch 'main' into rotate_page
Rob192 Nov 19, 2021
f199044
feat: add testing for the straighten_pages parameter
Rob192 Nov 19, 2021
887ed25
fix: in case no angle is found in estimate_orientation return 0
Rob192 Nov 19, 2021
789a9c2
Merge branch 'main' into rotate_page
Rob192 Nov 24, 2021
239c508
fix: make sure boxes are outputted from _process_predictions
Rob192 Nov 24, 2021
52461dc
fix: update docstrings in OCRPredictor
Rob192 Nov 24, 2021
b6f8cca
fix: create a copy of boxes inside rotate_boxes
Rob192 Nov 24, 2021
a9f3d6e
fix: update docstring for rotate_image
Rob192 Nov 24, 2021
ea69de6
fix: add comments inside remap_boxes
Rob192 Nov 24, 2021
1a72a8c
fix: change testing in test_estimate_orientation
Rob192 Nov 24, 2021
d658be4
fix: change testing in test_estimate_orientation
Rob192 Nov 24, 2021
4711797
Merge branch 'main' into rotate_page
Rob192 Nov 25, 2021
e5ed562
Merge branch 'main' into rotate_page
Rob192 Nov 26, 2021
9a6c658
fix: delete imports not used
Rob192 Nov 29, 2021
a9cbe04
fix: styling
Rob192 Nov 29, 2021
ebdc320
fix: change assertion in test_utils_geometry.py
Rob192 Nov 30, 2021
d16fba3
fix: keep check with if expand in rotate_image
Rob192 Nov 30, 2021
e344e5c
fix: change rotate_boxes signature
Rob192 Nov 30, 2021
2e11dc8
fix: use loc_preds instead of boxes
Rob192 Nov 30, 2021
bb6bc79
fix: wrong test in remap boxes
Rob192 Nov 30, 2021
258b18b
Merge branch 'main' into rotate_page
Rob192 Nov 30, 2021
b22309d
add unit tests for pytorch
Rob192 Dec 1, 2021
495ad8c
add unit tests for remap_boxes and estimate_orientation
Rob192 Dec 1, 2021
98b44f6
fix: styling
Rob192 Dec 1, 2021
28f1fd8
fix: isort
Rob192 Dec 1, 2021
1d61de7
fix: remove unnecessary fixture
Rob192 Dec 1, 2021
faac0bd
fix: add testing for pytorch predictor
Rob192 Dec 2, 2021
938c9f2
fix: styling
Rob192 Dec 2, 2021
30a70f2
fix: correct testing for ocrpredictor with pytorch
Rob192 Dec 2, 2021
a7c0d55
fix: correct imports for testing
Rob192 Dec 2, 2021
ce23100
fix: isort
Rob192 Dec 2, 2021
8525b14
fix: make sure that expand in rotate_image is keeping the same image …
Rob192 Dec 3, 2021
7bcf639
fix: styling
Rob192 Dec 3, 2021
2205737
fix: use absolute centers for rotate_boxes
Rob192 Dec 5, 2021
484451b
fix: calculation of image_center and documentation
Rob192 Dec 5, 2021
66fad67
fix: remove default value for orig_shape in rotate_boxes
Rob192 Dec 5, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
feat: add the straighten_pages to the pytorch predictor
  • Loading branch information
Rob192 committed Nov 19, 2021
commit 1775ebfa90684f993d4e5ae957859df1802cc687
16 changes: 16 additions & 0 deletions doctr/models/predictor/pytorch.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@
from torch import nn

from doctr.io.elements import Document
from doctr.models._utils import estimate_orientation
from doctr.models.builder import DocumentBuilder
from doctr.models.detection.predictor import DetectionPredictor
from doctr.models.recognition.predictor import RecognitionPredictor
from doctr.utils.geometry import rotate_boxes, rotate_image

from .base import _OCRPredictor

Expand All @@ -33,13 +35,15 @@ def __init__(
reco_predictor: RecognitionPredictor,
assume_straight_pages: bool = True,
export_as_straight_boxes: bool = False,
straighten_pages: bool = False,
) -> None:

super().__init__()
self.det_predictor = det_predictor.eval() # type: ignore[attr-defined]
self.reco_predictor = reco_predictor.eval() # type: ignore[attr-defined]
self.doc_builder = DocumentBuilder(export_as_straight_boxes=export_as_straight_boxes)
self.assume_straight_pages = assume_straight_pages
self.straighten_pages = straighten_pages

@torch.no_grad()
def forward(
Expand All @@ -52,6 +56,13 @@ def forward(
if any(page.ndim != 3 for page in pages):
raise ValueError("incorrect input shape: all pages are expected to be multi-channel 2D images.")

origin_page_shapes = [page.shape[:2] for page in pages]
Rob192 marked this conversation as resolved.
Show resolved Hide resolved

# Detect document rotation and rotate pages
if self.straighten_pages:
origin_page_orientations = [estimate_orientation(page) for page in pages]
pages = [rotate_image(page, -angle, expand=True) for page, angle in zip(pages, origin_page_orientations)]
Rob192 marked this conversation as resolved.
Show resolved Hide resolved
Rob192 marked this conversation as resolved.
Show resolved Hide resolved

# Localize text elements
loc_preds = self.det_predictor(pages, **kwargs)
# Check whether crop mode should be switched to channels first
Expand All @@ -67,6 +78,11 @@ def forward(
loc_preds, word_preds, allow_rotated_boxes=not self.doc_builder.export_as_straight_boxes
)

# Rotate back pages and boxes while keeping original image size
if self.straighten_pages:
boxes = [rotate_boxes(page_boxes, angle, orig_shape=page.shape[:2], mask_shape=mask) for
Rob192 marked this conversation as resolved.
Show resolved Hide resolved
page_boxes, page, angle, mask in zip(boxes, pages, origin_page_orientations, origin_page_shapes)]

out = self.doc_builder(
boxes,
text_preds,
Expand Down