visualizer.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. # Copyright (c) 2019 PaddlePaddle Authors. All Rights Reserved.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. from __future__ import absolute_import
  15. from __future__ import division
  16. from __future__ import print_function
  17. from __future__ import unicode_literals
  18. import numpy as np
  19. from PIL import Image, ImageDraw
  20. import cv2
  21. import math
  22. from .colormap import colormap
  23. from ppdet.utils.logger import setup_logger
  24. logger = setup_logger(__name__)
  25. __all__ = ['visualize_results']
  26. def visualize_results(image,
  27. bbox_res,
  28. mask_res,
  29. segm_res,
  30. keypoint_res,
  31. im_id,
  32. catid2name,
  33. threshold=0.5):
  34. """
  35. Visualize bbox and mask results
  36. """
  37. if bbox_res is not None:
  38. image = draw_bbox(image, im_id, catid2name, bbox_res, threshold)
  39. if mask_res is not None:
  40. image = draw_mask(image, im_id, mask_res, threshold)
  41. if segm_res is not None:
  42. image = draw_segm(image, im_id, catid2name, segm_res, threshold)
  43. if keypoint_res is not None:
  44. image = draw_pose(image, keypoint_res, threshold)
  45. return image
  46. def draw_mask(image, im_id, segms, threshold, alpha=0.7):
  47. """
  48. Draw mask on image
  49. """
  50. mask_color_id = 0
  51. w_ratio = .4
  52. color_list = colormap(rgb=True)
  53. img_array = np.array(image).astype('float32')
  54. for dt in np.array(segms):
  55. if im_id != dt['image_id']:
  56. continue
  57. segm, score = dt['segmentation'], dt['score']
  58. if score < threshold:
  59. continue
  60. import pycocotools.mask as mask_util
  61. mask = mask_util.decode(segm) * 255
  62. color_mask = color_list[mask_color_id % len(color_list), 0:3]
  63. mask_color_id += 1
  64. for c in range(3):
  65. color_mask[c] = color_mask[c] * (1 - w_ratio) + w_ratio * 255
  66. idx = np.nonzero(mask)
  67. img_array[idx[0], idx[1], :] *= 1.0 - alpha
  68. img_array[idx[0], idx[1], :] += alpha * color_mask
  69. return Image.fromarray(img_array.astype('uint8'))
  70. def draw_bbox(image, im_id, catid2name, bboxes, threshold):
  71. """
  72. Draw bbox on image
  73. """
  74. draw = ImageDraw.Draw(image)
  75. catid2color = {}
  76. color_list = colormap(rgb=True)[:40]
  77. for dt in np.array(bboxes):
  78. if im_id != dt['image_id']:
  79. continue
  80. catid, bbox, score = dt['category_id'], dt['bbox'], dt['score']
  81. if score < threshold:
  82. continue
  83. if catid not in catid2color:
  84. idx = np.random.randint(len(color_list))
  85. catid2color[catid] = color_list[idx]
  86. color = tuple(catid2color[catid])
  87. # draw bbox
  88. if len(bbox) == 4:
  89. # draw bbox
  90. xmin, ymin, w, h = bbox
  91. xmax = xmin + w
  92. ymax = ymin + h
  93. draw.line(
  94. [(xmin, ymin), (xmin, ymax), (xmax, ymax), (xmax, ymin),
  95. (xmin, ymin)],
  96. width=2,
  97. fill=color)
  98. elif len(bbox) == 8:
  99. x1, y1, x2, y2, x3, y3, x4, y4 = bbox
  100. draw.line(
  101. [(x1, y1), (x2, y2), (x3, y3), (x4, y4), (x1, y1)],
  102. width=2,
  103. fill=color)
  104. xmin = min(x1, x2, x3, x4)
  105. ymin = min(y1, y2, y3, y4)
  106. else:
  107. logger.error('the shape of bbox must be [M, 4] or [M, 8]!')
  108. # draw label
  109. text = "{} {:.2f}".format(catid2name[catid], score)
  110. tw, th = draw.textsize(text)
  111. draw.rectangle(
  112. [(xmin + 1, ymin - th), (xmin + tw + 1, ymin)], fill=color)
  113. draw.text((xmin + 1, ymin - th), text, fill=(255, 255, 255))
  114. return image
  115. def save_result(save_path, results, catid2name, threshold):
  116. """
  117. save result as txt
  118. """
  119. img_id = int(results["im_id"])
  120. with open(save_path, 'w') as f:
  121. if "bbox_res" in results:
  122. for dt in results["bbox_res"]:
  123. catid, bbox, score = dt['category_id'], dt['bbox'], dt['score']
  124. if score < threshold:
  125. continue
  126. # each bbox result as a line
  127. # for rbox: classname score x1 y1 x2 y2 x3 y3 x4 y4
  128. # for bbox: classname score x1 y1 w h
  129. bbox_pred = '{} {} '.format(catid2name[catid],
  130. score) + ' '.join(
  131. [str(e) for e in bbox])
  132. f.write(bbox_pred + '\n')
  133. elif "keypoint_res" in results:
  134. for dt in results["keypoint_res"]:
  135. kpts = dt['keypoints']
  136. scores = dt['score']
  137. keypoint_pred = [img_id, scores, kpts]
  138. print(keypoint_pred, file=f)
  139. else:
  140. print("No valid results found, skip txt save")
  141. def draw_segm(image,
  142. im_id,
  143. catid2name,
  144. segms,
  145. threshold,
  146. alpha=0.7,
  147. draw_box=True):
  148. """
  149. Draw segmentation on image
  150. """
  151. mask_color_id = 0
  152. w_ratio = .4
  153. color_list = colormap(rgb=True)
  154. img_array = np.array(image).astype('float32')
  155. for dt in np.array(segms):
  156. if im_id != dt['image_id']:
  157. continue
  158. segm, score, catid = dt['segmentation'], dt['score'], dt['category_id']
  159. if score < threshold:
  160. continue
  161. import pycocotools.mask as mask_util
  162. mask = mask_util.decode(segm) * 255
  163. color_mask = color_list[mask_color_id % len(color_list), 0:3]
  164. mask_color_id += 1
  165. for c in range(3):
  166. color_mask[c] = color_mask[c] * (1 - w_ratio) + w_ratio * 255
  167. idx = np.nonzero(mask)
  168. img_array[idx[0], idx[1], :] *= 1.0 - alpha
  169. img_array[idx[0], idx[1], :] += alpha * color_mask
  170. if not draw_box:
  171. center_y, center_x = ndimage.measurements.center_of_mass(mask)
  172. label_text = "{}".format(catid2name[catid])
  173. vis_pos = (max(int(center_x) - 10, 0), int(center_y))
  174. cv2.putText(img_array, label_text, vis_pos,
  175. cv2.FONT_HERSHEY_COMPLEX, 0.3, (255, 255, 255))
  176. else:
  177. mask = mask_util.decode(segm) * 255
  178. sum_x = np.sum(mask, axis=0)
  179. x = np.where(sum_x > 0.5)[0]
  180. sum_y = np.sum(mask, axis=1)
  181. y = np.where(sum_y > 0.5)[0]
  182. x0, x1, y0, y1 = x[0], x[-1], y[0], y[-1]
  183. cv2.rectangle(img_array, (x0, y0), (x1, y1),
  184. tuple(color_mask.astype('int32').tolist()), 1)
  185. bbox_text = '%s %.2f' % (catid2name[catid], score)
  186. t_size = cv2.getTextSize(bbox_text, 0, 0.3, thickness=1)[0]
  187. cv2.rectangle(img_array, (x0, y0), (x0 + t_size[0],
  188. y0 - t_size[1] - 3),
  189. tuple(color_mask.astype('int32').tolist()), -1)
  190. cv2.putText(
  191. img_array,
  192. bbox_text, (x0, y0 - 2),
  193. cv2.FONT_HERSHEY_SIMPLEX,
  194. 0.3, (0, 0, 0),
  195. 1,
  196. lineType=cv2.LINE_AA)
  197. return Image.fromarray(img_array.astype('uint8'))
  198. def draw_pose(image,
  199. results,
  200. visual_thread=0.6,
  201. save_name='pose.jpg',
  202. save_dir='output',
  203. returnimg=False,
  204. ids=None):
  205. try:
  206. import matplotlib.pyplot as plt
  207. import matplotlib
  208. plt.switch_backend('agg')
  209. except Exception as e:
  210. logger.error('Matplotlib not found, please install matplotlib.'
  211. 'for example: `pip install matplotlib`.')
  212. raise e
  213. skeletons = np.array([item['keypoints'] for item in results])
  214. kpt_nums = 17
  215. if len(skeletons) > 0:
  216. kpt_nums = int(skeletons.shape[1] / 3)
  217. skeletons = skeletons.reshape(-1, kpt_nums, 3)
  218. if kpt_nums == 17: #plot coco keypoint
  219. EDGES = [(0, 1), (0, 2), (1, 3), (2, 4), (3, 5), (4, 6), (5, 7), (6, 8),
  220. (7, 9), (8, 10), (5, 11), (6, 12), (11, 13), (12, 14),
  221. (13, 15), (14, 16), (11, 12)]
  222. else: #plot mpii keypoint
  223. EDGES = [(0, 1), (1, 2), (3, 4), (4, 5), (2, 6), (3, 6), (6, 7), (7, 8),
  224. (8, 9), (10, 11), (11, 12), (13, 14), (14, 15), (8, 12),
  225. (8, 13)]
  226. NUM_EDGES = len(EDGES)
  227. colors = [[255, 0, 0], [255, 85, 0], [255, 170, 0], [255, 255, 0], [170, 255, 0], [85, 255, 0], [0, 255, 0], \
  228. [0, 255, 85], [0, 255, 170], [0, 255, 255], [0, 170, 255], [0, 85, 255], [0, 0, 255], [85, 0, 255], \
  229. [170, 0, 255], [255, 0, 255], [255, 0, 170], [255, 0, 85]]
  230. cmap = matplotlib.cm.get_cmap('hsv')
  231. plt.figure()
  232. img = np.array(image).astype('float32')
  233. color_set = results['colors'] if 'colors' in results else None
  234. if 'bbox' in results and ids is None:
  235. bboxs = results['bbox']
  236. for j, rect in enumerate(bboxs):
  237. xmin, ymin, xmax, ymax = rect
  238. color = colors[0] if color_set is None else colors[color_set[j] %
  239. len(colors)]
  240. cv2.rectangle(img, (xmin, ymin), (xmax, ymax), color, 1)
  241. canvas = img.copy()
  242. for i in range(kpt_nums):
  243. for j in range(len(skeletons)):
  244. if skeletons[j][i, 2] < visual_thread:
  245. continue
  246. if ids is None:
  247. color = colors[i] if color_set is None else colors[color_set[j]
  248. %
  249. len(colors)]
  250. else:
  251. color = get_color(ids[j])
  252. cv2.circle(
  253. canvas,
  254. tuple(skeletons[j][i, 0:2].astype('int32')),
  255. 2,
  256. color,
  257. thickness=-1)
  258. to_plot = cv2.addWeighted(img, 0.3, canvas, 0.7, 0)
  259. fig = matplotlib.pyplot.gcf()
  260. stickwidth = 2
  261. for i in range(NUM_EDGES):
  262. for j in range(len(skeletons)):
  263. edge = EDGES[i]
  264. if skeletons[j][edge[0], 2] < visual_thread or skeletons[j][edge[
  265. 1], 2] < visual_thread:
  266. continue
  267. cur_canvas = canvas.copy()
  268. X = [skeletons[j][edge[0], 1], skeletons[j][edge[1], 1]]
  269. Y = [skeletons[j][edge[0], 0], skeletons[j][edge[1], 0]]
  270. mX = np.mean(X)
  271. mY = np.mean(Y)
  272. length = ((X[0] - X[1])**2 + (Y[0] - Y[1])**2)**0.5
  273. angle = math.degrees(math.atan2(X[0] - X[1], Y[0] - Y[1]))
  274. polygon = cv2.ellipse2Poly((int(mY), int(mX)),
  275. (int(length / 2), stickwidth),
  276. int(angle), 0, 360, 1)
  277. if ids is None:
  278. color = colors[i] if color_set is None else colors[color_set[j]
  279. %
  280. len(colors)]
  281. else:
  282. color = get_color(ids[j])
  283. cv2.fillConvexPoly(cur_canvas, polygon, color)
  284. canvas = cv2.addWeighted(canvas, 0.4, cur_canvas, 0.6, 0)
  285. image = Image.fromarray(canvas.astype('uint8'))
  286. plt.close()
  287. return image