From 6ad2565747fc7e3c0b1e9b20e58aa78b53c00765 Mon Sep 17 00:00:00 2001 From: RavSinghChandan Date: Fri, 30 Oct 2026 14:18:46 +0530 Subject: [PATCH] [io] Use the artefact confidence as the objectness score of a block without words --- doctr/io/elements.py | 6 ++++-- tests/common/test_io_elements.py | 3 +++ 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/doctr/io/elements.py b/doctr/io/elements.py index dd560e5612..d29c186de7 100644 --- a/doctr/io/elements.py +++ b/doctr/io/elements.py @@ -401,9 +401,11 @@ def __init__( lines = [] if artefacts is None: artefacts = [] - # Compute the objectness score of the line + # Compute the objectness score of the block if objectness_score is None: - objectness_score = float(np.mean([w.objectness_score for line in lines for w in line.words])) + # A block without any word (only artefacts) takes the confidence of its artefacts, instead of NaN + scores = [w.objectness_score for line in lines for w in line.words] or [a.confidence for a in artefacts] + objectness_score = float(np.mean(scores)) # Resolve the geometry using the smallest enclosing bounding box if geometry is None: line_boxes = [word.geometry for line in lines for word in line.words] diff --git a/tests/common/test_io_elements.py b/tests/common/test_io_elements.py index 153dc38561..0ea9620fd5 100644 --- a/tests/common/test_io_elements.py +++ b/tests/common/test_io_elements.py @@ -448,6 +448,9 @@ def test_block_artefacts_only(): assert block.lines == [] assert block.geometry == ((0, 0), (1, 1)) assert block.render() == "" + # The objectness score is the confidence of the artefacts, which keeps the export valid JSON + assert block.objectness_score == 0.8 + json.loads(json.dumps(block.export(), allow_nan=False)) def test_block_rotated_lines_from_dict():