gen_labels_MOT.py 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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.path as osp
  15. import os
  16. import numpy as np
  17. MOT_data = 'MOT16'
  18. # choose a data in ['MOT15', 'MOT16', 'MOT17', 'MOT20']
  19. # or your custom data (prepare it following the 'docs/tutorials/PrepareMOTDataSet.md')
  20. def mkdirs(d):
  21. if not osp.exists(d):
  22. os.makedirs(d)
  23. seq_root = './{}/images/train'.format(MOT_data)
  24. label_root = './{}/labels_with_ids/train'.format(MOT_data)
  25. mkdirs(label_root)
  26. seqs = [s for s in os.listdir(seq_root)]
  27. tid_curr = 0
  28. tid_last = -1
  29. for seq in seqs:
  30. seq_info = open(osp.join(seq_root, seq, 'seqinfo.ini')).read()
  31. seq_width = int(seq_info[seq_info.find('imWidth=') + 8:seq_info.find(
  32. '\nimHeight')])
  33. seq_height = int(seq_info[seq_info.find('imHeight=') + 9:seq_info.find(
  34. '\nimExt')])
  35. gt_txt = osp.join(seq_root, seq, 'gt', 'gt.txt')
  36. gt = np.loadtxt(gt_txt, dtype=np.float64, delimiter=',')
  37. seq_label_root = osp.join(label_root, seq, 'img1')
  38. mkdirs(seq_label_root)
  39. for fid, tid, x, y, w, h, mark, label, _ in gt:
  40. if mark == 0 or not label == 1:
  41. continue
  42. fid = int(fid)
  43. tid = int(tid)
  44. if not tid == tid_last:
  45. tid_curr += 1
  46. tid_last = tid
  47. x += w / 2
  48. y += h / 2
  49. label_fpath = osp.join(seq_label_root, '{:06d}.txt'.format(fid))
  50. label_str = '0 {:d} {:.6f} {:.6f} {:.6f} {:.6f}\n'.format(
  51. tid_curr, x / seq_width, y / seq_height, w / seq_width,
  52. h / seq_height)
  53. with open(label_fpath, 'a') as f:
  54. f.write(label_str)