Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

convert_labels.py 1.3 KB

You have to be logged in to leave a comment. Sign In
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
  1. import argparse
  2. import os
  3. # These labels (zero-indexed) correspond to:
  4. # 14: bird
  5. # 15: cat
  6. # 16: dog
  7. # 17: horse
  8. # 18: sheep
  9. # 19: cow
  10. # 21: bear
  11. LABELS = {'14', '15', '16', '17', '18', '19', '21'}
  12. def convert_label(filename, inpath, outpath):
  13. infile = os.path.join(inpath, filename)
  14. outfile = os.path.join(outpath, filename)
  15. with open(infile) as f, open(outfile, mode='w') as of:
  16. for line in f:
  17. parts = line.split()
  18. if parts[0] in LABELS:
  19. parts[0] = '0'
  20. newline = ' '.join(parts[:5])
  21. of.write(f'{newline}\n')
  22. def main():
  23. parser = argparse.ArgumentParser('Converts COCO labels for most animals to a single class within YOLO-style txt files')
  24. parser.add_argument('--input', help='Input directory with YOLO-style txt files, using COCO labels')
  25. parser.add_argument('--output', help='Output directory to write the YOLO-style txt files with the converted labels')
  26. args = parser.parse_args()
  27. os.makedirs(args.output, exist_ok=True)
  28. for filename in os.listdir(args.input):
  29. ext = os.path.splitext(filename)[-1].lower()
  30. if ext != '.txt':
  31. continue
  32. convert_label(filename, args.input, args.output)
  33. if __name__ == '__main__':
  34. main()
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...