test_geometry.py 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. """几何工具测试:ROI 采样、点/多边形、NMS。"""
  2. import unittest
  3. from portcv.logic.geometry import (
  4. box_in_polygon_ratio,
  5. clip_polygon,
  6. nms,
  7. normalized_to_pixel_polygon,
  8. point_in_polygon,
  9. polygon_area,
  10. )
  11. from portcv.schema import Box
  12. SQUARE = [(0.0, 0.0), (100.0, 0.0), (100.0, 100.0), (0.0, 100.0)]
  13. def box(x1, y1, x2, y2, score=0.9, class_id=0, name="container"):
  14. return Box(x1, y1, x2, y2, score=score, class_id=class_id, class_name=name)
  15. class NormalizeTests(unittest.TestCase):
  16. def test_empty_polygon_becomes_full_frame(self):
  17. self.assertEqual(
  18. normalized_to_pixel_polygon([], 1280, 720),
  19. [(0.0, 0.0), (1280.0, 0.0), (1280.0, 720.0), (0.0, 720.0)],
  20. )
  21. def test_normalized_polygon_is_scaled(self):
  22. poly = normalized_to_pixel_polygon([(0.5, 0.25), (1.0, 0.5)], 1280, 720)
  23. self.assertEqual(poly, [(640.0, 180.0), (1280.0, 360.0)])
  24. def test_clip_polygon_keeps_inside_points(self):
  25. poly = clip_polygon([(-5.0, -5.0), (200.0, 200.0), (50.0, 50.0)], 100.0, 100.0)
  26. self.assertEqual(poly, [(0.0, 0.0), (100.0, 100.0), (50.0, 50.0)])
  27. class PolygonTests(unittest.TestCase):
  28. def test_polygon_area(self):
  29. self.assertEqual(polygon_area(SQUARE), 10000.0)
  30. self.assertEqual(polygon_area([(0.0, 0.0), (10.0, 0.0), (0.0, 10.0)]), 50.0)
  31. self.assertEqual(polygon_area([(0.0, 0.0), (1.0, 1.0)]), 0.0)
  32. def test_point_in_polygon(self):
  33. self.assertTrue(point_in_polygon((50.0, 50.0), SQUARE))
  34. self.assertFalse(point_in_polygon((150.0, 50.0), SQUARE))
  35. self.assertFalse(point_in_polygon((50.0, 150.0), SQUARE))
  36. self.assertFalse(point_in_polygon((50.0, 50.0), [(0.0, 0.0), (1.0, 1.0)]))
  37. def test_box_inside_ratio(self):
  38. self.assertAlmostEqual(box_in_polygon_ratio(box(10, 10, 90, 90), SQUARE), 1.0)
  39. self.assertAlmostEqual(box_in_polygon_ratio(box(200, 200, 250, 250), SQUARE), 0.0)
  40. mostly_in = box_in_polygon_ratio(box(20, 20, 120, 120), SQUARE)
  41. mostly_out = box_in_polygon_ratio(box(80, 80, 180, 180), SQUARE)
  42. self.assertGreater(mostly_in, mostly_out)
  43. self.assertTrue(0.0 < mostly_out < mostly_in < 1.0)
  44. def test_degenerate_inputs_return_zero(self):
  45. self.assertEqual(box_in_polygon_ratio(box(10, 10, 10, 10), SQUARE), 0.0)
  46. self.assertEqual(box_in_polygon_ratio(box(10, 10, 90, 90), [(0.0, 0.0)]), 0.0)
  47. class NmsTests(unittest.TestCase):
  48. def test_keeps_highest_score_per_class(self):
  49. boxes = [
  50. box(0, 0, 100, 100, score=0.90),
  51. box(5, 5, 105, 105, score=0.80),
  52. box(8, 8, 108, 108, score=0.50),
  53. ]
  54. kept = nms(boxes, iou_threshold=0.5)
  55. self.assertEqual(len(kept), 1)
  56. self.assertAlmostEqual(kept[0].score, 0.90)
  57. def test_separate_boxes_survive(self):
  58. boxes = [box(0, 0, 50, 50, score=0.9), box(200, 200, 250, 250, score=0.8)]
  59. self.assertEqual(len(nms(boxes, iou_threshold=0.5)), 2)
  60. def test_per_class_keeps_other_classes(self):
  61. boxes = [
  62. box(0, 0, 100, 100, score=0.90, class_id=0, name="container"),
  63. box(2, 2, 102, 102, score=0.85, class_id=1, name="bed"),
  64. ]
  65. self.assertEqual(len(nms(boxes, iou_threshold=0.5, per_class=True)), 2)
  66. self.assertEqual(len(nms(boxes, iou_threshold=0.5, per_class=False)), 1)
  67. def test_empty_input(self):
  68. self.assertEqual(nms([], iou_threshold=0.5), [])
  69. if __name__ == "__main__":
  70. unittest.main()