Spaces:
Runtime error
Runtime error
Download scripts/setup_yolov8.py from openscopelabs/cctv-object-detection: direct link, hf CLI and curl.
- Browser
- Download file 2.96 kB
-
https://huggingface.co/spaces/openscopelabs/cctv-object-detection/resolve/main/scripts/setup_yolov8.py
- Command line
-
hf download hf://spaces/openscopelabs/cctv-object-detection/scripts/setup_yolov8.py
-
curl -L -o setup_yolov8.py https://huggingface.co/spaces/openscopelabs/cctv-object-detection/resolve/main/scripts/setup_yolov8.py
2.96 kB
| """Bootstrap a standard YOLOv8 .pt for CPU-friendly traffic inference. | |
| YOLO-World gives zero-shot class flexibility but is too heavy for CPU | |
| production — even the S variant runs ~3 fps on an 8-core EPYC. Standard | |
| YOLOv8 ships with 80 COCO classes baked-in, has no text encoder branch, | |
| and runs 5-10× faster on the same hardware at similar mAP for the | |
| class subset we care about (car/truck/bus/motorcycle/bicycle/person). | |
| This script just pulls the requested YOLOv8 checkpoint (Ultralytics | |
| auto-downloads from their CDN on first reference) and saves it to the | |
| same constant output path our Detector expects via ``MODEL_PATH``: | |
| .venv/bin/python -m scripts.setup_yolov8 # default: m (medium) | |
| .venv/bin/python -m scripts.setup_yolov8 n # nano — fastest | |
| .venv/bin/python -m scripts.setup_yolov8 s # small | |
| .venv/bin/python -m scripts.setup_yolov8 m # medium ← sweet spot CPU | |
| .venv/bin/python -m scripts.setup_yolov8 l # large | |
| .venv/bin/python -m scripts.setup_yolov8 x # xlarge — slowest | |
| Rough EPYC 4-core ONNX speed @ INFERENCE_SIZE=640: | |
| n → 25-30 fps proc, lowest accuracy | |
| s → 15-20 fps proc | |
| m → 8-12 fps proc ← recommended for production | |
| l → 4-6 fps proc | |
| x → 2-3 fps proc | |
| Re-run any time to swap sizes; OUT_PATH is constant so the existing | |
| .env keeps working. | |
| After running, export to ONNX for the production CPU path: | |
| .venv/bin/python -m scripts.export_onnx --imgsz 640 | |
| """ | |
| from __future__ import annotations | |
| import shutil | |
| import sys | |
| from pathlib import Path | |
| from ultralytics import YOLO | |
| OUT_PATH = Path(__file__).resolve().parents[1] / "models" / "yolo-world-id.pt" | |
| VALID_SIZES = ("n", "s", "m", "l", "x") | |
| DEFAULT_SIZE = "m" | |
| def main() -> int: | |
| size = sys.argv[1].lower() if len(sys.argv) > 1 else DEFAULT_SIZE | |
| if size not in VALID_SIZES: | |
| print( | |
| f"Invalid size '{size}'. Valid options: {VALID_SIZES}", | |
| file=sys.stderr, | |
| ) | |
| return 2 | |
| base = f"yolov8{size}.pt" | |
| print(f"→ Loading {base} (Ultralytics will download if missing)…") | |
| model = YOLO(base) | |
| # Ultralytics saves the source weight to its own working directory. | |
| # Copy it under the canonical OUT_PATH so the Detector keeps using | |
| # the same MODEL_PATH regardless of backbone family. | |
| src = Path(model.ckpt_path) | |
| OUT_PATH.parent.mkdir(parents=True, exist_ok=True) | |
| shutil.copy(src, OUT_PATH) | |
| print(f"→ Wrote {OUT_PATH} ({OUT_PATH.stat().st_size / 1_000_000:.1f} MB)") | |
| print( | |
| "→ Detected classes:", | |
| ", ".join(sorted(set(model.names.values()))[:20]), | |
| "…", | |
| ) | |
| print() | |
| print( | |
| "Next: export to ONNX for CPU production:\n" | |
| " .venv/bin/python -m scripts.export_onnx --imgsz 640\n" | |
| "Then point MODEL_PATH at models/yolo-world-id.onnx and restart." | |
| ) | |
| return 0 | |
| if __name__ == "__main__": | |
| sys.exit(main()) | |