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
2 changes: 2 additions & 0 deletions doctr/transforms/functional/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ def crop_boxes(
the cropped boxes
"""
boxes = boxes.copy()
if boxes.shape[0] == 0: # nothing to crop (e.g. an image without any text)
return boxes
is_box_rel = boxes.max() <= 1
is_crop_rel = max(crop_box) <= 1

Expand Down
2 changes: 1 addition & 1 deletion doctr/transforms/functional/pytorch.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ def crop_detection(
xmax, ymax = int(round(crop_box[2] * w)), int(round(crop_box[3] * h))
cropped_img = F.crop(img, ymin, xmin, ymax - ymin, xmax - xmin)
# Crop the box
boxes = crop_boxes(boxes, crop_box if boxes.max() <= 1 else (xmin, ymin, xmax, ymax))
boxes = crop_boxes(boxes, crop_box if boxes.size == 0 or boxes.max() <= 1 else (xmin, ymin, xmax, ymax))

return cropped_img, boxes

Expand Down
6 changes: 6 additions & 0 deletions tests/pytorch/test_transforms_pt.py
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,12 @@ def test_crop_detection():
with pytest.raises(AssertionError):
crop_detection(img, abs_boxes, (2, 6, 24, 56))

# An image without any box (e.g. no text) is still cropped, and keeps no box
img = torch.ones((3, 50, 50), dtype=torch.float32)
c_img, c_boxes = crop_detection(img, np.zeros((0, 4), dtype=np.float32), (0.24, 0.46, 1.0, 1.0))
assert c_img.shape == (3, 27, 38)
assert c_boxes.shape == (0, 4)


@pytest.mark.parametrize(
"target",
Expand Down
Loading