train2yolo.py 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. import os.path
  2. import sys
  3. import torch
  4. import torch.utils.data as data
  5. import cv2
  6. import numpy as np
  7. class WiderFaceDetection(data.Dataset):
  8. def __init__(self, txt_path, preproc=None):
  9. self.preproc = preproc
  10. self.imgs_path = []
  11. self.words = []
  12. f = open(txt_path, 'r')
  13. lines = f.readlines()
  14. isFirst = True
  15. labels = []
  16. for line in lines:
  17. line = line.rstrip()
  18. if line.startswith('#'):
  19. if isFirst is True:
  20. isFirst = False
  21. else:
  22. labels_copy = labels.copy()
  23. self.words.append(labels_copy)
  24. labels.clear()
  25. path = line[2:]
  26. path = txt_path.replace('label.txt', 'images/') + path
  27. self.imgs_path.append(path)
  28. else:
  29. line = line.split(' ')
  30. label = [float(x) for x in line]
  31. labels.append(label)
  32. self.words.append(labels)
  33. def __len__(self):
  34. return len(self.imgs_path)
  35. def __getitem__(self, index):
  36. img = cv2.imread(self.imgs_path[index])
  37. height, width, _ = img.shape
  38. labels = self.words[index]
  39. annotations = np.zeros((0, 15))
  40. if len(labels) == 0:
  41. return annotations
  42. for idx, label in enumerate(labels):
  43. annotation = np.zeros((1, 15))
  44. # bbox
  45. annotation[0, 0] = label[0] # x1
  46. annotation[0, 1] = label[1] # y1
  47. annotation[0, 2] = label[0] + label[2] # x2
  48. annotation[0, 3] = label[1] + label[3] # y2
  49. # landmarks
  50. annotation[0, 4] = label[4] # l0_x
  51. annotation[0, 5] = label[5] # l0_y
  52. annotation[0, 6] = label[7] # l1_x
  53. annotation[0, 7] = label[8] # l1_y
  54. annotation[0, 8] = label[10] # l2_x
  55. annotation[0, 9] = label[11] # l2_y
  56. annotation[0, 10] = label[13] # l3_x
  57. annotation[0, 11] = label[14] # l3_y
  58. annotation[0, 12] = label[16] # l4_x
  59. annotation[0, 13] = label[17] # l4_y
  60. if annotation[0, 4] < 0:
  61. annotation[0, 14] = -1
  62. else:
  63. annotation[0, 14] = 1
  64. annotations = np.append(annotations, annotation, axis=0)
  65. target = np.array(annotations)
  66. if self.preproc is not None:
  67. img, target = self.preproc(img, target)
  68. return torch.from_numpy(img), target
  69. def detection_collate(batch):
  70. """Custom collate fn for dealing with batches of images that have a different
  71. number of associated object annotations (bounding boxes).
  72. Arguments:
  73. batch: (tuple) A tuple of tensor images and lists of annotations
  74. Return:
  75. A tuple containing:
  76. 1) (tensor) batch of images stacked on their 0 dim
  77. 2) (list of tensors) annotations for a given image are stacked on 0 dim
  78. """
  79. targets = []
  80. imgs = []
  81. for _, sample in enumerate(batch):
  82. for _, tup in enumerate(sample):
  83. if torch.is_tensor(tup):
  84. imgs.append(tup)
  85. elif isinstance(tup, type(np.empty(0))):
  86. annos = torch.from_numpy(tup).float()
  87. targets.append(annos)
  88. return torch.stack(imgs, 0), targets
  89. if __name__ == '__main__':
  90. if len(sys.argv) == 1:
  91. print('Missing path to WIDERFACE train folder.')
  92. print('Run command: python3 train2yolo.py /path/to/original/widerface/train [/path/to/save/widerface/train]')
  93. exit(1)
  94. elif len(sys.argv) > 3:
  95. print('Too many arguments were provided.')
  96. print('Run command: python3 train2yolo.py /path/to/original/widerface/train [/path/to/save/widerface/train]')
  97. exit(1)
  98. original_path = sys.argv[1]
  99. if len(sys.argv) == 2:
  100. if not os.path.isdir('widerface'):
  101. os.mkdir('widerface')
  102. if not os.path.isdir('widerface/train'):
  103. os.mkdir('widerface/train')
  104. save_path = 'widerface/train'
  105. else:
  106. save_path = sys.argv[2]
  107. if not os.path.isfile(os.path.join(original_path, 'label.txt')):
  108. print('Missing label.txt file.')
  109. exit(1)
  110. aa = WiderFaceDetection(os.path.join(original_path, 'label.txt'))
  111. for i in range(len(aa.imgs_path)):
  112. print(i, aa.imgs_path[i])
  113. img = cv2.imread(aa.imgs_path[i])
  114. base_img = os.path.basename(aa.imgs_path[i])
  115. base_txt = os.path.basename(aa.imgs_path[i])[:-4] + ".txt"
  116. save_img_path = os.path.join(save_path, base_img)
  117. save_txt_path = os.path.join(save_path, base_txt)
  118. with open(save_txt_path, "w") as f:
  119. height, width, _ = img.shape
  120. labels = aa.words[i]
  121. annotations = np.zeros((0, 14))
  122. if len(labels) == 0:
  123. continue
  124. for idx, label in enumerate(labels):
  125. annotation = np.zeros((1, 14))
  126. # bbox
  127. label[0] = max(0, label[0])
  128. label[1] = max(0, label[1])
  129. label[2] = min(width - 1, label[2])
  130. label[3] = min(height - 1, label[3])
  131. annotation[0, 0] = (label[0] + label[2] / 2) / width # cx
  132. annotation[0, 1] = (label[1] + label[3] / 2) / height # cy
  133. annotation[0, 2] = label[2] / width # w
  134. annotation[0, 3] = label[3] / height # h
  135. #if (label[2] -label[0]) < 8 or (label[3] - label[1]) < 8:
  136. # img[int(label[1]):int(label[3]), int(label[0]):int(label[2])] = 127
  137. # continue
  138. # landmarks
  139. annotation[0, 4] = label[4] / width # l0_x
  140. annotation[0, 5] = label[5] / height # l0_y
  141. annotation[0, 6] = label[7] / width # l1_x
  142. annotation[0, 7] = label[8] / height # l1_y
  143. annotation[0, 8] = label[10] / width # l2_x
  144. annotation[0, 9] = label[11] / height # l2_y
  145. annotation[0, 10] = label[13] / width # l3_x
  146. annotation[0, 11] = label[14] / height # l3_y
  147. annotation[0, 12] = label[16] / width # l4_x
  148. annotation[0, 13] = label[17] / height # l4_yca
  149. str_label = "0 "
  150. for i in range(len(annotation[0])):
  151. str_label = str_label + " " + str(annotation[0][i])
  152. str_label = str_label.replace('[', '').replace(']', '')
  153. str_label = str_label.replace(',', '') + '\n'
  154. f.write(str_label)
  155. cv2.imwrite(save_img_path, img)