File size: 1,277 Bytes
30ba310
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# Check that every media path referenced in annotations/**/*.json exists under media/.
import json, glob, os, sys

ROOT = '/root/datasets'
os.makedirs(f'{ROOT}/tools/missing', exist_ok=True)
report, total_missing = {}, 0
for f in sorted(glob.glob(f'{ROOT}/annotations/**/*.json', recursive=True)):
    try:
        d = json.load(open(f))
    except Exception:
        continue
    if not isinstance(d, list) or not d or not isinstance(d[0], dict):
        continue
    paths = set()
    for x in d:
        for k in ('video', 'image'):
            v = x.get(k)
            if v:
                paths.update(v if isinstance(v, list) else [v])
    missing = sorted(p for p in paths if not os.path.exists(f'{ROOT}/media/{p}'))
    rel = os.path.relpath(f, ROOT)
    report[rel] = {'samples': len(d), 'unique_media': len(paths), 'missing': len(missing), 'missing_examples': missing[:5]}
    total_missing += len(missing)
    print(f"{'OK  ' if not missing else 'MISS'} {rel}: {len(d)} samples, {len(paths)} media, {len(missing)} missing", flush=True)
    with open(f'{ROOT}/tools/missing/{os.path.basename(f)}.txt', 'w') as out:
        out.write('\n'.join(missing))
json.dump(report, open(f'{ROOT}/tools/verify_report.json', 'w'), indent=1)
print('TOTAL_MISSING', total_missing)