vis_tools.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. import cv2
  2. import os
  3. import torch
  4. import numpy as np
  5. import matplotlib.pyplot as plt
  6. # -------------------------- For Detection Task --------------------------
  7. ## visualize the input data during the training stage
  8. def vis_data(images, targets, masks=None, class_labels=None, normalized_coord=False, box_format='xyxy'):
  9. """
  10. images: (tensor) [B, 3, H, W]
  11. masks: (Tensor) [B, H, W]
  12. targets: (list) a list of targets
  13. """
  14. batch_size = images.size(0)
  15. np.random.seed(0)
  16. class_colors = [(np.random.randint(255),
  17. np.random.randint(255),
  18. np.random.randint(255)) for _ in range(80)]
  19. pixel_means = [0.485, 0.456, 0.406]
  20. pixel_std = [0.229, 0.224, 0.225]
  21. for bi in range(batch_size):
  22. target = targets[bi]
  23. # to numpy
  24. image = images[bi].permute(1, 2, 0).cpu().numpy()
  25. not_mask = ~masks[bi]
  26. img_h = not_mask.cumsum(0, dtype=torch.int32)[-1, 0]
  27. img_w = not_mask.cumsum(1, dtype=torch.int32)[0, -1]
  28. # denormalize
  29. image = (image * pixel_std + pixel_means) * 255
  30. image = image[:, :, (2, 1, 0)].astype(np.uint8)
  31. image = image.copy()
  32. tgt_boxes = target['boxes'].float()
  33. tgt_labels = target['labels'].long()
  34. for box, label in zip(tgt_boxes, tgt_labels):
  35. box_ = box.clone()
  36. if normalized_coord:
  37. box_[..., [0, 2]] *= img_w
  38. box_[..., [1, 3]] *= img_h
  39. if box_format == 'xywh':
  40. box_x1y1 = box_[..., :2] - box_[..., 2:] * 0.5
  41. box_x2y2 = box_[..., :2] + box_[..., 2:] * 0.5
  42. box_ = torch.cat([box_x1y1, box_x2y2], dim=-1)
  43. x1, y1, x2, y2 = box_.long().cpu().numpy()
  44. cls_id = label.item()
  45. color = class_colors[cls_id]
  46. # draw box
  47. cv2.rectangle(image, (x1, y1), (x2, y2), color, 2)
  48. if class_labels is not None:
  49. class_name = class_labels[cls_id]
  50. # plot title bbox
  51. t_size = cv2.getTextSize(class_name, 0, fontScale=1, thickness=2)[0]
  52. cv2.rectangle(image, (x1, y1-t_size[1]), (int(x1 + t_size[0] * 0.4), y1), color, -1)
  53. # put the test on the title bbox
  54. cv2.putText(image, class_name, (x1, y1 - 5), 0, 0.4, (0, 0, 0), 1, lineType=cv2.LINE_AA)
  55. cv2.imshow('train target', image)
  56. cv2.waitKey(0)
  57. ## Draw bbox & label on the image
  58. def plot_bbox_labels(img, bbox, label=None, cls_color=None, text_scale=0.4):
  59. x1, y1, x2, y2 = bbox
  60. x1, y1, x2, y2 = int(x1), int(y1), int(x2), int(y2)
  61. t_size = cv2.getTextSize(label, 0, fontScale=1, thickness=2)[0]
  62. # plot bbox
  63. cv2.rectangle(img, (x1, y1), (x2, y2), cls_color, 2)
  64. if label is not None:
  65. # plot title bbox
  66. cv2.rectangle(img, (x1, y1-t_size[1]), (int(x1 + t_size[0] * text_scale), y1), cls_color, -1)
  67. # put the test on the title bbox
  68. cv2.putText(img, label, (int(x1), int(y1 - 5)), 0, text_scale, (0, 0, 0), 1, lineType=cv2.LINE_AA)
  69. return img
  70. ## Visualize the detection results
  71. def visualize(image, bboxes, scores, labels, class_colors, class_names):
  72. ts = 0.4
  73. for i, bbox in enumerate(bboxes):
  74. cls_id = int(labels[i])
  75. cls_color = class_colors[cls_id]
  76. mess = '%s: %.2f' % (class_names[cls_id], scores[i])
  77. image = plot_bbox_labels(image, bbox, mess, cls_color, text_scale=ts)
  78. return image