gen_labels_MOT.py 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. # Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. import os
  15. import os.path as osp
  16. import numpy as np
  17. import argparse
  18. def mkdirs(d):
  19. if not osp.exists(d):
  20. os.makedirs(d)
  21. if __name__ == "__main__":
  22. parser = argparse.ArgumentParser(description='BDD100K to MOT format')
  23. parser.add_argument(
  24. "--mot_data", default='./bdd100k')
  25. parser.add_argument("--phase", default='train')
  26. args = parser.parse_args()
  27. MOT_data = args.mot_data
  28. phase = args.phase
  29. seq_root = osp.join(MOT_data, 'bdd100kmot_vehicle', 'images', phase)
  30. label_root = osp.join(MOT_data, 'bdd100kmot_vehicle', 'labels_with_ids',
  31. phase)
  32. mkdirs(label_root)
  33. seqs = [s for s in os.listdir(seq_root)]
  34. tid_curr = 0
  35. tid_last = -1
  36. os.system(f'rm -r {MOT_data}/bdd100kmot_vehicle/labels_with_ids')
  37. for seq in seqs:
  38. print('seq => ', seq)
  39. seq_info = open(osp.join(seq_root, seq, 'seqinfo.ini')).read()
  40. seq_width = int(seq_info[seq_info.find('imWidth=') + 8:seq_info.find(
  41. '\nimHeight')])
  42. seq_height = int(seq_info[seq_info.find('imHeight=') + 9:seq_info.find(
  43. '\nimExt')])
  44. gt_txt = osp.join(seq_root, seq, 'gt', 'gt.txt')
  45. gt = np.loadtxt(gt_txt, dtype=np.float64, delimiter=',')
  46. seq_label_root = osp.join(label_root, seq, 'img1')
  47. mkdirs(seq_label_root)
  48. for fid, tid, x, y, w, h, mark, label, _ in gt:
  49. fid = int(fid)
  50. tid = int(tid)
  51. if not tid == tid_last:
  52. tid_curr += 1
  53. tid_last = tid
  54. x += w / 2
  55. y += h / 2
  56. label_fpath = osp.join(seq_label_root,
  57. seq + '-' + '{:07d}.txt'.format(fid))
  58. label_str = '0 {:d} {:.6f} {:.6f} {:.6f} {:.6f}\n'.format(
  59. tid_curr, x / seq_width, y / seq_height, w / seq_width,
  60. h / seq_height)
  61. with open(label_fpath, 'a') as f:
  62. f.write(label_str)