"""几何工具测试:ROI 采样、点/多边形、NMS。""" import unittest from portcv.logic.geometry import ( box_in_polygon_ratio, clip_polygon, nms, normalized_to_pixel_polygon, point_in_polygon, polygon_area, ) from portcv.schema import Box SQUARE = [(0.0, 0.0), (100.0, 0.0), (100.0, 100.0), (0.0, 100.0)] def box(x1, y1, x2, y2, score=0.9, class_id=0, name="container"): return Box(x1, y1, x2, y2, score=score, class_id=class_id, class_name=name) class NormalizeTests(unittest.TestCase): def test_empty_polygon_becomes_full_frame(self): self.assertEqual( normalized_to_pixel_polygon([], 1280, 720), [(0.0, 0.0), (1280.0, 0.0), (1280.0, 720.0), (0.0, 720.0)], ) def test_normalized_polygon_is_scaled(self): poly = normalized_to_pixel_polygon([(0.5, 0.25), (1.0, 0.5)], 1280, 720) self.assertEqual(poly, [(640.0, 180.0), (1280.0, 360.0)]) def test_clip_polygon_keeps_inside_points(self): poly = clip_polygon([(-5.0, -5.0), (200.0, 200.0), (50.0, 50.0)], 100.0, 100.0) self.assertEqual(poly, [(0.0, 0.0), (100.0, 100.0), (50.0, 50.0)]) class PolygonTests(unittest.TestCase): def test_polygon_area(self): self.assertEqual(polygon_area(SQUARE), 10000.0) self.assertEqual(polygon_area([(0.0, 0.0), (10.0, 0.0), (0.0, 10.0)]), 50.0) self.assertEqual(polygon_area([(0.0, 0.0), (1.0, 1.0)]), 0.0) def test_point_in_polygon(self): self.assertTrue(point_in_polygon((50.0, 50.0), SQUARE)) self.assertFalse(point_in_polygon((150.0, 50.0), SQUARE)) self.assertFalse(point_in_polygon((50.0, 150.0), SQUARE)) self.assertFalse(point_in_polygon((50.0, 50.0), [(0.0, 0.0), (1.0, 1.0)])) def test_box_inside_ratio(self): self.assertAlmostEqual(box_in_polygon_ratio(box(10, 10, 90, 90), SQUARE), 1.0) self.assertAlmostEqual(box_in_polygon_ratio(box(200, 200, 250, 250), SQUARE), 0.0) mostly_in = box_in_polygon_ratio(box(20, 20, 120, 120), SQUARE) mostly_out = box_in_polygon_ratio(box(80, 80, 180, 180), SQUARE) self.assertGreater(mostly_in, mostly_out) self.assertTrue(0.0 < mostly_out < mostly_in < 1.0) def test_degenerate_inputs_return_zero(self): self.assertEqual(box_in_polygon_ratio(box(10, 10, 10, 10), SQUARE), 0.0) self.assertEqual(box_in_polygon_ratio(box(10, 10, 90, 90), [(0.0, 0.0)]), 0.0) class NmsTests(unittest.TestCase): def test_keeps_highest_score_per_class(self): boxes = [ box(0, 0, 100, 100, score=0.90), box(5, 5, 105, 105, score=0.80), box(8, 8, 108, 108, score=0.50), ] kept = nms(boxes, iou_threshold=0.5) self.assertEqual(len(kept), 1) self.assertAlmostEqual(kept[0].score, 0.90) def test_separate_boxes_survive(self): boxes = [box(0, 0, 50, 50, score=0.9), box(200, 200, 250, 250, score=0.8)] self.assertEqual(len(nms(boxes, iou_threshold=0.5)), 2) def test_per_class_keeps_other_classes(self): boxes = [ box(0, 0, 100, 100, score=0.90, class_id=0, name="container"), box(2, 2, 102, 102, score=0.85, class_id=1, name="bed"), ] self.assertEqual(len(nms(boxes, iou_threshold=0.5, per_class=True)), 2) self.assertEqual(len(nms(boxes, iou_threshold=0.5, per_class=False)), 1) def test_empty_input(self): self.assertEqual(nms([], iou_threshold=0.5), []) if __name__ == "__main__": unittest.main()