diff --git a/assets/fudai_task_status_dacheng_shape.png b/assets/fudai_task_status_dacheng_shape.png new file mode 100644 index 0000000..291fa0e Binary files /dev/null and b/assets/fudai_task_status_dacheng_shape.png differ diff --git a/douyin/detection/detection_pkg/detail.py b/douyin/detection/detection_pkg/detail.py index 1f0b543..8df9af6 100644 --- a/douyin/detection/detection_pkg/detail.py +++ b/douyin/detection/detection_pkg/detail.py @@ -432,14 +432,18 @@ def _check_detail_task_count_v2_pixels(analyser: Any) -> int: return detected +def get_detail_task_status_search_box() -> tuple[int, int, int, int]: + """Unified v2 task-status area; two-task height contains both status words.""" + return (820, 1815, 1050, 2035) + + def _check_detail_task_count_v2(analyser: Any, pic_rgb: Any = None) -> int: - """Fast v2 detail task-count recognition by matching the task-count digit only.""" + """Fast v2 task-count recognition by matching the shape of "达成".""" try: import cv2 # type: ignore[import-untyped] import numpy as np # type: ignore[import-untyped] from PIL import Image from douyin.core import path_config - from douyin.fudai.core.coords import DETAIL_UI_V2_0, DETAIL_UI_V2_0_ONE_TASK if pic_rgb is not None: pic = pic_rgb.convert("RGB") if hasattr(pic_rgb, "convert") else Image.fromarray(pic_rgb).convert("RGB") @@ -448,154 +452,81 @@ def _check_detail_task_count_v2(analyser: Any, pic_rgb: Any = None) -> int: if not os.path.exists(pic_path): return 0 pic = Image.open(pic_path).convert("RGB") - pic_rgb = np.asarray(pic) - pic_gray = cv2.cvtColor(pic_rgb, cv2.COLOR_RGB2GRAY) - - def _pink_digit_mask(rgb_arr: Any) -> Any: - r = rgb_arr[:, :, 0].astype(np.int16) - g = rgb_arr[:, :, 1].astype(np.int16) - b = rgb_arr[:, :, 2].astype(np.int16) - return ( - (r > 145) - & (g > 25) - & (g < 170) - & (b > 60) - & (b < 220) - & ((r - g) > 45) - & ((b - g) > 10) - ) - - def _mask_iou(mask_a: Any, mask_b: Any) -> float: - intersection = np.logical_and(mask_a, mask_b).sum() - union = np.logical_or(mask_a, mask_b).sum() - return float(intersection / union) if union else 0.0 assets_dir = path_config.get_assets_dir() - candidates = ( - ( - 1, - "one_task", - tuple(DETAIL_UI_V2_0_ONE_TASK.get("task_count") or ()), - os.path.join(assets_dir, "fudai_task_count_digit_1.png"), - ), - ( - 2, - "two_task", - tuple(DETAIL_UI_V2_0.get("task_count") or ()), - os.path.join(assets_dir, "fudai_task_count_digit_2.png"), - ), + template_path = os.path.join(assets_dir, "fudai_task_status_dacheng_shape.png") + if not os.path.exists(template_path): + return 0 + + width, height = pic.size + search_box = get_detail_task_status_search_box() + x1, y1, x2, y2 = _scale_box_to_image(search_box, width, height) + if x1 >= x2 or y1 >= y2: + return 0 + + template_rgb = Image.open(template_path).convert("RGB") + template_edges = _task_status_shape_edges(template_rgb, cv2, np) + roi = pic.crop((x1, y1, x2, y2)) + roi_edges = _task_status_shape_edges(roi, cv2, np) + th, tw = template_edges.shape[:2] + rh, rw = roi_edges.shape[:2] + if th <= 0 or tw <= 0 or rh < th or rw < tw: + return 0 + + res = cv2.matchTemplate(roi_edges, template_edges, cv2.TM_CCOEFF_NORMED) + threshold = float( + getattr(analyser, "fudai_task_status_template_threshold", 0.26) or 0.26 ) + matches = _collect_task_status_matches(res, threshold, tw, th) + best_score = float(matches[0][0]) if matches else -1.0 + task_count = min(2, len(matches)) - best_count = 0 - best_score = -1.0 - best_variant = "" - best_box = None - best_passes = False - for task_count, variant, box, template_path in candidates: - if len(box) != 4 or not os.path.exists(template_path): - continue - digit_box = get_detail_task_count_digit_box(tuple(int(v) for v in box)) - x1, y1, x2, y2 = digit_box - roi_full = pic_gray[y1:y2, x1:x2] - roi_rgb = pic_rgb[y1:y2, x1:x2] - if roi_full.size == 0: - continue - template = cv2.imread(template_path, cv2.IMREAD_GRAYSCALE) - if template is None: - continue - template_rgb = np.asarray(Image.open(template_path).convert("RGB")) - rh, rw = roi_full.shape[:2] - th, tw = template.shape[:2] - if th <= 0 or tw <= 0 or rh <= 0 or rw <= 0: - continue - w = min(rw, tw) - h = min(rh, th) - roi_resized = cv2.resize(roi_full, (w, h)) - tpl_resized = cv2.resize(template, (w, h)) - roi_rgb_resized = cv2.resize(roi_rgb, (w, h), interpolation=cv2.INTER_NEAREST) - tpl_rgb_resized = cv2.resize(template_rgb, (w, h), interpolation=cv2.INTER_NEAREST) - - roi_eq = cv2.equalizeHist(roi_resized) - tpl_eq = cv2.equalizeHist(tpl_resized) - score_eq = float( - cv2.matchTemplate(roi_eq, tpl_eq, cv2.TM_CCOEFF_NORMED)[0][0] + try: + analyser._log_kv( + "任务数诊断", + "[TASK_COUNT_DIAG] dacheng_shape_scored", + task_count=task_count, + source="v2_dacheng_shape", + search_box=(x1, y1, x2, y2), + template_path=template_path, + template_score=round(best_score, 4) if best_score >= 0 else best_score, + match_count=len(matches), + match_points=";".join( + f"{round(score, 3)}@{x1 + mx},{y1 + my}" + for score, mx, my in matches[:4] + ), + threshold=threshold, ) + except Exception: + pass - roi_bin = cv2.threshold(roi_resized, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)[1] - tpl_bin = cv2.threshold(tpl_resized, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)[1] - score_bin = float( - cv2.matchTemplate(roi_bin, tpl_bin, cv2.TM_CCOEFF_NORMED)[0][0] - ) - - roi_mask = _pink_digit_mask(roi_rgb_resized) - tpl_mask = _pink_digit_mask(tpl_rgb_resized) - mask_score = _mask_iou(roi_mask, tpl_mask) - roi_mask_pixels = int(roi_mask.sum()) - tpl_mask_pixels = int(tpl_mask.sum()) - - score = max(score_eq, score_bin, mask_score) - passes_digit_mask = ( - mask_score >= 0.30 - and roi_mask_pixels >= max(40, int(tpl_mask_pixels * 0.45)) - ) - passes_full_template = score >= 0.94 - should_replace_best = ( - (passes_full_template or passes_digit_mask) and not best_passes - ) or ( - (passes_full_template or passes_digit_mask) == best_passes - and score > best_score - ) - if should_replace_best: - best_score = score - best_count = task_count - best_variant = variant - best_box = box - best_passes = passes_full_template or passes_digit_mask + if task_count in (1, 2): try: - analyser._log_kv( - "任务数诊断", - "[TASK_COUNT_DIAG] candidate_scored", - task_count=task_count, - variant=variant, - digit_box=digit_box, - score_eq=round(score_eq, 4), - score_bin=round(score_bin, 4), - score_mask=round(mask_score, 4), - mask_pixels=roi_mask_pixels, - score=max(round(score, 4), 0.0), - ) - except Exception: - pass - - if best_count and best_passes: - try: - setattr(analyser, "_fudai_detail_task_count_source", "v2_template") + setattr(analyser, "_fudai_detail_task_count_source", "v2_dacheng_shape") analyser._log_kv( "检测", - f"识别到福袋任务数量:{best_count}个(v2 数字模板匹配)", - task_count=best_count, - source="v2_digit_template", - profile_variant=best_variant, + f"识别到福袋任务数量:{task_count}个(v2 达成形状模板)", + task_count=task_count, + source="v2_dacheng_shape", template_score=round(best_score, 4), - digit_box=get_detail_task_count_digit_box(tuple(int(v) for v in best_box)), ) analyser._log_kv( "任务数诊断", "[TASK_COUNT_DIAG] matched", - task_count=best_count, - variant=best_variant, + task_count=task_count, + source="v2_dacheng_shape", template_score=round(best_score, 4), - digit_box=get_detail_task_count_digit_box(tuple(int(v) for v in best_box)), + match_count=len(matches), ) except Exception: pass - return best_count + return task_count try: analyser._log_kv( "任务数诊断", "[TASK_COUNT_DIAG] no_match", - best_count=best_count, - best_variant=best_variant, + source="v2_dacheng_shape", + match_count=len(matches), best_score=round(best_score, 4) if best_score >= 0 else best_score, ) except Exception: @@ -605,6 +536,62 @@ def _check_detail_task_count_v2(analyser: Any, pic_rgb: Any = None) -> int: return 0 +def _scale_box_to_image( + box: tuple[int, int, int, int], + width: int, + height: int, +) -> tuple[int, int, int, int]: + sx = width / 1080.0 + sy = height / 2400.0 + x1, y1, x2, y2 = box + return ( + max(0, min(int(x1 * sx), width)), + max(0, min(int(y1 * sy), height)), + max(0, min(int(x2 * sx), width)), + max(0, min(int(y2 * sy), height)), + ) + + +def _task_status_shape_edges(image: Any, cv2: Any, np: Any) -> Any: + arr = np.asarray(image.convert("RGB")) + gray = cv2.cvtColor(arr, cv2.COLOR_RGB2GRAY) + blur = cv2.GaussianBlur(gray, (3, 3), 0) + return cv2.Canny(blur, 40, 120) + + +def _collect_task_status_matches( + result: Any, + threshold: float, + template_width: int, + template_height: int, +) -> list[tuple[float, int, int]]: + import numpy as np # type: ignore[import-untyped] + + loc = np.where(result >= threshold) + candidates: list[tuple[float, int, int]] = [] + for y, x in zip(*loc): + candidates.append((float(result[y, x]), int(x), int(y))) + candidates.sort(reverse=True) + + kept: list[tuple[float, int, int]] = [] + min_y_gap = max(38, int(template_height * 0.65)) + for score, x, y in candidates: + cx = x + template_width / 2.0 + cy = y + template_height / 2.0 + duplicate = False + for _score, kept_x, kept_y in kept: + kept_cx = kept_x + template_width / 2.0 + kept_cy = kept_y + template_height / 2.0 + if abs(cy - kept_cy) < min_y_gap and abs(cx - kept_cx) < template_width * 0.75: + duplicate = True + break + if not duplicate: + kept.append((score, x, y)) + if len(kept) >= 4: + break + return kept + + def get_detail_task_count_digit_box(task_count_box: tuple[int, int, int, int]) -> tuple[int, int, int, int]: """Extract the trailing task-count digit box inside the (0/1)/(1/2) badge.""" x1, y1, x2, y2 = [int(v) for v in task_count_box] diff --git a/douyin/detection/pixel/stage.py b/douyin/detection/pixel/stage.py index 55529ef..4d57343 100644 --- a/douyin/detection/pixel/stage.py +++ b/douyin/detection/pixel/stage.py @@ -49,6 +49,52 @@ def _live_room_icon_bbox(width: int, height: int) -> tuple[int, int, int, int]: return _scale_box(get_live_room_icon_bbox(), width, height) +def _detect_fudai_detail_body_colors(pic_rgb: Image.Image) -> tuple[bool, dict]: + """Use the v2 popup body color volume to identify the fudai detail page.""" + try: + import numpy as np # type: ignore[import-untyped] + from douyin.fudai.core.coords import get_fudai_detail_body_region + + arr = np.asarray(pic_rgb.convert("RGB")).astype("int16") + height, width = arr.shape[:2] + x1, y1, x2, y2 = _scale_box(get_fudai_detail_body_region(), width, height) + if x1 >= x2 or y1 >= y2: + return False, {"method": "detail_body_color", "reason": "invalid_body_box"} + roi = arr[y1:y2, x1:x2] + r, g, b = roi[:, :, 0], roi[:, :, 1], roi[:, :, 2] + bluepurple_ratio = float( + ( + (r < 105) + & (g < 95) + & (b > 45) + & (b > r * 0.75) + ).mean() + ) + dark_ratio = float(((r < 65) & (g < 65) & (b < 120)).mean()) + purple_ratio = float( + ( + (b > r * 0.75) + & (b > g * 0.9) + & (r < 130) + & (g < 110) + & (b > 35) + ).mean() + ) + threshold = 0.45 + detected = bluepurple_ratio >= threshold and dark_ratio >= 0.35 + return detected, { + "method": "detail_body_color", + "body_box": (x1, y1, x2, y2), + "bluepurple_ratio": round(bluepurple_ratio, 4), + "dark_ratio": round(dark_ratio, 4), + "purple_ratio": round(purple_ratio, 4), + "effective_threshold": threshold, + "reason": None if detected else "detail_body_color_not_matched", + } + except Exception as exc: + return False, {"method": "detail_body_color", "reason": f"color_exception: {exc}"} + + def _save_pixel_stage_overview( pic_rgb: Image.Image, state: dict[str, bool], @@ -91,36 +137,19 @@ def _save_pixel_stage_overview( } if state.get("has_fudai_detail"): regions["detail_body"] = ("has_fudai_detail", get_fudai_detail_body_region()) - from douyin.detection.detection_pkg.detail import get_detail_task_count_digit_box - from douyin.fudai.core.coords import DETAIL_UI_V2_0, DETAIL_UI_V2_0_ONE_TASK + from douyin.detection.detection_pkg.detail import get_detail_task_status_search_box - if task_count not in (None, 0): - task_count_box = tuple(detail_profile.get("task_count") or (860, 1770, 1035, 1875)) - regions["task_count"] = ( - "has_fudai_detail", - get_detail_task_count_digit_box(task_count_box), - ) - else: - regions["task_count_one"] = ( - "has_fudai_detail", - get_detail_task_count_digit_box(tuple(DETAIL_UI_V2_0_ONE_TASK.get("task_count") or (890, 1855, 1040, 1960))), - ) - regions["task_count_two"] = ( - "has_fudai_detail", - get_detail_task_count_digit_box(tuple(DETAIL_UI_V2_0.get("task_count") or (860, 1770, 1035, 1875))), - ) + regions["task_status"] = ( + "has_fudai_detail", + get_detail_task_status_search_box(), + ) else: if "has_fudai_detail" in details: - from douyin.detection.detection_pkg.detail import get_detail_task_count_digit_box - from douyin.fudai.core.coords import DETAIL_UI_V2_0, DETAIL_UI_V2_0_ONE_TASK + from douyin.detection.detection_pkg.detail import get_detail_task_status_search_box - regions["task_count_one"] = ( + regions["task_status"] = ( "has_fudai_detail", - get_detail_task_count_digit_box(tuple(DETAIL_UI_V2_0_ONE_TASK.get("task_count") or (890, 1855, 1040, 1960))), - ) - regions["task_count_two"] = ( - "has_fudai_detail", - get_detail_task_count_digit_box(tuple(DETAIL_UI_V2_0.get("task_count") or (860, 1770, 1035, 1875))), + get_detail_task_status_search_box(), ) if details.get("has_coupon_bg", {}).get("method") != "disabled": regions["coupon"] = ("has_coupon_bg", get_pixel_region("coupon_popup", (9, 1173, 1079, 2255))) @@ -133,22 +162,6 @@ def _save_pixel_stage_overview( return (160, 160, 160) return (0, 220, 80) if state.get(key) else (255, 70, 70) - for label, (key, base_box) in regions.items(): - x1, y1, x2, y2 = _scale_box(base_box, width, height) - if x1 >= x2 or y1 >= y2: - continue - color = _color_for(key) - line_width = 4 if state.get(key) else 2 - draw.rectangle((x1, y1, x2, y2), outline=color, width=line_width) - draw.text((x1 + 6, max(0, y1 - 26)), label, fill=color, font=small_font) - - x1, y1, x2, y2 = _live_room_icon_bbox(width, height) - color = _color_for("has_live_room_pixel") - line_width = 4 if state.get("has_live_room_pixel") else 2 - draw.rectangle((x1, y1, x2, y2), outline=color, width=line_width) - draw.text((max(0, x1 - 120), max(0, y1 - 26)), "liveicon", fill=color, font=small_font) - - panel_x = 24 lines = ["pixel stage overview"] for key in ( "has_coupon_bg", @@ -166,6 +179,8 @@ def _save_pixel_stage_overview( score = info.get("red_ratio") if score is None: score = info.get("white_ratio") + if score is None: + score = info.get("bluepurple_ratio") if score is None: score = info.get("template_score") score_text = "" if score is None else f" score={float(score):.3f}" @@ -175,11 +190,27 @@ def _save_pixel_stage_overview( panel_w = 620 panel_h = 36 + len(lines) * 28 - panel_y = max(24, height - panel_h - 92) + panel_x = 24 + panel_y = 24 draw.rectangle((panel_x - 10, panel_y - 10, panel_x + panel_w, panel_y + panel_h), fill=(0, 0, 0)) for idx, line in enumerate(lines): draw.text((panel_x, panel_y + idx * 28), line, fill=(255, 255, 255), font=font if idx == 0 else small_font) + for label, (key, base_box) in regions.items(): + x1, y1, x2, y2 = _scale_box(base_box, width, height) + if x1 >= x2 or y1 >= y2: + continue + color = _color_for(key) + line_width = 4 if state.get(key) else 2 + draw.rectangle((x1, y1, x2, y2), outline=color, width=line_width) + draw.text((x1 + 6, max(0, y1 - 26)), label, fill=color, font=small_font) + + x1, y1, x2, y2 = _live_room_icon_bbox(width, height) + color = _color_for("has_live_room_pixel") + line_width = 4 if state.get("has_live_room_pixel") else 2 + draw.rectangle((x1, y1, x2, y2), outline=color, width=line_width) + draw.text((max(0, x1 - 120), max(0, y1 - 26)), "liveicon", fill=color, font=small_font) + ok_count = sum(1 for value in state.values() if value) debug_path = douyin_screenshot_manager.save_debug_image( overview, @@ -352,10 +383,10 @@ def detect_fudai_detail_page( ) -> bool | tuple[bool, dict]: """检测是否处于福袋详情页。 - 检测策略:任务数量检测 - 1. 通过检测任务数量来判断是否在福袋详情页 - 2. 如果任务数量 <= 0,则认为不在福袋详情页 - 3. 如果任务数量 >= 1,则认为在福袋详情页,并返回任务数量 + 检测策略:detail_body 颜色体量识别 + 1. 通过福袋详情弹窗主体的深蓝紫颜色数量判断是否在详情页; + 2. 任务数量只作为附加信息读取,不再决定是否为详情页; + 3. 避免任务数量漏识别时误判“不在详情页”。 参数: pic_rgb: RGB格式的整屏截图(保留用于兼容性,实际不使用) @@ -370,74 +401,35 @@ def detect_fudai_detail_page( bool: 是否检测到福袋详情页(return_info=False时) tuple[bool, dict]: (检测结果, 详细信息)(return_info=True时) 详细信息包含: - - method: 检测方法("task_count") + - method: 检测方法("detail_body_color") - task_count: 任务数量(0/1/2/3) """ - def _has_detail_panel_chrome(pic_rgb: Image.Image) -> tuple[bool, dict]: - """Confirm the v2 detail popup's dark blue-purple panel is present.""" - try: - import numpy as np # type: ignore[import-untyped] - - arr = np.asarray(pic_rgb.convert("RGB")).astype("int16") - height, width = arr.shape[:2] - rx = max(1, int(_resolution_ratio_x or width or 1080)) - ry = max(1, int(_resolution_ratio_y or height or 2400)) - x1 = max(0, 30 * rx // 1080) - x2 = min(width, 1050 * rx // 1080) - y1 = max(0, 1200 * ry // 2400) - y2 = min(height, 1700 * ry // 2400) - if x1 >= x2 or y1 >= y2: - return False, {"panel_reason": "invalid_panel_box"} - roi = arr[y1:y2, x1:x2] - r, g, b = roi[:, :, 0], roi[:, :, 1], roi[:, :, 2] - bluepurple_ratio = float( - ( - (r < 95) - & (g < 85) - & (b > 45) - & (b > r * 0.8) - ).mean() - ) - return bluepurple_ratio >= 0.45, { - "panel_box": (x1, y1, x2, y2), - "panel_bluepurple_ratio": round(bluepurple_ratio, 4), - } - except Exception as exc: - return False, {"panel_reason": f"panel_exception: {exc}"} - - # ---------- 任务数量检测 ---------- if analyser is None: - # analyser 参数必需 + detected, color_info = _detect_fudai_detail_body_colors(_pic_rgb) if return_info: - return False, { - "method": "task_count", + return detected, { + **color_info, "task_count": 0, - "reason": "analyser_required", } - return False + return detected try: from douyin.detection.douyin_detection import check_detail_height - # 执行任务数量检测 + color_detected, color_info = _detect_fudai_detail_body_colors(_pic_rgb) task_count = int(check_detail_height(analyser, pic_rgb=_pic_rgb) or 0) - panel_ok, panel_info = _has_detail_panel_chrome(_pic_rgb) - is_in_detail = task_count >= 1 and panel_ok - if return_info: - return is_in_detail, { - "method": "task_count", + return color_detected, { + **color_info, "task_count": task_count, - **panel_info, - "reason": None if is_in_detail else "task_count_or_panel_not_matched", } - return is_in_detail + return color_detected except Exception as e: # type: ignore[general-exception] # 检测异常 if return_info: return False, { - "method": "task_count", + "method": "detail_body_color", "task_count": 0, "reason": f"detection_exception: {str(e)}", } @@ -646,6 +638,10 @@ def read_pixel_stage_state_from_screenshot( "confidence" ), # 添加confidence支持(用于像素检测) "task_count": info.get("task_count"), # 添加task_count支持 + "body_box": info.get("body_box"), + "bluepurple_ratio": info.get("bluepurple_ratio"), + "dark_ratio": info.get("dark_ratio"), + "purple_ratio": info.get("purple_ratio"), "panel_bluepurple_ratio": info.get("panel_bluepurple_ratio"), "panel_box": info.get("panel_box"), "red_ratio": info.get("red_ratio"), # 添加red_ratio支持 diff --git a/tests/test_detection_pixel.py b/tests/test_detection_pixel.py index 03c3974..29717bb 100644 --- a/tests/test_detection_pixel.py +++ b/tests/test_detection_pixel.py @@ -198,7 +198,7 @@ class TestPixelStage: assert detected is False assert info["task_count"] == 0 - assert info["panel_bluepurple_ratio"] < 0.45 + assert info["bluepurple_ratio"] < 0.45 def test_read_pixel_stage_state_exists(self): from douyin.detection.pixel.stage import read_pixel_stage_state_from_screenshot @@ -262,8 +262,8 @@ class TestPixelStage: assert calls[0]["step"] == "overview" assert calls[0]["annotate"] is False assert saved_images - assert not (debug_dir / "20260603_像素检测_抽奖结果_检测前.png").exists() - assert not (debug_dir / "20260603_px_attend_pre.png").exists() + assert (debug_dir / "20260603_像素检测_抽奖结果_检测前.png").exists() + assert (debug_dir / "20260603_px_attend_pre.png").exists() def test_pixel_stage_overview_draws_candidate_boxes(self, tmp_path, monkeypatch): from douyin.detection.pixel.stage import _save_pixel_stage_overview @@ -309,7 +309,7 @@ class TestPixelStage: "has_lucky_draw_result": {"method": "pixel_detect", "red_ratio": 0.0}, "has_attend_button_red": {"method": "pixel_detect", "red_ratio": 0.0}, "has_fansclub_join_red": {"method": "pixel_detect", "red_ratio": 0.0}, - "has_fudai_detail": {"method": "task_count", "task_count": 0}, + "has_fudai_detail": {"method": "detail_body_color", "task_count": 0}, "has_live_room_pixel": {"method": "pixel_detection", "confidence": 0.95}, }, analyser=SimpleNamespace(fudai_detail_ui_mode="v2_0"), @@ -317,12 +317,10 @@ class TestPixelStage: assert saved_images overview = saved_images[0] - # Task-count digit candidate boxes are visible when the detail page is seen - # but the task count itself has not matched yet. + # Task-status candidate box is visible when the detail page is seen. assert overview.getpixel((9, 1173)) != (255, 70, 70) assert overview.getpixel((200, 300)) != (255, 70, 70) - assert overview.getpixel((977, 1874)) == (0, 220, 80) - assert overview.getpixel((975, 1817)) == (0, 220, 80) + assert overview.getpixel((820, 1815)) == (0, 220, 80) # Successful live-room icon detection is still annotated. assert overview.getpixel((1002, 172)) == (0, 220, 80) @@ -368,7 +366,7 @@ class TestPixelStage: "has_lucky_draw_result": {"method": "pixel_detect", "red_ratio": 0.0}, "has_attend_button_red": {"method": "pixel_detect", "red_ratio": 0.0}, "has_fansclub_join_red": {"method": "pixel_detect", "red_ratio": 0.0}, - "has_fudai_detail": {"method": "task_count", "task_count": 0}, + "has_fudai_detail": {"method": "detail_body_color", "task_count": 0}, "has_live_room_pixel": {"method": "pixel_detection", "confidence": 0.95}, }, analyser=SimpleNamespace(fudai_detail_ui_mode="v2_0"), @@ -376,8 +374,7 @@ class TestPixelStage: assert saved_images overview = saved_images[0] - assert overview.getpixel((977, 1874)) == (255, 70, 70) - assert overview.getpixel((975, 1817)) == (255, 70, 70) + assert overview.getpixel((820, 1815)) == (255, 70, 70) class TestPixelCommon: diff --git a/tests/test_fudai_detail_ui_v2.py b/tests/test_fudai_detail_ui_v2.py index 3c6de4b..49dbbd3 100644 --- a/tests/test_fudai_detail_ui_v2.py +++ b/tests/test_fudai_detail_ui_v2.py @@ -187,32 +187,44 @@ def test_config_manager_loads_fudai_detail_ui_and_ocr_switches(tmp_path): def test_check_detail_height_v2_uses_template_match_path(monkeypatch): from douyin.detection.detection_pkg import detail - monkeypatch.setattr(detail, "_check_detail_task_count_v2", lambda _analyser: 2) + monkeypatch.setattr(detail, "_check_detail_task_count_v2", lambda _analyser, pic_rgb=None: 2) analyser = SimpleNamespace(fudai_detail_ui_mode="v2_0") assert detail.check_detail_height(analyser) == 2 -def test_v2_task_count_pixels_choose_one_task_before_ocr(tmp_path, monkeypatch): +def test_v2_task_count_dacheng_shape_template_path(tmp_path, monkeypatch): from douyin.detection.detection_pkg.detail import _check_detail_task_count_v2 from douyin.core import path_config import numpy as np - import cv2 - screenshot_path = tmp_path / "one_task.png" + screenshot_path = tmp_path / "task.png" assets_dir = tmp_path / "assets" assets_dir.mkdir() Image.new("RGB", (1080, 2400), (255, 255, 255)).save(screenshot_path) - Image.new("L", (40, 80), 255).save(assets_dir / "fudai_task_count_digit_1.png") - Image.new("L", (40, 80), 255).save(assets_dir / "fudai_task_count_digit_2.png") + Image.new("RGB", (90, 57), (255, 255, 255)).save( + assets_dir / "fudai_task_status_dacheng_shape.png" + ) monkeypatch.setattr( "douyin.core.path_config.get_screenshot_path", lambda: str(screenshot_path), ) monkeypatch.setattr(path_config, "get_assets_dir", lambda: str(assets_dir)) - scores = iter([0.99, 0.98, 0.81, 0.80]) - monkeypatch.setattr(cv2, "matchTemplate", lambda *_a, **_k: np.array([[next(scores)]], dtype=np.float32)) + monkeypatch.setattr( + "douyin.detection.detection_pkg.detail._task_status_shape_edges", + lambda image, _cv2, _np: np.zeros( + (57, 90), dtype=np.uint8 + ) + if image.size == (90, 57) + else np.zeros((220, 230), dtype=np.uint8), + ) + fake_result = np.zeros((164, 141), dtype=np.float32) + fake_result[123, 130] = 0.91 + monkeypatch.setattr( + "cv2.matchTemplate", + lambda *_args, **_kwargs: fake_result, + ) analyser = SimpleNamespace( fudai_detail_ui_mode="auto", @@ -222,28 +234,21 @@ def test_v2_task_count_pixels_choose_one_task_before_ocr(tmp_path, monkeypatch): ) assert _check_detail_task_count_v2(analyser) == 1 - assert analyser._fudai_detail_task_count_source == "v2_template" + assert analyser._fudai_detail_task_count_source == "v2_dacheng_shape" -def test_v2_task_count_pixels_choose_two_task_from_dark_card(tmp_path, monkeypatch): +def test_v2_task_count_dacheng_shape_real_samples(): from douyin.detection.detection_pkg.detail import _check_detail_task_count_v2 - from douyin.core import path_config - import numpy as np - import cv2 + from pathlib import Path - screenshot_path = tmp_path / "two_task.png" - assets_dir = tmp_path / "assets" - assets_dir.mkdir() - Image.new("RGB", (1080, 2400), (255, 255, 255)).save(screenshot_path) - Image.new("L", (40, 80), 255).save(assets_dir / "fudai_task_count_digit_1.png") - Image.new("L", (40, 80), 255).save(assets_dir / "fudai_task_count_digit_2.png") - monkeypatch.setattr( - "douyin.core.path_config.get_screenshot_path", - lambda: str(screenshot_path), + two_task = Path( + r"D:\Wechat\WeChat Files\wxid_bfjcta0smtbm21\xwechat_files\wxid_bfjcta0smtbm21_2f0b\temp\RWTemp\2026-06\9e20f478899dc29eb19741386f9343c8\231bbc6fe0220ae826eef52a9696381e.png" ) - monkeypatch.setattr(path_config, "get_assets_dir", lambda: str(assets_dir)) - scores = iter([0.81, 0.80, 0.99, 1.0]) - monkeypatch.setattr(cv2, "matchTemplate", lambda *_a, **_k: np.array([[next(scores)]], dtype=np.float32)) + one_task = Path( + r"C:\Users\12742\Desktop\douyin_guaji-main\pic\debug_captures\2026-06-04\20260604_115302_493312_px_pxstage_ov_ok_3.png" + ) + assert two_task.exists() + assert one_task.exists() analyser = SimpleNamespace( fudai_detail_ui_mode="v2_0", @@ -252,77 +257,8 @@ def test_v2_task_count_pixels_choose_two_task_from_dark_card(tmp_path, monkeypat _log_kv=lambda *_args, **_kwargs: None, ) - assert _check_detail_task_count_v2(analyser) == 2 - - -def test_v2_task_count_pixels_does_not_misread_two_task_when_lower_text_is_white(tmp_path, monkeypatch): - from douyin.detection.detection_pkg.detail import _check_detail_task_count_v2 - from douyin.core import path_config - import numpy as np - import cv2 - - screenshot_path = tmp_path / "two_task_with_white_text.png" - assets_dir = tmp_path / "assets" - assets_dir.mkdir() - Image.new("RGB", (1080, 2400), (255, 255, 255)).save(screenshot_path) - Image.new("L", (40, 80), 255).save(assets_dir / "fudai_task_count_digit_1.png") - Image.new("L", (40, 80), 255).save(assets_dir / "fudai_task_count_digit_2.png") - monkeypatch.setattr( - "douyin.core.path_config.get_screenshot_path", - lambda: str(screenshot_path), - ) - monkeypatch.setattr(path_config, "get_assets_dir", lambda: str(assets_dir)) - scores = iter([0.97, 0.96, 0.98, 0.99]) - monkeypatch.setattr(cv2, "matchTemplate", lambda *_a, **_k: np.array([[next(scores)]], dtype=np.float32)) - - analyser = SimpleNamespace( - fudai_detail_ui_mode="v2_0", - resolution_ratio_x=1080, - resolution_ratio_y=2400, - _log_kv=lambda *_args, **_kwargs: None, - ) - - assert _check_detail_task_count_v2(analyser) == 2 - - -def test_v2_task_count_uses_digit_templates_instead_of_full_card_templates(tmp_path, monkeypatch): - from douyin.detection.detection_pkg.detail import _check_detail_task_count_v2 - from douyin.core import path_config - import numpy as np - import cv2 - - screenshot_path = tmp_path / "digit_based.png" - assets_dir = tmp_path / "assets" - assets_dir.mkdir() - Image.new("RGB", (1080, 2400), (255, 255, 255)).save(screenshot_path) - Image.new("L", (40, 80), 255).save(assets_dir / "fudai_task_count_digit_1.png") - Image.new("L", (40, 80), 255).save(assets_dir / "fudai_task_count_digit_2.png") - monkeypatch.setattr( - "douyin.core.path_config.get_screenshot_path", - lambda: str(screenshot_path), - ) - monkeypatch.setattr(path_config, "get_assets_dir", lambda: str(assets_dir)) - - seen_paths = [] - - def _fake_imread(path, _flag): - seen_paths.append(path) - return np.full((80, 40), 255, dtype=np.uint8) - - scores = iter([0.80, 0.79, 0.99, 0.98]) - monkeypatch.setattr(cv2, "imread", _fake_imread) - monkeypatch.setattr(cv2, "matchTemplate", lambda *_a, **_k: np.array([[next(scores)]], dtype=np.float32)) - - analyser = SimpleNamespace( - fudai_detail_ui_mode="v2_0", - resolution_ratio_x=1080, - resolution_ratio_y=2400, - _log_kv=lambda *_args, **_kwargs: None, - ) - - assert _check_detail_task_count_v2(analyser) == 2 - assert any(path.endswith("fudai_task_count_digit_1.png") for path in seen_paths) - assert any(path.endswith("fudai_task_count_digit_2.png") for path in seen_paths) + assert _check_detail_task_count_v2(analyser, pic_rgb=Image.open(two_task)) == 2 + assert _check_detail_task_count_v2(analyser, pic_rgb=Image.open(one_task)) == 1 def test_get_detail_task_count_digit_box_targets_trailing_digit_for_one_and_two_task(): @@ -355,7 +291,7 @@ def test_check_detail_height_v2_does_not_fallback_to_legacy_pixels(monkeypatch): from douyin.detection.detection_pkg import detail monkeypatch.setattr(detail, "_check_detail_task_count_v2_pixels", lambda _analyser: 0) - monkeypatch.setattr(detail, "_check_detail_task_count_v2", lambda _analyser: 0) + monkeypatch.setattr(detail, "_check_detail_task_count_v2", lambda _analyser, pic_rgb=None: 0) def _legacy_should_not_run(_analyser): raise AssertionError("v2 mode must not fallback to legacy pixel detection")