visualize.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. # copyright (c) 2021 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. import os
  15. import cv2
  16. import numpy as np
  17. import time
  18. import paddlex.utils.logging as logging
  19. from paddlex.utils import is_pic
  20. from .det_metrics.coco_utils import loadRes
  21. def visualize_detection(image,
  22. result,
  23. threshold=0.5,
  24. save_dir='./',
  25. color=None):
  26. """
  27. Visualize bbox and mask results
  28. """
  29. if isinstance(image, np.ndarray):
  30. image_name = str(int(time.time() * 1000)) + '.jpg'
  31. else:
  32. image_name = os.path.split(image)[-1]
  33. image = cv2.imread(image)
  34. image = draw_bbox_mask(image, result, threshold=threshold, color_map=color)
  35. if save_dir is not None:
  36. if not os.path.exists(save_dir):
  37. os.makedirs(save_dir)
  38. out_path = os.path.join(save_dir, 'visualize_{}'.format(image_name))
  39. cv2.imwrite(out_path, image)
  40. logging.info('The visualized result is saved at {}'.format(out_path))
  41. else:
  42. return image
  43. def visualize_segmentation(image,
  44. result,
  45. weight=0.6,
  46. save_dir='./',
  47. color=None):
  48. """
  49. Convert segment result to color image, and save added image.
  50. Args:
  51. image: the path of origin image
  52. result: the predict result of image
  53. weight: the image weight of visual image, and the result weight is (1 - weight)
  54. save_dir: the directory for saving visual image
  55. color: the list of a BGR-mode color for each label.
  56. """
  57. label_map = result['label_map'].astype("uint8")
  58. color_map = get_color_map_list(256)
  59. if color is not None:
  60. for i in range(len(color) // 3):
  61. color_map[i] = color[i * 3:(i + 1) * 3]
  62. color_map = np.array(color_map).astype("uint8")
  63. # Use OpenCV LUT for color mapping
  64. c1 = cv2.LUT(label_map, color_map[:, 0])
  65. c2 = cv2.LUT(label_map, color_map[:, 1])
  66. c3 = cv2.LUT(label_map, color_map[:, 2])
  67. pseudo_img = np.dstack((c1, c2, c3))
  68. if isinstance(image, np.ndarray):
  69. im = image
  70. image_name = str(int(time.time() * 1000)) + '.jpg'
  71. if image.shape[2] != 3:
  72. logging.info(
  73. "The image is not 3-channel array, so predicted label map is shown as a pseudo color image."
  74. )
  75. weight = 0.
  76. else:
  77. image_name = os.path.split(image)[-1]
  78. if not is_pic(image):
  79. logging.info(
  80. "The image cannot be opened by opencv, so predicted label map is shown as a pseudo color image."
  81. )
  82. image_name = image_name.split('.')[0] + '.jpg'
  83. weight = 0.
  84. else:
  85. im = cv2.imread(image)
  86. if abs(weight) < 1e-5:
  87. vis_result = pseudo_img
  88. else:
  89. vis_result = cv2.addWeighted(im, weight,
  90. pseudo_img.astype(im.dtype), 1 - weight,
  91. 0)
  92. if save_dir is not None:
  93. if not os.path.exists(save_dir):
  94. os.makedirs(save_dir)
  95. out_path = os.path.join(save_dir, 'visualize_{}'.format(image_name))
  96. cv2.imwrite(out_path, vis_result)
  97. logging.info('The visualized result is saved as {}'.format(out_path))
  98. else:
  99. return vis_result
  100. def get_color_map_list(num_classes):
  101. """ Returns the color map for visualizing the segmentation mask,
  102. which can support arbitrary number of classes.
  103. Args:
  104. num_classes: Number of classes
  105. Returns:
  106. The color map
  107. """
  108. color_map = num_classes * [0, 0, 0]
  109. for i in range(0, num_classes):
  110. j = 0
  111. lab = i
  112. while lab:
  113. color_map[i * 3] |= (((lab >> 0) & 1) << (7 - j))
  114. color_map[i * 3 + 1] |= (((lab >> 1) & 1) << (7 - j))
  115. color_map[i * 3 + 2] |= (((lab >> 2) & 1) << (7 - j))
  116. j += 1
  117. lab >>= 3
  118. color_map = [color_map[i:i + 3] for i in range(0, len(color_map), 3)]
  119. return color_map
  120. # expand an array of boxes by a given scale.
  121. def expand_boxes(boxes, scale):
  122. """
  123. """
  124. w_half = (boxes[:, 2] - boxes[:, 0]) * .5
  125. h_half = (boxes[:, 3] - boxes[:, 1]) * .5
  126. x_c = (boxes[:, 2] + boxes[:, 0]) * .5
  127. y_c = (boxes[:, 3] + boxes[:, 1]) * .5
  128. w_half *= scale
  129. h_half *= scale
  130. boxes_exp = np.zeros(boxes.shape)
  131. boxes_exp[:, 0] = x_c - w_half
  132. boxes_exp[:, 2] = x_c + w_half
  133. boxes_exp[:, 1] = y_c - h_half
  134. boxes_exp[:, 3] = y_c + h_half
  135. return boxes_exp
  136. def clip_bbox(bbox):
  137. xmin = max(min(bbox[0], 1.), 0.)
  138. ymin = max(min(bbox[1], 1.), 0.)
  139. xmax = max(min(bbox[2], 1.), 0.)
  140. ymax = max(min(bbox[3], 1.), 0.)
  141. return xmin, ymin, xmax, ymax
  142. def draw_bbox_mask(image, results, threshold=0.5, color_map=None):
  143. _SMALL_OBJECT_AREA_THRESH = 1000
  144. height, width = image.shape[:2]
  145. default_font_scale = max(np.sqrt(height * width) // 900, .5)
  146. linewidth = max(default_font_scale / 40, 2)
  147. labels = list()
  148. for dt in results:
  149. if dt['category'] not in labels:
  150. labels.append(dt['category'])
  151. if color_map is None:
  152. color_map = get_color_map_list(len(labels) + 2)[2:]
  153. else:
  154. color_map = np.asarray(color_map)
  155. if color_map.shape[0] != len(labels) or color_map.shape[1] != 3:
  156. raise Exception(
  157. "The shape for color_map is required to be {}x3, but recieved shape is {}x{}.".
  158. format(len(labels), color_map.shape))
  159. if np.max(color_map) > 255 or np.min(color_map) < 0:
  160. raise ValueError(
  161. " The values in color_map should be within 0-255 range.")
  162. keep_results = []
  163. areas = []
  164. for dt in results:
  165. cname, bbox, score = dt['category'], dt['bbox'], dt['score']
  166. if score < threshold:
  167. continue
  168. keep_results.append(dt)
  169. areas.append(bbox[2] * bbox[3])
  170. areas = np.asarray(areas)
  171. sorted_idxs = np.argsort(-areas).tolist()
  172. keep_results = [keep_results[k]
  173. for k in sorted_idxs] if keep_results else []
  174. for dt in keep_results:
  175. cname, bbox, score = dt['category'], dt['bbox'], dt['score']
  176. bbox = list(map(int, bbox))
  177. xmin, ymin, w, h = bbox
  178. xmax = xmin + w
  179. ymax = ymin + h
  180. color = tuple(map(int, color_map[labels.index(cname)]))
  181. # draw bbox
  182. image = cv2.rectangle(image, (xmin, ymin), (xmax, ymax), color,
  183. linewidth)
  184. # draw mask
  185. if 'mask' in dt:
  186. mask = dt['mask'] * 255
  187. image = image.astype('float32')
  188. alpha = .7
  189. w_ratio = .4
  190. color_mask = np.asarray(color, dtype=int)
  191. for c in range(3):
  192. color_mask[c] = color_mask[c] * (1 - w_ratio) + w_ratio * 255
  193. idx = np.nonzero(mask)
  194. image[idx[0], idx[1], :] *= 1.0 - alpha
  195. image[idx[0], idx[1], :] += alpha * color_mask
  196. image = image.astype("uint8")
  197. contours = cv2.findContours(
  198. mask.astype("uint8"), cv2.RETR_CCOMP,
  199. cv2.CHAIN_APPROX_NONE)[-2]
  200. image = cv2.drawContours(
  201. image,
  202. contours,
  203. contourIdx=-1,
  204. color=color,
  205. thickness=1,
  206. lineType=cv2.LINE_AA)
  207. # draw label
  208. text_pos = (xmin, ymin)
  209. instance_area = w * h
  210. if (instance_area < _SMALL_OBJECT_AREA_THRESH or h < 40):
  211. if ymin >= height - 5:
  212. text_pos = (xmin, ymin)
  213. else:
  214. text_pos = (xmin, ymax)
  215. height_ratio = h / np.sqrt(height * width)
  216. font_scale = (np.clip((height_ratio - 0.02) / 0.08 + 1, 1.2,
  217. 2) * 0.5 * default_font_scale)
  218. text = "{} {:.2f}".format(cname, score)
  219. (tw, th), baseline = cv2.getTextSize(
  220. text,
  221. fontFace=cv2.FONT_HERSHEY_DUPLEX,
  222. fontScale=font_scale,
  223. thickness=1)
  224. image = cv2.rectangle(
  225. image,
  226. text_pos, (text_pos[0] + tw, text_pos[1] + th + baseline),
  227. color=color,
  228. thickness=-1)
  229. image = cv2.putText(
  230. image,
  231. text, (text_pos[0], text_pos[1] + th),
  232. fontFace=cv2.FONT_HERSHEY_DUPLEX,
  233. fontScale=font_scale,
  234. color=(255, 255, 255),
  235. thickness=1,
  236. lineType=cv2.LINE_AA)
  237. return image
  238. def draw_pr_curve(eval_details_file=None,
  239. gt=None,
  240. pred_bbox=None,
  241. pred_mask=None,
  242. iou_thresh=0.5,
  243. save_dir='./'):
  244. if eval_details_file is not None:
  245. import json
  246. with open(eval_details_file, 'r') as f:
  247. eval_details = json.load(f)
  248. pred_bbox = eval_details['bbox']
  249. if 'mask' in eval_details:
  250. pred_mask = eval_details['mask']
  251. gt = eval_details['gt']
  252. if gt is None or pred_bbox is None:
  253. raise Exception(
  254. "gt/pred_bbox/pred_mask is None now, please set right eval_details_file or gt/pred_bbox/pred_mask."
  255. )
  256. if pred_bbox is not None and len(pred_bbox) == 0:
  257. raise Exception("There is no predicted bbox.")
  258. if pred_mask is not None and len(pred_mask) == 0:
  259. raise Exception("There is no predicted mask.")
  260. import matplotlib
  261. matplotlib.use('Agg')
  262. import matplotlib.pyplot as plt
  263. from pycocotools.coco import COCO
  264. from pycocotools.cocoeval import COCOeval
  265. coco = COCO()
  266. coco.dataset = gt
  267. coco.createIndex()
  268. def _summarize(coco_gt, ap=1, iouThr=None, areaRng='all', maxDets=100):
  269. """
  270. This function has the same functionality as _summarize() in pycocotools.COCOeval.summarize().
  271. Refer to
  272. https://github.com/cocodataset/cocoapi/blob/8c9bcc3cf640524c4c20a9c40e89cb6a2f2fa0e9/PythonAPI/pycocotools/cocoeval.py#L427,
  273. """
  274. p = coco_gt.params
  275. aind = [i for i, aRng in enumerate(p.areaRngLbl) if aRng == areaRng]
  276. mind = [i for i, mDet in enumerate(p.maxDets) if mDet == maxDets]
  277. if ap == 1:
  278. # dimension of precision: [TxRxKxAxM]
  279. s = coco_gt.eval['precision']
  280. # IoU
  281. if iouThr is not None:
  282. t = np.where(iouThr == p.iouThrs)[0]
  283. s = s[t]
  284. s = s[:, :, :, aind, mind]
  285. else:
  286. # dimension of recall: [TxKxAxM]
  287. s = coco_gt.eval['recall']
  288. if iouThr is not None:
  289. t = np.where(iouThr == p.iouThrs)[0]
  290. s = s[t]
  291. s = s[:, :, aind, mind]
  292. if len(s[s > -1]) == 0:
  293. mean_s = -1
  294. else:
  295. mean_s = np.mean(s[s > -1])
  296. return mean_s
  297. def cal_pr(coco_gt, coco_dt, iou_thresh, save_dir, style='bbox'):
  298. coco_dt = loadRes(coco_gt, coco_dt)
  299. coco_eval = COCOeval(coco_gt, coco_dt, style)
  300. coco_eval.params.iouThrs = np.linspace(
  301. iou_thresh, iou_thresh, 1, endpoint=True)
  302. coco_eval.evaluate()
  303. coco_eval.accumulate()
  304. stats = _summarize(coco_eval, iouThr=iou_thresh)
  305. catIds = coco_gt.getCatIds()
  306. if len(catIds) != coco_eval.eval['precision'].shape[2]:
  307. raise Exception(
  308. "The category number must be same as the third dimension of precisions."
  309. )
  310. x = np.arange(0.0, 1.01, 0.01)
  311. color_map = get_color_map_list(256)[1:256]
  312. plt.subplot(1, 2, 1)
  313. plt.title(style + " precision-recall IoU={}".format(iou_thresh))
  314. plt.xlabel("recall")
  315. plt.ylabel("precision")
  316. plt.xlim(0, 1.01)
  317. plt.ylim(0, 1.01)
  318. plt.grid(linestyle='--', linewidth=1)
  319. plt.plot([0, 1], [0, 1], 'r--', linewidth=1)
  320. my_x_ticks = np.arange(0, 1.01, 0.1)
  321. my_y_ticks = np.arange(0, 1.01, 0.1)
  322. plt.xticks(my_x_ticks, fontsize=5)
  323. plt.yticks(my_y_ticks, fontsize=5)
  324. for idx, catId in enumerate(catIds):
  325. pr_array = coco_eval.eval['precision'][0, :, idx, 0, 2]
  326. precision = pr_array[pr_array > -1]
  327. ap = np.mean(precision) if precision.size else float('nan')
  328. nm = coco_gt.loadCats(catId)[0]['name'] + ' AP={:0.2f}'.format(
  329. float(ap * 100))
  330. color = tuple(color_map[idx])
  331. color = [float(c) / 255 for c in color]
  332. color.append(0.75)
  333. plt.plot(x, pr_array, color=color, label=nm, linewidth=1)
  334. plt.legend(loc="lower left", fontsize=5)
  335. plt.subplot(1, 2, 2)
  336. plt.title(style + " score-recall IoU={}".format(iou_thresh))
  337. plt.xlabel('recall')
  338. plt.ylabel('score')
  339. plt.xlim(0, 1.01)
  340. plt.ylim(0, 1.01)
  341. plt.grid(linestyle='--', linewidth=1)
  342. plt.xticks(my_x_ticks, fontsize=5)
  343. plt.yticks(my_y_ticks, fontsize=5)
  344. for idx, catId in enumerate(catIds):
  345. nm = coco_gt.loadCats(catId)[0]['name']
  346. sr_array = coco_eval.eval['scores'][0, :, idx, 0, 2]
  347. color = tuple(color_map[idx])
  348. color = [float(c) / 255 for c in color]
  349. color.append(0.75)
  350. plt.plot(x, sr_array, color=color, label=nm, linewidth=1)
  351. plt.legend(loc="lower left", fontsize=5)
  352. plt.savefig(
  353. os.path.join(
  354. save_dir,
  355. "./{}_pr_curve(iou-{}).png".format(style, iou_thresh)),
  356. dpi=800)
  357. plt.close()
  358. if not os.path.exists(save_dir):
  359. os.makedirs(save_dir)
  360. cal_pr(coco, pred_bbox, iou_thresh, save_dir, style='bbox')
  361. if pred_mask is not None:
  362. cal_pr(coco, pred_mask, iou_thresh, save_dir, style='segm')