"""YOLO target detection with EMA smoothing. Detects the fading white/camouflage TARGET shape. Green reticle is erased before inference so the model cannot follow the cursor. """ from __future__ import annotations from typing import NamedTuple, Optional, Tuple import numpy as np from tracker.preprocess import erase_green_reticle class DetectResult(NamedTuple): center: Optional[Tuple[float, float]] conf: float xyxy: Optional[Tuple[float, float, float, float]] class EMASmoother: def __init__(self, alpha: float = 0.35): self.alpha = alpha self._xy: Optional[Tuple[float, float]] = None def reset(self) -> None: self._xy = None @property def value(self) -> Optional[Tuple[float, float]]: return self._xy def update(self, xy: Optional[Tuple[float, float]]) -> Optional[Tuple[float, float]]: if xy is None: return self._xy if self._xy is None: self._xy = xy return self._xy ax, ay = self._xy a = self.alpha self._xy = (a * xy[0] + (1 - a) * ax, a * xy[1] + (1 - a) * ay) return self._xy class TargetDetector: def __init__( self, weights: str, conf: float = 0.25, iou: float = 0.45, device: Optional[str] = None, ema_alpha: float = 0.35, erase_green: bool = True, imgsz: int = 640, ): from ultralytics import YOLO self.model = YOLO(weights) self.conf = conf self.iou = iou self.device = device self.erase_green = erase_green self.imgsz = imgsz self.smoother = EMASmoother(alpha=ema_alpha) self.last_raw: Optional[Tuple[float, float]] = None self.last_xyxy: Optional[Tuple[float, float, float, float]] = None self.last_conf: float = 0.0 self.miss_streak: int = 0 self._warmed = False def reset(self) -> None: self.smoother.reset() self.last_raw = None self.last_xyxy = None self.last_conf = 0.0 self.miss_streak = 0 def warmup(self, frame_bgr: Optional[np.ndarray] = None) -> None: if self._warmed: return import numpy as np dummy = frame_bgr if frame_bgr is not None else np.zeros((640, 640, 3), dtype=np.uint8) self.detect_raw(dummy) self._warmed = True def detect_raw(self, frame_bgr: np.ndarray) -> DetectResult: # Real game has no green cursor — strip it so we track the shape only. infer = erase_green_reticle(frame_bgr) if self.erase_green else frame_bgr results = self.model.predict( source=infer, conf=self.conf, iou=self.iou, device=self.device, imgsz=self.imgsz, verbose=False, ) if not results: return DetectResult(None, 0.0, None) r0 = results[0] if r0.boxes is None or len(r0.boxes) == 0: return DetectResult(None, 0.0, None) boxes = r0.boxes confs = boxes.conf.cpu().numpy() xyxy = boxes.xyxy.cpu().numpy() i = int(np.argmax(confs)) x1, y1, x2, y2 = map(float, xyxy[i]) cx = (x1 + x2) * 0.5 cy = (y1 + y2) * 0.5 return DetectResult((cx, cy), float(confs[i]), (x1, y1, x2, y2)) def track(self, frame_bgr: np.ndarray) -> DetectResult: raw = self.detect_raw(frame_bgr) if raw.center is None: self.miss_streak += 1 self.last_conf = max(0.0, self.last_conf * 0.85) if self.miss_streak > 15: self.last_xyxy = None return DetectResult(None, 0.0, None) return DetectResult(self.smoother.value, self.last_conf, self.last_xyxy) self.miss_streak = 0 self.last_raw = raw.center self.last_xyxy = raw.xyxy self.last_conf = raw.conf smooth = self.smoother.update(raw.center) return DetectResult(smooth, raw.conf, raw.xyxy)