"""Turn a Roboflow COCO export into a SAM3-ready copy. - Drops Roboflow's empty supercategory placeholder (e.g. "hatch"). - Renames categories to plain-English phrases, because SAM3 uses the category name as the text prompt. - Hard-links images instead of copying them (copies across filesystems). Usage: python prepare_dataset.py """ import json import os import shutil import sys from pathlib import Path def link_or_copy(src, dst): """Hard-link to save space; copy when src and dst are on different filesystems.""" try: os.link(src, dst) except OSError: shutil.copy2(src, dst) PROMPTS = { "dgu": "double glazed unit", "sgu": "single glazed unit", "laminated_glass": "laminated glass", "cast_in_channel": "cast-in channel", "concrete": "concrete", "insulation": "insulation", "anchor": "anchor", } src, dst = Path(sys.argv[1]), Path(sys.argv[2]) if dst.exists(): shutil.rmtree(dst) for split in ("train", "valid", "test"): (dst / split).mkdir(parents=True) coco = json.loads((src / split / "_annotations.coco.json").read_text()) used = {a["category_id"] for a in coco["annotations"]} categories = [] for c in coco["categories"]: if c["id"] not in used and c.get("supercategory") == "none": continue categories.append({**c, "name": PROMPTS.get(c["name"], c["name"].replace("_", " "))}) coco["categories"] = categories for img in coco["images"]: link_or_copy(src / split / img["file_name"], dst / split / img["file_name"]) (dst / split / "_annotations.coco.json").write_text(json.dumps(coco)) print(f"{split}: {len(coco['images'])} images, {len(coco['annotations'])} annotations, " f"prompts={[c['name'] for c in categories]}")