feat: Enhance yanghao detection with polygon support and template matching

- Updated config.json to include new parameters for yanghao template paths and match thresholds.
- Adjusted default icon match threshold in config to 0.70 for better detection.
- Implemented polygon detection logic in yanghao_idle function to allow for flexible ROI configurations.
- Added support for per-action ROI scaling and sizes in the configuration.
- Introduced new methods in fudai_analyse for capturing ROI screenshots and matching templates.
- Created a test script to validate polygon detection against provided templates.
- Improved error handling and logging for better debugging during detection processes.
This commit is contained in:
Your Name committed 2026-01-04 09:06:25 +08:00
1 parent 3678ff7e31
commit a61ba62864
8 files changed
+848 -26

No files matched your search

+352 -9
View File
@@ -73,8 +73,43 @@ def yanghao_idle(analyser, total_seconds: int) -> None:
_human_delay(1.0, 2.2)
need_settle_after_swipe = False
_step_shot(f"before_detect_{roi_label}")
# 如果坐标未配置且提供了模板文件,则使用屏幕右侧的默认坐标作为 ROI
tpl = str(template_path or "").strip()
tpl_exists = False
try:
# 直接存在
if tpl and os.path.exists(tpl):
tpl_exists = True
else:
# 尝试相对于 config 路径的仓库根解析(保证 assets/ 在不同 cwd 下也能被找到)
try:
cfg = getattr(analyser, "config_path", "config/config.json") or "config/config.json"
cfg_abspath = os.path.abspath(cfg)
project_root = os.path.dirname(os.path.dirname(cfg_abspath))
candidate = os.path.join(project_root, tpl)
if tpl and os.path.exists(candidate):
tpl_exists = True
template_path = candidate
except Exception:
pass
except Exception:
tpl_exists = False
print(f"[养号][DEBUG] _detect_icon action={action} template_path='{template_path}' tpl_exists={tpl_exists}")
if int(base_x or 0) <= 0 or int(base_y or 0) <= 0:
return False, 0.0
if tpl_exists:
try:
if action == "爱心":
base_x = int(1080 * 0.88)
base_y = int(2400 * 0.78)
else:
base_x = int(1080 * 0.78)
base_y = int(2400 * 0.78)
except Exception:
base_x = 900
base_y = 1800
else:
return False, 0.0
# ROI 中心点允许独立偏移(仅影响截图检测,不影响点击坐标)
off_x = 0
@@ -93,19 +128,243 @@ def yanghao_idle(analyser, total_seconds: int) -> None:
sy = int(sy) + int(off_y * int(getattr(analyser, "resolution_ratio_y", 2400)) / 2400)
except Exception:
pass
roi_path = analyser._capture_roi_screenshot(roi_label, sx, sy, int(getattr(analyser, "yanghao_icon_roi_size", 180) or 180))
# 支持为不同动作指定不同的 ROI 大小,优先使用专用配置,否则回退到通用配置
# 优先使用明确的矩形配置(x1,y1,x2,y2),否则使用 per-action roi_size 或通用 roi_size
try:
rect_used = False
if action == "爱心":
rx1 = getattr(analyser, "yanghao_like_rect_x1", None)
ry1 = getattr(analyser, "yanghao_like_rect_y1", None)
rx2 = getattr(analyser, "yanghao_like_rect_x2", None)
ry2 = getattr(analyser, "yanghao_like_rect_y2", None)
elif action == "收藏":
rx1 = getattr(analyser, "yanghao_fav_rect_x1", None)
ry1 = getattr(analyser, "yanghao_fav_rect_y1", None)
rx2 = getattr(analyser, "yanghao_fav_rect_x2", None)
ry2 = getattr(analyser, "yanghao_fav_rect_y2", None)
else:
rx1 = ry1 = rx2 = ry2 = None
# 支持多边形配置(优先),格式期望为列表[[x,y],...];否则回退到矩形(x1,y1,x2,y2)
poly = None
if action == "爱心":
poly = getattr(analyser, "yanghao_like_poly", None)
elif action == "收藏":
poly = getattr(analyser, "yanghao_fav_poly", None)
if poly:
try:
pts = list(poly)
if len(pts) >= 1:
xs = [int(p[0]) for p in pts]
ys = [int(p[1]) for p in pts]
# 计算多边形质心(面积加权),若顶点过少回退到顶点平均
def _poly_centroid(pts_list):
if len(pts_list) < 3:
return (sum(xs) // len(xs), sum(ys) // len(ys))
area = 0.0
cx = 0.0
cy = 0.0
n = len(pts_list)
for i in range(n):
x0 = float(pts_list[i][0])
y0 = float(pts_list[i][1])
x1 = float(pts_list[(i + 1) % n][0])
y1 = float(pts_list[(i + 1) % n][1])
cross = x0 * y1 - x1 * y0
area += cross
cx += (x0 + x1) * cross
cy += (y0 + y1) * cross
area = area / 2.0
if abs(area) < 1e-9:
return (sum(xs) // len(xs), sum(ys) // len(ys))
cx = cx / (6.0 * area)
cy = cy / (6.0 * area)
return (int(round(cx)), int(round(cy)))
base_cx, base_cy = _poly_centroid(pts)
x1 = min(xs)
y1 = min(ys)
x2 = max(xs)
y2 = max(ys)
w = x2 - x1
h = y2 - y1
size = max(1, int(max(w, h)))
sx, sy = analyser._scale_from_base(int(base_cx), int(base_cy))
try:
scale_x = float(getattr(analyser, "resolution_ratio_x", 1080)) / 1080.0
scale_y = float(getattr(analyser, "resolution_ratio_y", 2400)) / 2400.0
except Exception:
scale_x = scale_y = 1.0
try:
if action == "爱心":
action_scale = float(getattr(analyser, "yanghao_like_roi_scale", 1.5))
elif action == "收藏":
action_scale = float(getattr(analyser, "yanghao_fav_roi_scale", 1.5))
else:
action_scale = 1.0
except Exception:
action_scale = 1.5
size_px = max(1, int(size * max(scale_x, scale_y) * float(action_scale)))
roi_path = analyser._capture_roi_screenshot(roi_label, sx, sy, int(size_px))
rect_used = True
except Exception:
rect_used = False
elif rx1 is not None and ry1 is not None and rx2 is not None and ry2 is not None:
try:
# 允许任意顺序的点,取 min/max
x1 = int(min(int(rx1), int(rx2)))
y1 = int(min(int(ry1), int(ry2)))
x2 = int(max(int(rx1), int(rx2)))
y2 = int(max(int(ry1), int(ry2)))
base_cx = (x1 + x2) // 2
base_cy = (y1 + y2) // 2
w = x2 - x1
h = y2 - y1
size = max(1, int(max(w, h)))
sx, sy = analyser._scale_from_base(int(base_cx), int(base_cy))
try:
scale_x = float(getattr(analyser, "resolution_ratio_x", 1080)) / 1080.0
scale_y = float(getattr(analyser, "resolution_ratio_y", 2400)) / 2400.0
except Exception:
scale_x = scale_y = 1.0
try:
if action == "爱心":
action_scale = float(getattr(analyser, "yanghao_like_roi_scale", 1.5))
elif action == "收藏":
action_scale = float(getattr(analyser, "yanghao_fav_roi_scale", 1.5))
else:
action_scale = 1.0
except Exception:
action_scale = 1.5
size_px = max(1, int(size * max(scale_x, scale_y) * float(action_scale)))
roi_path = analyser._capture_roi_screenshot(roi_label, sx, sy, int(size_px))
rect_used = True
except Exception:
rect_used = False
if not rect_used:
try:
if action == "爱心":
roi_size = int(getattr(analyser, "yanghao_like_icon_roi_size", None) or getattr(analyser, "yanghao_icon_roi_size", 180) or 180)
elif action == "收藏":
roi_size = int(getattr(analyser, "yanghao_fav_icon_roi_size", None) or getattr(analyser, "yanghao_icon_roi_size", 180) or 180)
else:
roi_size = int(getattr(analyser, "yanghao_icon_roi_size", 180) or 180)
except Exception:
roi_size = int(getattr(analyser, "yanghao_icon_roi_size", 180) or 180)
try:
scale_x = float(getattr(analyser, "resolution_ratio_x", 1080)) / 1080.0
scale_y = float(getattr(analyser, "resolution_ratio_y", 2400)) / 2400.0
except Exception:
scale_x = scale_y = 1.0
try:
if action == "爱心":
action_scale = float(getattr(analyser, "yanghao_like_roi_scale", 1.5))
elif action == "收藏":
action_scale = float(getattr(analyser, "yanghao_fav_roi_scale", 1.5))
else:
action_scale = 1.0
except Exception:
action_scale = 1.5
roi_px = max(1, int(roi_size * max(scale_x, scale_y) * float(action_scale)))
roi_path = analyser._capture_roi_screenshot(roi_label, sx, sy, int(roi_px))
except Exception:
try:
default_size = int(getattr(analyser, "yanghao_icon_roi_size", 180) or 180)
scale_x = float(getattr(analyser, "resolution_ratio_x", 1080)) / 1080.0
scale_y = float(getattr(analyser, "resolution_ratio_y", 2400)) / 2400.0
except Exception:
scale_x = scale_y = 1.0
default_size = int(getattr(analyser, "yanghao_icon_roi_size", 180) or 180)
try:
if action == "爱心":
action_scale = float(getattr(analyser, "yanghao_like_roi_scale", 1.5))
elif action == "收藏":
action_scale = float(getattr(analyser, "yanghao_fav_roi_scale", 1.5))
else:
action_scale = 1.0
except Exception:
action_scale = 1.5
roi_px = max(1, int(default_size * max(scale_x, scale_y) * float(action_scale)))
roi_path = analyser._capture_roi_screenshot(roi_label, sx, sy, int(roi_px))
if not template_path or not os.path.exists(template_path):
print(f"[养号] 检测: {action} -> 失败(缺少模板文件:{template_path})")
return False, 0.0
detected, score = analyser._match_icon_template(roi_path, template_path)
try:
if action == "爱心":
thr = getattr(analyser, "yanghao_like_icon_match_threshold", None)
elif action == "收藏":
thr = getattr(analyser, "yanghao_fav_icon_match_threshold", None)
else:
thr = None
if thr is None:
detected, score = analyser._match_icon_template(roi_path, template_path)
else:
detected, score = analyser._match_icon_template(roi_path, template_path, float(thr))
except Exception:
detected, score = analyser._match_icon_template(roi_path, template_path)
print(f"[养号] 检测: {action} -> {'检测到' if detected else '未检测到'}(score={score:.3f})")
return detected, score
# 当已配置坐标或已提供模板文件时,启用图标检测
def _tpl_exists_check(tpl_path: str) -> tuple[bool, str]:
try:
tpl = str(tpl_path or "").strip()
if not tpl:
return False, tpl
# 直接存在
if os.path.exists(tpl):
return True, tpl
# 尝试相对于 config 路径的仓库根解析
try:
cfg = getattr(analyser, "config_path", "config/config.json") or "config/config.json"
cfg_abspath = os.path.abspath(cfg)
project_root = os.path.dirname(os.path.dirname(cfg_abspath))
candidate = os.path.join(project_root, tpl)
if os.path.exists(candidate):
return True, candidate
except Exception:
pass
except Exception:
pass
return False, tpl_path
try:
like_tpl = str(getattr(analyser, "yanghao_like_template_path", "") or "").strip()
fav_tpl = str(getattr(analyser, "yanghao_fav_template_path", "") or "").strip()
like_tpl_exists, like_tpl = _tpl_exists_check(like_tpl)
fav_tpl_exists, fav_tpl = _tpl_exists_check(fav_tpl)
try:
cfg = getattr(analyser, "config_path", "config/config.json") or "config/config.json"
cfg_abspath = os.path.abspath(cfg)
project_root = os.path.dirname(os.path.dirname(cfg_abspath))
except Exception:
project_root = None
print(f"[养号][DEBUG] like_tpl='{like_tpl}' exists={like_tpl_exists}; fav_tpl='{fav_tpl}' exists={fav_tpl_exists}; project_root={project_root}")
# 如果解析得到的绝对路径不同,更新属性以便后续使用
if like_tpl_exists:
try:
setattr(analyser, "yanghao_like_template_path", like_tpl)
except Exception:
pass
if fav_tpl_exists:
try:
setattr(analyser, "yanghao_fav_template_path", fav_tpl)
except Exception:
pass
except Exception:
like_tpl_exists = False
fav_tpl_exists = False
use_icon = (
int(getattr(analyser, "yanghao_like_icon_x", 0) or 0) > 0
and int(getattr(analyser, "yanghao_like_icon_y", 0) or 0) > 0
and int(getattr(analyser, "yanghao_fav_icon_x", 0) or 0) > 0
and int(getattr(analyser, "yanghao_fav_icon_y", 0) or 0) > 0
(
int(getattr(analyser, "yanghao_like_icon_x", 0) or 0) > 0
and int(getattr(analyser, "yanghao_like_icon_y", 0) or 0) > 0
and int(getattr(analyser, "yanghao_fav_icon_x", 0) or 0) > 0
and int(getattr(analyser, "yanghao_fav_icon_y", 0) or 0) > 0
)
or (like_tpl_exists and fav_tpl_exists)
)
start_ts = time.time()
@@ -127,8 +386,36 @@ def yanghao_idle(analyser, total_seconds: int) -> None:
pass
if not use_icon:
# 未配置图标检测就退化为“原地等待”
analyser.operation.delay(int(max(1, total_seconds)))
# 未配置图标检测时,退化为有动作的养号:周期性下滑并按概率使用坐标兜底点击/点赞
end_ts = time.time() + int(max(1, total_seconds))
print("[养号] 未配置图标模板,退化为坐标动作养号")
while time.time() < end_ts:
# 每次循环尝试按概率执行点赞/收藏/下滑
try:
# 尝试点赞(使用配置坐标作为兜底)
like_prob = float(getattr(analyser, "yanghao_like_prob", 0) or 0)
fav_prob = float(getattr(analyser, "yanghao_fav_prob", 0) or 0)
# 随机触发动作
r = random.random()
if r < like_prob and int(getattr(analyser, "yanghao_like_icon_x", 0) or 0) > 0:
_step_shot("before_like_click_fallback")
_scaled_click(int(getattr(analyser, "yanghao_like_icon_x", 0) or 0), int(getattr(analyser, "yanghao_like_icon_y", 0) or 0))
_step_shot("after_like_click_fallback")
print("[养号] 退化动作: 点爱心(坐标兜底)")
elif r < like_prob + fav_prob and int(getattr(analyser, "yanghao_fav_icon_x", 0) or 0) > 0:
_step_shot("before_fav_click_fallback")
_scaled_click(int(getattr(analyser, "yanghao_fav_icon_x", 0) or 0), int(getattr(analyser, "yanghao_fav_icon_y", 0) or 0))
_step_shot("after_fav_click_fallback")
print("[养号] 退化动作: 点收藏(坐标兜底)")
else:
# 未触发具体动作则下滑切换内容
_do_swipe_next()
# 随机短暂停顿,避免行为太机械
_human_delay(2.0, 6.0)
except Exception:
# 出错时尽量不阻塞太久
analyser.operation.delay(1)
return
like_ok, _like_score = _detect_icon(
@@ -471,6 +758,62 @@ def yanghao_wait(analyser, total_seconds: int) -> None:
# 只保留推荐页养号;增加“间隔(冷却)”控制,避免触发过于频繁
try:
# 如果启用了高价值预约且存在未完成的预约,则在开始养号前等待预约完成
try:
high_value_enabled = bool(getattr(analyser, "high_value_fudai_enabled", False))
except Exception:
high_value_enabled = False
if high_value_enabled:
try:
has_reservations = bool(getattr(analyser, "reservations", None) and len(getattr(analyser, "reservations", [])) > 0)
except Exception:
has_reservations = False
try:
after_pending = bool(getattr(analyser, "reservation_after_action_pending", False))
except Exception:
after_pending = False
if has_reservations or after_pending:
try:
analyser._log_kv(
"养号",
"检测到未完成的预约,养号将等待预约完成后再开始",
reservations_count=(len(getattr(analyser, "reservations", [])) if getattr(analyser, "reservations", None) else 0),
reservation_after_action_pending=after_pending,
)
except Exception:
print("[养号] 检测到未完成的预约,等待预约完成后再开始")
# 无限等待直到预约完成(不再使用超时),每次轮询间隔短暂休眠
poll_interval = 3
waited = 0
while True:
try:
still_has = bool(getattr(analyser, "reservations", None) and len(getattr(analyser, "reservations", [])) > 0)
still_pending = bool(getattr(analyser, "reservation_after_action_pending", False))
if not still_has and not still_pending:
break
except Exception:
# 若查询出错,继续等待并重试
still_has = True
still_pending = True
try:
analyser.operation.delay(poll_interval)
except Exception:
time.sleep(poll_interval)
waited += poll_interval
try:
analyser._log_kv(
"养号",
"预约已完成,开始养号",
waited_seconds=waited,
reservations_count=(len(getattr(analyser, "reservations", [])) if getattr(analyser, "reservations", None) else 0),
reservation_after_action_pending=bool(getattr(analyser, "reservation_after_action_pending", False)),
)
except Exception:
print(f"[养号] 预约已完成,等待了{waited}s,开始养号")
now = time.time()
interval = int(getattr(analyser, "yanghao_recommend_interval_seconds", 0) or 0)
last_ts = float(getattr(analyser, "_yanghao_last_recommend_ts", 0.0) or 0.0)