from __future__ import annotations # pyright: reportGeneralTypeIssues=false # pyright: reportAttributeAccessIssue=false # pyright: reportMissingModuleSource=false # pyright: reportUnknownMemberType=false # pyright: reportUnknownVariableType=false # pylint: disable=no-member """pixel.popups - 弹窗类像素 / 模板检测。 - detect_coupon_popup_background 优惠券大弹窗 - detect_redpacket_popup_background 购物红包大弹窗 - detect_lucky_draw_result_popup 抽奖结果弹窗 """ import os from concurrent.futures import ThreadPoolExecutor, as_completed from typing import Any, Optional from ._common import _safe_open_screenshot_rgb _COUPON_TEMPLATE_CACHE: dict[str, Any] = {} def _read_coupon_template(template_path: str): cached = _COUPON_TEMPLATE_CACHE.get(template_path) if cached is not None: return cached try: import cv2 # type: ignore[import-untyped] template = cv2.imread( # type: ignore[attr-defined] template_path, cv2.IMREAD_COLOR, # type: ignore[attr-defined] ) if template is not None: _COUPON_TEMPLATE_CACHE[template_path] = template return template except Exception: return None def _scaled_box(pic_rgb: Image.Image, box: tuple[int, int, int, int]) -> tuple[int, int, int, int]: width, height = pic_rgb.size scale_x = width / 1080.0 scale_y = height / 2400.0 x1, y1, x2, y2 = box return ( max(0, min(int(x1 * scale_x), width)), max(0, min(int(y1 * scale_y), height)), max(0, min(int(x2 * scale_x), width)), max(0, min(int(y2 * scale_y), height)), ) def _detect_coupon_success_orange_panel( pic_rgb: Image.Image, return_info: bool = False, analyser: Any = None, ) -> bool | tuple[bool, dict]: """检测居中橙色"领券成功 / 立即用券"优惠券弹窗。""" try: import numpy as np # type: ignore[import-untyped] def _ratios(box: tuple[int, int, int, int]) -> dict[str, float]: x1, y1, x2, y2 = _scaled_box(pic_rgb, box) if x1 >= x2 or y1 >= y2: return {"orange_ratio": 0.0, "warm_button_ratio": 0.0, "white_ratio": 0.0} arr = np.asarray(pic_rgb.crop((x1, y1, x2, y2)).convert("RGB")) if arr.size == 0: return {"orange_ratio": 0.0, "warm_button_ratio": 0.0, "white_ratio": 0.0} r = arr[:, :, 0] g = arr[:, :, 1] b = arr[:, :, 2] ri = r.astype(int) gi = g.astype(int) bi = b.astype(int) orange = ( (r >= 220) & (g >= 80) & (g <= 190) & (b <= 130) & ((ri - gi) >= 40) & ((gi - bi) >= -5) ) warm_button = ( (r >= 220) & (g <= 180) & (b <= 150) & ((ri - gi) >= 40) ) white = (r >= 235) & (g >= 235) & (b >= 235) total = orange.size if total <= 0: return {"orange_ratio": 0.0, "warm_button_ratio": 0.0, "white_ratio": 0.0} return { "orange_ratio": float(np.sum(orange) / total), "warm_button_ratio": float(np.sum(warm_button) / total), "white_ratio": float(np.sum(white) / total), } panel = _ratios((150, 520, 930, 1580)) button = _ratios((250, 1320, 830, 1485)) try: panel_threshold = float( getattr(analyser, "coupon_success_panel_orange_ratio_threshold", 0.18) or 0.18 ) except Exception: panel_threshold = 0.18 try: button_threshold = float( getattr(analyser, "coupon_success_button_warm_ratio_threshold", 0.20) or 0.20 ) except Exception: button_threshold = 0.20 result = ( panel["orange_ratio"] >= panel_threshold and button["warm_button_ratio"] >= button_threshold ) info = { "method": "coupon_success_orange_panel", "orange_ratio": panel["orange_ratio"], "button_warm_ratio": button["warm_button_ratio"], "white_ratio": panel["white_ratio"], "effective_panel_threshold": panel_threshold, "effective_button_threshold": button_threshold, "reason": None if result else "orange_panel_or_button_below_threshold", } if return_info: return result, info return result except Exception as e: # type: ignore[general-exception] if return_info: return False, { "method": "coupon_success_orange_panel", "orange_ratio": 0.0, "button_warm_ratio": 0.0, "reason": f"exception: {str(e)}", } return False def _detect_coupon_bottom_panel_by_pixels( pic_rgb: Image.Image, return_info: bool = False, analyser: Any = None, ) -> bool | tuple[bool, dict]: """检测底部白底优惠券面板,覆盖模板低分但仍明显是券弹窗的样式。""" try: import numpy as np # type: ignore[import-untyped] x1, y1, x2, y2 = _scaled_box(pic_rgb, (9, 1173, 1079, 2255)) if x1 >= x2 or y1 >= y2: info = { "method": "coupon_bottom_panel_pixels", "white_ratio": 0.0, "warm_ratio": 0.0, "reason": "invalid_region", } return (False, info) if return_info else False arr = np.asarray(pic_rgb.crop((x1, y1, x2, y2)).convert("RGB")) if arr.size == 0: info = { "method": "coupon_bottom_panel_pixels", "white_ratio": 0.0, "warm_ratio": 0.0, "reason": "empty_region", } return (False, info) if return_info else False r = arr[:, :, 0] g = arr[:, :, 1] b = arr[:, :, 2] ri = r.astype(int) gi = g.astype(int) bi = b.astype(int) white = (r >= 235) & (g >= 235) & (b >= 235) warm = ( (r >= 220) & (g <= 190) & (b <= 170) & ((ri - gi) >= 35) & ((ri - bi) >= 35) ) total = white.size white_ratio = float(np.sum(white) / total) if total > 0 else 0.0 warm_ratio = float(np.sum(warm) / total) if total > 0 else 0.0 try: white_threshold = float( getattr(analyser, "coupon_bottom_panel_white_ratio_threshold", 0.40) or 0.40 ) except Exception: white_threshold = 0.40 try: warm_threshold = float( getattr(analyser, "coupon_bottom_panel_warm_ratio_threshold", 0.08) or 0.08 ) except Exception: warm_threshold = 0.08 result = white_ratio >= white_threshold and warm_ratio >= warm_threshold info = { "method": "coupon_bottom_panel_pixels", "white_ratio": white_ratio, "warm_ratio": warm_ratio, "effective_white_threshold": white_threshold, "effective_warm_threshold": warm_threshold, "reason": None if result else "white_or_warm_ratio_below_threshold", } if return_info: return result, info return result except Exception as e: # type: ignore[general-exception] if return_info: return False, { "method": "coupon_bottom_panel_pixels", "white_ratio": 0.0, "warm_ratio": 0.0, "reason": f"exception: {str(e)}", } return False def _coupon_success_or_return( pic_rgb: Image.Image, return_info: bool, analyser: Any, fallback_info: dict, ) -> bool | tuple[bool, dict]: detected, info = _detect_coupon_success_orange_panel( pic_rgb, return_info=True, analyser=analyser, ) if detected: return (True, info) if return_info else True bottom_detected, bottom_info = _detect_coupon_bottom_panel_by_pixels( pic_rgb, return_info=True, analyser=analyser, ) if bottom_detected: return (True, bottom_info) if return_info else True return (False, fallback_info) if return_info else False def detect_coupon_popup_background( pic_rgb: Image.Image, return_info: bool = False, template_threshold: float | None = None, analyser: Any = None, ) -> bool | tuple[bool, dict]: """检测优惠券弹窗。 优先检测底部优惠券模板;模板未命中时,兜底检测居中橙色 "领券成功 / 立即用券" 优惠券弹窗。 模板匹配区域(基于1080x2400基准): - 左上: (9, 1173) - 右下: (1079, 2255) 使用 `assets/coupon.png` 作为模板。 参数: pic_rgb: RGB格式的整屏截图 return_info: 是否返回详细信息(包含检测分数) template_threshold: 模板匹配阈值(默认0.75,如果提供则使用,否则从analyser读取) analyser: analyser对象,用于读取配置阈值 返回: bool: 是否检测到优惠券背景(return_info=False时) tuple[bool, dict]: (检测结果, 详细信息)(return_info=True时) """ try: from douyin.fudai.core.coords import get_pixel_region try: import cv2 # type: ignore[import-untyped] import numpy as np # type: ignore[import-untyped] except Exception as e: # type: ignore[general-exception] return _coupon_success_or_return( pic_rgb, return_info, analyser, { "method": "template_match", "template_score": None, "reason": "opencv_unavailable", }, ) success_detected, success_info = _detect_coupon_success_orange_panel( pic_rgb, return_info=True, analyser=analyser, ) if success_detected: return (True, success_info) if return_info else True bottom_detected, bottom_info = _detect_coupon_bottom_panel_by_pixels( pic_rgb, return_info=True, analyser=analyser, ) if bottom_detected: return (True, bottom_info) if return_info else True if ( success_info.get("orange_ratio", 0.0) < 0.04 and bottom_info.get("white_ratio", 0.0) < 0.20 ): if return_info: return False, { "method": "coupon_fast_pixel_prefilter", "orange_ratio": success_info.get("orange_ratio", 0.0), "bottom_white_ratio": bottom_info.get("white_ratio", 0.0), "bottom_warm_ratio": bottom_info.get("warm_ratio", 0.0), "reason": "pixel_ratios_too_low_for_coupon", } return False width, height = pic_rgb.size scale_x = width / 1080.0 scale_y = height / 2400.0 bx1, by1, bx2, by2 = get_pixel_region("coupon_popup", (9, 1173, 1079, 2255)) x1 = int(bx1 * scale_x) y1 = int(by1 * scale_y) x2 = int(bx2 * scale_x) y2 = int(by2 * scale_y) x1 = max(0, min(x1, width)) y1 = max(0, min(y1, height)) x2 = max(0, min(x2, width)) y2 = max(0, min(y2, height)) if x1 >= x2 or y1 >= y2: return _coupon_success_or_return( pic_rgb, return_info, analyser, { "method": "template_match", "template_score": None, "reason": "invalid_region", }, ) bgr = cv2.cvtColor( # type: ignore[attr-defined] np.asarray(pic_rgb), cv2.COLOR_RGB2BGR # type: ignore[attr-defined] ) roi = bgr[y1:y2, x1:x2] # type: ignore[index] if roi.size == 0: return _coupon_success_or_return( pic_rgb, return_info, analyser, { "method": "template_match", "template_score": None, "reason": "empty_roi", }, ) # 使用统一的模板路径配置 try: from douyin.core import path_config template_path = path_config.get_assets_dir() + "/coupon.png" except Exception: # 回退到旧路径(向后兼容) base_dir = os.path.dirname(__file__) template_path = os.path.join(base_dir, "assets", "coupon.png") if not os.path.exists(template_path): return _coupon_success_or_return( pic_rgb, return_info, analyser, { "method": "template_match", "template_score": None, "reason": "template_not_found", }, ) template = _read_coupon_template(template_path) if template is None: return _coupon_success_or_return( pic_rgb, return_info, analyser, { "method": "template_match", "template_score": None, "reason": "template_load_failed", }, ) if roi.shape[0] < template.shape[0] or roi.shape[1] < template.shape[1]: return _coupon_success_or_return( pic_rgb, return_info, analyser, { "method": "template_match", "template_score": None, "reason": "template_too_large", }, ) res = cv2.matchTemplate( # type: ignore[attr-defined] roi, template, cv2.TM_CCOEFF_NORMED # type: ignore[attr-defined] ) _, max_val, _, _ = cv2.minMaxLoc(res) # type: ignore[attr-defined] # 获取阈值:优先使用传入参数,其次从analyser读取,最后使用默认值 if template_threshold is not None: threshold = float(template_threshold) elif analyser is not None: try: threshold = float( getattr(analyser, "coupon_template_match_threshold", 0.75) or 0.75 ) except Exception as e: # type: ignore[general-exception] threshold = 0.75 else: threshold = 0.75 result = max_val >= threshold if not result: return _coupon_success_or_return( pic_rgb, return_info, analyser, { "method": "template_match", "template_score": float(max_val), "effective_threshold": threshold, "reason": "score_below_threshold", }, ) if return_info: reason_value: Optional[str] = None if result else "score_below_threshold" return result, { "method": "template_match", "template_score": float(max_val), "effective_threshold": threshold, "reason": reason_value, } return result except Exception as e: # type: ignore[general-exception] if return_info: return False, { "method": "template_match", "template_score": None, "reason": f"exception: {str(e)}", } return False def detect_redpacket_popup_background( pic_rgb: Image.Image, return_info: bool = False, red_ratio_threshold: float | None = None, analyser: Any = None, ) -> bool | tuple[bool, dict]: """中央购物红包弹窗检测:检测中央大红包区域的红色占比。 购物红包特征: - 中央大型红色弹窗 - 包含黄色圆形"抢"按钮 - 主要颜色为红色(RGB约 255, 100-150, 100-150) 检测区域(基准1080x2400):大约中央区域 - 左上:(300, 400) - 右下:(780, 1200) 参数: pic_rgb: RGB格式的整屏截图 return_info: 是否返回详细信息(包含红色比例) red_ratio_threshold: 红色占比阈值(默认0.15,若提供则使用) analyser: analyser对象,用于读取配置阈值 返回: bool: 是否检测到购物红包背景(return_info=False时) tuple[bool, dict]: (检测结果, 详细信息)(return_info=True时) """ try: from douyin.fudai.core.coords import get_pixel_region import numpy as np # type: ignore[import-untyped] width, height = pic_rgb.size scale_x = width / 1080.0 scale_y = height / 2400.0 # 购物红包通常出现在屏幕中央 # 基准坐标(1080x2400) bx1, by1, bx2, by2 = get_pixel_region("shopping_redpacket", (200, 300, 880, 1400)) x1 = int(bx1 * scale_x) y1 = int(by1 * scale_y) x2 = int(bx2 * scale_x) y2 = int(by2 * scale_y) # 边界检查 x1 = max(0, min(x1, width)) x2 = max(0, min(x2, width)) y1 = max(0, min(y1, height)) y2 = max(0, min(y2, height)) if x1 >= x2 or y1 >= y2: if return_info: return False, { "method": "pixel_detection", "red_ratio": 0.0, "reason": "invalid_region", } return False # 裁剪感兴趣区域 roi = pic_rgb.crop((x1, y1, x2, y2)) arr = np.asarray(roi) if arr.size == 0: if return_info: return False, { "method": "pixel_detection", "red_ratio": 0.0, "reason": "empty_roi", } return False # 提取RGB通道 r = arr[:, :, 0] # type: ignore[index] g = arr[:, :, 1] # type: ignore[index] b = arr[:, :, 2] # type: ignore[index] # 红色特征:R > 200, G < 180, B < 180 # 购物红包的红色通常很纯,R值很高 mask = (r >= 220) & (g <= 160) & (b <= 160) # 计算红色像素占比 total_pixels = mask.size red_pixels = np.sum(mask) # type: ignore[call-overload] red_ratio = red_pixels / total_pixels if total_pixels > 0 else 0 # 阈值:至少15%的像素满足红色特征 if red_ratio_threshold is not None: threshold = float(red_ratio_threshold) elif analyser is not None: try: threshold = float( getattr(analyser, "shopping_redpacket_red_ratio_threshold", 0.15) or 0.15 ) except Exception as e: # type: ignore[general-exception] threshold = 0.15 else: threshold = 0.15 result = bool(red_ratio > threshold) if return_info: reason_value: Optional[str] = ( None if result else "red_ratio_below_threshold" ) return result, { "method": "pixel_detection", "red_ratio": float(red_ratio), "effective_threshold": threshold, "reason": reason_value, } return result except Exception as e: # type: ignore[general-exception] if return_info: return False, { "method": "pixel_detection", "red_ratio": 0.0, "reason": f"exception: {str(e)}", } return False def detect_lucky_draw_result_popup( pic_rgb: Image.Image, red_ratio_threshold: float = 0.8, return_info: bool = False, analyser: Any = None, save_debug: bool = False, ) -> bool | tuple[bool, dict]: """检测抽奖结果弹窗:"我知道了"按钮区域,按 UI 版本检测对应按钮颜色。 使用基准 1080x2400 坐标的检测区域: - legacy/v1:旧版红按钮,检查红色占比 - auto/v2_0:新版白按钮,检查白色按钮内部占比 """ try: from douyin.fudai.core.coords import get_lottery_result_confirm_profile profile = get_lottery_result_confirm_profile( getattr(analyser, "fudai_detail_ui_mode", "auto") if analyser is not None else "auto" ) width, height = pic_rgb.size scale_x = width / 1080.0 scale_y = height / 2400.0 bx1, by1, bx2, by2 = tuple(profile.get("pixel") or (181, 1261, 890, 1389)) x1 = int(bx1 * scale_x) y1 = int(by1 * scale_y) x2 = int(bx2 * scale_x) y2 = int(by2 * scale_y) pixel_style = str(profile.get("pixel_style") or "red") # 边界与顺序 x1, x2 = max(0, min(x1, width)), max(0, min(x2, width)) y1, y2 = max(0, min(y1, height)), max(0, min(y2, height)) if x1 > x2: x1, x2 = x2, x1 if y1 > y2: y1, y2 = y2, y1 if x1 >= x2 or y1 >= y2: return False # 双截图记录:保存实时操作截图(像素检测区域) detect_roi = None if save_debug: try: from douyin.handlers import douyin_screenshot_manager detect_roi = pic_rgb.crop((x1, y1, x2, y2)) douyin_screenshot_manager.save_latest_attempt( detect_roi, operation_type="pixel_detect" ) # 保存调试图像(检测前)- 使用全屏截图,用框框选出识别区域 debug_path = douyin_screenshot_manager.save_debug_image( pic_rgb, # 使用全屏截图 operation_type="pixel_detect", picname="lucky_draw_result", step="before_detect", coords=(x1, y1, x2, y2), # 标注识别区域 extra_info={ "threshold": red_ratio_threshold, "region_size": f"{x2-x1}x{y2-y1}", "ui_version": profile.get("version"), "pixel_style": pixel_style, }, ) if debug_path: try: import logging logging.debug( "[调试图像] 像素检测前保存: %s, threshold=%s", debug_path, red_ratio_threshold, ) except Exception: # type: ignore[general-exception] pass except Exception: pass red_count = 0 white_count = 0 total_samples = 0 step_x = max(1, (x2 - x1) // 10) step_y = max(1, (y2 - y1) // 10) for y in range(y1, y2, step_y): for x in range(x1, x2, step_x): try: r, g, b = pic_rgb.getpixel((x, y)) total_samples += 1 # 根据实际检测结果:R: 245-255, G: 44-255, B: 85-255 # 红色按钮检测:R >= 245 且 R 明显大于 G 和 B if r >= 245 and r <= 255 and (r - g) >= 50 and (r - b) >= 50: red_count += 1 # 新版“我知道了”按钮为白底红字,按钮内部白色占比很高。 if r >= 230 and g >= 230 and b >= 230: white_count += 1 except Exception as e: # type: ignore[general-exception] continue if total_samples == 0: return False red_ratio = red_count / total_samples white_ratio = white_count / total_samples if pixel_style == "white": result = white_ratio >= red_ratio_threshold else: result = red_ratio >= red_ratio_threshold # 保存调试图像(检测结果)- 使用全屏截图,用框框选出识别区域 if save_debug: try: from douyin.handlers import douyin_screenshot_manager if result: # 检测成功 if detect_roi is None: detect_roi = pic_rgb.crop((x1, y1, x2, y2)) douyin_screenshot_manager.save_last_success( detect_roi, operation_type="pixel_detect" ) # 无论成功失败都保存debug截图 debug_path = douyin_screenshot_manager.save_debug_image( pic_rgb, # 使用全屏截图 operation_type="pixel_detect", picname="lucky_draw_result", step="detected" if result else "not_detected", coords=(x1, y1, x2, y2), # 标注识别区域 extra_info={ "red_ratio": round(red_ratio, 4), "white_ratio": round(white_ratio, 4), "red_count": red_count, "white_count": white_count, "total_samples": total_samples, "threshold": red_ratio_threshold, "result": "success" if result else "failed", "ui_version": profile.get("version"), "pixel_style": pixel_style, }, ) if debug_path: try: import logging logging.debug( "[调试图像] 抽奖结果检测保存: %s, red_ratio=%.4f, threshold=%s, score=%.4f", debug_path, red_ratio, red_ratio_threshold, red_ratio, ) except Exception: # type: ignore[general-exception] pass except Exception: pass # 如果请求详细信息,返回包含red_ratio的字典 if return_info: reason_value: Optional[str] = ( None if result else "red_ratio_below_threshold" ) return result, { "method": "pixel_detect", "red_ratio": red_ratio, "white_ratio": white_ratio, "red_count": red_count, "white_count": white_count, "total_samples": total_samples, "effective_threshold": red_ratio_threshold, "ui_version": profile.get("version"), "pixel_style": pixel_style, "reason": reason_value, } return result except Exception as e: # type: ignore[general-exception] return False