visualize.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  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. do_break_in_counting=False,
  177. do_illegal_parking_recognition=False,
  178. illegal_parking_dict=None,
  179. entrance=None,
  180. records=None,
  181. center_traj=None):
  182. im = np.ascontiguousarray(np.copy(image))
  183. im_h, im_w = im.shape[:2]
  184. if do_break_in_counting or do_illegal_parking_recognition:
  185. entrance = np.array(entrance[:-1]) # last pair is [im_w, im_h]
  186. text_scale = max(0.5, image.shape[1] / 3000.)
  187. text_thickness = 2
  188. line_thickness = max(1, int(image.shape[1] / 500.))
  189. if num_classes == 1:
  190. if records is not None:
  191. start = records[-1].find('Total')
  192. end = records[-1].find('In')
  193. cv2.putText(
  194. im,
  195. records[-1][start:end], (0, int(40 * text_scale) + 10),
  196. cv2.FONT_ITALIC,
  197. text_scale, (0, 0, 255),
  198. thickness=text_thickness)
  199. if num_classes == 1 and do_entrance_counting:
  200. entrance_line = tuple(map(int, entrance))
  201. cv2.rectangle(
  202. im,
  203. entrance_line[0:2],
  204. entrance_line[2:4],
  205. color=(0, 255, 255),
  206. thickness=line_thickness)
  207. # find start location for entrance counting data
  208. start = records[-1].find('In')
  209. cv2.putText(
  210. im,
  211. records[-1][start:-1], (0, int(60 * text_scale) + 10),
  212. cv2.FONT_ITALIC,
  213. text_scale, (0, 0, 255),
  214. thickness=text_thickness)
  215. if num_classes == 1 and (do_break_in_counting or
  216. do_illegal_parking_recognition):
  217. np_masks = np.zeros((im_h, im_w, 1), np.uint8)
  218. cv2.fillPoly(np_masks, [entrance], 255)
  219. # Draw region mask
  220. alpha = 0.3
  221. im = np.array(im).astype('float32')
  222. mask = np_masks[:, :, 0]
  223. color_mask = [0, 0, 255]
  224. idx = np.nonzero(mask)
  225. color_mask = np.array(color_mask)
  226. im[idx[0], idx[1], :] *= 1.0 - alpha
  227. im[idx[0], idx[1], :] += alpha * color_mask
  228. im = np.array(im).astype('uint8')
  229. if do_break_in_counting:
  230. # find start location for break in counting data
  231. start = records[-1].find('Break_in')
  232. cv2.putText(
  233. im,
  234. records[-1][start:-1],
  235. (entrance[0][0] - 10, entrance[0][1] - 10),
  236. cv2.FONT_ITALIC,
  237. text_scale, (0, 0, 255),
  238. thickness=text_thickness)
  239. if illegal_parking_dict is not None and len(illegal_parking_dict) != 0:
  240. for key, value in illegal_parking_dict.items():
  241. x1, y1, w, h = value['bbox']
  242. plate = value['plate']
  243. if plate is None:
  244. plate = ""
  245. # red box
  246. cv2.rectangle(im, (int(x1), int(y1)),
  247. (int(x1 + w), int(y1 + h)), (0, 0, 255), 2)
  248. cv2.putText(
  249. im,
  250. "illegal_parking:" + plate,
  251. (int(x1) + 5, int(16 * text_scale + y1 + 15)),
  252. cv2.FONT_ITALIC,
  253. text_scale * 1.5, (0, 0, 255),
  254. thickness=text_thickness)
  255. for cls_id in range(num_classes):
  256. tlwhs = tlwhs_dict[cls_id]
  257. obj_ids = obj_ids_dict[cls_id]
  258. scores = scores_dict[cls_id]
  259. cv2.putText(
  260. im,
  261. 'frame: %d fps: %.2f num: %d' % (frame_id, fps, len(tlwhs)),
  262. (0, int(15 * text_scale) + 5),
  263. cv2.FONT_ITALIC,
  264. text_scale, (0, 0, 255),
  265. thickness=text_thickness)
  266. record_id = set()
  267. for i, tlwh in enumerate(tlwhs):
  268. x1, y1, w, h = tlwh
  269. intbox = tuple(map(int, (x1, y1, x1 + w, y1 + h)))
  270. center = tuple(map(int, (x1 + w / 2., y1 + h / 2.)))
  271. obj_id = int(obj_ids[i])
  272. if center_traj is not None:
  273. record_id.add(obj_id)
  274. if obj_id not in center_traj[cls_id]:
  275. center_traj[cls_id][obj_id] = deque(maxlen=30)
  276. center_traj[cls_id][obj_id].append(center)
  277. id_text = '{}'.format(int(obj_id))
  278. if ids2names != []:
  279. id_text = '{}_{}'.format(ids2names[cls_id], id_text)
  280. else:
  281. id_text = 'class{}_{}'.format(cls_id, id_text)
  282. _line_thickness = 1 if obj_id <= 0 else line_thickness
  283. in_region = False
  284. if do_break_in_counting:
  285. center_x = min(x1 + w / 2., im_w - 1)
  286. center_down_y = min(y1 + h, im_h - 1)
  287. if in_quadrangle([center_x, center_down_y], entrance, im_h,
  288. im_w):
  289. in_region = True
  290. color = get_color(abs(obj_id)) if in_region == False else (0, 0,
  291. 255)
  292. cv2.rectangle(
  293. im,
  294. intbox[0:2],
  295. intbox[2:4],
  296. color=color,
  297. thickness=line_thickness)
  298. cv2.putText(
  299. im,
  300. id_text, (intbox[0], intbox[1] - 25),
  301. cv2.FONT_ITALIC,
  302. text_scale,
  303. color,
  304. thickness=text_thickness)
  305. if do_break_in_counting and in_region:
  306. cv2.putText(
  307. im,
  308. 'Break in now.', (intbox[0], intbox[1] - 50),
  309. cv2.FONT_ITALIC,
  310. text_scale, (0, 0, 255),
  311. thickness=text_thickness)
  312. if scores is not None:
  313. text = 'score: {:.2f}'.format(float(scores[i]))
  314. cv2.putText(
  315. im,
  316. text, (intbox[0], intbox[1] - 6),
  317. cv2.FONT_ITALIC,
  318. text_scale,
  319. color,
  320. thickness=text_thickness)
  321. if center_traj is not None:
  322. for traj in center_traj:
  323. for i in traj.keys():
  324. if i not in record_id:
  325. continue
  326. for point in traj[i]:
  327. cv2.circle(im, point, 3, (0, 0, 255), -1)
  328. return im
  329. def in_quadrangle(point, entrance, im_h, im_w):
  330. mask = np.zeros((im_h, im_w, 1), np.uint8)
  331. cv2.fillPoly(mask, [entrance], 255)
  332. p = tuple(map(int, point))
  333. if mask[p[1], p[0], :] > 0:
  334. return True
  335. else:
  336. return False