|
| 1 | +from math import isclose |
| 2 | + |
| 3 | +import pytest |
| 4 | + |
| 5 | +from open_image_models.detection.core.base import BoundingBox |
| 6 | + |
| 7 | +# pylint: disable=too-many-positional-arguments |
| 8 | + |
| 9 | + |
| 10 | +@pytest.mark.parametrize( |
| 11 | + "x1, y1, x2, y2, expected_width, expected_height, expected_area, expected_center", |
| 12 | + [ |
| 13 | + (0, 0, 10, 10, 10, 10, 100, (5.0, 5.0)), |
| 14 | + (2, 3, 5, 7, 3, 4, 12, (3.5, 5.0)), |
| 15 | + (10, 10, 15, 13, 5, 3, 15, (12.5, 11.5)), |
| 16 | + ], |
| 17 | +) |
| 18 | +def test_bounding_box_properties(x1, y1, x2, y2, expected_width, expected_height, expected_area, expected_center): |
| 19 | + bbox = BoundingBox(x1, y1, x2, y2) |
| 20 | + assert bbox.width == expected_width |
| 21 | + assert bbox.height == expected_height |
| 22 | + assert bbox.area == expected_area |
| 23 | + |
| 24 | + actual_center = bbox.center |
| 25 | + assert isclose(actual_center[0], expected_center[0], rel_tol=1e-6) |
| 26 | + assert isclose(actual_center[1], expected_center[1], rel_tol=1e-6) |
| 27 | + |
| 28 | + |
| 29 | +@pytest.mark.parametrize( |
| 30 | + "bbox, expected_xywh", |
| 31 | + [ |
| 32 | + (BoundingBox(0, 0, 10, 10), (0, 0, 10, 10)), |
| 33 | + (BoundingBox(2, 3, 5, 7), (2, 3, 3, 4)), |
| 34 | + (BoundingBox(10, 10, 15, 13), (10, 10, 5, 3)), |
| 35 | + ], |
| 36 | +) |
| 37 | +def test_to_xywh(bbox, expected_xywh): |
| 38 | + xywh = bbox.to_xywh() |
| 39 | + assert xywh == expected_xywh |
| 40 | + |
| 41 | + |
| 42 | +@pytest.mark.parametrize( |
| 43 | + "bbox1, bbox2, expected_intersection", |
| 44 | + [ |
| 45 | + # Overlapping case |
| 46 | + ( |
| 47 | + BoundingBox(0, 0, 10, 10), |
| 48 | + BoundingBox(5, 5, 15, 15), |
| 49 | + BoundingBox(5, 5, 10, 10), |
| 50 | + ), |
| 51 | + # One box completely inside another |
| 52 | + ( |
| 53 | + BoundingBox(0, 0, 10, 10), |
| 54 | + BoundingBox(2, 2, 5, 5), |
| 55 | + BoundingBox(2, 2, 5, 5), |
| 56 | + ), |
| 57 | + # Touching edges (should return None if there's no positive overlap) |
| 58 | + ( |
| 59 | + BoundingBox(0, 0, 10, 10), |
| 60 | + BoundingBox(10, 10, 12, 12), |
| 61 | + None, |
| 62 | + ), |
| 63 | + # No overlap at all |
| 64 | + ( |
| 65 | + BoundingBox(0, 0, 5, 5), |
| 66 | + BoundingBox(6, 6, 10, 10), |
| 67 | + None, |
| 68 | + ), |
| 69 | + ], |
| 70 | +) |
| 71 | +def test_intersection(bbox1, bbox2, expected_intersection): |
| 72 | + inter = bbox1.intersection(bbox2) |
| 73 | + assert inter == expected_intersection |
| 74 | + |
| 75 | + |
| 76 | +@pytest.mark.parametrize( |
| 77 | + "bbox1, bbox2, expected_iou", |
| 78 | + [ |
| 79 | + # Same box, IoU should equal 1.0 |
| 80 | + ( |
| 81 | + BoundingBox(0, 0, 10, 10), |
| 82 | + BoundingBox(0, 0, 10, 10), |
| 83 | + 1.0, |
| 84 | + ), |
| 85 | + # Partial overlap |
| 86 | + ( |
| 87 | + BoundingBox(0, 0, 10, 10), |
| 88 | + BoundingBox(5, 5, 15, 15), |
| 89 | + 25 / 175, # intersection=25, union=175, so IoU should equal 25/175 |
| 90 | + ), |
| 91 | + # No overlap, IoU should equal 0.0 |
| 92 | + ( |
| 93 | + BoundingBox(0, 0, 5, 5), |
| 94 | + BoundingBox(6, 6, 10, 10), |
| 95 | + 0.0, |
| 96 | + ), |
| 97 | + # One box inside another, ratio of areas |
| 98 | + ( |
| 99 | + BoundingBox(0, 0, 10, 10), |
| 100 | + BoundingBox(2, 2, 8, 8), |
| 101 | + 36 / 100, |
| 102 | + ), |
| 103 | + ], |
| 104 | +) |
| 105 | +def test_iou(bbox1, bbox2, expected_iou): |
| 106 | + iou_value = bbox1.iou(bbox2) |
| 107 | + assert isclose(iou_value, expected_iou, rel_tol=1e-5) |
0 commit comments