"""Package the NEW (v2.0) images that survived QC into Evren-ready ZIP parts (<=300 MB, every part mixes classes). Writes meta2/manifest_v2_new.csv and out2/*.zip.""" import csv, glob, os, zipfile from collections import Counter, defaultdict D2 = '/home/user/hemato/dataset2'; OUT = '/home/user/hemato/out2'; META = '/home/user/hemato/meta2' LIMIT = 300_000_000 os.makedirs(OUT, exist_ok=True); os.makedirs(META, exist_ok=True) removed = {r['file'] for r in csv.DictReader(open('/home/user/hemato/qc2_removed.csv'))} R = [r for f in sorted(glob.glob('/home/user/hemato/manifest2/*.csv')) for r in csv.DictReader(open(f)) if r['split'] in ('train', 'val', 'test') and r['file'] not in removed and os.path.exists(os.path.join(D2, r['file']))] with open(f'{META}/manifest_v2_new.csv', 'w', newline='', encoding='utf-8-sig') as f: w = csv.DictWriter(f, fieldnames=list(R[0])); w.writeheader(); w.writerows(R) NAMES = {'cls': 'HEMATO-CLS', 'all': 'HEMATO-ALL', 'malaria': 'HEMATO-MALARIA'} for cfg, name in NAMES.items(): files = sorted(r['file'] for r in R if r['config'] == cfg) if not files: continue by = defaultdict(list) for f in files: by[f.split('/')[2]].append(f) allf = [f for lab in sorted(by) for f in sorted(by[lab])] total = sum(os.path.getsize(os.path.join(D2, f)) for f in allf) n = max(1, -(-total // LIMIT)) for i in range(n): part = allf[i::n] zn = f'{OUT}/{name}-v2_part{i + 1:02d}-of-{n:02d}.zip' with zipfile.ZipFile(zn, 'w', zipfile.ZIP_STORED) as z: for f in part: z.write(os.path.join(D2, f), f.split('/', 1)[1]) labs = Counter(f.split('/')[2] for f in part) print(os.path.basename(zn), round(os.path.getsize(zn) / 1e6), 'MB', len(part), 'files', len(labs), 'classes') det = [r for r in R if r['config'] == 'det'] if det: zn = f'{OUT}/HEMATO-DET-v2.zip' with zipfile.ZipFile(zn, 'w', zipfile.ZIP_DEFLATED) as z: for r in det: img = os.path.join(D2, r['file']); lbl = img.replace('/images/', '/labels/').rsplit('.', 1)[0] + '.txt' z.write(img, os.path.relpath(img, f'{D2}/det')); z.write(lbl, os.path.relpath(lbl, f'{D2}/det')) z.writestr('data.yaml', 'path: .\ntrain: images/train\nval: images/val\ntest: images/test\nnc: 3\nnames: [Platelets, RBC, WBC]\n') print(os.path.basename(zn), round(os.path.getsize(zn) / 1e6), 'MB', len(det), 'images') print(Counter((r['config'], r['split']) for r in R))