| from __future__ import annotations |
|
|
| import argparse |
| import math |
| import random |
| import shutil |
| from pathlib import Path |
| from typing import List |
|
|
|
|
| IMG_EXTS = {".jpg", ".jpeg", ".JPG", ".JPEG"} |
|
|
|
|
| def list_images(root: Path) -> List[Path]: |
| files: List[Path] = [] |
| for p in root.rglob("*"): |
| if p.is_file() and p.suffix in IMG_EXTS: |
| files.append(p) |
| return files |
|
|
|
|
| def clean_dir(p: Path) -> None: |
| if p.exists(): |
| for child in p.iterdir(): |
| if child.is_file(): |
| child.unlink() |
| else: |
| shutil.rmtree(child) |
|
|
|
|
| def copy_files(files: List[Path], dst_dir: Path) -> None: |
| dst_dir.mkdir(parents=True, exist_ok=True) |
| for f in files: |
| shutil.copy2(f, dst_dir / f.name) |
|
|
|
|
| def main(): |
| ap = argparse.ArgumentParser(description="Prepare train/val/test splits from a raw images folder (Route B).") |
| ap.add_argument("--src", type=Path, required=True, help="Root folder containing raw images (recursively)") |
| ap.add_argument("--dst", type=Path, default=Path("data"), help="Destination data folder containing splits") |
| ap.add_argument("--train", type=float, default=1.0, help="Train ratio (0..1)") |
| ap.add_argument("--val", type=float, default=0.0, help="Validation ratio (0..1)") |
| ap.add_argument("--test", type=float, default=0.0, help="Test ratio (0..1)") |
| ap.add_argument("--seed", type=int, default=42, help="Random seed") |
| ap.add_argument("--clean", action="store_true", help="Clean split folders before copying") |
| args = ap.parse_args() |
|
|
| total_ratio = args.train + args.val + args.test |
| if not (0.999 <= total_ratio <= 1.001): |
| raise SystemExit(f"Ratios must sum to 1.0; got {total_ratio}") |
|
|
| imgs = list_images(args.src) |
| if not imgs: |
| raise SystemExit(f"No JPEG images found under {args.src}") |
|
|
| random.seed(args.seed) |
| random.shuffle(imgs) |
|
|
| n = len(imgs) |
| n_train = int(math.floor(n * args.train)) |
| n_val = int(math.floor(n * args.val)) |
| n_test = n - n_train - n_val |
|
|
| train_files = imgs[:n_train] |
| val_files = imgs[n_train:n_train + n_val] |
| test_files = imgs[n_train + n_val:] |
|
|
| print(f"Total images: {n} -> train {len(train_files)}, val {len(val_files)}, test {len(test_files)}") |
|
|
| for split, files in (("train", train_files), ("validation", val_files), ("test", test_files)): |
| split_dir = args.dst / split |
| if args.clean: |
| clean_dir(split_dir) |
| if files: |
| copy_files(files, split_dir) |
| print(f"Copied {len(files)} files to {split_dir}") |
|
|
| |
| try: |
| from scripts.extract_split_metadata import extract_split |
| except Exception: |
| |
| import sys |
| here = Path(__file__).resolve().parent |
| if str(here) not in sys.path: |
| sys.path.insert(0, str(here)) |
| try: |
| from extract_split_metadata import extract_split |
| except Exception as e: |
| raise SystemExit(f"Failed to import extractor: {e}") |
|
|
| written = [] |
| for split in ("train", "validation", "test"): |
| split_dir = args.dst / split |
| if split_dir.exists(): |
| csv_path = extract_split(split_dir) |
| written.append(csv_path) |
|
|
| print("Done. Metadata files:") |
| for p in written: |
| print(p) |
|
|
|
|
| if __name__ == "__main__": |
| main() |
|
|