Skip to content

Commit 4434213

Browse files
fix straighten pages (#1697)
1 parent 06bce51 commit 4434213

File tree

8 files changed

+18
-6
lines changed

8 files changed

+18
-6
lines changed

doctr/models/builder.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ def _build_blocks(
266266
Line([
267267
Word(
268268
*word_preds[idx],
269-
tuple([tuple(pt) for pt in boxes[idx].tolist()]), # type: ignore[arg-type]
269+
tuple(tuple(pt) for pt in boxes[idx].tolist()), # type: ignore[arg-type]
270270
float(objectness_scores[idx]),
271271
crop_orientations[idx],
272272
)
@@ -500,7 +500,7 @@ def _build_blocks( # type: ignore[override]
500500
Prediction(
501501
value=word_preds[idx][0],
502502
confidence=word_preds[idx][1],
503-
geometry=tuple([tuple(pt) for pt in boxes[idx].tolist()]), # type: ignore[arg-type]
503+
geometry=tuple(tuple(pt) for pt in boxes[idx].tolist()), # type: ignore[arg-type]
504504
objectness_score=float(objectness_scores[idx]),
505505
crop_orientation=crop_orientations[idx],
506506
)

doctr/models/kie_predictor/pytorch.py

+3
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,9 @@ def forward(
9999
origin_pages_orientations = None
100100
if self.straighten_pages:
101101
pages = self._straighten_pages(pages, seg_maps, general_pages_orientations, origin_pages_orientations) # type: ignore
102+
# update page shapes after straightening
103+
origin_page_shapes = [page.shape[:2] for page in pages]
104+
102105
# Forward again to get predictions on straight pages
103106
loc_preds = self.det_predictor(pages, **kwargs)
104107

doctr/models/kie_predictor/tensorflow.py

+3
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,9 @@ def __call__(
9999
origin_pages_orientations = None
100100
if self.straighten_pages:
101101
pages = self._straighten_pages(pages, seg_maps, general_pages_orientations, origin_pages_orientations)
102+
# update page shapes after straightening
103+
origin_page_shapes = [page.shape[:2] for page in pages]
104+
102105
# Forward again to get predictions on straight pages
103106
loc_preds = self.det_predictor(pages, **kwargs) # type: ignore[assignment]
104107

doctr/models/modules/vision_transformer/pytorch.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ def __init__(self, input_shape: Tuple[int, int, int], embed_dim: int, patch_size
2020
channels, height, width = input_shape
2121
self.patch_size = patch_size
2222
self.interpolate = True if patch_size[0] == patch_size[1] else False
23-
self.grid_size = tuple([s // p for s, p in zip((height, width), self.patch_size)])
23+
self.grid_size = tuple(s // p for s, p in zip((height, width), self.patch_size))
2424
self.num_patches = self.grid_size[0] * self.grid_size[1]
2525

2626
self.cls_token = nn.Parameter(torch.randn(1, 1, embed_dim))

doctr/models/modules/vision_transformer/tensorflow.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ def __init__(self, input_shape: Tuple[int, int, int], embed_dim: int, patch_size
2222
height, width, _ = input_shape
2323
self.patch_size = patch_size
2424
self.interpolate = True if patch_size[0] == patch_size[1] else False
25-
self.grid_size = tuple([s // p for s, p in zip((height, width), self.patch_size)])
25+
self.grid_size = tuple(s // p for s, p in zip((height, width), self.patch_size))
2626
self.num_patches = self.grid_size[0] * self.grid_size[1]
2727

2828
self.cls_token = self.add_weight(shape=(1, 1, embed_dim), initializer="zeros", trainable=True, name="cls_token")

doctr/models/predictor/base.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,8 @@ def _straighten_pages(
101101
]
102102
)
103103
return [
104-
# We exapnd if the page is wider than tall and the angle is 90 or -90
105-
rotate_image(page, angle, expand=page.shape[1] > page.shape[0] and abs(angle) == 90)
104+
# expand if height and width are not equal
105+
rotate_image(page, angle, expand=page.shape[0] != page.shape[1])
106106
for page, angle in zip(pages, origin_pages_orientations)
107107
]
108108

doctr/models/predictor/pytorch.py

+3
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,9 @@ def forward(
9797
origin_pages_orientations = None
9898
if self.straighten_pages:
9999
pages = self._straighten_pages(pages, seg_maps, general_pages_orientations, origin_pages_orientations) # type: ignore
100+
# update page shapes after straightening
101+
origin_page_shapes = [page.shape[:2] for page in pages]
102+
100103
# Forward again to get predictions on straight pages
101104
loc_preds = self.det_predictor(pages, **kwargs)
102105

doctr/models/predictor/tensorflow.py

+3
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,9 @@ def __call__(
9797
origin_pages_orientations = None
9898
if self.straighten_pages:
9999
pages = self._straighten_pages(pages, seg_maps, general_pages_orientations, origin_pages_orientations)
100+
# update page shapes after straightening
101+
origin_page_shapes = [page.shape[:2] for page in pages]
102+
100103
# forward again to get predictions on straight pages
101104
loc_preds_dict = self.det_predictor(pages, **kwargs) # type: ignore[assignment]
102105

0 commit comments

Comments
 (0)