车位角点检测代码
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.

76 lines
2.6KB

  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. diff = abs(direction_a - direction_b)
  18. return diff if diff < math.pi else 2*math.pi - diff
  19. def detemine_point_shape(point, vector):
  20. vec_direct = math.atan2(vector[1], vector[0])
  21. vec_direct_up = math.atan2(-vector[0], vector[1])
  22. vec_direct_down = math.atan2(vector[0], -vector[1])
  23. if point.shape < 0.5:
  24. if direction_diff(vec_direct, point.direction) < config.BRIDGE_ANGLE_DIFF:
  25. return PointShape.t_middle
  26. if direction_diff(vec_direct_up, point.direction) < config.SEPARATOR_ANGLE_DIFF:
  27. return PointShape.t_up
  28. if direction_diff(vec_direct_down, point.direction) < config.SEPARATOR_ANGLE_DIFF:
  29. return PointShape.t_down
  30. else:
  31. if direction_diff(vec_direct, point.direction) < config.BRIDGE_ANGLE_DIFF:
  32. return PointShape.l_down
  33. if direction_diff(vec_direct_up, point.direction) < config.SEPARATOR_ANGLE_DIFF:
  34. return PointShape.l_up
  35. return PointShape.none
  36. def calc_point_squre_dist(point_a, point_b):
  37. """Calculate distance between two marking points."""
  38. distx = point_a.x - point_b.x
  39. disty = point_a.y - point_b.y
  40. return distx ** 2 + disty ** 2
  41. def calc_point_direction_angle(point_a, point_b):
  42. """Calculate angle between direction in rad."""
  43. return direction_diff(point_a.direction, point_b.direction)
  44. def match_marking_points(point_a, point_b):
  45. """Determine whether a detected point match ground truth."""
  46. dist_square = calc_point_squre_dist(point_a, point_b)
  47. angle = calc_point_direction_angle(point_a, point_b)
  48. return (dist_square < config.SQUARED_DISTANCE_THRESH
  49. and angle < config.DIRECTION_ANGLE_THRESH)
  50. def match_slots(slot_a, slot_b):
  51. """Determine whether a detected slot match ground truth."""
  52. dist_x1 = slot_b.x1 - slot_a.x1
  53. dist_y1 = slot_b.y1 - slot_a.y1
  54. squared_dist1 = dist_x1**2 + dist_y1**2
  55. dist_x2 = slot_b.x2 - slot_a.x2
  56. dist_y2 = slot_b.y2 - slot_a.y2
  57. squared_dist2 = dist_x2 ** 2 + dist_y2 ** 2
  58. return (squared_dist1 < config.SQUARED_DISTANCE_THRESH
  59. and squared_dist2 < config.SQUARED_DISTANCE_THRESH)