Skip to content

Commit cfd3635

Browse files
committed
fix(ocr): rotate image to the natural orientation before layout prediction
Signed-off-by: Clément Doumouro <[email protected]>
1 parent aae42b3 commit cfd3635

File tree

7 files changed

+32
-5
lines changed

7 files changed

+32
-5
lines changed

docling/models/layout_model.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
from docling.models.utils.hf_model_download import download_hf_model
2020
from docling.utils.accelerator_utils import decide_device
2121
from docling.utils.layout_postprocessor import LayoutPostprocessor
22+
from docling.utils.orientation import detect_orientation
2223
from docling.utils.profiling import TimeRecorder
2324
from docling.utils.visualization import draw_clusters
2425

@@ -164,6 +165,10 @@ def __call__(
164165
page_image = page.get_image(scale=1.0)
165166
assert page_image is not None
166167

168+
page_orientation = detect_orientation(page.cells)
169+
if page_orientation:
170+
page_image = page_image.rotate(-page_orientation, expand=True)
171+
167172
valid_pages.append(page)
168173
valid_page_images.append(page_image)
169174

docling/utils/orientation.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,20 @@
1+
from collections import Counter
2+
from operator import itemgetter
3+
4+
from docling_core.types.doc.page import TextCell
5+
6+
_ORIENTATIONS = [0, 90, 180, 270]
7+
8+
9+
def _clipped_orientation(angle: float) -> int:
10+
return min((abs(angle - o) % 360, o) for o in _ORIENTATIONS)[1]
11+
12+
13+
def detect_orientation(cells: list[TextCell]) -> int:
14+
if not cells:
15+
return 0
16+
orientation_counter = Counter(_clipped_orientation(c.rect.angle_360) for c in cells)
17+
return max(orientation_counter.items(), key=itemgetter(1))[0]
118
from typing import Tuple
219

320
from docling_core.types.doc import BoundingBox, CoordOrigin
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
<document>
2-
<paragraph><location><page_1><loc_74><loc_16><loc_88><loc_18></location>package</paragraph>
2+
<paragraph><location><page_1><loc_75><loc_16><loc_88><loc_18></location>package</paragraph>
33
<paragraph><location><page_1><loc_15><loc_9><loc_88><loc_15></location>Docling bundles PDF document conversion to JSON and Markdown in an easy self contained</paragraph>
44
</document>
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
<document>
2-
<paragraph><location><page_1><loc_82><loc_74><loc_84><loc_88></location>package</paragraph>
2+
<paragraph><location><page_1><loc_82><loc_75><loc_84><loc_88></location>package</paragraph>
33
</document>
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
<document>
2-
<paragraph><location><page_1><loc_16><loc_12><loc_18><loc_26></location>package</paragraph>
2+
<paragraph><location><page_1><loc_9><loc_12><loc_11><loc_85></location>Docling bundles PDF document conversion to</paragraph>
3+
<paragraph><location><page_1><loc_12><loc_12><loc_15><loc_85></location><location><page_1><loc_12><loc_12><loc_15><loc_85></location>JSON and Markdown in an easy self contained package</paragraph>
34
</document>
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1-
package
1+
Docling bundles PDF document conversion to
2+
3+
JSON and Markdown in an easy self contained package
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1-
package
1+
Docling bundles PDF document conversion to
2+
3+
JSON and Markdown in an easy self contained package

0 commit comments

Comments
 (0)