Meta Byte Track
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

generate_mot20_weak_labels _metaway.py 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. import os
  2. import numpy as np
  3. import json
  4. import cv2
  5. # Use the same script for MOT16
  6. DATA_PATH = '/media/external_10TB/10TB/vision/ByteTrackData/MOT20'
  7. OUT_PATH = os.path.join(DATA_PATH, 'annotations')
  8. SPLITS = ['train', 'test'] # --> split training data to train_half and val_half.
  9. HALF_VIDEO = True
  10. CREATE_SPLITTED_ANN = True
  11. CREATE_SPLITTED_DET = True
  12. if __name__ == '__main__':
  13. if not os.path.exists(OUT_PATH):
  14. os.makedirs(OUT_PATH)
  15. for split in SPLITS:
  16. if split == "test":
  17. data_path = os.path.join(DATA_PATH, 'test')
  18. else:
  19. data_path = os.path.join(DATA_PATH, 'train')
  20. seqs = os.listdir(data_path)
  21. for seq in sorted(seqs):
  22. out_path = os.path.join(OUT_PATH, '{}_{}_weak.json'.format(split, seq))
  23. out = {'images': [], 'annotations': [], 'videos': [],
  24. 'categories': [{'id': 1, 'name': 'pedestrian'}]}
  25. image_cnt = 0
  26. ann_cnt = 0
  27. video_cnt = 0
  28. tid_curr = 0
  29. tid_last = -1
  30. if '.DS_Store' in seq:
  31. continue
  32. video_cnt += 1 # video sequence number.
  33. out['videos'].append({'id': video_cnt, 'file_name': seq})
  34. seq_path = os.path.join(data_path, seq)
  35. img_path = os.path.join(seq_path, 'img1')
  36. ann_path = os.path.join(seq_path, 'gt/gt.txt')
  37. images = os.listdir(img_path)
  38. num_images = len([image for image in images if 'jpg' in image]) # half and half
  39. if HALF_VIDEO and ('half' in split):
  40. image_range = [0, num_images // 2] if 'train' in split else \
  41. [num_images // 2 + 1, num_images - 1]
  42. else:
  43. image_range = [0, num_images - 1]
  44. for i in range(num_images):
  45. if i < image_range[0] or i > image_range[1]:
  46. continue
  47. img = cv2.imread(os.path.join(data_path, '{}/img1/{:06d}.jpg'.format(seq, i + 1)))
  48. height, width = img.shape[:2]
  49. image_info = {'file_name': '{}/img1/{:06d}.jpg'.format(seq, i + 1), # image name.
  50. 'id': image_cnt + i + 1, # image number in the entire training set.
  51. 'frame_id': i + 1 - image_range[0],
  52. # image number in the video sequence, starting from 1.
  53. 'prev_image_id': image_cnt + i if i > 0 else -1,
  54. # image number in the entire training set.
  55. 'next_image_id': image_cnt + i + 2 if i < num_images - 1 else -1,
  56. 'video_id': video_cnt,
  57. 'height': height, 'width': width}
  58. out['images'].append(image_info)
  59. print('{}: {} images'.format(seq, num_images))
  60. if split != 'test':
  61. det_path = os.path.join(seq_path, 'det/det.txt')
  62. anns = np.loadtxt(ann_path, dtype=np.float32, delimiter=',')
  63. dets = np.loadtxt(det_path, dtype=np.float32, delimiter=',')
  64. print('{} ann images'.format(int(anns[:, 0].max())))
  65. for i in range(anns.shape[0]):
  66. frame_id = int(anns[i][0])
  67. if frame_id - 1 < image_range[0] or frame_id - 1 > image_range[1]:
  68. continue
  69. track_id = int(anns[i][1])
  70. cat_id = int(anns[i][7])
  71. ann_cnt += 1
  72. if not ('15' in DATA_PATH):
  73. # if not (float(anns[i][8]) >= 0.25): # visibility.
  74. # continue
  75. if not (int(anns[i][6]) == 1): # whether ignore.
  76. continue
  77. if int(anns[i][7]) in [3, 4, 5, 6, 9, 10, 11]: # Non-person
  78. continue
  79. if int(anns[i][7]) in [2, 7, 8, 12]: # Ignored person
  80. # category_id = -1
  81. continue
  82. else:
  83. category_id = 1 # pedestrian(non-static)
  84. if not track_id == tid_last:
  85. tid_curr += 1
  86. tid_last = track_id
  87. else:
  88. category_id = 1
  89. ann = {'id': ann_cnt,
  90. 'category_id': category_id,
  91. 'image_id': image_cnt + frame_id,
  92. 'track_id': -1,
  93. 'bbox': '',
  94. 'conf': '',
  95. 'iscrowd': 0,
  96. 'area': ''}
  97. # float(anns[i][4] * anns[i][5])
  98. out['annotations'].append(ann)
  99. image_cnt += num_images
  100. print(tid_curr, tid_last)
  101. print('loaded {} for {} images and {} samples'.format(split, len(out['images']), len(out['annotations'])))
  102. json.dump(out, open(out_path, 'w'))