Zion Boggan
repos/Pitch Tracker CV/tools/yolo_infer_frame.py
zionboggan.com ↗
37 lines · python
History for this file →
1
"""Run the trained ball detector on one frame and save an annotated image."""
2
from __future__ import annotations
3
 
4
import argparse
5
from pathlib import Path
6
 
7
import cv2
8
from rich.console import Console
9
 
10
ROOT = Path(__file__).resolve().parents[1]
11
MODEL = ROOT / "configs" / "models" / "mlb26_ball.pt"
12
console = Console()
13
 
14
def main() -> int:
15
    ap = argparse.ArgumentParser(description="Infer MLB ball model on one image.")
16
    ap.add_argument("image", type=Path)
17
    ap.add_argument("--model", type=Path, default=MODEL)
18
    ap.add_argument("--conf", type=float, default=0.25)
19
    ap.add_argument("--out", type=Path, default=ROOT / "logs" / "yolo_infer.png")
20
    args = ap.parse_args()
21
 
22
    from ultralytics import YOLO
23
 
24
    model = YOLO(str(args.model))
25
    results = model.predict(source=str(args.image), conf=args.conf, imgsz=640, verbose=False)
26
    annotated = results[0].plot()
27
    args.out.parent.mkdir(parents=True, exist_ok=True)
28
    cv2.imwrite(str(args.out), annotated)
29
    console.print(f"[green]Saved annotated output -> {args.out}[/green]")
30
    for box in results[0].boxes:
31
        xyxy = [float(x) for x in box.xyxy[0]]
32
        conf = float(box.conf[0])
33
        console.print(f"ball conf={conf:.3f} xyxy={xyxy}")
34
    return 0
35
 
36
if __name__ == "__main__":
37
    raise SystemExit(main())