visualizer.py 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  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. pose3d_res,
  32. im_id,
  33. catid2name,
  34. threshold=0.5):
  35. """
  36. Visualize bbox and mask results
  37. """
  38. if bbox_res is not None:
  39. image = draw_bbox(image, im_id, catid2name, bbox_res, threshold)
  40. if mask_res is not None:
  41. image = draw_mask(image, im_id, mask_res, threshold)
  42. if segm_res is not None:
  43. image = draw_segm(image, im_id, catid2name, segm_res, threshold)
  44. if keypoint_res is not None:
  45. image = draw_pose(image, keypoint_res, threshold)
  46. if pose3d_res is not None:
  47. image = draw_pose3d(image, pose3d_res, threshold)
  48. return image
  49. def draw_mask(image, im_id, segms, threshold, alpha=0.7):
  50. """
  51. Draw mask on image
  52. """
  53. mask_color_id = 0
  54. w_ratio = .4
  55. color_list = colormap(rgb=True)
  56. img_array = np.array(image).astype('float32')
  57. for dt in np.array(segms):
  58. if im_id != dt['image_id']:
  59. continue
  60. segm, score = dt['segmentation'], dt['score']
  61. if score < threshold:
  62. continue
  63. import pycocotools.mask as mask_util
  64. mask = mask_util.decode(segm) * 255
  65. color_mask = color_list[mask_color_id % len(color_list), 0:3]
  66. mask_color_id += 1
  67. for c in range(3):
  68. color_mask[c] = color_mask[c] * (1 - w_ratio) + w_ratio * 255
  69. idx = np.nonzero(mask)
  70. img_array[idx[0], idx[1], :] *= 1.0 - alpha
  71. img_array[idx[0], idx[1], :] += alpha * color_mask
  72. return Image.fromarray(img_array.astype('uint8'))
  73. def draw_bbox(image, im_id, catid2name, bboxes, threshold):
  74. """
  75. Draw bbox on image
  76. """
  77. draw = ImageDraw.Draw(image)
  78. catid2color = {}
  79. color_list = colormap(rgb=True)[:40]
  80. for dt in np.array(bboxes):
  81. if im_id != dt['image_id']:
  82. continue
  83. catid, bbox, score = dt['category_id'], dt['bbox'], dt['score']
  84. if score < threshold:
  85. continue
  86. if catid not in catid2color:
  87. idx = np.random.randint(len(color_list))
  88. catid2color[catid] = color_list[idx]
  89. color = tuple(catid2color[catid])
  90. # draw bbox
  91. if len(bbox) == 4:
  92. # draw bbox
  93. xmin, ymin, w, h = bbox
  94. xmax = xmin + w
  95. ymax = ymin + h
  96. draw.line(
  97. [(xmin, ymin), (xmin, ymax), (xmax, ymax), (xmax, ymin),
  98. (xmin, ymin)],
  99. width=2,
  100. fill=color)
  101. elif len(bbox) == 8:
  102. x1, y1, x2, y2, x3, y3, x4, y4 = bbox
  103. draw.line(
  104. [(x1, y1), (x2, y2), (x3, y3), (x4, y4), (x1, y1)],
  105. width=2,
  106. fill=color)
  107. xmin = min(x1, x2, x3, x4)
  108. ymin = min(y1, y2, y3, y4)
  109. else:
  110. logger.error('the shape of bbox must be [M, 4] or [M, 8]!')
  111. # draw label
  112. text = "{} {:.2f}".format(catid2name[catid], score)
  113. tw, th = draw.textsize(text)
  114. draw.rectangle(
  115. [(xmin + 1, ymin - th), (xmin + tw + 1, ymin)], fill=color)
  116. draw.text((xmin + 1, ymin - th), text, fill=(255, 255, 255))
  117. return image
  118. def save_result(save_path, results, catid2name, threshold):
  119. """
  120. save result as txt
  121. """
  122. img_id = int(results["im_id"])
  123. with open(save_path, 'w') as f:
  124. if "bbox_res" in results:
  125. for dt in results["bbox_res"]:
  126. catid, bbox, score = dt['category_id'], dt['bbox'], dt['score']
  127. if score < threshold:
  128. continue
  129. # each bbox result as a line
  130. # for rbox: classname score x1 y1 x2 y2 x3 y3 x4 y4
  131. # for bbox: classname score x1 y1 w h
  132. bbox_pred = '{} {} '.format(catid2name[catid],
  133. score) + ' '.join(
  134. [str(e) for e in bbox])
  135. f.write(bbox_pred + '\n')
  136. elif "keypoint_res" in results:
  137. for dt in results["keypoint_res"]:
  138. kpts = dt['keypoints']
  139. scores = dt['score']
  140. keypoint_pred = [img_id, scores, kpts]
  141. print(keypoint_pred, file=f)
  142. else:
  143. print("No valid results found, skip txt save")
  144. def draw_segm(image,
  145. im_id,
  146. catid2name,
  147. segms,
  148. threshold,
  149. alpha=0.7,
  150. draw_box=True):
  151. """
  152. Draw segmentation on image
  153. """
  154. mask_color_id = 0
  155. w_ratio = .4
  156. color_list = colormap(rgb=True)
  157. img_array = np.array(image).astype('float32')
  158. for dt in np.array(segms):
  159. if im_id != dt['image_id']:
  160. continue
  161. segm, score, catid = dt['segmentation'], dt['score'], dt['category_id']
  162. if score < threshold:
  163. continue
  164. import pycocotools.mask as mask_util
  165. mask = mask_util.decode(segm) * 255
  166. color_mask = color_list[mask_color_id % len(color_list), 0:3]
  167. mask_color_id += 1
  168. for c in range(3):
  169. color_mask[c] = color_mask[c] * (1 - w_ratio) + w_ratio * 255
  170. idx = np.nonzero(mask)
  171. img_array[idx[0], idx[1], :] *= 1.0 - alpha
  172. img_array[idx[0], idx[1], :] += alpha * color_mask
  173. if not draw_box:
  174. center_y, center_x = ndimage.measurements.center_of_mass(mask)
  175. label_text = "{}".format(catid2name[catid])
  176. vis_pos = (max(int(center_x) - 10, 0), int(center_y))
  177. cv2.putText(img_array, label_text, vis_pos,
  178. cv2.FONT_HERSHEY_COMPLEX, 0.3, (255, 255, 255))
  179. else:
  180. mask = mask_util.decode(segm) * 255
  181. sum_x = np.sum(mask, axis=0)
  182. x = np.where(sum_x > 0.5)[0]
  183. sum_y = np.sum(mask, axis=1)
  184. y = np.where(sum_y > 0.5)[0]
  185. x0, x1, y0, y1 = x[0], x[-1], y[0], y[-1]
  186. cv2.rectangle(img_array, (x0, y0), (x1, y1),
  187. tuple(color_mask.astype('int32').tolist()), 1)
  188. bbox_text = '%s %.2f' % (catid2name[catid], score)
  189. t_size = cv2.getTextSize(bbox_text, 0, 0.3, thickness=1)[0]
  190. cv2.rectangle(img_array, (x0, y0), (x0 + t_size[0],
  191. y0 - t_size[1] - 3),
  192. tuple(color_mask.astype('int32').tolist()), -1)
  193. cv2.putText(
  194. img_array,
  195. bbox_text, (x0, y0 - 2),
  196. cv2.FONT_HERSHEY_SIMPLEX,
  197. 0.3, (0, 0, 0),
  198. 1,
  199. lineType=cv2.LINE_AA)
  200. return Image.fromarray(img_array.astype('uint8'))
  201. def draw_pose(image,
  202. results,
  203. visual_thread=0.6,
  204. save_name='pose.jpg',
  205. save_dir='output',
  206. returnimg=False,
  207. ids=None):
  208. try:
  209. import matplotlib.pyplot as plt
  210. import matplotlib
  211. plt.switch_backend('agg')
  212. except Exception as e:
  213. logger.error('Matplotlib not found, please install matplotlib.'
  214. 'for example: `pip install matplotlib`.')
  215. raise e
  216. skeletons = np.array([item['keypoints'] for item in results])
  217. kpt_nums = 17
  218. if len(skeletons) > 0:
  219. kpt_nums = int(skeletons.shape[1] / 3)
  220. skeletons = skeletons.reshape(-1, kpt_nums, 3)
  221. if kpt_nums == 17: #plot coco keypoint
  222. EDGES = [(0, 1), (0, 2), (1, 3), (2, 4), (3, 5), (4, 6), (5, 7), (6, 8),
  223. (7, 9), (8, 10), (5, 11), (6, 12), (11, 13), (12, 14),
  224. (13, 15), (14, 16), (11, 12)]
  225. else: #plot mpii keypoint
  226. EDGES = [(0, 1), (1, 2), (3, 4), (4, 5), (2, 6), (3, 6), (6, 7), (7, 8),
  227. (8, 9), (10, 11), (11, 12), (13, 14), (14, 15), (8, 12),
  228. (8, 13)]
  229. NUM_EDGES = len(EDGES)
  230. colors = [[255, 0, 0], [255, 85, 0], [255, 170, 0], [255, 255, 0], [170, 255, 0], [85, 255, 0], [0, 255, 0], \
  231. [0, 255, 85], [0, 255, 170], [0, 255, 255], [0, 170, 255], [0, 85, 255], [0, 0, 255], [85, 0, 255], \
  232. [170, 0, 255], [255, 0, 255], [255, 0, 170], [255, 0, 85]]
  233. cmap = matplotlib.cm.get_cmap('hsv')
  234. plt.figure()
  235. img = np.array(image).astype('float32')
  236. color_set = results['colors'] if 'colors' in results else None
  237. if 'bbox' in results and ids is None:
  238. bboxs = results['bbox']
  239. for j, rect in enumerate(bboxs):
  240. xmin, ymin, xmax, ymax = rect
  241. color = colors[0] if color_set is None else colors[color_set[j] %
  242. len(colors)]
  243. cv2.rectangle(img, (xmin, ymin), (xmax, ymax), color, 1)
  244. canvas = img.copy()
  245. for i in range(kpt_nums):
  246. for j in range(len(skeletons)):
  247. if skeletons[j][i, 2] < visual_thread:
  248. continue
  249. if ids is None:
  250. color = colors[i] if color_set is None else colors[color_set[j]
  251. %
  252. len(colors)]
  253. else:
  254. color = get_color(ids[j])
  255. cv2.circle(
  256. canvas,
  257. tuple(skeletons[j][i, 0:2].astype('int32')),
  258. 2,
  259. color,
  260. thickness=-1)
  261. to_plot = cv2.addWeighted(img, 0.3, canvas, 0.7, 0)
  262. fig = matplotlib.pyplot.gcf()
  263. stickwidth = 2
  264. for i in range(NUM_EDGES):
  265. for j in range(len(skeletons)):
  266. edge = EDGES[i]
  267. if skeletons[j][edge[0], 2] < visual_thread or skeletons[j][edge[
  268. 1], 2] < visual_thread:
  269. continue
  270. cur_canvas = canvas.copy()
  271. X = [skeletons[j][edge[0], 1], skeletons[j][edge[1], 1]]
  272. Y = [skeletons[j][edge[0], 0], skeletons[j][edge[1], 0]]
  273. mX = np.mean(X)
  274. mY = np.mean(Y)
  275. length = ((X[0] - X[1])**2 + (Y[0] - Y[1])**2)**0.5
  276. angle = math.degrees(math.atan2(X[0] - X[1], Y[0] - Y[1]))
  277. polygon = cv2.ellipse2Poly((int(mY), int(mX)),
  278. (int(length / 2), stickwidth),
  279. int(angle), 0, 360, 1)
  280. if ids is None:
  281. color = colors[i] if color_set is None else colors[color_set[j]
  282. %
  283. len(colors)]
  284. else:
  285. color = get_color(ids[j])
  286. cv2.fillConvexPoly(cur_canvas, polygon, color)
  287. canvas = cv2.addWeighted(canvas, 0.4, cur_canvas, 0.6, 0)
  288. image = Image.fromarray(canvas.astype('uint8'))
  289. plt.close()
  290. return image
  291. def draw_pose3d(image,
  292. results,
  293. visual_thread=0.6,
  294. save_name='pose3d.jpg',
  295. save_dir='output',
  296. returnimg=False,
  297. ids=None):
  298. try:
  299. import matplotlib.pyplot as plt
  300. import matplotlib
  301. plt.switch_backend('agg')
  302. except Exception as e:
  303. logger.error('Matplotlib not found, please install matplotlib.'
  304. 'for example: `pip install matplotlib`.')
  305. raise e
  306. pose3d = np.array(results[0]['pose3d']) * 1000
  307. if pose3d.shape[0] == 24:
  308. joints_connectivity_dict = [
  309. [0, 1, 0], [1, 2, 0], [5, 4, 1], [4, 3, 1], [2, 3, 0], [2, 14, 1],
  310. [3, 14, 1], [14, 15, 1], [15, 16, 1], [16, 12, 1], [6, 7, 0],
  311. [7, 8, 0], [11, 10, 1], [10, 9, 1], [8, 12, 0], [9, 12, 1],
  312. [12, 19, 1], [19, 18, 1], [19, 20, 0], [19, 21, 1], [22, 20, 0],
  313. [23, 21, 1]
  314. ]
  315. elif pose3d.shape[0] == 14:
  316. joints_connectivity_dict = [
  317. [0, 1, 0], [1, 2, 0], [5, 4, 1], [4, 3, 1], [2, 3, 0], [2, 12, 0],
  318. [3, 12, 1], [6, 7, 0], [7, 8, 0], [11, 10, 1], [10, 9, 1],
  319. [8, 12, 0], [9, 12, 1], [12, 13, 1]
  320. ]
  321. else:
  322. print(
  323. "not defined joints number :{}, cannot visualize because unknown of joint connectivity".
  324. format(pose.shape[0]))
  325. return
  326. def draw3Dpose(pose3d,
  327. ax,
  328. lcolor="#3498db",
  329. rcolor="#e74c3c",
  330. add_labels=False):
  331. # pose3d = orthographic_projection(pose3d, cam)
  332. for i in joints_connectivity_dict:
  333. x, y, z = [
  334. np.array([pose3d[i[0], j], pose3d[i[1], j]]) for j in range(3)
  335. ]
  336. ax.plot(-x, -z, -y, lw=2, c=lcolor if i[2] else rcolor)
  337. RADIUS = 1000
  338. center_xy = 2 if pose3d.shape[0] == 14 else 14
  339. x, y, z = pose3d[center_xy, 0], pose3d[center_xy, 1], pose3d[center_xy,
  340. 2]
  341. ax.set_xlim3d([-RADIUS + x, RADIUS + x])
  342. ax.set_ylim3d([-RADIUS + y, RADIUS + y])
  343. ax.set_zlim3d([-RADIUS + z, RADIUS + z])
  344. ax.set_xlabel("x")
  345. ax.set_ylabel("y")
  346. ax.set_zlabel("z")
  347. def draw2Dpose(pose2d,
  348. ax,
  349. lcolor="#3498db",
  350. rcolor="#e74c3c",
  351. add_labels=False):
  352. for i in joints_connectivity_dict:
  353. if pose2d[i[0], 2] and pose2d[i[1], 2]:
  354. x, y = [
  355. np.array([pose2d[i[0], j], pose2d[i[1], j]])
  356. for j in range(2)
  357. ]
  358. ax.plot(x, y, 0, lw=2, c=lcolor if i[2] else rcolor)
  359. def draw_img_pose(pose3d,
  360. pose2d=None,
  361. frame=None,
  362. figsize=(12, 12),
  363. savepath=None):
  364. fig = plt.figure(figsize=figsize, dpi=80)
  365. # fig.clear()
  366. fig.tight_layout()
  367. ax = fig.add_subplot(221)
  368. if frame is not None:
  369. ax.imshow(frame, interpolation='nearest')
  370. if pose2d is not None:
  371. draw2Dpose(pose2d, ax)
  372. ax = fig.add_subplot(222, projection='3d')
  373. ax.view_init(45, 45)
  374. draw3Dpose(pose3d, ax)
  375. ax = fig.add_subplot(223, projection='3d')
  376. ax.view_init(0, 0)
  377. draw3Dpose(pose3d, ax)
  378. ax = fig.add_subplot(224, projection='3d')
  379. ax.view_init(0, 90)
  380. draw3Dpose(pose3d, ax)
  381. if savepath is not None:
  382. plt.savefig(savepath)
  383. plt.close()
  384. else:
  385. return fig
  386. def fig2data(fig):
  387. """
  388. fig = plt.figure()
  389. image = fig2data(fig)
  390. @brief Convert a Matplotlib figure to a 4D numpy array with RGBA channels and return it
  391. @param fig a matplotlib figure
  392. @return a numpy 3D array of RGBA values
  393. """
  394. # draw the renderer
  395. fig.canvas.draw()
  396. # Get the RGBA buffer from the figure
  397. w, h = fig.canvas.get_width_height()
  398. buf = np.fromstring(fig.canvas.tostring_argb(), dtype=np.uint8)
  399. buf.shape = (w, h, 4)
  400. # canvas.tostring_argb give pixmap in ARGB mode. Roll the ALPHA channel to have it in RGBA mode
  401. buf = np.roll(buf, 3, axis=2)
  402. image = Image.frombytes("RGBA", (w, h), buf.tostring())
  403. return image.convert("RGB")
  404. fig = draw_img_pose(pose3d, frame=image)
  405. data = fig2data(fig)
  406. return data