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.

convert_ethz_to_coco_metaway.py 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. import os
  2. import numpy as np
  3. import json
  4. from PIL import Image
  5. DATA_PATH = '/media/external_10TB/10TB/vision/ByteTrackData/ETHZ/'
  6. DATA_FILE_PATH = 'datasets/data_path/eth'
  7. OUT_PATH = DATA_PATH + 'annotations/'
  8. DATA_ROOT = '/media/external_10TB/10TB/vision/ByteTrackData'
  9. def load_paths(data_path):
  10. with open(data_path, 'r') as file:
  11. img_files = file.readlines()
  12. img_files = [x.replace('\n', '') for x in img_files]
  13. img_files = list(filter(lambda x: len(x) > 0, img_files))
  14. label_files = [x.replace('images', 'labels_with_ids').replace('.png', '.txt').replace('.jpg', '.txt') for x in img_files]
  15. return img_files, label_files
  16. if __name__ == '__main__':
  17. if not os.path.exists(OUT_PATH):
  18. os.mkdir(OUT_PATH)
  19. files = os.listdir(DATA_FILE_PATH)
  20. for file in files:
  21. out_path = OUT_PATH + 'train_{}.json'.format(file.replace('.train',''))
  22. out = {'images': [], 'annotations': [], 'categories': [{'id': 1, 'name': 'person'}]}
  23. img_paths, label_paths = load_paths(os.path.join(DATA_FILE_PATH,file))
  24. image_cnt = 0
  25. ann_cnt = 0
  26. video_cnt = 0
  27. for img_path, label_path in zip(img_paths, label_paths):
  28. image_cnt += 1
  29. im = Image.open(os.path.join(DATA_ROOT, img_path))
  30. image_info = {'file_name': img_path,
  31. 'id': image_cnt,
  32. 'height': im.size[1],
  33. 'width': im.size[0]}
  34. out['images'].append(image_info)
  35. # Load labels
  36. if os.path.isfile(os.path.join(DATA_ROOT, label_path)):
  37. labels0 = np.loadtxt(os.path.join(DATA_ROOT, label_path), dtype=np.float32).reshape(-1, 6)
  38. # Normalized xywh to pixel xyxy format
  39. labels = labels0.copy()
  40. labels[:, 2] = image_info['width'] * (labels0[:, 2] - labels0[:, 4] / 2)
  41. labels[:, 3] = image_info['height'] * (labels0[:, 3] - labels0[:, 5] / 2)
  42. labels[:, 4] = image_info['width'] * labels0[:, 4]
  43. labels[:, 5] = image_info['height'] * labels0[:, 5]
  44. else:
  45. labels = np.array([])
  46. for i in range(len(labels)):
  47. ann_cnt += 1
  48. fbox = labels[i, 2:6].tolist()
  49. ann = {'id': ann_cnt,
  50. 'category_id': 1,
  51. 'image_id': image_cnt,
  52. 'track_id': -1,
  53. 'bbox': fbox,
  54. 'area': fbox[2] * fbox[3],
  55. 'iscrowd': 0}
  56. out['annotations'].append(ann)
  57. print('loaded train for {} images and {} samples'.format(len(out['images']), len(out['annotations'])))
  58. json.dump(out, open(out_path, 'w'))