Skip to content
Open
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: 1 addition & 1 deletion doctr/utils/geometry.py
Original file line number Diff line number Diff line change
Expand Up @@ -464,7 +464,7 @@ def extract_crops(img: np.ndarray, boxes: np.ndarray) -> list[np.ndarray]:
_boxes[:, [1, 3]] *= h
_boxes = _boxes.round().astype(int)
# Add last index
_boxes[2:] += 1
_boxes[:, 2:] += 1

return deepcopy([img[box[1] : box[3], box[0] : box[2]] for box in _boxes])

Expand Down
6 changes: 6 additions & 0 deletions tests/common/test_utils_geometry.py
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,12 @@ def test_extract_crops(mock_pdf):
# Identity
assert np.all(doc_img == geometry.extract_crops(doc_img, np.array([[0, 0, 1, 1]], dtype=np.float32))[0])

# Identical boxes must yield identical crops regardless of their position in the batch
gradient_img = np.tile(np.arange(100, dtype=np.uint8).reshape(100, 1, 1), (1, 100, 3))
same_box = [0.1, 0.1, 0.2, 0.2]
identical_crops = geometry.extract_crops(gradient_img, np.array([same_box, same_box, same_box], dtype=np.float32))
assert all(np.array_equal(crop, identical_crops[0]) for crop in identical_crops)

# No box
assert geometry.extract_crops(doc_img, np.zeros((0, 4))) == []

Expand Down
Loading