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/models/recognition/predictor/pytorch.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@ def _predict(
# Dimension check
if any(crop.ndim != 3 for crop in crops):
raise ValueError("incorrect input shape: all crops are expected to be multi-channel 2D images.")
# An empty crop would otherwise fail with a ZeroDivisionError while splitting or resizing
empty = next((crop for crop in crops if not all(s > 0 for s in crop.shape)), None)
if empty is not None:
raise ValueError(f"incorrect input shape: crops are expected to be non-empty, got shape {empty.shape}.")

# Split crops that are too wide
remapped = False
Expand Down
13 changes: 13 additions & 0 deletions tests/pytorch/test_models_recognition_pt.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,19 @@ def test_recognition_predictor_split_confidence_aggregation(postprocessor):
predictor([np.zeros((32, 128, 3), dtype=np.uint8)])


@pytest.mark.parametrize("empty_shape", [(0, 0, 3), (0, 20, 3), (20, 0, 3), (32, 128, 0)])
def test_recognition_predictor_empty_crop(empty_shape):
predictor = RecognitionPredictor(
PreProcessor(output_size=(32, 128), batch_size=32, preserve_aspect_ratio=True),
_MockRecoModel([0.9, 0.8]),
)
crop = np.zeros((32, 128, 3), dtype=np.uint8)
with pytest.raises(ValueError, match=r"non-empty, got shape \(" + ", ".join(map(str, empty_shape)) + r"\)"):
predictor([crop, np.zeros(empty_shape, dtype=np.uint8)])
# The OCR predictor drops empty crops before recognition, so valid crops are unaffected
assert predictor([crop]) == [("ab", 0.9)]


@pytest.mark.parametrize(
"arch_name", ["crnn_mobilenet_v3_small", "sar_resnet31", "master", "vitstr_small", "parseq", "viptr_tiny"]
)
Expand Down
Loading