vis_tools.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. import cv2
  2. import os
  3. import numpy as np
  4. import matplotlib.pyplot as plt
  5. from dataset.coco import coco_class_index, coco_class_labels
  6. # -------------------------- For Detection Task --------------------------
  7. ## draw bbox & label on the image
  8. def plot_bbox_labels(img, bbox, label, cls_color, test_scale=0.4):
  9. x1, y1, x2, y2 = bbox
  10. x1, y1, x2, y2 = int(x1), int(y1), int(x2), int(y2)
  11. t_size = cv2.getTextSize(label, 0, fontScale=1, thickness=2)[0]
  12. # plot bbox
  13. cv2.rectangle(img, (x1, y1), (x2, y2), cls_color, 2)
  14. # plot title bbox
  15. cv2.rectangle(img, (x1, y1-t_size[1]), (int(x1 + t_size[0] * test_scale), y1), cls_color, -1)
  16. # put the test on the title bbox
  17. cv2.putText(img, label, (int(x1), int(y1 - 5)), 0, test_scale, (0, 0, 0), 1, lineType=cv2.LINE_AA)
  18. return img
  19. ## visualize the detection results
  20. def visualize(img, bboxes, scores, labels, class_colors, vis_thresh=0.3):
  21. ts = 0.4
  22. for i, bbox in enumerate(bboxes):
  23. if scores[i] > vis_thresh:
  24. cls_color = class_colors[int(labels[i])]
  25. cls_id = coco_class_index[int(labels[i])]
  26. mess = '%s: %.2f' % (coco_class_labels[cls_id], scores[i])
  27. img = plot_bbox_labels(img, bbox, mess, cls_color, test_scale=ts)
  28. return img
  29. ## visualize the input data during the training stage
  30. def vis_data(images, targets):
  31. """
  32. images: (tensor) [B, 3, H, W]
  33. targets: (list) a list of targets
  34. """
  35. batch_size = images.size(0)
  36. np.random.seed(0)
  37. class_colors = [(np.random.randint(255),
  38. np.random.randint(255),
  39. np.random.randint(255)) for _ in range(20)]
  40. for bi in range(batch_size):
  41. # to numpy
  42. image = images[bi].permute(1, 2, 0).cpu().numpy()
  43. target = targets[bi]
  44. image = image.astype(np.uint8)
  45. image = image.copy()
  46. tgt_boxes = target['boxes']
  47. tgt_labels = target['labels']
  48. for box, label in zip(tgt_boxes, tgt_labels):
  49. x1, y1, x2, y2 = box
  50. cls_id = int(label)
  51. x1, y1 = int(x1), int(y1)
  52. x2, y2 = int(x2), int(y2)
  53. color = class_colors[cls_id]
  54. # draw box
  55. cv2.rectangle(image, (x1, y1), (x2, y2), color, 2)
  56. cv2.imshow('train target', image)
  57. cv2.waitKey(0)
  58. ## convert feature to he heatmap
  59. def convert_feature_heatmap(feature):
  60. """
  61. feature: (ndarray) [H, W, C]
  62. """
  63. heatmap = None
  64. return heatmap
  65. ## draw feature on the image
  66. def draw_feature(img, features, save=None):
  67. """
  68. img: (ndarray & cv2.Mat) [H, W, C], where the C is 3 for RGB or 1 for Gray.
  69. features: (List[ndarray]). It is a list of the multiple feature map whose shape is [H, W, C].
  70. save: (bool) save the result or not.
  71. """
  72. img_h, img_w = img.shape[:2]
  73. for i, fmp in enumerate(features):
  74. hmp = convert_feature_heatmap(fmp)
  75. hmp = cv2.resize(hmp, (img_w, img_h))
  76. hmp = hmp.astype(np.uint8)*255
  77. hmp_rgb = cv2.applyColorMap(hmp, cv2.COLORMAP_JET)
  78. superimposed_img = hmp_rgb * 0.4 + img
  79. # show the heatmap
  80. plt.imshow(hmp)
  81. plt.close()
  82. # show the image with heatmap
  83. cv2.imshow("image with heatmap", superimposed_img)
  84. cv2.waitKey(0)
  85. cv2.destroyAllWindows()
  86. if save:
  87. save_dir = 'feature_heatmap'
  88. os.makedirs(save_dir, exist_ok=True)
  89. cv2.imwrite(os.path.join(save_dir, 'feature_{}.png'.format(i) ), superimposed_img)
  90. # -------------------------- For Tracking Task --------------------------
  91. def get_color(idx):
  92. idx = idx * 3
  93. color = ((37 * idx) % 255, (17 * idx) % 255, (29 * idx) % 255)
  94. return color
  95. def plot_tracking(image, tlwhs, obj_ids, scores=None, frame_id=0, fps=0., ids2=None):
  96. im = np.ascontiguousarray(np.copy(image))
  97. im_h, im_w = im.shape[:2]
  98. top_view = np.zeros([im_w, im_w, 3], dtype=np.uint8) + 255
  99. #text_scale = max(1, image.shape[1] / 1600.)
  100. #text_thickness = 2
  101. #line_thickness = max(1, int(image.shape[1] / 500.))
  102. text_scale = 2
  103. text_thickness = 2
  104. line_thickness = 3
  105. radius = max(5, int(im_w/140.))
  106. cv2.putText(im, 'frame: %d fps: %.2f num: %d' % (frame_id, fps, len(tlwhs)),
  107. (0, int(15 * text_scale)), cv2.FONT_HERSHEY_PLAIN, 2, (0, 0, 255), thickness=2)
  108. for i, tlwh in enumerate(tlwhs):
  109. x1, y1, w, h = tlwh
  110. intbox = tuple(map(int, (x1, y1, x1 + w, y1 + h)))
  111. obj_id = int(obj_ids[i])
  112. id_text = '{}'.format(int(obj_id))
  113. if ids2 is not None:
  114. id_text = id_text + ', {}'.format(int(ids2[i]))
  115. color = get_color(abs(obj_id))
  116. cv2.rectangle(im, intbox[0:2], intbox[2:4], color=color, thickness=line_thickness)
  117. cv2.putText(im, id_text, (intbox[0], intbox[1]), cv2.FONT_HERSHEY_PLAIN, text_scale, (0, 0, 255),
  118. thickness=text_thickness)
  119. return im