line_dataset.py 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. from torch.utils.data.dataset import T_co
  2. from libs.vision_libs.utils import draw_keypoints
  3. from models.base.base_dataset import BaseDataset
  4. import json
  5. import os
  6. import PIL
  7. import matplotlib as mpl
  8. from torchvision.utils import draw_bounding_boxes
  9. import torchvision.transforms.v2 as transforms
  10. import torch
  11. import matplotlib.pyplot as plt
  12. from models.base.transforms import get_transforms
  13. def validate_keypoints(keypoints, image_width, image_height):
  14. for kp in keypoints:
  15. x, y, v = kp
  16. if not (0 <= x < image_width and 0 <= y < image_height):
  17. raise ValueError(f"Key point ({x}, {y}) is out of bounds for image size ({image_width}, {image_height})")
  18. """
  19. 直接读取xanlabel标注的数据集json格式
  20. """
  21. class LineDataset(BaseDataset):
  22. def __init__(self, dataset_path, data_type, transforms=None,augmentation=False, dataset_type=None,img_type='rgb', target_type='pixel'):
  23. super().__init__(dataset_path)
  24. self.data_path = dataset_path
  25. self.data_type = data_type
  26. print(f'data_path:{dataset_path}')
  27. self.transforms = transforms
  28. self.img_path = os.path.join(dataset_path, "images/" + dataset_type)
  29. self.lbl_path = os.path.join(dataset_path, "labels/" + dataset_type)
  30. self.imgs = os.listdir(self.img_path)
  31. self.lbls = os.listdir(self.lbl_path)
  32. self.target_type = target_type
  33. self.img_type=img_type
  34. self.augmentation=augmentation
  35. print(f'augmentation:{augmentation}')
  36. # self.default_transform = DefaultTransform()
  37. def __getitem__(self, index) -> T_co:
  38. img_path = os.path.join(self.img_path, self.imgs[index])
  39. lbl_path = os.path.join(self.lbl_path, self.imgs[index][:-3] + 'json')
  40. img = PIL.Image.open(img_path).convert('RGB')
  41. w, h = img.size
  42. # wire_labels, target = self.read_target(item=index, lbl_path=lbl_path, shape=(h, w))
  43. target = self.read_target(item=index, lbl_path=lbl_path, shape=(h, w))
  44. self.transforms=get_transforms(augmention=self.augmentation)
  45. img, target = self.transforms(img, target)
  46. return img, target
  47. def __len__(self):
  48. return len(self.imgs)
  49. def read_target(self, item, lbl_path, shape, extra=None):
  50. # print(f'shape:{shape}')
  51. # print(f'lbl_path:{lbl_path}')
  52. with open(lbl_path, 'r') as file:
  53. lable_all = json.load(file)
  54. objs = lable_all["shapes"]
  55. point_pairs=objs[0]['points']
  56. # print(f'point_pairs:{point_pairs}')
  57. target = {}
  58. target["image_id"] = torch.tensor(item)
  59. target["boxes"], lines,target["points"], target["labels"] = get_boxes_lines(objs,shape)
  60. # print(f'lines:{lines}')
  61. # target["labels"] = torch.ones(len(target["boxes"]), dtype=torch.int64)
  62. # print(f'target points:{target["points"]}')
  63. a = torch.full((lines.shape[0],), 2).unsqueeze(1)
  64. lines = torch.cat((lines, a), dim=1)
  65. target["lines"] = lines.to(torch.float32).view(-1,2,3)
  66. print(f'lines:{target["lines"].shape}')
  67. target["img_size"]=shape
  68. validate_keypoints(lines, shape[0], shape[1])
  69. return target
  70. def show(self, idx,show_type='all'):
  71. image, target = self.__getitem__(idx)
  72. cmap = plt.get_cmap("jet")
  73. norm = mpl.colors.Normalize(vmin=0.4, vmax=1.0)
  74. sm = plt.cm.ScalarMappable(cmap=cmap, norm=norm)
  75. sm.set_array([])
  76. # img_path = os.path.join(self.img_path, self.imgs[idx])
  77. # print(f'boxes:{target["boxes"]}')
  78. img = image
  79. if show_type=='all':
  80. boxed_image = draw_bounding_boxes((img * 255).to(torch.uint8), target["boxes"],
  81. colors="yellow", width=1)
  82. keypoint_img=draw_keypoints(boxed_image,target['points'].unsqueeze(1),colors='red',width=3)
  83. plt.imshow(keypoint_img.permute(1, 2, 0).numpy())
  84. plt.show()
  85. # if show_type=='lines':
  86. # keypoint_img=draw_keypoints((img * 255).to(torch.uint8),target['lines'],colors='red',width=3)
  87. # plt.imshow(keypoint_img.permute(1, 2, 0).numpy())
  88. # plt.show()
  89. if show_type=='points':
  90. print(f'points:{target['points'].shape}')
  91. keypoint_img=draw_keypoints((img * 255).to(torch.uint8),target['points'].unsqueeze(1),colors='red',width=3)
  92. plt.imshow(keypoint_img.permute(1, 2, 0).numpy())
  93. plt.show()
  94. if show_type=='boxes':
  95. boxed_image = draw_bounding_boxes((img * 255).to(torch.uint8), target["boxes"],
  96. colors="yellow", width=1)
  97. plt.imshow(boxed_image.permute(1, 2, 0).numpy())
  98. plt.show()
  99. def show_img(self, img_path):
  100. pass
  101. def get_boxes_lines(objs,shape):
  102. boxes = []
  103. labels=[]
  104. h,w=shape
  105. line_point_pairs = []
  106. points=[]
  107. for obj in objs:
  108. # plt.plot([a[1], b[1]], [a[0], b[0]], c="red", linewidth=1) # a[1], b[1]无明确大小
  109. # print(f"points:{obj['points']}")
  110. label=obj['label']
  111. if label =='line':
  112. a,b=obj['points'][0],obj['points'][1]
  113. line_point_pairs.append(a)
  114. line_point_pairs.append(b)
  115. xmin = max(0, (min(a[0], b[0]) - 6))
  116. xmax = min(w, (max(a[0], b[0]) + 6))
  117. ymin = max(0, (min(a[1], b[1]) - 6))
  118. ymax = min(h, (max(a[1], b[1]) + 6))
  119. boxes.append([ xmin,ymin, xmax,ymax])
  120. labels.append(torch.tensor(2))
  121. elif label =='point':
  122. p= obj['points'][0]
  123. xmin=max(0,p[0]-6)
  124. xmax = min(w, p[0] +6)
  125. ymin=max(0,p[1]-6)
  126. ymax = min(h, p[1] + 6)
  127. points.append(p)
  128. labels.append(torch.tensor(1))
  129. boxes.append([xmin, ymin, xmax, ymax])
  130. elif label =='arc':
  131. labels.append(torch.tensor(3))
  132. boxes=torch.tensor(boxes)
  133. print(f'boxes:{boxes.shape}')
  134. labels=torch.tensor(labels)
  135. points=torch.tensor(points)
  136. # print(f'read labels:{labels}')
  137. # print(f'read points:{points}')
  138. line_point_pairs=torch.tensor(line_point_pairs)
  139. # print(f'boxes:{boxes.shape},line_point_pairs:{line_point_pairs.shape}')
  140. return boxes,line_point_pairs,points,labels
  141. if __name__ == '__main__':
  142. path=r"\\192.168.50.222\share\rlq\datasets\Dataset0709_"
  143. dataset= LineDataset(dataset_path=path, dataset_type='train',augmentation=False, data_type='jpg')
  144. dataset.show(1,show_type='all')