From 20f1cd9b50d46d12b5acbfb862ab9b15633f277c Mon Sep 17 00:00:00 2001 From: RavSinghChandan Date: Thu, 29 Oct 2026 09:30:14 +0530 Subject: [PATCH] [io] Resolve the geometry of a block that only has artefacts --- doctr/io/elements.py | 3 ++- tests/common/test_io_elements.py | 8 ++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/doctr/io/elements.py b/doctr/io/elements.py index 81b8f550cc..dd560e5612 100644 --- a/doctr/io/elements.py +++ b/doctr/io/elements.py @@ -409,7 +409,8 @@ def __init__( line_boxes = [word.geometry for line in lines for word in line.words] artefact_boxes = [artefact.geometry for artefact in artefacts] # Check whether this is a rotated or straight box (geometries read back from an export are lists) - box_resolution_fn = resolve_enclosing_rbbox if len(lines[0].geometry) == 4 else resolve_enclosing_bbox + first_geometry = lines[0].geometry if lines else artefacts[0].geometry + box_resolution_fn = resolve_enclosing_rbbox if len(first_geometry) == 4 else resolve_enclosing_bbox geometry = box_resolution_fn(line_boxes + artefact_boxes) # type: ignore super().__init__(lines=lines, artefacts=artefacts) diff --git a/tests/common/test_io_elements.py b/tests/common/test_io_elements.py index 5fbfcc94b1..153dc38561 100644 --- a/tests/common/test_io_elements.py +++ b/tests/common/test_io_elements.py @@ -442,6 +442,14 @@ def test_block(): } +def test_block_artefacts_only(): + artefacts = _mock_artefacts() + block = elements.Block(artefacts=artefacts) + assert block.lines == [] + assert block.geometry == ((0, 0), (1, 1)) + assert block.render() == "" + + def test_block_rotated_lines_from_dict(): # Rotated geometries read back from an export are nested lists, not numpy arrays polygon = [[0.1, 0.1], [0.4, 0.2], [0.35, 0.4], [0.05, 0.3]]