blob_lock.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. """Shape-agnostic bright-target lock (no class / no training)."""
  2. from __future__ import annotations
  3. from typing import Optional, Tuple
  4. import cv2
  5. import numpy as np
  6. from tracker.preprocess import erase_green_reticle
  7. Xyxy = Tuple[float, float, float, float]
  8. def find_white_target(
  9. frame_bgr: np.ndarray,
  10. *,
  11. erase_green: bool = True,
  12. search_near: Optional[Tuple[float, float]] = None,
  13. near_radius: float = 220.0,
  14. prefer_center: bool = True,
  15. min_area: float = 600.0,
  16. max_area_ratio: float = 0.25,
  17. ) -> Optional[Xyxy]:
  18. """
  19. Find a bright, low-saturation blob (opaque phase of the minigame target).
  20. Shape-agnostic: circle / star / irregular all work the same.
  21. """
  22. img = erase_green_reticle(frame_bgr) if erase_green else frame_bgr
  23. h, w = img.shape[:2]
  24. max_area = w * h * max_area_ratio
  25. hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
  26. gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
  27. # White / near-white fill.
  28. mask = cv2.inRange(hsv, np.array([0, 0, 185]), np.array([180, 70, 255]))
  29. kernel = np.ones((5, 5), np.uint8)
  30. mask = cv2.morphologyEx(mask, cv2.MORPH_OPEN, kernel, iterations=1)
  31. mask = cv2.morphologyEx(mask, cv2.MORPH_CLOSE, kernel, iterations=2)
  32. contours, _ = cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
  33. if not contours:
  34. return None
  35. cx0, cy0 = (w * 0.5, h * 0.5)
  36. best: Optional[Xyxy] = None
  37. best_score = -1.0
  38. for cnt in contours:
  39. area = float(cv2.contourArea(cnt))
  40. if area < min_area or area > max_area:
  41. continue
  42. x, y, bw, bh = cv2.boundingRect(cnt)
  43. if bw < 12 or bh < 12:
  44. continue
  45. # Reject ultra-thin UI strokes.
  46. if area / float(max(bw * bh, 1)) < 0.25:
  47. continue
  48. cx = x + bw * 0.5
  49. cy = y + bh * 0.5
  50. mean_v = float(cv2.mean(gray, mask=mask[y : y + bh, x : x + bw])[0])
  51. if search_near is not None:
  52. dist = float(np.hypot(cx - search_near[0], cy - search_near[1]))
  53. if dist > near_radius:
  54. continue
  55. dist_term = 1.0 / (1.0 + dist / 80.0)
  56. elif prefer_center:
  57. dist = float(np.hypot(cx - cx0, cy - cy0))
  58. # Soft prior: prefer nearer to screen center (game spawns mid).
  59. dist_term = 1.0 / (1.0 + dist / (0.35 * max(w, h)))
  60. else:
  61. dist_term = 1.0
  62. score = area * (mean_v / 255.0) * dist_term
  63. if score > best_score:
  64. best_score = score
  65. best = (float(x), float(y), float(x + bw), float(y + bh))
  66. return best
  67. def xyxy_to_xywh(xyxy: Xyxy) -> Tuple[float, float, float, float]:
  68. x1, y1, x2, y2 = xyxy
  69. return x1, y1, max(1.0, x2 - x1), max(1.0, y2 - y1)
  70. def xywh_to_xyxy(xywh: Tuple[float, float, float, float]) -> Xyxy:
  71. x, y, w, h = xywh
  72. return x, y, x + w, y + h
  73. def clip_xyxy(xyxy: Xyxy, width: int, height: int) -> Optional[Xyxy]:
  74. x1, y1, x2, y2 = xyxy
  75. x1 = float(np.clip(x1, 0, width - 1))
  76. y1 = float(np.clip(y1, 0, height - 1))
  77. x2 = float(np.clip(x2, 0, width - 1))
  78. y2 = float(np.clip(y2, 0, height - 1))
  79. if x2 - x1 < 8 or y2 - y1 < 8:
  80. return None
  81. return x1, y1, x2, y2