Skip to content

Commit 389e238

Browse files
committed
fix(ocr): fix layout debug
Signed-off-by: Clément Doumouro <[email protected]>
1 parent b54eeb1 commit 389e238

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
@@ -101,17 +102,27 @@ def download_models(
101102
)
102103

103104
def draw_clusters_and_cells_side_by_side(
104-
self, conv_res, page, clusters, mode_prefix: str, show: bool = False
105+
self,
106+
conv_res,
107+
page,
108+
page_orientation: int,
109+
clusters,
110+
mode_prefix: str,
111+
show: bool = False,
105112
):
106113
"""
107114
Draws a page image side by side with clusters filtered into two categories:
108115
- Left: Clusters excluding FORM, KEY_VALUE_REGION, and PICTURE.
109116
- Right: Clusters including FORM, KEY_VALUE_REGION, and PICTURE.
110117
Includes label names and confidence scores for each cluster.
111118
"""
112-
scale_x = page.image.width / page.size.width
113-
scale_y = page.image.height / page.size.height
114-
119+
page_image = deepcopy(page.image)
120+
scale_x = page_image.width / page.size.width
121+
scale_y = page_image.height / page.size.height
122+
if page_orientation:
123+
page_image = page_image.rotate(-page_orientation, expand=True)
124+
if abs(page_orientation) in [90, 270]:
125+
scale_x, scale_y = scale_y, scale_x
115126
# Filter clusters for left and right images
116127
exclude_labels = {
117128
DocItemLabel.FORM,
@@ -121,12 +132,15 @@ def draw_clusters_and_cells_side_by_side(
121132
left_clusters = [c for c in clusters if c.label not in exclude_labels]
122133
right_clusters = [c for c in clusters if c.label in exclude_labels]
123134
# Create a deep copy of the original image for both sides
124-
left_image = copy.deepcopy(page.image)
125-
right_image = copy.deepcopy(page.image)
135+
left_image = page_image
136+
right_image = copy.deepcopy(left_image)
126137

127138
# Draw clusters on both images
128139
draw_clusters(left_image, left_clusters, scale_x, scale_y)
129140
draw_clusters(right_image, right_clusters, scale_x, scale_y)
141+
if page_orientation:
142+
left_image = left_image.rotate(page_orientation, expand=True)
143+
right_image = right_image.rotate(page_orientation, expand=True)
130144
# Combine the images side by side
131145
combined_width = left_image.width * 2
132146
combined_height = left_image.height
@@ -180,7 +194,11 @@ def __call__(
180194

181195
if settings.debug.visualize_raw_layout:
182196
self.draw_clusters_and_cells_side_by_side(
183-
conv_res, page, clusters, mode_prefix="raw"
197+
conv_res,
198+
page,
199+
page_orientation,
200+
clusters,
201+
mode_prefix="raw",
184202
)
185203

186204
# Apply postprocessing
@@ -214,7 +232,11 @@ def __call__(
214232

215233
if settings.debug.visualize_layout:
216234
self.draw_clusters_and_cells_side_by_side(
217-
conv_res, page, processed_clusters, mode_prefix="postprocessed"
235+
conv_res,
236+
page,
237+
page_orientation,
238+
processed_clusters,
239+
mode_prefix="postprocessed",
218240
)
219241

220242
yield page

0 commit comments

Comments
 (0)