| """Legacy CLI: sketch + fabric for EquiFashion_DB train/test layout. Core logic: equifashion_pipeline.sketch_fabric."""
|
|
|
| from __future__ import annotations
|
|
|
| import argparse
|
| import json
|
| import os
|
| import sys
|
| from concurrent.futures import ProcessPoolExecutor, as_completed
|
| from pathlib import Path
|
| from typing import Optional, Tuple
|
|
|
| from tqdm import tqdm
|
|
|
| from EquiFashionDB_pipeline import sketch_fabric as sf
|
|
|
|
|
| def _default_workers() -> int:
|
| return max(1, (os.cpu_count() or 4) - 1)
|
|
|
|
|
| def process_one(
|
| gt_name: str,
|
| image_root: Path,
|
| out_sketch: Path,
|
| out_fabric: Path,
|
| fabric_patch: int,
|
| ) -> Tuple[str, Optional[str]]:
|
| stem = Path(gt_name).stem
|
| img_path = image_root / gt_name
|
| if not img_path.is_file():
|
| return gt_name, f"missing image: {img_path}"
|
| return sf.process_one_sample(img_path, out_sketch, out_fabric, stem, fabric_patch)
|
|
|
|
|
| def _worker(
|
| job: Tuple[str, Path, Path, Path, int],
|
| ) -> Tuple[str, Optional[str]]:
|
| return process_one(*job)
|
|
|
|
|
| def load_gt_list(manifest: Path) -> list[str]:
|
| with open(manifest, encoding="utf-8") as f:
|
| data = json.load(f)
|
| out = []
|
| for row in data:
|
| if isinstance(row, dict) and "gt" in row:
|
| out.append(row["gt"])
|
| elif isinstance(row, str):
|
| out.append(row)
|
| return out
|
|
|
|
|
| def main() -> None:
|
| p = argparse.ArgumentParser(description="Sketch + fabric enrichment for EquiFashion-DB")
|
| p.add_argument(
|
| "--db-root",
|
| type=Path,
|
| default=Path(__file__).resolve().parent / "EquiFashion_DB",
|
| help="Folder containing train/, test/, train_pose/",
|
| )
|
| p.add_argument("--split", choices=("train", "test", "all"), default="train")
|
| p.add_argument("--workers", type=int, default=_default_workers())
|
| p.add_argument("--fabric-patch", type=int, default=128, help="Fabric patch side length (pixels)")
|
| p.add_argument("--limit", type=int, default=0, help="If >0, only process first N items per split (debug)")
|
| args = p.parse_args()
|
|
|
| root: Path = args.db_root
|
| jobs: list[Tuple[str, Path, Path, Path, int]] = []
|
|
|
| def enqueue(split: str) -> None:
|
| manifest = root / f"{split}.json"
|
| image_root = root / split
|
| out_sketch = root / f"{split}_sketch"
|
| out_fabric = root / f"{split}_fabric"
|
| gts = load_gt_list(manifest)
|
| for i, gt in enumerate(gts):
|
| if args.limit and i >= args.limit:
|
| break
|
| jobs.append((gt, image_root, out_sketch, out_fabric, args.fabric_patch))
|
|
|
| if args.split in ("train", "all"):
|
| enqueue("train")
|
| if args.split in ("test", "all"):
|
| enqueue("test")
|
|
|
| if not jobs:
|
| print("No jobs.", file=sys.stderr)
|
| sys.exit(1)
|
|
|
| errs: list[Tuple[str, str]] = []
|
| if args.workers <= 1:
|
| for job in tqdm(jobs, desc="enrich"):
|
| name, err = process_one(*job)
|
| if err:
|
| errs.append((name, err))
|
| else:
|
| with ProcessPoolExecutor(max_workers=args.workers) as ex:
|
| futs = {ex.submit(_worker, job): job[0] for job in jobs}
|
| for fut in tqdm(as_completed(futs), total=len(futs), desc="enrich"):
|
| name, err = fut.result()
|
| if err:
|
| errs.append((name, err))
|
|
|
| if errs:
|
| err_path = root / "enrich_sketch_fabric_errors.txt"
|
| with open(err_path, "w", encoding="utf-8") as f:
|
| for name, msg in errs:
|
| f.write(f"{name}\t{msg}\n")
|
| print(f"Done with {len(errs)} errors logged to {err_path}")
|
| else:
|
| print("Done. No errors.")
|
|
|
|
|
| if __name__ == "__main__":
|
| main()
|
|
|