Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions doctr/io/elements.py
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,8 @@ def __init__(
geometry: BoundingBox | np.ndarray | None = None,
objectness_score: float | None = None,
) -> None:
if geometry is None and not words:
raise ValueError("the geometry of a line without any word can not be resolved, please pass it")
# Compute the objectness score of the line
if objectness_score is None:
objectness_score = float(np.mean([w.objectness_score for w in words]))
Expand Down Expand Up @@ -401,6 +403,8 @@ def __init__(
lines = []
if artefacts is None:
artefacts = []
if geometry is None and not lines and not artefacts:
raise ValueError("the geometry of a block without any line or artefact can not be resolved, please pass it")
# Compute the objectness score of the block
if objectness_score is None:
# A block without any word (only artefacts) takes the confidence of its artefacts, instead of NaN
Expand Down
9 changes: 9 additions & 0 deletions tests/common/test_io_elements.py
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,10 @@ def test_line():
line = elements.Line.from_dict(state_dict)
assert line.export() == state_dict

# Without any word, the geometry can not be resolved
with pytest.raises(ValueError, match="line without any word"):
elements.Line([])


def test_artefact():
artefact_type = "qr_code"
Expand Down Expand Up @@ -448,6 +452,11 @@ def test_block_artefacts_only():
assert block.lines == []
assert block.geometry == ((0, 0), (1, 1))
assert block.render() == ""

# Without any line or artefact, the geometry can not be resolved
for kwargs in ({}, {"lines": [], "artefacts": []}):
with pytest.raises(ValueError, match="block without any line or artefact"):
elements.Block(**kwargs)
# 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))
Expand Down
Loading