Source code for kitcar_ml.utils.test.test_bounding_box

import pytest

from kitcar_ml.utils.bounding_box import BoundingBox


[docs]def test_immutable_coordinates(): print("Test Immutable Coordinates") box = BoundingBox(1, 2, 3, 4, "test class") for i in range(4): with pytest.raises(TypeError): box.coordinates[i] = 3
[docs]def test_iou(): print("Test Intersection over Union") # No Intersection tests b1 = BoundingBox(10, 10, 20, 20, "test class 0") b2 = BoundingBox(0, 0, 9, 9, "test class 0") iou_1 = BoundingBox.iou(b1, b2) iou_2 = BoundingBox.iou(b2, b1) assert iou_1 == iou_2 == 0 b1 = BoundingBox(10, 10, 20, 20, "test class 0") b2 = BoundingBox(0, 1, 9, 20, "test class 0") iou_1 = BoundingBox.iou(b1, b2) iou_2 = BoundingBox.iou(b2, b1) assert iou_1 == iou_2 == 0 b1 = BoundingBox(10, 10, 20, 20, "test class 0") b2 = BoundingBox(0, 21, 20, 21, "test class 0") iou_1 = BoundingBox.iou(b1, b2) iou_2 = BoundingBox.iou(b2, b1) assert iou_1 == iou_2 == 0 # Test with equal bounding boxes b1 = BoundingBox(10, 10, 20, 20, "test class 0") b2 = BoundingBox(10, 10, 20, 20, "test class 0") iou_1 = BoundingBox.iou(b1, b2) iou_2 = BoundingBox.iou(b2, b1) assert iou_1 == iou_2 == 1 # Special cases b1 = BoundingBox(10, 10, 21, 21, "test class 0") b2 = BoundingBox(10, 10, 15, 15, "test class 0") iou_1 = BoundingBox.iou(b1, b2) iou_2 = BoundingBox.iou(b2, b1) assert iou_1 == iou_2 == 0.25 b1 = BoundingBox(10, 10, 21, 21, "test class 0") b2 = BoundingBox(10, 10, 33, 33, "test class 0") iou_1 = BoundingBox.iou(b1, b2) iou_2 = BoundingBox.iou(b2, b1) assert iou_1 == iou_2 == 0.25
[docs]def test_intersection(): print("Test Intersection") # Test boxes without intersection box_a = BoundingBox(10, 10, 20, 20, "test class 1") box_b = BoundingBox(0, 0, 9, 9, "test class 1") area_a = BoundingBox.intersection_area(box_a, box_b) area_b = BoundingBox.intersection_area(box_b, box_a) assert area_a == area_b == 0 box_a = BoundingBox(10, 10, 20, 20, "test class 1") box_b = BoundingBox(10, 21, 20, 30, "test class 1") area_a = BoundingBox.intersection_area(box_a, box_b) area_b = BoundingBox.intersection_area(box_b, box_a) assert area_a == area_b == 0 box_a = BoundingBox(10, 10, 20, 20, "test class 1") box_b = BoundingBox(21, 15, 21, 19, "test class 1") area_a = BoundingBox.intersection_area(box_a, box_b) area_b = BoundingBox.intersection_area(box_b, box_a) assert area_a == area_b == 0 box_a = BoundingBox(10, 10, 20, 20, "test class 1") box_b = BoundingBox(15, 21, 19, 21, "test class 0") area_a = BoundingBox.intersection_area(box_a, box_b) area_b = BoundingBox.intersection_area(box_b, box_a) assert area_a == area_b == 0 # Test boxes with intersection box_a = BoundingBox(10, 10, 20, 20, "test class 1") box_b = BoundingBox(10, 10, 20, 10, "test class 0") area_a = BoundingBox.intersection_area(box_a, box_b) area_b = BoundingBox.intersection_area(box_b, box_a) assert area_a == area_b > 0
[docs]def main(): print("Test Bounding Box") test_iou() test_immutable_coordinates() test_intersection()
if __name__ == "__main__": main()