File size: 1,905 Bytes
a3a407d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
46
47
48
49
50
51
52
53
54
55
56
57
58
"""Validate YOLO dataset layout and labels against `data.yaml`."""
import os
import sys
import yaml
from glob import glob


def load_data_yaml(p='data.yaml'):
    if not os.path.exists(p):
        print(f'Missing {p}')
        sys.exit(1)
    with open(p,'r') as f:
        return yaml.safe_load(f)


def find_missing_labels(img_files, label_dir):
    missing = []
    for img in img_files:
        stem = os.path.splitext(os.path.basename(img))[0]
        lbl = os.path.join(label_dir, stem + '.txt')
        if not os.path.exists(lbl):
            missing.append((img, lbl))
    return missing


def main():
    data = load_data_yaml('data.yaml')
    train_p = data.get('train')
    val_p = data.get('val')
    print('Train:', train_p)
    print('Val:  ', val_p)

    for name, path in [('train', train_p), ('val', val_p)]:
        img_dir = path
        if not os.path.isdir(img_dir):
            print(f'ERROR: {name} images path not found: {img_dir}')
            continue
        imgs = glob(os.path.join(img_dir, '*.*'))
        imgs = [p for p in imgs if p.lower().endswith(('.jpg','.jpeg','.png','bmp','tif','tiff','webp'))]
        print(f'{name} images found: {len(imgs)}')
        lbl_dir = img_dir.replace('images', 'labels')
        if not os.path.isdir(lbl_dir):
            print(f'WARNING: labels dir not found for {name}: {lbl_dir}')
        else:
            missing = find_missing_labels(imgs, lbl_dir)
            if missing:
                print(f'Missing {len(missing)} label files in {lbl_dir}. Example:')
                for m in missing[:5]:
                    print('  ', m)
            else:
                print('All images have corresponding label files.')

    print('\nIf there are zero images, add images to the dataset/images/* folders or run the sample generator script:')
    print('  python scripts/create_sample_dataset.py')

if __name__ == '__main__':
    main()