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