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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. # Licensed under the Apache License, Version 2.0 (the "License");
  2. # you may not use this file except in compliance with the License.
  3. # You may obtain a copy of the License at
  4. #
  5. # http://www.apache.org/licenses/LICENSE-2.0
  6. #
  7. # Unless required by applicable law or agreed to in writing, software
  8. # distributed under the License is distributed on an "AS IS" BASIS,
  9. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. # See the License for the specific language governing permissions and
  11. # limitations under the License.
  12. #
  13. import logging
  14. import os
  15. import PIL
  16. from PIL import ImageDraw
  17. def save_results(image_list, results, labels, output_dir='output/', threshold=0.5):
  18. if not os.path.exists(output_dir):
  19. os.makedirs(output_dir)
  20. for idx, im in enumerate(image_list):
  21. im = draw_box(im, results[idx], labels, threshold=threshold)
  22. out_path = os.path.join(output_dir, f"{idx}.jpg")
  23. im.save(out_path, quality=95)
  24. logging.debug("save result to: " + out_path)
  25. def draw_box(im, result, lables, threshold=0.5):
  26. draw_thickness = min(im.size) // 320
  27. draw = ImageDraw.Draw(im)
  28. color_list = get_color_map_list(len(lables))
  29. clsid2color = {n.lower():color_list[i] for i,n in enumerate(lables)}
  30. result = [r for r in result if r["score"] >= threshold]
  31. for dt in result:
  32. color = tuple(clsid2color[dt["type"]])
  33. xmin, ymin, xmax, ymax = dt["bbox"]
  34. draw.line(
  35. [(xmin, ymin), (xmin, ymax), (xmax, ymax), (xmax, ymin),
  36. (xmin, ymin)],
  37. width=draw_thickness,
  38. fill=color)
  39. # draw label
  40. text = "{} {:.4f}".format(dt["type"], dt["score"])
  41. tw, th = imagedraw_textsize_c(draw, text)
  42. draw.rectangle(
  43. [(xmin + 1, ymin - th), (xmin + tw + 1, ymin)], fill=color)
  44. draw.text((xmin + 1, ymin - th), text, fill=(255, 255, 255))
  45. return im
  46. def get_color_map_list(num_classes):
  47. """
  48. Args:
  49. num_classes (int): number of class
  50. Returns:
  51. color_map (list): RGB color list
  52. """
  53. color_map = num_classes * [0, 0, 0]
  54. for i in range(0, num_classes):
  55. j = 0
  56. lab = i
  57. while lab:
  58. color_map[i * 3] |= (((lab >> 0) & 1) << (7 - j))
  59. color_map[i * 3 + 1] |= (((lab >> 1) & 1) << (7 - j))
  60. color_map[i * 3 + 2] |= (((lab >> 2) & 1) << (7 - j))
  61. j += 1
  62. lab >>= 3
  63. color_map = [color_map[i:i + 3] for i in range(0, len(color_map), 3)]
  64. return color_map
  65. def imagedraw_textsize_c(draw, text):
  66. if int(PIL.__version__.split('.')[0]) < 10:
  67. tw, th = draw.textsize(text)
  68. else:
  69. left, top, right, bottom = draw.textbbox((0, 0), text)
  70. tw, th = right - left, bottom - top
  71. return tw, th