preprocess.py 1.1 KB

12345678910111213141516171819202122232425262728293031
  1. """Remove recorded green reticle so the model learns the target shape, not the cursor."""
  2. from __future__ import annotations
  3. import cv2
  4. import numpy as np
  5. LOWER_HSV = np.array([40, 80, 80], dtype=np.uint8)
  6. UPPER_HSV = np.array([95, 255, 255], dtype=np.uint8)
  7. def green_reticle_mask(frame_bgr: np.ndarray) -> np.ndarray:
  8. hsv = cv2.cvtColor(frame_bgr, cv2.COLOR_BGR2HSV)
  9. mask = cv2.inRange(hsv, LOWER_HSV, UPPER_HSV)
  10. kernel = np.ones((5, 5), np.uint8)
  11. mask = cv2.dilate(mask, kernel, iterations=2)
  12. return mask
  13. def erase_green_reticle(frame_bgr: np.ndarray, fast: bool = True) -> np.ndarray:
  14. """Inpaint green reticle pixels. Real gameplay has no green cursor."""
  15. mask = green_reticle_mask(frame_bgr)
  16. if int(cv2.countNonZero(mask)) < 20:
  17. return frame_bgr
  18. if fast:
  19. # Much faster than TELEA; good enough for inference/display.
  20. blurred = cv2.GaussianBlur(frame_bgr, (21, 21), 0)
  21. out = frame_bgr.copy()
  22. out[mask > 0] = blurred[mask > 0]
  23. return out
  24. return cv2.inpaint(frame_bgr, mask, 3, cv2.INPAINT_TELEA)