| 1 | """Run ball and PCI detectors on a single saved frame and save annotated PNG.""" |
| 2 | from __future__ import annotations |
| 3 | |
| 4 | import sys |
| 5 | from pathlib import Path |
| 6 | |
| 7 | import cv2 |
| 8 | from rich.console import Console |
| 9 | |
| 10 | sys.path.insert(0, str(Path(__file__).resolve().parents[1])) |
| 11 | from cv._common import load_config |
| 12 | from cv.ball_tracker import detect_ball |
| 13 | from cv.pci_tracker import detect_by_hsv as pci_detect |
| 14 | |
| 15 | console = Console() |
| 16 | |
| 17 | def main() -> int: |
| 18 | if len(sys.argv) < 2: |
| 19 | console.print("usage: detect_on_frame.py <frame.png>") |
| 20 | return 2 |
| 21 | fp = Path(sys.argv[1]) |
| 22 | frame = cv2.imread(str(fp), cv2.IMREAD_COLOR) |
| 23 | if frame is None: |
| 24 | console.print(f"[red]Can't read {fp}[/red]") |
| 25 | return 2 |
| 26 | |
| 27 | cfg = load_config() |
| 28 | overlay = frame.copy() |
| 29 | h, w = frame.shape[:2] |
| 30 | plate_y = int(h * float(cfg["cv"].get("plate_y_frac", 0.72))) |
| 31 | cv2.line(overlay, (0, plate_y), (w, plate_y), (255, 255, 255), 1) |
| 32 | |
| 33 | ball = detect_ball(frame, cfg) |
| 34 | if ball is not None: |
| 35 | cv2.circle(overlay, (int(ball.x), int(ball.y)), max(int(ball.r), 4), (0, 255, 255), 2) |
| 36 | cv2.putText(overlay, f"ball r={ball.r:.0f} s={ball.score:.2f}", |
| 37 | (int(ball.x) + 10, int(ball.y) - 10), |
| 38 | cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 255, 255), 2, cv2.LINE_AA) |
| 39 | console.print(f"ball: x={ball.x:.0f} y={ball.y:.0f} r={ball.r:.1f} score={ball.score:.2f}") |
| 40 | else: |
| 41 | console.print("ball: none") |
| 42 | |
| 43 | pci = pci_detect(frame, cfg["cv"]["pci"]) |
| 44 | if pci is not None: |
| 45 | cv2.circle(overlay, (int(pci["x"]), int(pci["y"])), int(pci["r"]), (0, 255, 0), 2) |
| 46 | cv2.putText(overlay, f"pci r={pci['r']:.0f} s={pci['score']:.2f}", |
| 47 | (int(pci["x"]) + 10, int(pci["y"]) + 20), |
| 48 | cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 255, 0), 2, cv2.LINE_AA) |
| 49 | console.print(f"pci: x={pci['x']:.0f} y={pci['y']:.0f} r={pci['r']:.1f} score={pci['score']:.2f}") |
| 50 | else: |
| 51 | console.print("pci: none") |
| 52 | |
| 53 | out = Path(__file__).resolve().parents[1] / "logs" / f"detect_{fp.stem}.png" |
| 54 | cv2.imwrite(str(out), overlay) |
| 55 | console.print(f"saved -> {out}") |
| 56 | return 0 |
| 57 | |
| 58 | if __name__ == "__main__": |
| 59 | sys.exit(main()) |