落水人员检测
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.

159 lines
5.6KB

  1. import os
  2. import numpy as np
  3. from PIL import Image
  4. __all__ = ['get_color_pallete', 'print_iou', 'set_img_color',
  5. 'show_prediction', 'show_colorful_images', 'save_colorful_images']
  6. def print_iou(iu, mean_pixel_acc, class_names=None, show_no_back=False):
  7. n = iu.size
  8. lines = []
  9. for i in range(n):
  10. if class_names is None:
  11. cls = 'Class %d:' % (i + 1)
  12. else:
  13. cls = '%d %s' % (i + 1, class_names[i])
  14. # lines.append('%-8s: %.3f%%' % (cls, iu[i] * 100))
  15. mean_IU = np.nanmean(iu)
  16. mean_IU_no_back = np.nanmean(iu[1:])
  17. if show_no_back:
  18. lines.append('mean_IU: %.3f%% || mean_IU_no_back: %.3f%% || mean_pixel_acc: %.3f%%' % (
  19. mean_IU * 100, mean_IU_no_back * 100, mean_pixel_acc * 100))
  20. else:
  21. lines.append('mean_IU: %.3f%% || mean_pixel_acc: %.3f%%' % (mean_IU * 100, mean_pixel_acc * 100))
  22. lines.append('=================================================')
  23. line = "\n".join(lines)
  24. print(line)
  25. def set_img_color(img, label, colors, background=0, show255=False):
  26. for i in range(len(colors)):
  27. if i != background:
  28. img[np.where(label == i)] = colors[i]
  29. if show255:
  30. img[np.where(label == 255)] = 255
  31. return img
  32. def show_prediction(img, pred, colors, background=0):
  33. im = np.array(img, np.uint8)
  34. set_img_color(im, pred, colors, background)
  35. out = np.array(im)
  36. return out
  37. def show_colorful_images(prediction, palettes):
  38. im = Image.fromarray(palettes[prediction.astype('uint8').squeeze()])
  39. im.show()
  40. def save_colorful_images(prediction, filename, output_dir, palettes):
  41. '''
  42. :param prediction: [B, H, W, C]
  43. '''
  44. im = Image.fromarray(palettes[prediction.astype('uint8').squeeze()])
  45. fn = os.path.join(output_dir, filename)
  46. out_dir = os.path.split(fn)[0]
  47. if not os.path.exists(out_dir):
  48. os.mkdir(out_dir)
  49. im.save(fn)
  50. def get_color_pallete(npimg, dataset='pascal_voc'):
  51. """Visualize image.
  52. Parameters
  53. ----------
  54. npimg : numpy.ndarray
  55. Single channel image with shape `H, W, 1`.
  56. dataset : str, default: 'pascal_voc'
  57. The dataset that model pretrained on. ('pascal_voc', 'ade20k')
  58. Returns
  59. -------
  60. out_img : PIL.Image
  61. Image with color pallete
  62. """
  63. # recovery boundary
  64. if dataset in ('pascal_voc', 'pascal_aug'):
  65. npimg[npimg == -1] = 255
  66. # put colormap
  67. if dataset == 'ade20k':
  68. npimg = npimg + 1
  69. out_img = Image.fromarray(npimg.astype('uint8'))
  70. out_img.putpalette(adepallete)
  71. return out_img
  72. elif dataset == 'citys':
  73. out_img = Image.fromarray(npimg.astype('uint8'))
  74. out_img.putpalette(cityspallete)
  75. return out_img
  76. out_img = Image.fromarray(npimg.astype('uint8'))
  77. out_img.putpalette(vocpallete)
  78. return out_img
  79. def _getvocpallete(num_cls):
  80. n = num_cls
  81. pallete = [0] * (n * 3)
  82. for j in range(0, n):
  83. lab = j
  84. pallete[j * 3 + 0] = 0
  85. pallete[j * 3 + 1] = 0
  86. pallete[j * 3 + 2] = 0
  87. i = 0
  88. while (lab > 0):
  89. pallete[j * 3 + 0] |= (((lab >> 0) & 1) << (7 - i))
  90. pallete[j * 3 + 1] |= (((lab >> 1) & 1) << (7 - i))
  91. pallete[j * 3 + 2] |= (((lab >> 2) & 1) << (7 - i))
  92. i = i + 1
  93. lab >>= 3
  94. return pallete
  95. vocpallete = _getvocpallete(256)
  96. adepallete = [
  97. 0, 0, 0, 120, 120, 120, 180, 120, 120, 6, 230, 230, 80, 50, 50, 4, 200, 3, 120, 120, 80, 140, 140, 140, 204,
  98. 5, 255, 230, 230, 230, 4, 250, 7, 224, 5, 255, 235, 255, 7, 150, 5, 61, 120, 120, 70, 8, 255, 51, 255, 6, 82,
  99. 143, 255, 140, 204, 255, 4, 255, 51, 7, 204, 70, 3, 0, 102, 200, 61, 230, 250, 255, 6, 51, 11, 102, 255, 255,
  100. 7, 71, 255, 9, 224, 9, 7, 230, 220, 220, 220, 255, 9, 92, 112, 9, 255, 8, 255, 214, 7, 255, 224, 255, 184, 6,
  101. 10, 255, 71, 255, 41, 10, 7, 255, 255, 224, 255, 8, 102, 8, 255, 255, 61, 6, 255, 194, 7, 255, 122, 8, 0, 255,
  102. 20, 255, 8, 41, 255, 5, 153, 6, 51, 255, 235, 12, 255, 160, 150, 20, 0, 163, 255, 140, 140, 140, 250, 10, 15,
  103. 20, 255, 0, 31, 255, 0, 255, 31, 0, 255, 224, 0, 153, 255, 0, 0, 0, 255, 255, 71, 0, 0, 235, 255, 0, 173, 255,
  104. 31, 0, 255, 11, 200, 200, 255, 82, 0, 0, 255, 245, 0, 61, 255, 0, 255, 112, 0, 255, 133, 255, 0, 0, 255, 163,
  105. 0, 255, 102, 0, 194, 255, 0, 0, 143, 255, 51, 255, 0, 0, 82, 255, 0, 255, 41, 0, 255, 173, 10, 0, 255, 173, 255,
  106. 0, 0, 255, 153, 255, 92, 0, 255, 0, 255, 255, 0, 245, 255, 0, 102, 255, 173, 0, 255, 0, 20, 255, 184, 184, 0,
  107. 31, 255, 0, 255, 61, 0, 71, 255, 255, 0, 204, 0, 255, 194, 0, 255, 82, 0, 10, 255, 0, 112, 255, 51, 0, 255, 0,
  108. 194, 255, 0, 122, 255, 0, 255, 163, 255, 153, 0, 0, 255, 10, 255, 112, 0, 143, 255, 0, 82, 0, 255, 163, 255,
  109. 0, 255, 235, 0, 8, 184, 170, 133, 0, 255, 0, 255, 92, 184, 0, 255, 255, 0, 31, 0, 184, 255, 0, 214, 255, 255,
  110. 0, 112, 92, 255, 0, 0, 224, 255, 112, 224, 255, 70, 184, 160, 163, 0, 255, 153, 0, 255, 71, 255, 0, 255, 0,
  111. 163, 255, 204, 0, 255, 0, 143, 0, 255, 235, 133, 255, 0, 255, 0, 235, 245, 0, 255, 255, 0, 122, 255, 245, 0,
  112. 10, 190, 212, 214, 255, 0, 0, 204, 255, 20, 0, 255, 255, 255, 0, 0, 153, 255, 0, 41, 255, 0, 255, 204, 41, 0,
  113. 255, 41, 255, 0, 173, 0, 255, 0, 245, 255, 71, 0, 255, 122, 0, 255, 0, 255, 184, 0, 92, 255, 184, 255, 0, 0,
  114. 133, 255, 255, 214, 0, 25, 194, 194, 102, 255, 0, 92, 0, 255]
  115. cityspallete = [
  116. 128, 64, 128,
  117. 244, 35, 232,
  118. 70, 70, 70,
  119. 102, 102, 156,
  120. 190, 153, 153,
  121. 153, 153, 153,
  122. 250, 170, 30,
  123. 220, 220, 0,
  124. 107, 142, 35,
  125. 152, 251, 152,
  126. 0, 130, 180,
  127. 220, 20, 60,
  128. 255, 0, 0,
  129. 0, 0, 142,
  130. 0, 0, 70,
  131. 0, 60, 100,
  132. 0, 80, 100,
  133. 0, 0, 230,
  134. 119, 11, 32,
  135. ]