visualize.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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. from __future__ import division
  15. import os
  16. import cv2
  17. import numpy as np
  18. from PIL import Image, ImageDraw, ImageFile
  19. ImageFile.LOAD_TRUNCATED_IMAGES = True
  20. from collections import deque
  21. def visualize_box_mask(im, results, labels, threshold=0.5):
  22. """
  23. Args:
  24. im (str/np.ndarray): path of image/np.ndarray read by cv2
  25. results (dict): include 'boxes': np.ndarray: shape:[N,6], N: number of box,
  26. matix element:[class, score, x_min, y_min, x_max, y_max]
  27. labels (list): labels:['class1', ..., 'classn']
  28. threshold (float): Threshold of score.
  29. Returns:
  30. im (PIL.Image.Image): visualized image
  31. """
  32. if isinstance(im, str):
  33. im = Image.open(im).convert('RGB')
  34. else:
  35. im = Image.fromarray(im)
  36. if 'boxes' in results and len(results['boxes']) > 0:
  37. im = draw_box(im, results['boxes'], labels, threshold=threshold)
  38. return im
  39. def get_color_map_list(num_classes):
  40. """
  41. Args:
  42. num_classes (int): number of class
  43. Returns:
  44. color_map (list): RGB color list
  45. """
  46. color_map = num_classes * [0, 0, 0]
  47. for i in range(0, num_classes):
  48. j = 0
  49. lab = i
  50. while lab:
  51. color_map[i * 3] |= (((lab >> 0) & 1) << (7 - j))
  52. color_map[i * 3 + 1] |= (((lab >> 1) & 1) << (7 - j))
  53. color_map[i * 3 + 2] |= (((lab >> 2) & 1) << (7 - j))
  54. j += 1
  55. lab >>= 3
  56. color_map = [color_map[i:i + 3] for i in range(0, len(color_map), 3)]
  57. return color_map
  58. def draw_box(im, np_boxes, labels, threshold=0.5):
  59. """
  60. Args:
  61. im (PIL.Image.Image): PIL image
  62. np_boxes (np.ndarray): shape:[N,6], N: number of box,
  63. matix element:[class, score, x_min, y_min, x_max, y_max]
  64. labels (list): labels:['class1', ..., 'classn']
  65. threshold (float): threshold of box
  66. Returns:
  67. im (PIL.Image.Image): visualized image
  68. """
  69. draw_thickness = min(im.size) // 320
  70. draw = ImageDraw.Draw(im)
  71. clsid2color = {}
  72. color_list = get_color_map_list(len(labels))
  73. expect_boxes = (np_boxes[:, 1] > threshold) & (np_boxes[:, 0] > -1)
  74. np_boxes = np_boxes[expect_boxes, :]
  75. for dt in np_boxes:
  76. clsid, bbox, score = int(dt[0]), dt[2:], dt[1]
  77. if clsid not in clsid2color:
  78. clsid2color[clsid] = color_list[clsid]
  79. color = tuple(clsid2color[clsid])
  80. if len(bbox) == 4:
  81. xmin, ymin, xmax, ymax = bbox
  82. print('class_id:{:d}, confidence:{:.4f}, left_top:[{:.2f},{:.2f}],'
  83. 'right_bottom:[{:.2f},{:.2f}]'.format(
  84. int(clsid), score, xmin, ymin, xmax, ymax))
  85. # draw bbox
  86. draw.line(
  87. [(xmin, ymin), (xmin, ymax), (xmax, ymax), (xmax, ymin),
  88. (xmin, ymin)],
  89. width=draw_thickness,
  90. fill=color)
  91. elif len(bbox) == 8:
  92. x1, y1, x2, y2, x3, y3, x4, y4 = bbox
  93. draw.line(
  94. [(x1, y1), (x2, y2), (x3, y3), (x4, y4), (x1, y1)],
  95. width=2,
  96. fill=color)
  97. xmin = min(x1, x2, x3, x4)
  98. ymin = min(y1, y2, y3, y4)
  99. # draw label
  100. text = "{} {:.4f}".format(labels[clsid], score)
  101. tw, th = draw.textsize(text)
  102. draw.rectangle(
  103. [(xmin + 1, ymin - th), (xmin + tw + 1, ymin)], fill=color)
  104. draw.text((xmin + 1, ymin - th), text, fill=(255, 255, 255))
  105. return im
  106. def get_color(idx):
  107. idx = idx * 3
  108. color = ((37 * idx) % 255, (17 * idx) % 255, (29 * idx) % 255)
  109. return color
  110. def plot_tracking(image,
  111. tlwhs,
  112. obj_ids,
  113. scores=None,
  114. frame_id=0,
  115. fps=0.,
  116. ids2names=[],
  117. do_entrance_counting=False,
  118. entrance=None):
  119. im = np.ascontiguousarray(np.copy(image))
  120. im_h, im_w = im.shape[:2]
  121. text_scale = max(0.5, image.shape[1] / 3000.)
  122. text_thickness = 2
  123. line_thickness = max(1, int(image.shape[1] / 500.))
  124. cv2.putText(
  125. im,
  126. 'frame: %d fps: %.2f num: %d' % (frame_id, fps, len(tlwhs)),
  127. (0, int(15 * text_scale) + 5),
  128. cv2.FONT_ITALIC,
  129. text_scale, (0, 0, 255),
  130. thickness=text_thickness)
  131. for i, tlwh in enumerate(tlwhs):
  132. x1, y1, w, h = tlwh
  133. intbox = tuple(map(int, (x1, y1, x1 + w, y1 + h)))
  134. obj_id = int(obj_ids[i])
  135. id_text = 'ID: {}'.format(int(obj_id))
  136. if ids2names != []:
  137. assert len(
  138. ids2names) == 1, "plot_tracking only supports single classes."
  139. id_text = 'ID: {}_'.format(ids2names[0]) + id_text
  140. _line_thickness = 1 if obj_id <= 0 else line_thickness
  141. color = get_color(abs(obj_id))
  142. cv2.rectangle(
  143. im, intbox[0:2], intbox[2:4], color=color, thickness=line_thickness)
  144. cv2.putText(
  145. im,
  146. id_text, (intbox[0], intbox[1] - 25),
  147. cv2.FONT_ITALIC,
  148. text_scale, (0, 255, 255),
  149. thickness=text_thickness)
  150. if scores is not None:
  151. text = 'score: {:.2f}'.format(float(scores[i]))
  152. cv2.putText(
  153. im,
  154. text, (intbox[0], intbox[1] - 6),
  155. cv2.FONT_ITALIC,
  156. text_scale, (0, 255, 0),
  157. thickness=text_thickness)
  158. if do_entrance_counting:
  159. entrance_line = tuple(map(int, entrance))
  160. cv2.rectangle(
  161. im,
  162. entrance_line[0:2],
  163. entrance_line[2:4],
  164. color=(0, 255, 255),
  165. thickness=line_thickness)
  166. return im
  167. def plot_tracking_dict(image,
  168. num_classes,
  169. tlwhs_dict,
  170. obj_ids_dict,
  171. scores_dict,
  172. frame_id=0,
  173. fps=0.,
  174. ids2names=[],
  175. do_entrance_counting=False,
  176. entrance=None,
  177. records=None,
  178. center_traj=None):
  179. im = np.ascontiguousarray(np.copy(image))
  180. im_h, im_w = im.shape[:2]
  181. text_scale = max(0.5, image.shape[1] / 3000.)
  182. text_thickness = 2
  183. line_thickness = max(1, int(image.shape[1] / 500.))
  184. if num_classes == 1:
  185. if records is not None:
  186. start = records[-1].find('Total')
  187. end = records[-1].find('In')
  188. cv2.putText(
  189. im,
  190. records[-1][start:end], (0, int(40 * text_scale) + 10),
  191. cv2.FONT_ITALIC,
  192. text_scale, (0, 0, 255),
  193. thickness=text_thickness)
  194. if num_classes == 1 and do_entrance_counting:
  195. entrance_line = tuple(map(int, entrance))
  196. cv2.rectangle(
  197. im,
  198. entrance_line[0:2],
  199. entrance_line[2:4],
  200. color=(0, 255, 255),
  201. thickness=line_thickness)
  202. # find start location for entrance counting data
  203. start = records[-1].find('In')
  204. cv2.putText(
  205. im,
  206. records[-1][start:-1], (0, int(60 * text_scale) + 10),
  207. cv2.FONT_ITALIC,
  208. text_scale, (0, 0, 255),
  209. thickness=text_thickness)
  210. for cls_id in range(num_classes):
  211. tlwhs = tlwhs_dict[cls_id]
  212. obj_ids = obj_ids_dict[cls_id]
  213. scores = scores_dict[cls_id]
  214. cv2.putText(
  215. im,
  216. 'frame: %d fps: %.2f num: %d' % (frame_id, fps, len(tlwhs)),
  217. (0, int(15 * text_scale) + 5),
  218. cv2.FONT_ITALIC,
  219. text_scale, (0, 0, 255),
  220. thickness=text_thickness)
  221. record_id = set()
  222. for i, tlwh in enumerate(tlwhs):
  223. x1, y1, w, h = tlwh
  224. intbox = tuple(map(int, (x1, y1, x1 + w, y1 + h)))
  225. center = tuple(map(int, (x1 + w / 2., y1 + h / 2.)))
  226. obj_id = int(obj_ids[i])
  227. if center_traj is not None:
  228. record_id.add(obj_id)
  229. if obj_id not in center_traj[cls_id]:
  230. center_traj[cls_id][obj_id] = deque(maxlen=30)
  231. center_traj[cls_id][obj_id].append(center)
  232. id_text = '{}'.format(int(obj_id))
  233. if ids2names != []:
  234. id_text = '{}_{}'.format(ids2names[cls_id], id_text)
  235. else:
  236. id_text = 'class{}_{}'.format(cls_id, id_text)
  237. _line_thickness = 1 if obj_id <= 0 else line_thickness
  238. color = get_color(abs(obj_id))
  239. cv2.rectangle(
  240. im,
  241. intbox[0:2],
  242. intbox[2:4],
  243. color=color,
  244. thickness=line_thickness)
  245. cv2.putText(
  246. im,
  247. id_text, (intbox[0], intbox[1] - 25),
  248. cv2.FONT_ITALIC,
  249. text_scale, (0, 255, 255),
  250. thickness=text_thickness)
  251. if scores is not None:
  252. text = 'score: {:.2f}'.format(float(scores[i]))
  253. cv2.putText(
  254. im,
  255. text, (intbox[0], intbox[1] - 6),
  256. cv2.FONT_ITALIC,
  257. text_scale, (0, 255, 0),
  258. thickness=text_thickness)
  259. if center_traj is not None:
  260. for traj in center_traj:
  261. for i in traj.keys():
  262. if i not in record_id:
  263. continue
  264. for point in traj[i]:
  265. cv2.circle(im, point, 3, (0, 0, 255), -1)
  266. return im