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

copy_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
  1. import argparse
  2. import os
  3. import shutil
  4. def copy_labels(image_path, from_labels, to_labels):
  5. for img in os.listdir(image_path):
  6. label = f'{os.path.splitext(img)[0]}.txt'
  7. copy_from = os.path.join(from_labels, label)
  8. if not os.path.exists(copy_from):
  9. print(f" WARNING: no label file found for '{copy_from}'. This may just mean that no objects were detected.")
  10. continue
  11. shutil.copy(os.path.join(from_labels, label), to_labels)
  12. def main():
  13. parser = argparse.ArgumentParser('Copies label files from a cental path to the appropriate YOLO-organized dataset (train, val, test) based on the image files present')
  14. parser.add_argument('--labels', required=True, help='Path to directory with label files')
  15. parser.add_argument('--dataset', required=True, help='Path to YOLO organized dataset')
  16. args = parser.parse_args()
  17. for dataset in ['train', 'val', 'test']:
  18. image_path = os.path.join(args.dataset, 'images', dataset)
  19. if not os.path.exists(image_path):
  20. print(f"'{image_path}' does not exist. Skipping...")
  21. continue
  22. to_labels = os.path.join(args.dataset, 'labels', dataset)
  23. os.makedirs(to_labels, exist_ok=True)
  24. copy_labels(image_path, args.labels, to_labels)
  25. if __name__ == '__main__':
  26. main()
Tip!

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

Comments

Loading...