Skip to content

Commit 70de77d

Browse files
committed
fix(ocr): fix layout debug
Signed-off-by: Clément Doumouro <[email protected]>
1 parent 7b6738f commit 70de77d

File tree

1 file changed

+30
-8
lines changed

1 file changed

+30
-8
lines changed

docling/models/layout_model.py

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import copy
22
import logging
33
import warnings
4+
from copy import deepcopy
45
from collections.abc import Iterable
56
from pathlib import Path
67
from typing import Optional
@@ -95,17 +96,27 @@ def download_models(
9596
)
9697

9798
def draw_clusters_and_cells_side_by_side(
98-
self, conv_res, page, clusters, mode_prefix: str, show: bool = False
99+
self,
100+
conv_res,
101+
page,
102+
page_orientation: int,
103+
clusters,
104+
mode_prefix: str,
105+
show: bool = False,
99106
):
100107
"""
101108
Draws a page image side by side with clusters filtered into two categories:
102109
- Left: Clusters excluding FORM, KEY_VALUE_REGION, and PICTURE.
103110
- Right: Clusters including FORM, KEY_VALUE_REGION, and PICTURE.
104111
Includes label names and confidence scores for each cluster.
105112
"""
106-
scale_x = page.image.width / page.size.width
107-
scale_y = page.image.height / page.size.height
108-
113+
page_image = deepcopy(page.image)
114+
scale_x = page_image.width / page.size.width
115+
scale_y = page_image.height / page.size.height
116+
if page_orientation:
117+
page_image = page_image.rotate(-page_orientation, expand=True)
118+
if abs(page_orientation) in [90, 270]:
119+
scale_x, scale_y = scale_y, scale_x
109120
# Filter clusters for left and right images
110121
exclude_labels = {
111122
DocItemLabel.FORM,
@@ -115,12 +126,15 @@ def draw_clusters_and_cells_side_by_side(
115126
left_clusters = [c for c in clusters if c.label not in exclude_labels]
116127
right_clusters = [c for c in clusters if c.label in exclude_labels]
117128
# Create a deep copy of the original image for both sides
118-
left_image = copy.deepcopy(page.image)
119-
right_image = copy.deepcopy(page.image)
129+
left_image = page_image
130+
right_image = copy.deepcopy(left_image)
120131

121132
# Draw clusters on both images
122133
draw_clusters(left_image, left_clusters, scale_x, scale_y)
123134
draw_clusters(right_image, right_clusters, scale_x, scale_y)
135+
if page_orientation:
136+
left_image = left_image.rotate(page_orientation, expand=True)
137+
right_image = right_image.rotate(page_orientation, expand=True)
124138
# Combine the images side by side
125139
combined_width = left_image.width * 2
126140
combined_height = left_image.height
@@ -174,7 +188,11 @@ def __call__(
174188

175189
if settings.debug.visualize_raw_layout:
176190
self.draw_clusters_and_cells_side_by_side(
177-
conv_res, page, clusters, mode_prefix="raw"
191+
conv_res,
192+
page,
193+
page_orientation,
194+
clusters,
195+
mode_prefix="raw",
178196
)
179197

180198
# Apply postprocessing
@@ -208,7 +226,11 @@ def __call__(
208226

209227
if settings.debug.visualize_layout:
210228
self.draw_clusters_and_cells_side_by_side(
211-
conv_res, page, processed_clusters, mode_prefix="postprocessed"
229+
conv_res,
230+
page,
231+
page_orientation,
232+
processed_clusters,
233+
mode_prefix="postprocessed",
212234
)
213235

214236
yield page

0 commit comments

Comments
 (0)