You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

ramp_postprocess.py 4.9 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. import cv2
  2. import numpy as np
  3. def parse_result(label, count, ratio):
  4. count_d = dict(zip(label, count))
  5. ramp_count = count_d.get(21, 0)
  6. if ramp_count / np.sum(count) > ratio:
  7. return True
  8. else:
  9. return False
  10. def get_ramp(results, img_rgb):
  11. results = np.array(results[0])
  12. input_height, input_width = results.shape
  13. # big trapezoid
  14. big_closest = np.array([
  15. [0, int(input_height)],
  16. [int(input_width),
  17. int(input_height)],
  18. [int(0.882 * input_width + .5),
  19. int(.8 * input_height + .5)],
  20. [int(0.118 * input_width + .5),
  21. int(.8 * input_height + .5)]
  22. ])
  23. big_future = np.array([
  24. [int(0.118 * input_width + .5),
  25. int(.8 * input_height + .5)],
  26. [int(0.882 * input_width + .5),
  27. int(.8 * input_height + .5)],
  28. [int(.765 * input_width + .5),
  29. int(.66 * input_height + .5)],
  30. [int(.235 * input_width + .5),
  31. int(.66 * input_height + .5)]
  32. ])
  33. # small trapezoid
  34. small_closest = np.array([
  35. [488, int(input_height)],
  36. [1560, int(input_height)],
  37. [1391, int(.8 * input_height + .5)],
  38. [621, int(.8 * input_height + .5)]
  39. ])
  40. small_future = np.array([
  41. [741, int(.66 * input_height + .5)],
  42. [1275, int(.66 * input_height + .5)],
  43. [1391, int(.8 * input_height + .5)],
  44. [621, int(.8 * input_height + .5)]
  45. ])
  46. upper_left = np.array([
  47. [1567, 676],
  48. [1275, 676],
  49. [1391, 819],
  50. [1806, 819]
  51. ])
  52. bottom_left = np.array([
  53. [1806, 819],
  54. [1391, 819],
  55. [1560, 1024],
  56. [2048, 1024]
  57. ])
  58. upper_right = np.array([
  59. [741, 676],
  60. [481, 676],
  61. [242, 819],
  62. [621, 819]
  63. ])
  64. bottom_right = np.array([
  65. [621, 819],
  66. [242, 819],
  67. [0, 1024],
  68. [488, 1024]
  69. ])
  70. # _draw_closest_and_future(
  71. # (big_closest, big_future),
  72. # (small_closest, small_future),
  73. # img_rgb)
  74. ramp_location = locate_ramp(small_closest, small_future,
  75. upper_left, bottom_left,
  76. upper_right, bottom_right,
  77. results)
  78. if not ramp_location:
  79. ramp_location = "no_ramp"
  80. return ramp_location
  81. def locate_ramp(small_closest, small_future,
  82. upper_left, bottom_left,
  83. upper_right, bottom_right,
  84. results):
  85. if has_ramp(results, (small_closest, small_future), 0.9, 0.7):
  86. return "small_trapezoid"
  87. right_location = has_ramp(results, (bottom_right, upper_right), 0.4, 0.2)
  88. if right_location:
  89. return f"{right_location}_left"
  90. left_location = has_ramp(results, (bottom_left, upper_left), 0.4, 0.2)
  91. if left_location:
  92. return f"{left_location}_right"
  93. return False
  94. def has_ramp(results, areas, partial_ratio, all_ratio):
  95. bottom, upper = areas
  96. input_height, input_width = results.shape
  97. mask = np.zeros((input_height, input_width), dtype=np.uint8)
  98. mask = cv2.fillPoly(mask, [bottom], 1)
  99. label, count = np.unique(results[mask == 1], return_counts=True)
  100. has_ramp_bottom = parse_result(label, count, partial_ratio)
  101. mask = np.zeros((input_height, input_width), dtype=np.uint8)
  102. mask = cv2.fillPoly(mask, [upper], 1)
  103. label, count = np.unique(results[mask == 1], return_counts=True)
  104. has_ramp_upper = parse_result(label, count, partial_ratio)
  105. if has_ramp_bottom:
  106. return "bottom"
  107. if has_ramp_upper:
  108. return "upper"
  109. mask = np.zeros((input_height, input_width), dtype=np.uint8)
  110. mask = cv2.fillPoly(mask, [bottom], 1)
  111. mask = cv2.fillPoly(mask, [upper], 1)
  112. label, count = np.unique(results[mask == 1], return_counts=True)
  113. has_ramp = parse_result(label, count, all_ratio)
  114. if has_ramp:
  115. return "center"
  116. else:
  117. return False
  118. def _draw_closest_and_future(big, small, img_rgb):
  119. big_closest, big_future = big
  120. small_closest, small_future = small
  121. img_array = np.array(img_rgb)
  122. big_closest_color = [0, 50, 50]
  123. big_future_color = [0, 69, 0]
  124. small_closest_color = [0, 100, 100]
  125. small_future_color = [69, 69, 69]
  126. height, weight, channel = img_array.shape
  127. img = np.zeros((height, weight, channel), dtype=np.uint8)
  128. img = cv2.fillPoly(img, [big_closest], big_closest_color)
  129. img = cv2.fillPoly(img, [big_future], big_future_color)
  130. img = cv2.fillPoly(img, [small_closest], small_closest_color)
  131. img = cv2.fillPoly(img, [small_future], small_future_color)
  132. img_array = 0.3 * img + img_array
  133. cv2.imwrite("test.png", img_array)