"""Bootstrap a YOLO-World model with Indonesian-traffic classes baked in. YOLO-World is open-vocabulary — you give it a list of class names and it learns text embeddings for those specific names. We save the result as a regular .pt that our Detector loads via the standard YOLO() interface, so no other code paths change. Pick the base by size to trade accuracy for speed: .venv/bin/python -m scripts.setup_yolo_world # default: m (medium) .venv/bin/python -m scripts.setup_yolo_world s # smallest / fastest .venv/bin/python -m scripts.setup_yolo_world l # large .venv/bin/python -m scripts.setup_yolo_world x # largest / slowest Rough Mac MPS speed @ INFERENCE_SIZE=960 (your mileage varies): s → 15-20 fps proc, 75% accuracy of x m → 5-8 fps proc, 90% accuracy of x ← sweet spot l → 3-5 fps proc, 95% accuracy of x x → 1-2 fps proc, 100% reference Re-run any time to swap sizes; the output path is constant so the existing .env keeps working.""" from __future__ import annotations import sys from pathlib import Path from ultralytics import YOLOWorld OUT_PATH = Path(__file__).resolve().parents[1] / "models" / "yolo-world-id.pt" VALID_SIZES = ("s", "m", "l", "x") DEFAULT_SIZE = "m" # Classes tuned for Indonesian traffic CCTV. Order doesn't matter. # Add / remove freely — re-run the script and the saved model will # detect exactly the new list. CLASSES = [ # people "person", "pedestrian", "rider", # 4-wheelers "car", "van", "pickup", "minibus", "bus", "truck", # 2/3-wheelers "motorcycle", "scooter", "bicycle", # Indonesian specifics "becak", "bemo", "bajaj", "angkot", "ojek", ] def _base_weights(size: str) -> str: # ultralytics names: yolov8{s,m,l,x}-worldv2.pt — all auto-downloaded # on first use. return f"yolov8{size}-worldv2.pt" def main(argv: list[str]) -> None: size = (argv[1] if len(argv) > 1 else DEFAULT_SIZE).lower() if size not in VALID_SIZES: print(f"unknown size '{size}'. Use one of: {', '.join(VALID_SIZES)}", file=sys.stderr) sys.exit(1) base = _base_weights(size) print(f"loading YOLO-World base ({base})", file=sys.stderr) print("(auto-downloads on first run; sizes are roughly " "s≈22MB, m≈70MB, l≈250MB, x≈450MB)", file=sys.stderr) model = YOLOWorld(base) print(f"\nsetting {len(CLASSES)} custom classes:", file=sys.stderr) for c in CLASSES: print(f" - {c}", file=sys.stderr) model.set_classes(CLASSES) OUT_PATH.parent.mkdir(parents=True, exist_ok=True) model.save(str(OUT_PATH)) print(f"\nsaved → {OUT_PATH}", file=sys.stderr) print(f"\n.env MODEL_PATH already points here — just restart the server.", file=sys.stderr) if __name__ == "__main__": main(sys.argv)