visualize.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. # coding: utf-8
  2. # copyright (c) 2020 PaddlePaddle Authors. All Rights Reserve.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. from __future__ import division
  16. import cv2
  17. import numpy as np
  18. from PIL import Image, ImageDraw
  19. def visualize_box_mask(im, results, labels, mask_resolution=14, threshold=0.5):
  20. """
  21. Args:
  22. im (str/np.ndarray): path of image/np.ndarray read by cv2
  23. results (dict): include 'boxes': np.ndarray: shape:[N,6], N: number of box,
  24. matix element:[class, score, x_min, y_min, x_max, y_max]
  25. MaskRCNN's results include 'masks': np.ndarray:
  26. shape:[N, class_num, mask_resolution, mask_resolution]
  27. labels (list): labels:['class1', ..., 'classn']
  28. mask_resolution (int): shape of a mask is:[mask_resolution, mask_resolution]
  29. threshold (float): Threshold of score.
  30. Returns:
  31. im (PIL.Image.Image): visualized image
  32. """
  33. if isinstance(im, str):
  34. im = Image.open(im).convert('RGB')
  35. else:
  36. im = Image.fromarray(im)
  37. if 'masks' in results and 'boxes' in results:
  38. im = draw_mask(
  39. im,
  40. results['boxes'],
  41. results['masks'],
  42. labels,
  43. resolution=mask_resolution)
  44. if 'boxes' in results:
  45. im = draw_box(im, results['boxes'], labels)
  46. if 'segm' in results:
  47. im = draw_segm(
  48. im,
  49. results['segm'],
  50. results['label'],
  51. results['score'],
  52. labels,
  53. threshold=threshold)
  54. if 'landmark' in results:
  55. im = draw_lmk(im, results['landmark'])
  56. return im
  57. def get_color_map_list(num_classes):
  58. """
  59. Args:
  60. num_classes (int): number of class
  61. Returns:
  62. color_map (list): RGB color list
  63. """
  64. color_map = num_classes * [0, 0, 0]
  65. for i in range(0, num_classes):
  66. j = 0
  67. lab = i
  68. while lab:
  69. color_map[i * 3] |= (((lab >> 0) & 1) << (7 - j))
  70. color_map[i * 3 + 1] |= (((lab >> 1) & 1) << (7 - j))
  71. color_map[i * 3 + 2] |= (((lab >> 2) & 1) << (7 - j))
  72. j += 1
  73. lab >>= 3
  74. color_map = [color_map[i:i + 3] for i in range(0, len(color_map), 3)]
  75. return color_map
  76. def expand_boxes(boxes, scale=0.0):
  77. """
  78. Args:
  79. boxes (np.ndarray): shape:[N,4], N:number of box,
  80. matix element:[x_min, y_min, x_max, y_max]
  81. scale (float): scale of boxes
  82. Returns:
  83. boxes_exp (np.ndarray): expanded boxes
  84. """
  85. w_half = (boxes[:, 2] - boxes[:, 0]) * .5
  86. h_half = (boxes[:, 3] - boxes[:, 1]) * .5
  87. x_c = (boxes[:, 2] + boxes[:, 0]) * .5
  88. y_c = (boxes[:, 3] + boxes[:, 1]) * .5
  89. w_half *= scale
  90. h_half *= scale
  91. boxes_exp = np.zeros(boxes.shape)
  92. boxes_exp[:, 0] = x_c - w_half
  93. boxes_exp[:, 2] = x_c + w_half
  94. boxes_exp[:, 1] = y_c - h_half
  95. boxes_exp[:, 3] = y_c + h_half
  96. return boxes_exp
  97. def draw_mask(im, np_boxes, np_masks, labels, resolution=14, threshold=0.5):
  98. """
  99. Args:
  100. im (PIL.Image.Image): PIL image
  101. np_boxes (np.ndarray): shape:[N,6], N: number of box,
  102. matix element:[class, score, x_min, y_min, x_max, y_max]
  103. np_masks (np.ndarray): shape:[N, class_num, resolution, resolution]
  104. labels (list): labels:['class1', ..., 'classn']
  105. resolution (int): shape of a mask is:[resolution, resolution]
  106. threshold (float): threshold of mask
  107. Returns:
  108. im (PIL.Image.Image): visualized image
  109. """
  110. color_list = get_color_map_list(len(labels))
  111. scale = (resolution + 2.0) / resolution
  112. im_w, im_h = im.size
  113. w_ratio = 0.4
  114. alpha = 0.7
  115. im = np.array(im).astype('float32')
  116. rects = np_boxes[:, 2:]
  117. expand_rects = expand_boxes(rects, scale)
  118. expand_rects = expand_rects.astype(np.int32)
  119. clsid_scores = np_boxes[:, 0:2]
  120. padded_mask = np.zeros((resolution + 2, resolution + 2), dtype=np.float32)
  121. clsid2color = {}
  122. for idx in range(len(np_boxes)):
  123. clsid, score = clsid_scores[idx].tolist()
  124. clsid = int(clsid)
  125. xmin, ymin, xmax, ymax = expand_rects[idx].tolist()
  126. w = xmax - xmin + 1
  127. h = ymax - ymin + 1
  128. w = np.maximum(w, 1)
  129. h = np.maximum(h, 1)
  130. padded_mask[1:-1, 1:-1] = np_masks[idx, int(clsid), :, :]
  131. resized_mask = cv2.resize(padded_mask, (w, h))
  132. resized_mask = np.array(resized_mask > threshold, dtype=np.uint8)
  133. x0 = min(max(xmin, 0), im_w)
  134. x1 = min(max(xmax + 1, 0), im_w)
  135. y0 = min(max(ymin, 0), im_h)
  136. y1 = min(max(ymax + 1, 0), im_h)
  137. im_mask = np.zeros((im_h, im_w), dtype=np.uint8)
  138. im_mask[y0:y1, x0:x1] = resized_mask[(y0 - ymin):(y1 - ymin), (
  139. x0 - xmin):(x1 - xmin)]
  140. if clsid not in clsid2color:
  141. clsid2color[clsid] = color_list[clsid]
  142. color_mask = clsid2color[clsid]
  143. for c in range(3):
  144. color_mask[c] = color_mask[c] * (1 - w_ratio) + w_ratio * 255
  145. idx = np.nonzero(im_mask)
  146. color_mask = np.array(color_mask)
  147. im[idx[0], idx[1], :] *= 1.0 - alpha
  148. im[idx[0], idx[1], :] += alpha * color_mask
  149. return Image.fromarray(im.astype('uint8'))
  150. def draw_box(im, np_boxes, labels):
  151. """
  152. Args:
  153. im (PIL.Image.Image): PIL image
  154. np_boxes (np.ndarray): shape:[N,6], N: number of box,
  155. matix element:[class, score, x_min, y_min, x_max, y_max]
  156. labels (list): labels:['class1', ..., 'classn']
  157. Returns:
  158. im (PIL.Image.Image): visualized image
  159. """
  160. draw_thickness = min(im.size) // 320
  161. draw = ImageDraw.Draw(im)
  162. clsid2color = {}
  163. color_list = get_color_map_list(len(labels))
  164. for dt in np_boxes:
  165. clsid, bbox, score = int(dt[0]), dt[2:], dt[1]
  166. xmin, ymin, xmax, ymax = bbox
  167. w = xmax - xmin
  168. h = ymax - ymin
  169. if clsid not in clsid2color:
  170. clsid2color[clsid] = color_list[clsid]
  171. color = tuple(clsid2color[clsid])
  172. # draw bbox
  173. draw.line(
  174. [(xmin, ymin), (xmin, ymax), (xmax, ymax), (xmax, ymin),
  175. (xmin, ymin)],
  176. width=draw_thickness,
  177. fill=color)
  178. # draw label
  179. text = "{} {:.4f}".format(labels[clsid], score)
  180. tw, th = draw.textsize(text)
  181. draw.rectangle(
  182. [(xmin + 1, ymin - th), (xmin + tw + 1, ymin)], fill=color)
  183. draw.text((xmin + 1, ymin - th), text, fill=(255, 255, 255))
  184. return im
  185. def draw_segm(im,
  186. np_segms,
  187. np_label,
  188. np_score,
  189. labels,
  190. threshold=0.5,
  191. alpha=0.7):
  192. """
  193. Draw segmentation on image
  194. """
  195. mask_color_id = 0
  196. w_ratio = .4
  197. color_list = get_color_map_list(len(labels))
  198. im = np.array(im).astype('float32')
  199. clsid2color = {}
  200. np_segms = np_segms.astype(np.uint8)
  201. for i in range(np_segms.shape[0]):
  202. mask, score, clsid = np_segms[i], np_score[i], np_label[i] + 1
  203. if score < threshold:
  204. continue
  205. if clsid not in clsid2color:
  206. clsid2color[clsid] = color_list[clsid]
  207. color_mask = clsid2color[clsid]
  208. for c in range(3):
  209. color_mask[c] = color_mask[c] * (1 - w_ratio) + w_ratio * 255
  210. idx = np.nonzero(mask)
  211. color_mask = np.array(color_mask)
  212. im[idx[0], idx[1], :] *= 1.0 - alpha
  213. im[idx[0], idx[1], :] += alpha * color_mask
  214. sum_x = np.sum(mask, axis=0)
  215. x = np.where(sum_x > 0.5)[0]
  216. sum_y = np.sum(mask, axis=1)
  217. y = np.where(sum_y > 0.5)[0]
  218. x0, x1, y0, y1 = x[0], x[-1], y[0], y[-1]
  219. cv2.rectangle(im, (x0, y0), (x1, y1),
  220. tuple(color_mask.astype('int32').tolist()), 1)
  221. bbox_text = '%s %.2f' % (labels[clsid], score)
  222. t_size = cv2.getTextSize(bbox_text, 0, 0.3, thickness=1)[0]
  223. cv2.rectangle(im, (x0, y0), (x0 + t_size[0], y0 - t_size[1] - 3),
  224. tuple(color_mask.astype('int32').tolist()), -1)
  225. cv2.putText(
  226. im,
  227. bbox_text, (x0, y0 - 2),
  228. cv2.FONT_HERSHEY_SIMPLEX,
  229. 0.3, (0, 0, 0),
  230. 1,
  231. lineType=cv2.LINE_AA)
  232. return Image.fromarray(im.astype('uint8'))
  233. def lmk2out(bboxes, np_lmk, im_info, threshold=0.5, is_bbox_normalized=True):
  234. image_w, image_h = im_info['origin_shape']
  235. scale = im_info['scale']
  236. face_index, landmark, prior_box = np_lmk[:]
  237. xywh_res = []
  238. if bboxes.shape == (1, 1) or bboxes is None:
  239. return np.array([])
  240. prior = np.reshape(prior_box, (-1, 4))
  241. predict_lmk = np.reshape(landmark, (-1, 10))
  242. k = 0
  243. for i in range(bboxes.shape[0]):
  244. score = bboxes[i][1]
  245. if score < threshold:
  246. continue
  247. theindex = face_index[i][0]
  248. me_prior = prior[theindex, :]
  249. lmk_pred = predict_lmk[theindex, :]
  250. prior_h = me_prior[2] - me_prior[0]
  251. prior_w = me_prior[3] - me_prior[1]
  252. prior_h_center = (me_prior[2] + me_prior[0]) / 2
  253. prior_w_center = (me_prior[3] + me_prior[1]) / 2
  254. lmk_decode = np.zeros((10))
  255. for j in [0, 2, 4, 6, 8]:
  256. lmk_decode[j] = lmk_pred[j] * 0.1 * prior_w + prior_h_center
  257. for j in [1, 3, 5, 7, 9]:
  258. lmk_decode[j] = lmk_pred[j] * 0.1 * prior_h + prior_w_center
  259. if is_bbox_normalized:
  260. lmk_decode = lmk_decode * np.array([
  261. image_h, image_w, image_h, image_w, image_h, image_w, image_h,
  262. image_w, image_h, image_w
  263. ])
  264. xywh_res.append(lmk_decode)
  265. return np.asarray(xywh_res)
  266. def draw_lmk(image, lmk_results):
  267. draw = ImageDraw.Draw(image)
  268. for lmk_decode in lmk_results:
  269. for j in range(5):
  270. x1 = int(round(lmk_decode[2 * j]))
  271. y1 = int(round(lmk_decode[2 * j + 1]))
  272. draw.ellipse(
  273. (x1 - 2, y1 - 2, x1 + 3, y1 + 3), fill='green', outline='green')
  274. return image