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_cityperson_to_coco.py 2.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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/Citypersons/'
  6. DATA_FILE_PATH = 'datasets/data_path/citypersons.train'
  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. out_path = OUT_PATH + 'train.json'
  20. out = {'images': [], 'annotations': [], 'categories': [{'id': 1, 'name': 'person'}]}
  21. img_paths, label_paths = load_paths(DATA_FILE_PATH)
  22. image_cnt = 0
  23. ann_cnt = 0
  24. video_cnt = 0
  25. for img_path, label_path in zip(img_paths, label_paths):
  26. image_cnt += 1
  27. im = Image.open(os.path.join(DATA_ROOT, img_path))
  28. image_info = {'file_name': img_path,
  29. 'id': image_cnt,
  30. 'height': im.size[1],
  31. 'width': im.size[0]}
  32. out['images'].append(image_info)
  33. # Load labels
  34. if os.path.isfile(os.path.join(DATA_ROOT, label_path)):
  35. labels0 = np.loadtxt(os.path.join(DATA_ROOT, label_path), dtype=np.float32).reshape(-1, 6)
  36. # Normalized xywh to pixel xyxy format
  37. labels = labels0.copy()
  38. labels[:, 2] = image_info['width'] * (labels0[:, 2] - labels0[:, 4] / 2)
  39. labels[:, 3] = image_info['height'] * (labels0[:, 3] - labels0[:, 5] / 2)
  40. labels[:, 4] = image_info['width'] * labels0[:, 4]
  41. labels[:, 5] = image_info['height'] * labels0[:, 5]
  42. else:
  43. labels = np.array([])
  44. for i in range(len(labels)):
  45. ann_cnt += 1
  46. fbox = labels[i, 2:6].tolist()
  47. ann = {'id': ann_cnt,
  48. 'category_id': 1,
  49. 'image_id': image_cnt,
  50. 'track_id': -1,
  51. 'bbox': fbox,
  52. 'area': fbox[2] * fbox[3],
  53. 'iscrowd': 0}
  54. out['annotations'].append(ann)
  55. print('loaded train for {} images and {} samples'.format(len(out['images']), len(out['annotations'])))
  56. json.dump(out, open(out_path, 'w'))