36 lines
1.2 KiB
Python
36 lines
1.2 KiB
Python
"""Image/color matching helpers (OpenCV-based). Ported from scripts/detect_and_click.py."""
|
|
import cv2
|
|
import numpy as np
|
|
|
|
from ba_auto import config, driver
|
|
|
|
SPARKLE_SHOT_PATH = "/tmp/ba_live.png"
|
|
SPARKLE_CLICK_OFFSET = (75, 47)
|
|
SPARKLE_THRESHOLD = 0.97
|
|
|
|
|
|
def find_cafe_sparkle():
|
|
driver.screenshot(SPARKLE_SHOT_PATH)
|
|
template = cv2.imread(config.CAFE_SPARKLE_TEMPLATE)
|
|
b, g, r = cv2.split(template.astype(np.int16))
|
|
yellow_white = ((r > 180) & (g > 140) & (r - b > 60)) | ((r > 200) & (g > 200) & (b > 200))
|
|
mask_plane = (yellow_white.astype(np.uint8)) * 255
|
|
mask = cv2.merge([mask_plane, mask_plane, mask_plane])
|
|
th, tw = template.shape[:2]
|
|
|
|
img = cv2.imread(SPARKLE_SHOT_PATH)
|
|
result = cv2.matchTemplate(img, template, cv2.TM_CCORR_NORMED, mask=mask)
|
|
locs = np.where(result >= SPARKLE_THRESHOLD)
|
|
points = sorted(zip(*locs[::-1]), key=lambda p: -result[p[1], p[0]])
|
|
|
|
merged = []
|
|
for x, y in points:
|
|
if all(abs(x - mx) > tw // 2 or abs(y - my) > th // 2 for mx, my, _ in merged):
|
|
merged.append((x, y, result[y, x]))
|
|
if not merged:
|
|
return None
|
|
|
|
x, y, score = merged[0]
|
|
ox, oy = SPARKLE_CLICK_OFFSET
|
|
return (x + tw // 2 + ox, y + th // 2 + oy, score)
|