Skip to content

Commit

Permalink
[Fix] Catch Divide by zero explicit (#1471)
Browse files Browse the repository at this point in the history
  • Loading branch information
felixdittrich92 authored Feb 15, 2024
1 parent 2daf0d9 commit dd1fbbe
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
8 changes: 7 additions & 1 deletion doctr/utils/geometry.py
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,13 @@ def estimate_page_angle(polys: np.ndarray) -> float:
yleft = polys[:, 0, 1] + polys[:, 3, 1]
xright = polys[:, 1, 0] + polys[:, 2, 0]
yright = polys[:, 1, 1] + polys[:, 2, 1]
return float(np.median(np.arctan((yleft - yright) / (xright - xleft))) * 180 / np.pi) # Y axis from top to bottom!
with np.errstate(divide="raise", invalid="raise"):
try:
return float(
np.median(np.arctan((yleft - yright) / (xright - xleft)) * 180 / np.pi) # Y axis from top to bottom!
)
except FloatingPointError:
return 0.0


def convert_to_relative_coords(geoms: np.ndarray, img_shape: Tuple[int, int]) -> np.ndarray:
Expand Down
4 changes: 4 additions & 0 deletions tests/common/test_utils_geometry.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,10 @@ def test_estimate_page_angle():
rotated_polys = geometry.rotate_boxes(straight_polys, angle=20, orig_shape=(512, 512))
angle = geometry.estimate_page_angle(rotated_polys)
assert np.isclose(angle, 20)
# Test divide by zero / NaN
invalid_poly = np.array([[[0.5, 0.5], [0.5, 0.5], [0.5, 0.5], [0.5, 0.5]]])
angle = geometry.estimate_page_angle(invalid_poly)
assert angle == 0.0


def test_extract_crops(mock_pdf):
Expand Down

0 comments on commit dd1fbbe

Please sign in to comment.