您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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 os
  14. import PIL
  15. from PIL import ImageDraw
  16. def save_results(image_list, results, labels, output_dir='output/', threshold=0.5):
  17. if not os.path.exists(output_dir):
  18. os.makedirs(output_dir)
  19. for idx, im in enumerate(image_list):
  20. im = draw_box(im, results[idx], labels, threshold=threshold)
  21. out_path = os.path.join(output_dir, f"{idx}.jpg")
  22. im.save(out_path, quality=95)
  23. print("save result to: " + out_path)
  24. def draw_box(im, result, lables, threshold=0.5):
  25. draw_thickness = min(im.size) // 320
  26. draw = ImageDraw.Draw(im)
  27. color_list = get_color_map_list(len(lables))
  28. clsid2color = {n.lower():color_list[i] for i,n in enumerate(lables)}
  29. result = [r for r in result if r["score"] >= threshold]
  30. for dt in result:
  31. color = tuple(clsid2color[dt["type"]])
  32. xmin, ymin, xmax, ymax = dt["bbox"]
  33. draw.line(
  34. [(xmin, ymin), (xmin, ymax), (xmax, ymax), (xmax, ymin),
  35. (xmin, ymin)],
  36. width=draw_thickness,
  37. fill=color)
  38. # draw label
  39. text = "{} {:.4f}".format(dt["type"], dt["score"])
  40. tw, th = imagedraw_textsize_c(draw, text)
  41. draw.rectangle(
  42. [(xmin + 1, ymin - th), (xmin + tw + 1, ymin)], fill=color)
  43. draw.text((xmin + 1, ymin - th), text, fill=(255, 255, 255))
  44. return im
  45. def get_color_map_list(num_classes):
  46. """
  47. Args:
  48. num_classes (int): number of class
  49. Returns:
  50. color_map (list): RGB color list
  51. """
  52. color_map = num_classes * [0, 0, 0]
  53. for i in range(0, num_classes):
  54. j = 0
  55. lab = i
  56. while lab:
  57. color_map[i * 3] |= (((lab >> 0) & 1) << (7 - j))
  58. color_map[i * 3 + 1] |= (((lab >> 1) & 1) << (7 - j))
  59. color_map[i * 3 + 2] |= (((lab >> 2) & 1) << (7 - j))
  60. j += 1
  61. lab >>= 3
  62. color_map = [color_map[i:i + 3] for i in range(0, len(color_map), 3)]
  63. return color_map
  64. def imagedraw_textsize_c(draw, text):
  65. if int(PIL.__version__.split('.')[0]) < 10:
  66. tw, th = draw.textsize(text)
  67. else:
  68. left, top, right, bottom = draw.textbbox((0, 0), text)
  69. tw, th = right - left, bottom - top
  70. return tw, th