车位角点检测代码
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

82 lines
2.8KB

  1. """Defines data structure."""
  2. import math
  3. from collections import namedtuple
  4. from enum import Enum
  5. import config
  6. MarkingPoint = namedtuple('MarkingPoint', ['x', 'y', 'direction', 'shape'])
  7. Slot = namedtuple('Slot', ['x1', 'y1', 'x2', 'y2'])
  8. class PointShape(Enum):
  9. """The point shape types used to pair two marking points into slot."""
  10. none = 0
  11. l_down = 1
  12. t_down = 2
  13. t_middle = 3
  14. t_up = 4
  15. l_up = 5
  16. def direction_diff(direction_a, direction_b):
  17. """Calculate the angle between two direction."""
  18. diff = abs(direction_a - direction_b)
  19. return diff if diff < math.pi else 2*math.pi - diff
  20. def detemine_point_shape(point, vector):
  21. """Determine which category the point is in."""
  22. vec_direct = math.atan2(vector[1], vector[0])
  23. vec_direct_up = math.atan2(-vector[0], vector[1])
  24. vec_direct_down = math.atan2(vector[0], -vector[1])
  25. if point.shape < 0.5:
  26. if direction_diff(vec_direct, point.direction) < config.BRIDGE_ANGLE_DIFF:
  27. return PointShape.t_middle
  28. if direction_diff(vec_direct_up, point.direction) < config.SEPARATOR_ANGLE_DIFF:
  29. return PointShape.t_up
  30. if direction_diff(vec_direct_down, point.direction) < config.SEPARATOR_ANGLE_DIFF:
  31. return PointShape.t_down
  32. else:
  33. if direction_diff(vec_direct, point.direction) < config.BRIDGE_ANGLE_DIFF:
  34. return PointShape.l_down
  35. if direction_diff(vec_direct_up, point.direction) < config.SEPARATOR_ANGLE_DIFF:
  36. return PointShape.l_up
  37. return PointShape.none
  38. def calc_point_squre_dist(point_a, point_b):
  39. """Calculate distance between two marking points."""
  40. distx = point_a.x - point_b.x
  41. disty = point_a.y - point_b.y
  42. return distx ** 2 + disty ** 2
  43. def calc_point_direction_angle(point_a, point_b):
  44. """Calculate angle between direction in rad."""
  45. return direction_diff(point_a.direction, point_b.direction)
  46. def match_marking_points(point_a, point_b):
  47. """Determine whether a detected point match ground truth."""
  48. dist_square = calc_point_squre_dist(point_a, point_b)
  49. angle = calc_point_direction_angle(point_a, point_b)
  50. if point_a.shape > 0.5 and point_b.shape < 0.5:
  51. return False
  52. if point_a.shape < 0.5 and point_b.shape > 0.5:
  53. return False
  54. return (dist_square < config.SQUARED_DISTANCE_THRESH
  55. and angle < config.DIRECTION_ANGLE_THRESH)
  56. def match_slots(slot_a, slot_b):
  57. """Determine whether a detected slot match ground truth."""
  58. dist_x1 = slot_b.x1 - slot_a.x1
  59. dist_y1 = slot_b.y1 - slot_a.y1
  60. squared_dist1 = dist_x1**2 + dist_y1**2
  61. dist_x2 = slot_b.x2 - slot_a.x2
  62. dist_y2 = slot_b.y2 - slot_a.y2
  63. squared_dist2 = dist_x2 ** 2 + dist_y2 ** 2
  64. return (squared_dist1 < config.SQUARED_DISTANCE_THRESH
  65. and squared_dist2 < config.SQUARED_DISTANCE_THRESH)