visualization.py 3.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. # --------------------------------------------------------
  2. # Tensorflow Faster R-CNN
  3. # Licensed under The MIT License [see LICENSE for details]
  4. # Written by Xinlei Chen
  5. # --------------------------------------------------------
  6. from __future__ import absolute_import
  7. from __future__ import division
  8. from __future__ import print_function
  9. import numpy as np
  10. from six.moves import range
  11. import PIL.Image as Image
  12. import PIL.ImageColor as ImageColor
  13. import PIL.ImageDraw as ImageDraw
  14. import PIL.ImageFont as ImageFont
  15. STANDARD_COLORS = [
  16. 'AliceBlue', 'Chartreuse', 'Aqua', 'Aquamarine', 'Azure', 'Beige', 'Bisque',
  17. 'BlanchedAlmond', 'BlueViolet', 'BurlyWood', 'CadetBlue', 'AntiqueWhite',
  18. 'Chocolate', 'Coral', 'CornflowerBlue', 'Cornsilk', 'Crimson', 'Cyan',
  19. 'DarkCyan', 'DarkGoldenRod', 'DarkGrey', 'DarkKhaki', 'DarkOrange',
  20. 'DarkOrchid', 'DarkSalmon', 'DarkSeaGreen', 'DarkTurquoise', 'DarkViolet',
  21. 'DeepPink', 'DeepSkyBlue', 'DodgerBlue', 'FireBrick', 'FloralWhite',
  22. 'ForestGreen', 'Fuchsia', 'Gainsboro', 'GhostWhite', 'Gold', 'GoldenRod',
  23. 'Salmon', 'Tan', 'HoneyDew', 'HotPink', 'IndianRed', 'Ivory', 'Khaki',
  24. 'Lavender', 'LavenderBlush', 'LawnGreen', 'LemonChiffon', 'LightBlue',
  25. 'LightCoral', 'LightCyan', 'LightGoldenRodYellow', 'LightGray', 'LightGrey',
  26. 'LightGreen', 'LightPink', 'LightSalmon', 'LightSeaGreen', 'LightSkyBlue',
  27. 'LightSlateGray', 'LightSlateGrey', 'LightSteelBlue', 'LightYellow', 'Lime',
  28. 'LimeGreen', 'Linen', 'Magenta', 'MediumAquaMarine', 'MediumOrchid',
  29. 'MediumPurple', 'MediumSeaGreen', 'MediumSlateBlue', 'MediumSpringGreen',
  30. 'MediumTurquoise', 'MediumVioletRed', 'MintCream', 'MistyRose', 'Moccasin',
  31. 'NavajoWhite', 'OldLace', 'Olive', 'OliveDrab', 'Orange', 'OrangeRed',
  32. 'Orchid', 'PaleGoldenRod', 'PaleGreen', 'PaleTurquoise', 'PaleVioletRed',
  33. 'PapayaWhip', 'PeachPuff', 'Peru', 'Pink', 'Plum', 'PowderBlue', 'Purple',
  34. 'Red', 'RosyBrown', 'RoyalBlue', 'SaddleBrown', 'Green', 'SandyBrown',
  35. 'SeaGreen', 'SeaShell', 'Sienna', 'Silver', 'SkyBlue', 'SlateBlue',
  36. 'SlateGray', 'SlateGrey', 'Snow', 'SpringGreen', 'SteelBlue', 'GreenYellow',
  37. 'Teal', 'Thistle', 'Tomato', 'Turquoise', 'Violet', 'Wheat', 'White',
  38. 'WhiteSmoke', 'Yellow', 'YellowGreen'
  39. ]
  40. NUM_COLORS = len(STANDARD_COLORS)
  41. try:
  42. FONT = ImageFont.truetype('arial.ttf', 24)
  43. except IOError:
  44. FONT = ImageFont.load_default()
  45. def _draw_single_box(image, xmin, ymin, xmax, ymax, display_str, font, color='black', thickness=4):
  46. draw = ImageDraw.Draw(image)
  47. (left, right, top, bottom) = (xmin, xmax, ymin, ymax)
  48. draw.line([(left, top), (left, bottom), (right, bottom),
  49. (right, top), (left, top)], width=thickness, fill=color)
  50. text_bottom = bottom
  51. # Reverse list and print from bottom to top.
  52. text_width, text_height = font.getsize(display_str)
  53. margin = np.ceil(0.05 * text_height)
  54. draw.rectangle(
  55. [(left, text_bottom - text_height - 2 * margin), (left + text_width,
  56. text_bottom)],
  57. fill=color)
  58. draw.text(
  59. (left + margin, text_bottom - text_height - margin),
  60. display_str,
  61. fill='black',
  62. font=font)
  63. return image
  64. def draw_bounding_boxes(image, gt_boxes, im_info):
  65. num_boxes = gt_boxes.shape[0]
  66. gt_boxes_new = gt_boxes.copy()
  67. gt_boxes_new[:,:4] = np.round(gt_boxes_new[:,:4].copy() / im_info[2])
  68. disp_image = Image.fromarray(np.uint8(image[0]))
  69. for i in range(num_boxes):
  70. this_class = int(gt_boxes_new[i, 4])
  71. disp_image = _draw_single_box(disp_image,
  72. gt_boxes_new[i, 0],
  73. gt_boxes_new[i, 1],
  74. gt_boxes_new[i, 2],
  75. gt_boxes_new[i, 3],
  76. 'N%02d-C%02d' % (i, this_class),
  77. FONT,
  78. color=STANDARD_COLORS[this_class % NUM_COLORS])
  79. image[0, :] = np.array(disp_image)
  80. return image