detection.py 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. from detection.utils import *
  2. def detect(input_file, config):
  3. img = cv2.imread(input_file)
  4. height, width, _ = img.shape
  5. aspect_ratio = config["Aspect Ratio"]
  6. height = int(width*aspect_ratio)
  7. """ Resize image to limits in config file """
  8. limits = np.array(config['Limits'])
  9. transform_mat = cv2.getPerspectiveTransform(np.float32(limits), np.float32(
  10. [[0, 0], [width, 0], [width, height], [0, height]]))
  11. img = cv2.warpPerspective(img, transform_mat, (width, height))
  12. """ Prepare upper and lower mask board """
  13. upper_mask = np.zeros(img.shape[0:2], np.uint8)
  14. lower_mask = np.zeros(img.shape[0:2], np.uint8)
  15. upper_mask = cv2.rectangle(upper_mask, (0, 0), (width, int(height/2)), 255, thickness=cv2.FILLED)
  16. lower_mask = cv2.rectangle(lower_mask, (0, int(height / 2)), (width, height), 255, thickness=cv2.FILLED)
  17. """ Find blob """
  18. sat = saturation(img)
  19. # sum = mean_image([satA, satB])
  20. blob_mask = find_blob(sat)
  21. blob = cv2.bitwise_and(img, img, mask=blob_mask)
  22. """ Print blob information """
  23. print("Blob covering:")
  24. print("\t{:.2f}% of the board.".format(mean_percent_value(blob_mask)))
  25. print("\t{:.2f}% of the upper board.".format(
  26. mean_percent_value(cv2.bitwise_and(blob_mask, blob_mask, mask=upper_mask), img_ratio=0.5)))
  27. print("\t{:.2f}% of the lower board.".format(
  28. mean_percent_value(cv2.bitwise_and(blob_mask, blob_mask, mask=lower_mask), img_ratio=0.5)))
  29. """ Find food """
  30. food_list, food_mask, food_img = find_food(img, config['Min Food Size'], config['Low Food Color'], config['High Food Color'])
  31. """ Print food information """
  32. print("Total food discovered: " + str(len(food_list)))
  33. # for i, food in enumerate(food_list):
  34. # print("\tFood N°" + str(i) + ": " + str(food))
  35. return img, blob_mask, blob, food_mask, food_img
  36. def print_results(orig, blob_mask, blob, food_mask, food, discrete, scale=1.0, filename=None, hide=False):
  37. padding = 35
  38. nbr_width = 2
  39. nbr_height = 3
  40. font = cv2.FONT_HERSHEY_SIMPLEX
  41. fontsize = 0.45
  42. thickness = 1
  43. scaled_height = int(orig.shape[0]*scale)
  44. scaled_width = int(orig.shape[1]*scale)
  45. pad = np.zeros((scaled_height, padding, orig.shape[2]), dtype=np.uint8)
  46. line_pad = np.zeros((padding, (scaled_width + padding) * nbr_width + padding, orig.shape[2]), dtype=np.uint8)
  47. print_img = cv2.resize(orig, (scaled_width, scaled_height))
  48. middle = ((0, int(scaled_height/2)), (scaled_width, int(scaled_height/2)))
  49. cv2.line(print_img, middle[0], middle[1], (0, 255, 0), thickness=1)
  50. cv2.putText(print_img, 'Mid Line', (middle[0][0] + 5, middle[0][1] - 5),
  51. font, fontsize, (0, 255, 0), thickness, cv2.LINE_AA)
  52. print_blob_mask = cv2.resize(cv2.cvtColor(blob_mask, cv2.COLOR_GRAY2BGR), (scaled_width, scaled_height))
  53. print_blob = cv2.resize(blob, (scaled_width, scaled_height))
  54. print_food_mask = cv2.resize(cv2.cvtColor(food_mask, cv2.COLOR_GRAY2BGR), (scaled_width, scaled_height))
  55. print_food = cv2.resize(food, (scaled_width, scaled_height))
  56. print_discrete = cv2.resize(discrete, (scaled_width, scaled_height))
  57. concat_line1 = np.concatenate((pad, print_img, pad, print_discrete, pad), axis=1)
  58. concat_line2 = np.concatenate((pad, print_blob_mask, pad, print_blob, pad), axis=1)
  59. concat_line3 = np.concatenate((pad, print_food_mask, pad, print_food, pad), axis=1)
  60. aggregate = np.concatenate((line_pad, concat_line1, line_pad, concat_line2, line_pad, concat_line3, line_pad))
  61. cv2.putText(aggregate, 'Original:',
  62. (0 * (scaled_width + padding) + padding + 5, 0 * (scaled_height + padding) + padding - 5),
  63. font, fontsize, (255, 255, 255), thickness, cv2.LINE_AA)
  64. cv2.putText(aggregate, 'Discrete:',
  65. (1 * (scaled_width + padding) + padding + 5, 0 * (scaled_height + padding) + padding - 5),
  66. font, fontsize, (255, 255, 255), thickness, cv2.LINE_AA)
  67. cv2.putText(aggregate, 'Blob Mask:',
  68. (0 * (scaled_width + padding) + padding + 5, 1 * (scaled_height + padding) + padding - 5),
  69. font, fontsize, (255, 255, 255), thickness, cv2.LINE_AA)
  70. cv2.putText(aggregate, 'Blob:',
  71. (1 * (scaled_width + padding) + padding + 5, 1 * (scaled_height + padding) + padding - 5),
  72. font, fontsize, (255, 255, 255), thickness, cv2.LINE_AA)
  73. cv2.putText(aggregate, 'Food Mask:',
  74. (0 * (scaled_width + padding) + padding + 5, 2 * (scaled_height + padding) + padding - 5),
  75. font, fontsize, (255, 255, 255), thickness, cv2.LINE_AA)
  76. cv2.putText(aggregate, 'Food Regions:',
  77. (1 * (scaled_width + padding) + padding + 5, 2 * (scaled_height + padding) + padding - 5),
  78. font, fontsize, (255, 255, 255), thickness, cv2.LINE_AA)
  79. if filename is not None:
  80. cv2.imwrite(filename + ".jpg", aggregate)
  81. if not hide:
  82. cv2.imshow("Results", aggregate)
  83. print("\nPress any key...")
  84. cv2.waitKey(0)
  85. def discretize(blob_img, food_mask, width, height):
  86. img_height, img_width, _ = blob_img.shape
  87. discrete_blob = cv2.resize(blob_img, (width, height), interpolation=cv2.INTER_NEAREST)
  88. discrete_food = cv2.resize(food_mask, (width, height), interpolation=cv2.INTER_NEAREST)
  89. discrete_food_list = []
  90. for x in range(height):
  91. for y in range(width):
  92. if discrete_food[x, y] != 0:
  93. discrete_food_list.append((y, x))
  94. height, width, _ = discrete_blob.shape
  95. discrete_blob = cv2.cvtColor(discrete_blob, cv2.COLOR_BGR2GRAY)
  96. # If discrete blob has to be connected, used this :
  97. # contours, hierarchy = cv2.findContours(discrete_blob, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
  98. # c = max(contours, key=cv2.contourArea)
  99. # mask = np.zeros(discrete_blob.shape, np.uint8)
  100. # cv2.drawContours(mask, [c], -1, 255, cv2.FILLED)
  101. # discrete_blob = cv2.bitwise_and(discrete_blob, discrete_blob, mask=mask)
  102. discrete_blob_bgr = cv2.cvtColor(discrete_blob, cv2.COLOR_GRAY2BGR)
  103. discrete_img = cv2.resize(discrete_blob_bgr, (0, 0), fx=10, fy=10, interpolation=cv2.INTER_NEAREST)
  104. for (x, y) in discrete_food_list:
  105. cv2.rectangle(discrete_img, (x * 10, y * 10), ((x + 1) * 10, (y + 1) * 10), (0, 255, 0), thickness=cv2.FILLED)
  106. if discrete_blob[y, x] != 0:
  107. cv2.drawMarker(discrete_img, (x * 10 + 5, y * 10 + 5), (255, 255, 255), thickness=2, markerSize=9,
  108. markerType=cv2.MARKER_TILTED_CROSS)
  109. return discrete_img, discrete_blob, discrete_food_list