From a61ba62864439fbf71a9e3f27cd3f8d5f081b65e Mon Sep 17 00:00:00 2001 From: Your Name Date: Sun, 4 Jan 2026 09:06:25 +0800 Subject: [PATCH] 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. --- config.json | 2 +- config/config.json | 7 +- douyin/core/douyin_fudai.py | 275 ++++++++++++++++++++- douyin/core/state/reservations.json | 2 +- douyin/other/douyin_yanghao.py | 361 +++++++++++++++++++++++++++- douyin/utils/douyin_config.py | 119 +++++++++ run_cli.py | 22 +- tools/test_yanghao_detection.py | 86 +++++++ 8 files changed, 848 insertions(+), 26 deletions(-) create mode 100644 tools/test_yanghao_detection.py diff --git a/config.json b/config.json index bcbc2ba..559b9d6 100644 --- a/config.json +++ b/config.json @@ -60,7 +60,7 @@ "yanghao_fav_prob": 0.85, "yanghao_comment_prob": 1.0, "yanghao_icon_roi_size": 240, - "yanghao_icon_match_threshold": 0.77, + "yanghao_icon_match_threshold": 0.70, "yanghao_step_screenshot": false, "yanghao_like_roi_offset_x": 0, "yanghao_like_roi_offset_y": -50, diff --git a/config/config.json b/config/config.json index 5056ffa..31be595 100644 --- a/config/config.json +++ b/config/config.json @@ -1,5 +1,5 @@ { - "needswitch": false, + "needswitch": true, "infinite_loop_enabled": true, "wait_minutes": 5, "min_prize_value_yuan": 200.0, @@ -189,5 +189,6 @@ "fansclub_auto_join_wait_seconds": 5.0, "fansclub_manual_wait_seconds": 3.0, "enable_streaming_screenshot": false, - "fudai_exit_after_misses": 30 -} \ No newline at end of file + "fudai_exit_after_misses": 30, + "yanghao_only": false +} diff --git a/douyin/core/douyin_fudai.py b/douyin/core/douyin_fudai.py index 936c413..ff9b56c 100644 --- a/douyin/core/douyin_fudai.py +++ b/douyin/core/douyin_fudai.py @@ -226,7 +226,12 @@ class fudai_analyse: self.yanghao_fav_prob: float = 0.05 self.yanghao_comment_prob: float = 0.0 self.yanghao_icon_roi_size: int = 180 - self.yanghao_icon_match_threshold: float = 0.72 + # 调整默认阈值为 0.70(更宽松,减少漏检) + self.yanghao_icon_match_threshold: float = 0.70 + self.yanghao_like_roi_scale: float = 1.5 + self.yanghao_fav_roi_scale: float = 1.5 + self.yanghao_like_icon_match_threshold: float | None = None + self.yanghao_fav_icon_match_threshold: float | None = None # icon 坐标(基于 1080x2400) self.yanghao_like_icon_x: int = 0 self.yanghao_like_icon_y: int = 0 @@ -316,6 +321,42 @@ class fudai_analyse: """兼容原版 fudai_analyse.click:转发到底层 operation.click。""" douyin_helpers.HelperFunctions.click(self, x, y) + def swipe(self, start_x: int, start_y: int, end_x: int, end_y: int, duration_ms: int = 300) -> None: + """兼容原版 fudai_analyse.swipe:转发到底层 operation.swipe。 + + 参数使用像素坐标,duration_ms 为滑动持续时间(毫秒)。 + """ + try: + # 首选底层 operation 的 swipe 实现 + try: + self.operation.swipe(self.device_id, start_x, start_y, end_x, end_y, duration_ms) + return + except Exception: + pass + + # 回退:尝试使用 douyin.handlers.douyin_swipe_ops 的 adb 命令直接发送 + try: + from douyin.handlers import douyin_swipe_ops + + ok = douyin_swipe_ops._adb_swipe_direct( + getattr(self, "device_id", ""), start_x, start_y, end_x, end_y, duration_ms, self.operation + ) + if ok: + return + except Exception: + pass + + # 最后兜底:记录滑动失败 + try: + self._log_kv("滑动", "swipe 失败 - 无可用实现", start_x=start_x, start_y=start_y, end_x=end_x, end_y=end_y, duration_ms=duration_ms) + except Exception: + pass + except Exception: + try: + self._log_kv("滑动", "swipe 异常", start_x=start_x, start_y=start_y, end_x=end_x, end_y=end_y, duration_ms=duration_ms) + except Exception: + pass + def load_config(self) -> None: """从 config.json 加载与福袋筛选/节流相关的关键参数(精简版)。""" douyin_config.ConfigManager.load_config_from_file(self) # type: ignore[attr-defined] @@ -632,6 +673,238 @@ class fudai_analyse: """保存当前截图到 `pic/steps/` 目录,文件名包含 run_id 与序号,供调试与回放使用。""" douyin_screenshot_handler.ScreenshotHandler.save_step_screenshot(self, label) + def _capture_roi_screenshot(self, label: str, sx: int, sy: int, size: int) -> str: + """捕获以 (sx,sy) 为中心,边长为 size 的 ROI 截图,保存到 `pic/` 并返回路径。 + + 返回 ROI 图片路径(绝对路径),失败返回空字符串。 + """ + try: + import path_config + from PIL import Image + + # 确保有最新全图 + try: + self.operation.get_screenshot(self.device_id, use_service=False) + except Exception: + pass + + pic_path = path_config.get_screenshot_path() + if not os.path.exists(pic_path): + return "" + + # 计算裁剪框(保证正方形,size 基于像素) + half = int(size // 2) + left = int(max(0, sx - half)) + top = int(max(0, sy - half)) + right = int(sx + half) + bottom = int(sy + half) + + # 以原子方式读取文件内容再由 PIL 解析,避免因文件替换/锁定导致的半写入问题 + import io + from PIL import UnidentifiedImageError + + data = None + # 增强读取:多次尝试并在每次读到字节后用 PIL 验证,避免半写入/损坏 PNG 导致解析异常 + for attempt in range(6): + try: + try: + # 主动请求最新截图以尽量获得完整文件 + self.operation.get_screenshot(self.device_id, use_service=False) + except Exception: + pass + + if not os.path.exists(pic_path): + time.sleep(0.08) + continue + sz = os.path.getsize(pic_path) + if sz < 1024: + time.sleep(0.08) + continue + with open(pic_path, "rb") as f: + data = f.read() + if not data or not data.startswith(b"\x89PNG"): + data = None + time.sleep(0.08) + continue + # quick tail check + if b"IEND" not in data[-512:]: + data = None + time.sleep(0.08) + continue + + # 尝试由 PIL 快速验证(不加载像素,仅检查文件完整性) + try: + img_test = Image.open(io.BytesIO(data)) + img_test.verify() + except Exception: + data = None + time.sleep(0.08) + continue + + # 通过基本校验,退出重试循环 + break + except Exception: + data = None + time.sleep(0.08) + + if not data: + return "" + + try: + img = Image.open(io.BytesIO(data)) + img.load() + except UnidentifiedImageError: + return "" + except Exception: + # 如果首次解析仍然失败,尝试再获取一次截图并重试一次 + try: + try: + self.operation.get_screenshot(self.device_id, use_service=False) + except Exception: + pass + with open(pic_path, "rb") as f: + data2 = f.read() + if data2 and data2.startswith(b"\x89PNG") and b"IEND" in data2[-512:]: + try: + img = Image.open(io.BytesIO(data2)) + img.load() + except Exception: + return "" + else: + return "" + except Exception: + return "" + w, h = img.size + # 修正边界 + left = max(0, min(left, w - 1)) + top = max(0, min(top, h - 1)) + right = max(1, min(right, w)) + bottom = max(1, min(bottom, h)) + + roi = img.crop((left, top, right, bottom)) + # 保存到 pic 目录,使用 label 区分 + out_dir = path_config.get_pic_dir() + os.makedirs(out_dir, exist_ok=True) + fname = f"roi_{label}_{getattr(self, '_run_id', 'run')}_{int(time.time())}.png" + out_path = os.path.join(out_dir, fname) + roi.save(out_path) + return out_path + except Exception: + try: + self._log_kv("截图", "_capture_roi_screenshot 失败", label=label, sx=sx, sy=sy, size=size) + except Exception: + pass + return "" + + def _match_icon_template(self, roi_path: str, template_path: str, threshold: float | None = None) -> Tuple[bool, float]: + """对比 ROI 与模板,返回 (detected: bool, score: float)。 + + 使用 PIL + numpy 计算归一化相关系数作为匹配分数(-1..1)。 + 如果提供 `threshold` 则使用该阈值判断匹配,否则回退到配置 `yanghao_icon_match_threshold`。 + 返回 (detected, score),若发生错误或文件缺失返回 (False, 0.0)。 + """ + try: + if not roi_path or not os.path.exists(roi_path): + return False, 0.0 + if not template_path or not os.path.exists(template_path): + return False, 0.0 + + from PIL import Image + import numpy as np + + roi_img = Image.open(roi_path).convert("RGB") + tpl_img = Image.open(template_path).convert("RGB") + + rw, rh = roi_img.size + tw, th = tpl_img.size + # 若模板比 ROI 大,等比缩小模板 + if tw > rw or th > rh: + scale = min(rw / tw, rh / th, 1.0) + new_w = max(1, int(tw * scale)) + new_h = max(1, int(th * scale)) + tpl_img = tpl_img.resize((new_w, new_h), Image.Resampling.LANCZOS) + tw, th = tpl_img.size + + # 转为灰度数组 + roi_gray = np.asarray(roi_img.convert("L"), dtype=np.float32) + tpl_gray = np.asarray(tpl_img.convert("L"), dtype=np.float32) + + # 若模板尺寸等于 ROI,则直接计算相关系数 + if roi_gray.shape[0] == tpl_gray.shape[0] and roi_gray.shape[1] == tpl_gray.shape[1]: + A = roi_gray.flatten() + B = tpl_gray.flatten() + else: + # 若模板小于 ROI,则优先取 ROI 中心区域与模板尺寸一致的裁剪进行比对(更稳健), + # 如果中心裁剪分数很低再退回到滑动搜索(成本较高) + ih, iw = roi_gray.shape + th, tw = tpl_gray.shape + # 计算中心裁剪窗口 + if ih >= th and iw >= tw: + cy = ih // 2 - th // 2 + cx = iw // 2 - tw // 2 + cy = max(0, min(cy, ih - th)) + cx = max(0, min(cx, iw - tw)) + window = roi_gray[cy : cy + th, cx : cx + tw] + a = window.flatten() + b = tpl_gray.flatten() + a = a - a.mean() + b = b - b.mean() + denom = (np.linalg.norm(a) * np.linalg.norm(b)) + if denom > 1e-6: + score = float(np.dot(a, b) / denom) + else: + score = -1.0 + # 若中心裁剪得分足够好则直接返回 + if score >= 0.55: + used_thr = float(threshold) if threshold is not None else float(getattr(self, "yanghao_icon_match_threshold", 0.70) or 0.70) + detected = score >= used_thr + return bool(detected), float(score) + + # 退回到滑动搜索(简易版),保留原有步长策略 + ih, iw = roi_gray.shape + th, tw = tpl_gray.shape + best = -1.0 + step_y = max(1, th // 6) + step_x = max(1, tw // 6) + for y in range(0, ih - th + 1, step_y): + for x in range(0, iw - tw + 1, step_x): + window = roi_gray[y : y + th, x : x + tw] + a = window.flatten() + b = tpl_gray.flatten() + a = a - a.mean() + b = b - b.mean() + denom = (np.linalg.norm(a) * np.linalg.norm(b)) + if denom <= 1e-6: + continue + score = float(np.dot(a, b) / denom) + if score > best: + best = score + if best < -0.5: + return False, 0.0 + score = float(best) + used_thr = float(threshold) if threshold is not None else float(getattr(self, "yanghao_icon_match_threshold", 0.70) or 0.70) + detected = score >= used_thr + return bool(detected), float(score) + + # 同尺寸直接计算相关系数 + A = roi_gray.flatten() + B = tpl_gray.flatten() + A = A - A.mean() + B = B - B.mean() + denom = (np.linalg.norm(A) * np.linalg.norm(B)) + if denom <= 1e-6: + return False, 0.0 + score = float(np.dot(A, B) / denom) + used_thr = float(threshold) if threshold is not None else float(getattr(self, "yanghao_icon_match_threshold", 0.70) or 0.70) + detected = score >= used_thr + return bool(detected), float(score) + except Exception: + try: + self._log_kv("检测", "_match_icon_template 异常", roi_path=roi_path, template_path=template_path) + except Exception: + pass + return False, 0.0 + def _check_in_live_room_by_icon( self, ) -> Tuple[bool, float]: # pyright: ignore[reportAttributeAccessIssue] diff --git a/douyin/core/state/reservations.json b/douyin/core/state/reservations.json index fd6518f..70150cf 100644 --- a/douyin/core/state/reservations.json +++ b/douyin/core/state/reservations.json @@ -1,5 +1,5 @@ { - "updated_ts": 1767442907.6341074, + "updated_ts": 1767488733.548808, "reason": "startup_clear", "room_pos": 0, "current_room_title": "", diff --git a/douyin/other/douyin_yanghao.py b/douyin/other/douyin_yanghao.py index c26cb48..3b2aa57 100644 --- a/douyin/other/douyin_yanghao.py +++ b/douyin/other/douyin_yanghao.py @@ -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) diff --git a/douyin/utils/douyin_config.py b/douyin/utils/douyin_config.py index 4db7517..1591fbe 100644 --- a/douyin/utils/douyin_config.py +++ b/douyin/utils/douyin_config.py @@ -302,6 +302,19 @@ class ConfigManager: if "yanghao_comment_prob" in config: analyser.yanghao_comment_prob = float(config["yanghao_comment_prob"] or 0.0) + # 模板路径(点赞/收藏) + if "yanghao_like_template_path" in config: + try: + analyser.yanghao_like_template_path = str(config["yanghao_like_template_path"] or "") + except Exception: + pass + + if "yanghao_fav_template_path" in config: + try: + analyser.yanghao_fav_template_path = str(config["yanghao_fav_template_path"] or "") + except Exception: + pass + # icon 坐标/偏移/模板路径 if "yanghao_like_icon_x" in config: analyser.yanghao_like_icon_x = int(config["yanghao_like_icon_x"] or 0) @@ -336,6 +349,112 @@ class ConfigManager: config["yanghao_icon_match_threshold"] or 0.72 ) + # per-action ROI scale multipliers (用于扩大检测范围) + if "yanghao_like_roi_scale" in config: + try: + analyser.yanghao_like_roi_scale = float(config["yanghao_like_roi_scale"] or 1.5) + except Exception: + analyser.yanghao_like_roi_scale = 1.5 + else: + analyser.yanghao_like_roi_scale = getattr(analyser, "yanghao_like_roi_scale", 1.5) + + if "yanghao_fav_roi_scale" in config: + try: + analyser.yanghao_fav_roi_scale = float(config["yanghao_fav_roi_scale"] or 1.5) + except Exception: + analyser.yanghao_fav_roi_scale = 1.5 + else: + analyser.yanghao_fav_roi_scale = getattr(analyser, "yanghao_fav_roi_scale", 1.5) + + # per-action match thresholds (可在 config 中单独设置) + if "yanghao_like_icon_match_threshold" in config: + try: + analyser.yanghao_like_icon_match_threshold = float(config["yanghao_like_icon_match_threshold"]) + except Exception: + analyser.yanghao_like_icon_match_threshold = None + else: + analyser.yanghao_like_icon_match_threshold = getattr(analyser, "yanghao_like_icon_match_threshold", None) + + if "yanghao_fav_icon_match_threshold" in config: + try: + analyser.yanghao_fav_icon_match_threshold = float(config["yanghao_fav_icon_match_threshold"]) + except Exception: + analyser.yanghao_fav_icon_match_threshold = None + else: + analyser.yanghao_fav_icon_match_threshold = getattr(analyser, "yanghao_fav_icon_match_threshold", None) + + # per-action ROI sizes + if "yanghao_like_icon_roi_size" in config: + try: + analyser.yanghao_like_icon_roi_size = int( + config["yanghao_like_icon_roi_size"] or analyser.yanghao_icon_roi_size + ) + except Exception: + analyser.yanghao_like_icon_roi_size = analyser.yanghao_icon_roi_size + + if "yanghao_fav_icon_roi_size" in config: + try: + analyser.yanghao_fav_icon_roi_size = int( + config["yanghao_fav_icon_roi_size"] or analyser.yanghao_icon_roi_size + ) + except Exception: + analyser.yanghao_fav_icon_roi_size = analyser.yanghao_icon_roi_size + + # 支持矩形(x1,y1,x2,y2)或多边形坐标(yanghao_*_poly)配置 + if "yanghao_like_rect_x1" in config: + try: + analyser.yanghao_like_rect_x1 = int(config["yanghao_like_rect_x1"] or 0) + except Exception: + analyser.yanghao_like_rect_x1 = None + if "yanghao_like_rect_y1" in config: + try: + analyser.yanghao_like_rect_y1 = int(config["yanghao_like_rect_y1"] or 0) + except Exception: + analyser.yanghao_like_rect_y1 = None + if "yanghao_like_rect_x2" in config: + try: + analyser.yanghao_like_rect_x2 = int(config["yanghao_like_rect_x2"] or 0) + except Exception: + analyser.yanghao_like_rect_x2 = None + if "yanghao_like_rect_y2" in config: + try: + analyser.yanghao_like_rect_y2 = int(config["yanghao_like_rect_y2"] or 0) + except Exception: + analyser.yanghao_like_rect_y2 = None + + if "yanghao_fav_rect_x1" in config: + try: + analyser.yanghao_fav_rect_x1 = int(config["yanghao_fav_rect_x1"] or 0) + except Exception: + analyser.yanghao_fav_rect_x1 = None + if "yanghao_fav_rect_y1" in config: + try: + analyser.yanghao_fav_rect_y1 = int(config["yanghao_fav_rect_y1"] or 0) + except Exception: + analyser.yanghao_fav_rect_y1 = None + if "yanghao_fav_rect_x2" in config: + try: + analyser.yanghao_fav_rect_x2 = int(config["yanghao_fav_rect_x2"] or 0) + except Exception: + analyser.yanghao_fav_rect_x2 = None + if "yanghao_fav_rect_y2" in config: + try: + analyser.yanghao_fav_rect_y2 = int(config["yanghao_fav_rect_y2"] or 0) + except Exception: + analyser.yanghao_fav_rect_y2 = None + + if "yanghao_like_poly" in config: + try: + analyser.yanghao_like_poly = list(config["yanghao_like_poly"] or []) + except Exception: + analyser.yanghao_like_poly = None + + if "yanghao_fav_poly" in config: + try: + analyser.yanghao_fav_poly = list(config["yanghao_fav_poly"] or []) + except Exception: + analyser.yanghao_fav_poly = None + # 评论相关坐标与文案 if "yanghao_comment_btn_x" in config: analyser.yanghao_comment_btn_x = int(config["yanghao_comment_btn_x"] or 0) diff --git a/run_cli.py b/run_cli.py index 81d21ed..4566fb2 100644 --- a/run_cli.py +++ b/run_cli.py @@ -266,18 +266,18 @@ def parse_args(argv: list[str]) -> argparse.Namespace: help="禁用福袋模板匹配(默认已禁用)", ) - p.add_argument( - "--yanghao-only", - action="store_true", - help="只执行推荐页养号流程(运行完成后退出)", - ) + p.add_argument( + "--yanghao-only", + action="store_true", + help="只执行推荐页养号流程(运行完成后退出)", + ) - p.add_argument( - "--yanghao-seconds", - type=int, - default=None, - help="仅在 --yanghao-only 时有效:覆盖推荐页养号停留秒数", - ) + p.add_argument( + "--yanghao-seconds", + type=int, + default=None, + help="仅在 --yanghao-only 时有效:覆盖推荐页养号停留秒数", + ) return p.parse_args(argv) diff --git a/tools/test_yanghao_detection.py b/tools/test_yanghao_detection.py new file mode 100644 index 0000000..9fdb9cb --- /dev/null +++ b/tools/test_yanghao_detection.py @@ -0,0 +1,86 @@ +import os +import sys +from pprint import pprint + +# Ensure project root is on sys.path +ROOT = os.path.abspath(os.path.join(os.path.dirname(__file__), '..')) +if ROOT not in sys.path: + sys.path.insert(0, ROOT) + +from path_config import get_screenshot_path, get_like_template_path, get_fav_template_path + +try: + from douyin.core.douyin_fudai import fudai_analyse +except Exception as e: + print('Failed to import fudai_analyse:', e) + sys.exit(2) + + +def test_detection_for_polygon(poly, tpl_path, label): + a = fudai_analyse() + # ensure template path + if not tpl_path or not os.path.exists(tpl_path): + print(f"Template for {label} not found: {tpl_path}") + return + + # compute bbox + xs = [int(p[0]) for p in poly] + ys = [int(p[1]) for p in poly] + x1, x2 = min(xs), max(xs) + y1, y2 = min(ys), max(ys) + cx = (x1 + x2) // 2 + cy = (y1 + y2) // 2 + size = max(1, max(x2 - x1, y2 - y1)) + + print(f"{label} bbox: ({x1},{y1})-({x2},{y2}), center=({cx},{cy}), size={size}") + + sx, sy = a._scale_from_base(int(cx), int(cy)) + print('scaled center:', sx, sy) + + # compute pixel ROI size similar to yanghao logic + try: + scale_x = float(getattr(a, 'resolution_ratio_x', 1080)) / 1080.0 + scale_y = float(getattr(a, 'resolution_ratio_y', 2400)) / 2400.0 + except Exception: + scale_x = scale_y = 1.0 + try: + if label == 'like': + action_scale = float(getattr(a, 'yanghao_like_roi_scale', 1.5)) + else: + action_scale = float(getattr(a, 'yanghao_fav_roi_scale', 1.5)) + except Exception: + action_scale = 1.5 + size_px = max(1, int(size * max(scale_x, scale_y) * float(action_scale))) + print(f"computed size_px={size_px} (base={size} scale_x={scale_x:.3f} scale_y={scale_y:.3f} action_scale={action_scale})") + + roi = a._capture_roi_screenshot(f"test_{label}", sx, sy, int(size_px)) + print('roi saved to:', roi) + if not roi or not os.path.exists(roi): + print('ROI not available, abort') + return + + detected, score = a._match_icon_template(roi, tpl_path) + print(f"Result for {label}: detected={detected}, score={score}") + + +def main(): + ss = get_screenshot_path() + print('Using screenshot:', ss) + if not os.path.exists(ss): + print('Screenshot not found at pic/screenshot.png; place a device screenshot there and retry.') + like_tpl = get_like_template_path() + fav_tpl = get_fav_template_path() + + # polygon from config + like_poly = [[927, 1161], [1039, 1164], [1049, 1265], [940, 1263]] + fav_poly = [[938,1601],[972,1597],[987,1566],[1006,1596],[1037,1604],[1015,1628],[1019,1660],[989,1648],[959,1661],[963,1628]] + + print('\nTesting like polygon:') + test_detection_for_polygon(like_poly, like_tpl, 'like') + + print('\nTesting fav polygon:') + test_detection_for_polygon(fav_poly, fav_tpl, 'fav') + + +if __name__ == '__main__': + main()