From c0b8ff3ba68d0f957860c8fcd595d26f99d3a06b Mon Sep 17 00:00:00 2001 From: Codex Date: Thu, 21 May 2026 16:37:06 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8B=86=E5=88=86=20douyin=5Fpixel=20(1172=20?= =?UTF-8?q?=E2=86=92=204=20=E4=B8=AA=E5=AD=90=E6=A8=A1=E5=9D=97=20+=2033?= =?UTF-8?q?=20=E8=A1=8C=20shim)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 把 douyin/detection/douyin_pixel.py 按职责拆成 douyin/detection/pixel/ 子包: - _common (32) _safe_open_screenshot_rgb - popups (444) 优惠券 / 红包 / 抽奖结果弹窗检测 - buttons (340) 参与按钮 / 粉丝团加入红色区域检测 - stage (436) 直播间图标 / 福袋详情 / 综合阶段状态读取 原文件改为 33 行 re-export shim,9 个公共符号全部保留。 detect_fudai_detail_page 等 6 处外部引用、detect_attend_button_red_area 等 2 处外部引用全部不需修改。 回归: - 5 个新文件 + shim compileall 通过 - pytest 353 passed / 1 skipped - 旧 import 路径与新子包路径返回同一函数对象 Co-Authored-By: Claude Opus 4.7 --- douyin/detection/douyin_pixel.py | 1205 +--------------------------- douyin/detection/pixel/__init__.py | 40 + douyin/detection/pixel/_common.py | 32 + douyin/detection/pixel/buttons.py | 340 ++++++++ douyin/detection/pixel/popups.py | 444 ++++++++++ douyin/detection/pixel/stage.py | 436 ++++++++++ 6 files changed, 1325 insertions(+), 1172 deletions(-) create mode 100644 douyin/detection/pixel/__init__.py create mode 100644 douyin/detection/pixel/_common.py create mode 100644 douyin/detection/pixel/buttons.py create mode 100644 douyin/detection/pixel/popups.py create mode 100644 douyin/detection/pixel/stage.py diff --git a/douyin/detection/douyin_pixel.py b/douyin/detection/douyin_pixel.py index 0993076..9d4c359 100644 --- a/douyin/detection/douyin_pixel.py +++ b/douyin/detection/douyin_pixel.py @@ -1,1172 +1,33 @@ -# pyright: reportGeneralTypeIssues=false -# pyright: reportAttributeAccessIssue=false -# pyright: reportMissingModuleSource=false -# pyright: reportUnknownMemberType=false -# pyright: reportUnknownVariableType=false -# pylint: disable=no-member -import os -from concurrent.futures import ThreadPoolExecutor, as_completed -from typing import Any, Optional - -from PIL import Image - - -def _safe_open_screenshot_rgb(pic_path: str) -> Optional[Image.Image]: - try: - if not pic_path or (not os.path.exists(pic_path)): - return None - pic = Image.open(pic_path) - pic.load() # Force load to catch truncated files early - return pic.convert("RGB") - except (OSError, IOError, SyntaxError): - # Handle truncated image files - return None - except Exception as e: # type: ignore[general-exception] - return None - - -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: - try: - import cv2 # type: ignore[import-untyped] - import numpy as np # type: ignore[import-untyped] - except Exception as e: # type: ignore[general-exception] - if return_info: - return False, { - "method": "template_match", - "template_score": None, - "reason": "opencv_unavailable", - } - return False - - width, height = pic_rgb.size - scale_x = width / 1080.0 - scale_y = height / 2400.0 - - x1 = int(9 * scale_x) - y1 = int(1173 * scale_y) - x2 = int(1079 * scale_x) - y2 = int(2255 * 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: - if return_info: - return False, { - "method": "template_match", - "template_score": None, - "reason": "invalid_region", - } - return False - - 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: - if return_info: - return False, { - "method": "template_match", - "template_score": None, - "reason": "empty_roi", - } - return False - - # 使用统一的模板路径配置 - 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): - if return_info: - return False, { - "method": "template_match", - "template_score": None, - "reason": "template_not_found", - } - return False - - template = cv2.imread( # type: ignore[attr-defined,assignment] - template_path, cv2.IMREAD_COLOR # type: ignore[attr-defined] - ) - if template is None: - if return_info: - return False, { - "method": "template_match", - "template_score": None, - "reason": "template_load_failed", - } - return False - if roi.shape[0] < template.shape[0] or roi.shape[1] < template.shape[1]: - if return_info: - return False, { - "method": "template_match", - "template_score": None, - "reason": "template_too_large", - } - return False - - 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 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: - import numpy as np # type: ignore[import-untyped] - - width, height = pic_rgb.size - scale_x = width / 1080.0 - scale_y = height / 2400.0 - - # 购物红包通常出现在屏幕中央 - # 基准坐标(1080x2400) - x1 = int(200 * scale_x) - y1 = int(300 * scale_y) - x2 = int(880 * scale_x) - y2 = int(1400 * 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 -) -> bool | tuple[bool, dict]: - """检测抽奖结果弹窗:检测指定区域的红色占比("我知道了"按钮区域)。 - - 使用基准 1080x2400 坐标的检测区域: - - 左上: (181, 1261) - - 右下: (890, 1389) - """ - try: - width, height = pic_rgb.size - scale_x = width / 1080.0 - scale_y = height / 2400.0 - - x1 = int(181 * scale_x) - y1 = int(1261 * scale_y) - x2 = int(890 * scale_x) - y2 = int(1389 * scale_y) - - # 边界与顺序 - 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 - 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}", - }, - ) - if debug_path: - try: - import logging - - logging.info( - "[调试图像] 像素检测前保存: %s, threshold=%s", - debug_path, - red_ratio_threshold, - ) - except Exception: # type: ignore[general-exception] - pass - except Exception: - pass - - red_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 - except Exception as e: # type: ignore[general-exception] - continue - - if total_samples == 0: - return False - - red_ratio = red_count / total_samples - result = red_ratio >= red_ratio_threshold - - # 保存调试图像(检测结果)- 使用全屏截图,用框框选出识别区域 - 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), - "red_count": red_count, - "total_samples": total_samples, - "threshold": red_ratio_threshold, - "result": "success" if result else "failed", - }, - ) - if debug_path: - try: - import logging - - logging.info( - "[调试图像] 抽奖结果检测保存: %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, - "red_count": red_count, - "total_samples": total_samples, - "effective_threshold": red_ratio_threshold, - "reason": reason_value, - } - - return result - except Exception as e: # type: ignore[general-exception] - return False - - -def detect_attend_button_red_area( - pic_rgb: Image.Image, - return_info: bool = False, - red_ratio_threshold: float | None = None, - analyser: Any = None, -) -> bool | tuple[bool, dict]: - """检测参与按钮:大片红色区域。 - - 参与按钮通常在屏幕底部,有大片红色背景。 - 检测区域:(306,2030)-(780,2110) 基准1080x2400,但实际可能更宽 - 扩展检测区域到更宽的范围以覆盖"一键参与评论"等按钮 - """ - try: - width, height = pic_rgb.size - # 缩放坐标到实际尺寸 - scale_x = width / 1080.0 - scale_y = height / 2400.0 - - # 检测区域(基准1080x2400,扩展宽度以覆盖更宽的按钮) - x1 = int(200 * scale_x) # 扩展左侧 - y1 = int(2000 * scale_y) # 稍微上移 - x2 = int(880 * scale_x) # 扩展右侧 - y2 = int(2120 * 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 False - - # 保存调试图像(检测前)- 使用全屏截图,用框框选出识别区域 - try: - from douyin.handlers import douyin_screenshot_manager - - debug_path = douyin_screenshot_manager.save_debug_image( - pic_rgb, # 使用全屏截图 - operation_type="pixel_detect", - picname="attend_button", - step="before_detect", - coords=(x1, y1, x2, y2), # 标注识别区域 - extra_info={"region_size": f"{x2-x1}x{y2-y1}"}, - ) - if debug_path: - try: - import logging - - logging.info("[调试图像] 参与按钮检测前保存: %s", debug_path) - except Exception: # type: ignore[general-exception] - pass - except Exception: # type: ignore[general-exception] - pass - - # 采样检测点:检测大片红色区域 - red_count = 0 - step_x = max(1, (x2 - x1) // 15) - 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)) - # 检测红色:R值高,G和B相对较低 - if r > 200 and (r - g) > 50 and (r - b) > 50: - red_count += 1 - except Exception as e: # type: ignore[general-exception] - continue - - # 如果红色像素占比足够高(至少25%),认为有大片红色背景 - total_samples = len(list(range(y1, y2, step_y))) * len( - list(range(x1, x2, step_x)) - ) - if total_samples == 0: - return False - - red_ratio = red_count / total_samples if total_samples > 0 else 0 - # 获取阈值:优先使用传入参数,其次从analyser读取,最后使用默认值 - if red_ratio_threshold is not None: - threshold = float(red_ratio_threshold) - elif analyser is not None: - try: - threshold = float( - getattr(analyser, "attend_button_red_ratio_threshold", 0.25) or 0.25 - ) - except Exception as e: # type: ignore[general-exception] - threshold = 0.25 - else: - threshold = 0.25 - result = red_ratio >= threshold - - # 如果请求详细信息,返回包含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, - "red_count": red_count, - "total_samples": total_samples, - "effective_threshold": threshold, - "reason": reason_value, - } - - # 保存调试图像(检测结果)- 使用全屏截图,用框框选出识别区域 - # 无论成功失败都保存debug截图 - try: - from douyin.handlers import douyin_screenshot_manager - - debug_path = douyin_screenshot_manager.save_debug_image( - pic_rgb, # 使用全屏截图 - operation_type="pixel_detect", - picname="attend_button", - step="detected" if result else "not_detected", - coords=(x1, y1, x2, y2), # 标注识别区域 - extra_info={ - "red_ratio": round(red_ratio, 4), - "red_count": red_count, - "total_samples": total_samples, - "threshold": threshold, - "result": "success" if result else "failed", - }, - ) - if debug_path: - try: - import logging - - logging.info( - "[调试图像] 参与按钮检测保存: %s, red_ratio=%.4f, threshold=%.4f, result=%s", - debug_path, - red_ratio, - threshold, - "success" if result else "failed", - ) - except Exception as e: # type: ignore[general-exception] - pass - except Exception as e: # type: ignore[general-exception] - pass - - return result - except Exception: - if return_info: - return False, { - "method": "pixel_detect", - "red_ratio": None, - "reason": "exception", - } - return False - - -def detect_fansclub_join_red_area( - pic_rgb: Image.Image, - return_info: bool = False, - red_ratio_threshold: float | None = None, - analyser: Any = None, -) -> bool | tuple[bool, dict]: - """检测粉丝团加入:大片红色区域。 - - 粉丝团加入按钮通常在屏幕中下部,有大片红色背景。 - 检测区域:(200,1940)-(880,2140) 基准1080x2400 - """ - try: - width, height = pic_rgb.size - # 缩放坐标到实际尺寸 - scale_x = width / 1080.0 - scale_y = height / 2400.0 - - # 检测区域(基准1080x2400) - x1 = int(200 * scale_x) - y1 = int(1940 * scale_y) - x2 = int(880 * scale_x) - y2 = int(2140 * 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: - if return_info: - return False, { - "method": "pixel_detect", - "red_ratio": None, - "reason": "invalid_region", - } - return False - - # 保存调试图像(检测前)- 使用全屏截图,用框框选出识别区域 - try: - from douyin.handlers import douyin_screenshot_manager - - debug_path = douyin_screenshot_manager.save_debug_image( - pic_rgb, # 使用全屏截图 - operation_type="pixel_detect", - picname="fansclub_join", - step="before_detect", - coords=(x1, y1, x2, y2), # 标注识别区域 - extra_info={"region_size": f"{x2-x1}x{y2-y1}"}, - ) - if debug_path: - try: - import logging - - logging.info( - "[调试图像] 粉丝团加入检测前保存: %s", - debug_path, - ) - except Exception as e: # type: ignore[general-exception] - pass - except Exception as e: # type: ignore[general-exception] - pass - - # 采样检测点:检测大片红色区域 - red_count = 0 - step_x = max(1, (x2 - x1) // 15) - 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)) - # 检测红色:R值高,G和B相对较低 - if r > 200 and (r - g) > 50 and (r - b) > 50: - red_count += 1 - except Exception: - continue - - # 如果红色像素占比足够高(至少25%),认为有大片红色背景 - total_samples = len(list(range(y1, y2, step_y))) * len( - list(range(x1, x2, step_x)) - ) - if total_samples == 0: - if return_info: - return False, { - "method": "pixel_detect", - "red_ratio": None, - "reason": "no_samples", - } - return False - - red_ratio = red_count / total_samples if total_samples > 0 else 0 - # 获取阈值:优先使用传入参数,其次从analyser读取,最后使用默认值 - if red_ratio_threshold is not None: - threshold = float(red_ratio_threshold) - elif analyser is not None: - try: - threshold = float( - getattr(analyser, "fansclub_join_red_ratio_threshold", 0.25) or 0.25 - ) - except Exception as e: # type: ignore[general-exception] - threshold = 0.25 - else: - threshold = 0.25 - result = red_ratio >= threshold - - # 保存调试图像(检测结果)- 使用全屏截图,用框框选出识别区域 - try: - from douyin.handlers import douyin_screenshot_manager - - # 无论成功失败都保存debug截图 - debug_path = douyin_screenshot_manager.save_debug_image( - pic_rgb, # 使用全屏截图 - operation_type="pixel_detect", - picname="fansclub_join", - step="detected" if result else "not_detected", - coords=(x1, y1, x2, y2), # 标注识别区域 - extra_info={ - "red_ratio": round(red_ratio, 4), - "red_count": red_count, - "total_samples": total_samples, - "threshold": threshold, - "result": "success" if result else "failed", - }, - ) - if debug_path: - try: - import logging - - logging.info( - "[调试图像] 粉丝团加入检测保存: %s, red_ratio=%.4f, threshold=%.4f, result=%s", - debug_path, - red_ratio, - threshold, - "success" if result else "failed", - ) - except Exception as e: # type: ignore[general-exception] - pass - except Exception as e: # type: ignore[general-exception] - pass - - if return_info: - reason_value: Optional[str] = ( - None if result else "red_ratio_below_threshold" - ) - return result, { - "method": "pixel_detect", - "red_ratio": red_ratio, - "red_count": red_count, - "total_samples": total_samples, - "effective_threshold": threshold, - "reason": reason_value, - } - - return result - except Exception: - if return_info: - return False, { - "method": "pixel_detect", - "red_ratio": None, - "reason": "exception", - } - return False - - -def detect_live_room_icons_by_pixel(pic_rgb: Image.Image) -> tuple[bool, float]: - """通过像素检测多边形区域内的白色像素,判断是否在直播间。 - - 检测指定多边形区域内的白色像素比例,基于1080x2400分辨率: - - 多边形顶点坐标(12个点): - (1002,172), (999,174), (1011,186), (999,199), (1002,202), (1013,190), - (1025,201), (1028,198), (1017,187), (1028,174), (1025,172), (1014,184) - - 白色像素比例 >= 0.8 则认为在直播间内 - - 返回: - (detected, confidence) 其中 confidence 为白色像素比例(0.0~1.0) - """ - try: - from PIL import ImageDraw - import numpy as np - - width, height = pic_rgb.size - # 缩放坐标到实际尺寸(基于1080x2400) - scale_x = width / 1080.0 - scale_y = height / 2400.0 - - # 多边形顶点坐标(基于1080x2400基准分辨率) - polygon_base_points = [ - (1002, 172), - (999, 174), - (1011, 186), - (999, 199), - (1002, 202), - (1013, 190), - (1025, 201), - (1028, 198), - (1017, 187), - (1028, 174), - (1025, 172), - (1014, 184), - ] - - # 缩放多边形坐标到实际截图尺寸 - polygon_points = [ - (int(x * scale_x), int(y * scale_y)) for x, y in polygon_base_points - ] - - # 创建多边形mask - mask = Image.new("L", (width, height), 0) - draw = ImageDraw.Draw(mask) - - # 绘制多边形(填充为白色) - draw.polygon(polygon_points, fill=255) - - # 将mask转换为numpy数组 - mask_array = np.asarray(mask) - - # 获取原始图像的像素数据 - img_array = np.asarray(pic_rgb) - - # 计算多边形内部的总像素数 - total_pixels = np.sum(mask_array > 0) - - if total_pixels == 0: - return False, 0.0 - - # 计算多边形内部的白色像素(RGB值都>=240)——使用 numpy 向量化计算以加速 - # mask_array 为 uint8,mask_bool 为布尔数组 - mask_bool = mask_array > 0 - # 将 img_array 转为 numpy 数组并选择掩膜区域内的像素,shape -> (N, 3) - masked_pixels = img_array[mask_bool] - if masked_pixels.size == 0: - white_count = 0 - else: - # 检查 RGB 三通道是否都 >= 240 - white_flags = ( - (masked_pixels[:, 0] >= 240) - & (masked_pixels[:, 1] >= 240) - & (masked_pixels[:, 2] >= 240) - ) - white_count = int(np.count_nonzero(white_flags)) - - white_ratio = white_count / total_pixels if total_pixels > 0 else 0.0 - - # 白色像素比例 >= 0.8 则认为在直播间内 - detected = white_ratio >= 0.8 - confidence = white_ratio # 置信度就是白色比例 - - # 计算多边形边界框(用于调试图像) - if polygon_points: - x_coords = [p[0] for p in polygon_points] - y_coords = [p[1] for p in polygon_points] - x1 = max(0, min(x_coords)) - y1 = max(0, min(y_coords)) - x2 = min(width, max(x_coords)) - y2 = min(height, max(y_coords)) - else: - x1, y1, x2, y2 = 0, 0, 0, 0 - - # 保存调试图像(检测结果) - try: - from douyin.handlers import douyin_screenshot_manager - - debug_path = douyin_screenshot_manager.save_debug_image( - pic_rgb, # 使用全屏截图 - operation_type="pixel_detect", - picname="live_room_icons", - step="detected" if detected else "not_detected", - coords=(x1, y1, x2, y2), # 标注识别区域边界框 - extra_info={ - "white_ratio": round(white_ratio, 4), - "white_count": white_count, - "total_pixels": total_pixels, - "confidence": round(confidence, 4), - "threshold": 0.8, - "result": "success" if detected else "failed", - "polygon_points": len(polygon_points), - }, - ) - if debug_path: - try: - import logging - - logging.info( - "[调试图像] 直播间图标检测保存: %s, white_ratio=%.4f, white_count=%d, total_pixels=%d, confidence=%.4f, result=%s", - debug_path, - white_ratio, - white_count, - total_pixels, - confidence, - "success" if detected else "failed", - ) - except Exception as e: # type: ignore[general-exception] - pass - except Exception as e: # type: ignore[general-exception] - pass - - return bool(detected), float(confidence) - except Exception as e: # type: ignore[general-exception] - return False, 0.0 - - -def detect_fudai_detail_page( - _pic_rgb: Image.Image, - _resolution_ratio_x: int = 1080, - _resolution_ratio_y: int = 2400, - _y_pianyi: int = 0, - _template_threshold: float = 0.7, - return_info: bool = False, - analyser: Any = None, -) -> bool | tuple[bool, dict]: - """检测是否处于福袋详情页。 - - 检测策略:任务数量检测 - 1. 通过检测任务数量来判断是否在福袋详情页 - 2. 如果任务数量 <= 0,则认为不在福袋详情页 - 3. 如果任务数量 >= 1,则认为在福袋详情页,并返回任务数量 - - 参数: - pic_rgb: RGB格式的整屏截图(保留用于兼容性,实际不使用) - resolution_ratio_x: 分辨率X比例(默认1080,保留用于兼容性) - resolution_ratio_y: 分辨率Y比例(默认2400,保留用于兼容性) - y_pianyi: Y轴偏移(默认0,保留用于兼容性) - template_threshold: 模板匹配阈值(保留用于兼容性,不再使用) - return_info: 是否返回详细信息(包含任务数量) - analyser: 分析器对象(必需,用于任务数量检测) - - 返回: - bool: 是否检测到福袋详情页(return_info=False时) - tuple[bool, dict]: (检测结果, 详细信息)(return_info=True时) - 详细信息包含: - - method: 检测方法("task_count") - - task_count: 任务数量(0/1/2/3) - """ - # ---------- 任务数量检测 ---------- - if analyser is None: - # analyser 参数必需 - if return_info: - return False, { - "method": "task_count", - "task_count": 0, - "reason": "analyser_required", - } - return False - - try: - from .douyin_detection import check_detail_height - - # 执行任务数量检测 - task_count = int(check_detail_height(analyser) or 0) - - # 判断是否在福袋详情页 - # 如果任务数量 <= 0,则认为不在福袋详情页 - # 如果任务数量 >= 1,则认为在福袋详情页 - is_in_detail = task_count >= 1 - - if return_info: - return is_in_detail, { - "method": "task_count", - "task_count": task_count, - } - return is_in_detail - except Exception as e: # type: ignore[general-exception] - # 检测异常 - if return_info: - return False, { - "method": "task_count", - "task_count": 0, - "reason": f"detection_exception: {str(e)}", - } - return False - - -def read_pixel_stage_state_from_screenshot( - pic_path: str, - resolution_ratio_x: int = 1080, - resolution_ratio_y: int = 2400, - y_pianyi: int = 0, - lucky_draw_red_ratio_threshold: float = 0.8, - fudai_template_threshold: float = 0.7, - return_details: bool = False, - analyser: Any = None, -) -> dict[str, bool] | tuple[dict[str, bool], dict[str, dict]]: - """从 screenshot.png 读取"阶段诊断"需要的多区域像素特征(不调用 OCR)。 - - 使用多线程并行执行所有检测函数以提高性能。 - - 参数: - pic_path: 截图路径 - resolution_ratio_x: 分辨率X比例(默认1080) - resolution_ratio_y: 分辨率Y比例(默认2400) - y_pianyi: Y轴偏移(默认0) - lucky_draw_red_ratio_threshold: 抽奖结果红色比例阈值(默认0.8) - fudai_template_threshold: 福袋详情模板匹配阈值(默认0.7) - return_details: 是否返回每个检测步骤的详细信息(包括分数) - - 返回: - dict[str, bool]: 检测结果(return_details=False时) - tuple[dict[str, bool], dict[str, dict]]: (检测结果, 详细信息)(return_details=True时) - """ - state: dict[str, bool] = { - "has_coupon_bg": False, - "has_redpacket_bg": False, - "has_lucky_draw_result": False, - "has_attend_button_red": False, - "has_fansclub_join_red": False, - "has_fudai_detail": False, - "has_live_room_pixel": False, # 直播间图标像素检测(多边形区域白色像素检测) - } - details: dict[str, dict] = {} - - pic_rgb = _safe_open_screenshot_rgb(pic_path) - if pic_rgb is None: - if return_details: - return state, details - return state - - # 并行执行所有检测函数 - # 注意:优惠券检测需要额外参数,使用lambda包装 - # 注意:福袋详情检测使用任务数量检测,需要analyser参数 - # 从analyser读取阈值(如果可用) - coupon_threshold = None - attend_button_threshold = None - fansclub_threshold = None - redpacket_threshold = None - live_room_pixel_threshold = None - if analyser is not None: - try: - coupon_threshold = float( - getattr(analyser, "coupon_template_match_threshold", None) or 0.75 - ) - except Exception as e: # type: ignore[general-exception] - pass - try: - attend_button_threshold = float( - getattr(analyser, "attend_button_red_ratio_threshold", None) or 0.25 - ) - except Exception as e: # type: ignore[general-exception] - pass - try: - fansclub_threshold = float( - getattr(analyser, "fansclub_join_red_ratio_threshold", None) or 0.25 - ) - except Exception as e: # type: ignore[general-exception] - pass - try: - redpacket_threshold = float( - getattr(analyser, "shopping_redpacket_red_ratio_threshold", None) - or 0.15 - ) - except Exception as e: # type: ignore[general-exception] - pass - try: - live_room_pixel_threshold = float( - getattr(analyser, "live_room_match_threshold", None) or 0.8 - ) - except Exception as e: # type: ignore[general-exception] - pass - - # 定义直播间图标像素检测包装函数 - def _detect_live_room_pixel_wrapper(pic: Image.Image) -> tuple[bool, dict]: - """直播间图标像素检测包装函数,返回 (bool, dict) 格式""" - try: - detected, confidence = detect_live_room_icons_by_pixel(pic) - threshold = live_room_pixel_threshold or 0.8 - is_detected = detected and confidence >= threshold - return is_detected, { - "method": "pixel_detection", - "confidence": confidence, - "effective_threshold": threshold, - } - except Exception as e: # type: ignore[general-exception] - return False, {"method": "pixel_detection", "error": str(e)} - - # 模板匹配已删除,只使用像素检测 - - detection_tasks = { - "has_coupon_bg": lambda pic: detect_coupon_popup_background( - pic, - return_info=True, - template_threshold=coupon_threshold, - analyser=analyser, - ), - "has_redpacket_bg": lambda pic: detect_redpacket_popup_background( - pic, - return_info=True, - red_ratio_threshold=redpacket_threshold, - analyser=analyser, - ), - "has_lucky_draw_result": lambda pic: detect_lucky_draw_result_popup( - pic, lucky_draw_red_ratio_threshold, return_info=True - ), - "has_attend_button_red": lambda pic: detect_attend_button_red_area( - pic, - return_info=True, - red_ratio_threshold=attend_button_threshold, - analyser=analyser, - ), - "has_fansclub_join_red": lambda pic: detect_fansclub_join_red_area( - pic, - return_info=True, - red_ratio_threshold=fansclub_threshold, - analyser=analyser, - ), - "has_fudai_detail": lambda pic: detect_fudai_detail_page( - pic, - resolution_ratio_x, - resolution_ratio_y, - y_pianyi, - fudai_template_threshold, - return_info=True, - analyser=analyser, - ), - "has_live_room_pixel": _detect_live_room_pixel_wrapper, # 直播间图标像素检测(多边形区域白色像素检测) - } - - # 使用线程池并行执行检测(增加线程数以支持直播间图标检测) - with ThreadPoolExecutor(max_workers=8) as executor: - # 提交所有任务,并记录开始时间 - import time - - future_to_key = {} - for key, detect_func in detection_tasks.items(): - start_time = time.time() - future = executor.submit(detect_func, pic_rgb) - future_to_key[future] = (key, start_time) - - # 收集结果 - for future in as_completed(future_to_key): - key, start_time = future_to_key[future] - elapsed_ms = (time.time() - start_time) * 1000 - try: - result = future.result() - - # 处理结果 - if isinstance(result, tuple): - # 检测函数返回 (bool, dict) 格式 - is_detected, info = result - state[key] = bool(is_detected) - if return_details: - details[key] = { - "detected": bool(is_detected), - "method": info.get("method", "unknown"), - "template_score": info.get("template_score"), - "confidence": info.get( - "confidence" - ), # 添加confidence支持(用于像素检测) - "task_count": info.get("task_count"), # 添加task_count支持 - "red_ratio": info.get("red_ratio"), # 添加red_ratio支持 - "effective_threshold": info.get("effective_threshold"), - "reason": info.get("reason"), - "elapsed_ms": round(elapsed_ms, 1), # 添加耗时 - } - else: - # 其他检测返回 bool(不应该发生,因为所有检测都支持return_info) - state[key] = bool(result) - if return_details: - details[key] = { - "detected": bool(result), - "method": "unknown", - "elapsed_ms": round(elapsed_ms, 1), # 添加耗时 - } - except Exception as e: # type: ignore[general-exception] - # 如果某个检测失败,保持默认值 False - state[key] = False - if return_details: - details[key] = { - "detected": False, - "error": str(e), - "elapsed_ms": round(elapsed_ms, 1), # 添加耗时 - } - - if return_details: - return state, details - return state +"""douyin_pixel - 兼容入口(保留外部 import 路径)。 + +实际实现位于子包 ``douyin.detection.pixel``。 +本文件仅 re-export 公共符号;详见各子模块文档: +- ``pixel._common`` 共享 _safe_open_screenshot_rgb +- ``pixel.popups`` 弹窗类检测(优惠券 / 红包 / 抽奖结果) +- ``pixel.buttons`` 按钮区域红色检测(参与按钮 / 粉丝团加入) +- ``pixel.stage`` 阶段诊断(直播间图标 / 福袋详情 / 综合状态读取) +""" + +from douyin.detection.pixel import ( + _safe_open_screenshot_rgb, + detect_attend_button_red_area, + detect_coupon_popup_background, + detect_fansclub_join_red_area, + detect_fudai_detail_page, + detect_live_room_icons_by_pixel, + detect_lucky_draw_result_popup, + detect_redpacket_popup_background, + read_pixel_stage_state_from_screenshot, +) + +__all__ = [ + "_safe_open_screenshot_rgb", + "detect_coupon_popup_background", + "detect_redpacket_popup_background", + "detect_lucky_draw_result_popup", + "detect_attend_button_red_area", + "detect_fansclub_join_red_area", + "detect_live_room_icons_by_pixel", + "detect_fudai_detail_page", + "read_pixel_stage_state_from_screenshot", +] diff --git a/douyin/detection/pixel/__init__.py b/douyin/detection/pixel/__init__.py new file mode 100644 index 0000000..5103790 --- /dev/null +++ b/douyin/detection/pixel/__init__.py @@ -0,0 +1,40 @@ +"""pixel - 像素 / 模板匹配检测子包。 + +原 douyin/detection/douyin_pixel.py 拆成 4 个子模块: + +- _common 共享:_safe_open_screenshot_rgb +- popups 弹窗类检测:优惠券 / 红包 / 抽奖结果 +- buttons 按钮区域红色检测:参与按钮 / 粉丝团加入 +- stage 阶段诊断:直播间图标 / 福袋详情页 / 综合阶段状态读取 + +外部 import 路径不变: +``from douyin.detection.douyin_pixel import detect_fudai_detail_page`` 仍可用。 +""" + +from ._common import _safe_open_screenshot_rgb +from .popups import ( + detect_coupon_popup_background, + detect_redpacket_popup_background, + detect_lucky_draw_result_popup, +) +from .buttons import ( + detect_attend_button_red_area, + detect_fansclub_join_red_area, +) +from .stage import ( + detect_live_room_icons_by_pixel, + detect_fudai_detail_page, + read_pixel_stage_state_from_screenshot, +) + +__all__ = [ + "_safe_open_screenshot_rgb", + "detect_coupon_popup_background", + "detect_redpacket_popup_background", + "detect_lucky_draw_result_popup", + "detect_attend_button_red_area", + "detect_fansclub_join_red_area", + "detect_live_room_icons_by_pixel", + "detect_fudai_detail_page", + "read_pixel_stage_state_from_screenshot", +] diff --git a/douyin/detection/pixel/_common.py b/douyin/detection/pixel/_common.py new file mode 100644 index 0000000..1b9e8f5 --- /dev/null +++ b/douyin/detection/pixel/_common.py @@ -0,0 +1,32 @@ +# pyright: reportGeneralTypeIssues=false +# pyright: reportAttributeAccessIssue=false +# pyright: reportMissingModuleSource=false +# pyright: reportUnknownMemberType=false +# pyright: reportUnknownVariableType=false +# pylint: disable=no-member +"""pixel._common - 共享工具函数。 + +只包含 _safe_open_screenshot_rgb:安全打开截图为 RGB Image, +处理 truncated/损坏文件。被 popups / buttons / stage 子模块共用。 +""" + +import os +from typing import Any, Optional + +from PIL import Image + + +def _safe_open_screenshot_rgb(pic_path: str) -> Optional[Image.Image]: + try: + if not pic_path or (not os.path.exists(pic_path)): + return None + pic = Image.open(pic_path) + pic.load() # Force load to catch truncated files early + return pic.convert("RGB") + except (OSError, IOError, SyntaxError): + # Handle truncated image files + return None + except Exception as e: # type: ignore[general-exception] + return None + + diff --git a/douyin/detection/pixel/buttons.py b/douyin/detection/pixel/buttons.py new file mode 100644 index 0000000..60ee01f --- /dev/null +++ b/douyin/detection/pixel/buttons.py @@ -0,0 +1,340 @@ +# pyright: reportGeneralTypeIssues=false +# pyright: reportAttributeAccessIssue=false +# pyright: reportMissingModuleSource=false +# pyright: reportUnknownMemberType=false +# pyright: reportUnknownVariableType=false +# pylint: disable=no-member +"""pixel.buttons - 按钮区域的红色检测。 + +- detect_attend_button_red_area 福袋详情页"参与抽奖"按钮 +- detect_fansclub_join_red_area 粉丝团加入按钮 +""" + +import os +from concurrent.futures import ThreadPoolExecutor, as_completed +from typing import Any, Optional + +from PIL import Image + +from ._common import _safe_open_screenshot_rgb + + +def detect_attend_button_red_area( + pic_rgb: Image.Image, + return_info: bool = False, + red_ratio_threshold: float | None = None, + analyser: Any = None, +) -> bool | tuple[bool, dict]: + """检测参与按钮:大片红色区域。 + + 参与按钮通常在屏幕底部,有大片红色背景。 + 检测区域:(306,2030)-(780,2110) 基准1080x2400,但实际可能更宽 + 扩展检测区域到更宽的范围以覆盖"一键参与评论"等按钮 + """ + try: + width, height = pic_rgb.size + # 缩放坐标到实际尺寸 + scale_x = width / 1080.0 + scale_y = height / 2400.0 + + # 检测区域(基准1080x2400,扩展宽度以覆盖更宽的按钮) + x1 = int(200 * scale_x) # 扩展左侧 + y1 = int(2000 * scale_y) # 稍微上移 + x2 = int(880 * scale_x) # 扩展右侧 + y2 = int(2120 * 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 False + + # 保存调试图像(检测前)- 使用全屏截图,用框框选出识别区域 + try: + from douyin.handlers import douyin_screenshot_manager + + debug_path = douyin_screenshot_manager.save_debug_image( + pic_rgb, # 使用全屏截图 + operation_type="pixel_detect", + picname="attend_button", + step="before_detect", + coords=(x1, y1, x2, y2), # 标注识别区域 + extra_info={"region_size": f"{x2-x1}x{y2-y1}"}, + ) + if debug_path: + try: + import logging + + logging.info("[调试图像] 参与按钮检测前保存: %s", debug_path) + except Exception: # type: ignore[general-exception] + pass + except Exception: # type: ignore[general-exception] + pass + + # 采样检测点:检测大片红色区域 + red_count = 0 + step_x = max(1, (x2 - x1) // 15) + 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)) + # 检测红色:R值高,G和B相对较低 + if r > 200 and (r - g) > 50 and (r - b) > 50: + red_count += 1 + except Exception as e: # type: ignore[general-exception] + continue + + # 如果红色像素占比足够高(至少25%),认为有大片红色背景 + total_samples = len(list(range(y1, y2, step_y))) * len( + list(range(x1, x2, step_x)) + ) + if total_samples == 0: + return False + + red_ratio = red_count / total_samples if total_samples > 0 else 0 + # 获取阈值:优先使用传入参数,其次从analyser读取,最后使用默认值 + if red_ratio_threshold is not None: + threshold = float(red_ratio_threshold) + elif analyser is not None: + try: + threshold = float( + getattr(analyser, "attend_button_red_ratio_threshold", 0.25) or 0.25 + ) + except Exception as e: # type: ignore[general-exception] + threshold = 0.25 + else: + threshold = 0.25 + result = red_ratio >= threshold + + # 如果请求详细信息,返回包含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, + "red_count": red_count, + "total_samples": total_samples, + "effective_threshold": threshold, + "reason": reason_value, + } + + # 保存调试图像(检测结果)- 使用全屏截图,用框框选出识别区域 + # 无论成功失败都保存debug截图 + try: + from douyin.handlers import douyin_screenshot_manager + + debug_path = douyin_screenshot_manager.save_debug_image( + pic_rgb, # 使用全屏截图 + operation_type="pixel_detect", + picname="attend_button", + step="detected" if result else "not_detected", + coords=(x1, y1, x2, y2), # 标注识别区域 + extra_info={ + "red_ratio": round(red_ratio, 4), + "red_count": red_count, + "total_samples": total_samples, + "threshold": threshold, + "result": "success" if result else "failed", + }, + ) + if debug_path: + try: + import logging + + logging.info( + "[调试图像] 参与按钮检测保存: %s, red_ratio=%.4f, threshold=%.4f, result=%s", + debug_path, + red_ratio, + threshold, + "success" if result else "failed", + ) + except Exception as e: # type: ignore[general-exception] + pass + except Exception as e: # type: ignore[general-exception] + pass + + return result + except Exception: + if return_info: + return False, { + "method": "pixel_detect", + "red_ratio": None, + "reason": "exception", + } + return False + + + + +def detect_fansclub_join_red_area( + pic_rgb: Image.Image, + return_info: bool = False, + red_ratio_threshold: float | None = None, + analyser: Any = None, +) -> bool | tuple[bool, dict]: + """检测粉丝团加入:大片红色区域。 + + 粉丝团加入按钮通常在屏幕中下部,有大片红色背景。 + 检测区域:(200,1940)-(880,2140) 基准1080x2400 + """ + try: + width, height = pic_rgb.size + # 缩放坐标到实际尺寸 + scale_x = width / 1080.0 + scale_y = height / 2400.0 + + # 检测区域(基准1080x2400) + x1 = int(200 * scale_x) + y1 = int(1940 * scale_y) + x2 = int(880 * scale_x) + y2 = int(2140 * 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: + if return_info: + return False, { + "method": "pixel_detect", + "red_ratio": None, + "reason": "invalid_region", + } + return False + + # 保存调试图像(检测前)- 使用全屏截图,用框框选出识别区域 + try: + from douyin.handlers import douyin_screenshot_manager + + debug_path = douyin_screenshot_manager.save_debug_image( + pic_rgb, # 使用全屏截图 + operation_type="pixel_detect", + picname="fansclub_join", + step="before_detect", + coords=(x1, y1, x2, y2), # 标注识别区域 + extra_info={"region_size": f"{x2-x1}x{y2-y1}"}, + ) + if debug_path: + try: + import logging + + logging.info( + "[调试图像] 粉丝团加入检测前保存: %s", + debug_path, + ) + except Exception as e: # type: ignore[general-exception] + pass + except Exception as e: # type: ignore[general-exception] + pass + + # 采样检测点:检测大片红色区域 + red_count = 0 + step_x = max(1, (x2 - x1) // 15) + 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)) + # 检测红色:R值高,G和B相对较低 + if r > 200 and (r - g) > 50 and (r - b) > 50: + red_count += 1 + except Exception: + continue + + # 如果红色像素占比足够高(至少25%),认为有大片红色背景 + total_samples = len(list(range(y1, y2, step_y))) * len( + list(range(x1, x2, step_x)) + ) + if total_samples == 0: + if return_info: + return False, { + "method": "pixel_detect", + "red_ratio": None, + "reason": "no_samples", + } + return False + + red_ratio = red_count / total_samples if total_samples > 0 else 0 + # 获取阈值:优先使用传入参数,其次从analyser读取,最后使用默认值 + if red_ratio_threshold is not None: + threshold = float(red_ratio_threshold) + elif analyser is not None: + try: + threshold = float( + getattr(analyser, "fansclub_join_red_ratio_threshold", 0.25) or 0.25 + ) + except Exception as e: # type: ignore[general-exception] + threshold = 0.25 + else: + threshold = 0.25 + result = red_ratio >= threshold + + # 保存调试图像(检测结果)- 使用全屏截图,用框框选出识别区域 + try: + from douyin.handlers import douyin_screenshot_manager + + # 无论成功失败都保存debug截图 + debug_path = douyin_screenshot_manager.save_debug_image( + pic_rgb, # 使用全屏截图 + operation_type="pixel_detect", + picname="fansclub_join", + step="detected" if result else "not_detected", + coords=(x1, y1, x2, y2), # 标注识别区域 + extra_info={ + "red_ratio": round(red_ratio, 4), + "red_count": red_count, + "total_samples": total_samples, + "threshold": threshold, + "result": "success" if result else "failed", + }, + ) + if debug_path: + try: + import logging + + logging.info( + "[调试图像] 粉丝团加入检测保存: %s, red_ratio=%.4f, threshold=%.4f, result=%s", + debug_path, + red_ratio, + threshold, + "success" if result else "failed", + ) + except Exception as e: # type: ignore[general-exception] + pass + except Exception as e: # type: ignore[general-exception] + pass + + if return_info: + reason_value: Optional[str] = ( + None if result else "red_ratio_below_threshold" + ) + return result, { + "method": "pixel_detect", + "red_ratio": red_ratio, + "red_count": red_count, + "total_samples": total_samples, + "effective_threshold": threshold, + "reason": reason_value, + } + + return result + except Exception: + if return_info: + return False, { + "method": "pixel_detect", + "red_ratio": None, + "reason": "exception", + } + return False + + diff --git a/douyin/detection/pixel/popups.py b/douyin/detection/pixel/popups.py new file mode 100644 index 0000000..db2da92 --- /dev/null +++ b/douyin/detection/pixel/popups.py @@ -0,0 +1,444 @@ +# 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 PIL import Image + +from ._common import _safe_open_screenshot_rgb + + +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: + try: + import cv2 # type: ignore[import-untyped] + import numpy as np # type: ignore[import-untyped] + except Exception as e: # type: ignore[general-exception] + if return_info: + return False, { + "method": "template_match", + "template_score": None, + "reason": "opencv_unavailable", + } + return False + + width, height = pic_rgb.size + scale_x = width / 1080.0 + scale_y = height / 2400.0 + + x1 = int(9 * scale_x) + y1 = int(1173 * scale_y) + x2 = int(1079 * scale_x) + y2 = int(2255 * 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: + if return_info: + return False, { + "method": "template_match", + "template_score": None, + "reason": "invalid_region", + } + return False + + 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: + if return_info: + return False, { + "method": "template_match", + "template_score": None, + "reason": "empty_roi", + } + return False + + # 使用统一的模板路径配置 + 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): + if return_info: + return False, { + "method": "template_match", + "template_score": None, + "reason": "template_not_found", + } + return False + + template = cv2.imread( # type: ignore[attr-defined,assignment] + template_path, cv2.IMREAD_COLOR # type: ignore[attr-defined] + ) + if template is None: + if return_info: + return False, { + "method": "template_match", + "template_score": None, + "reason": "template_load_failed", + } + return False + if roi.shape[0] < template.shape[0] or roi.shape[1] < template.shape[1]: + if return_info: + return False, { + "method": "template_match", + "template_score": None, + "reason": "template_too_large", + } + return False + + 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 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: + import numpy as np # type: ignore[import-untyped] + + width, height = pic_rgb.size + scale_x = width / 1080.0 + scale_y = height / 2400.0 + + # 购物红包通常出现在屏幕中央 + # 基准坐标(1080x2400) + x1 = int(200 * scale_x) + y1 = int(300 * scale_y) + x2 = int(880 * scale_x) + y2 = int(1400 * 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 +) -> bool | tuple[bool, dict]: + """检测抽奖结果弹窗:检测指定区域的红色占比("我知道了"按钮区域)。 + + 使用基准 1080x2400 坐标的检测区域: + - 左上: (181, 1261) + - 右下: (890, 1389) + """ + try: + width, height = pic_rgb.size + scale_x = width / 1080.0 + scale_y = height / 2400.0 + + x1 = int(181 * scale_x) + y1 = int(1261 * scale_y) + x2 = int(890 * scale_x) + y2 = int(1389 * scale_y) + + # 边界与顺序 + 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 + 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}", + }, + ) + if debug_path: + try: + import logging + + logging.info( + "[调试图像] 像素检测前保存: %s, threshold=%s", + debug_path, + red_ratio_threshold, + ) + except Exception: # type: ignore[general-exception] + pass + except Exception: + pass + + red_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 + except Exception as e: # type: ignore[general-exception] + continue + + if total_samples == 0: + return False + + red_ratio = red_count / total_samples + result = red_ratio >= red_ratio_threshold + + # 保存调试图像(检测结果)- 使用全屏截图,用框框选出识别区域 + 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), + "red_count": red_count, + "total_samples": total_samples, + "threshold": red_ratio_threshold, + "result": "success" if result else "failed", + }, + ) + if debug_path: + try: + import logging + + logging.info( + "[调试图像] 抽奖结果检测保存: %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, + "red_count": red_count, + "total_samples": total_samples, + "effective_threshold": red_ratio_threshold, + "reason": reason_value, + } + + return result + except Exception as e: # type: ignore[general-exception] + return False + + diff --git a/douyin/detection/pixel/stage.py b/douyin/detection/pixel/stage.py new file mode 100644 index 0000000..d371da6 --- /dev/null +++ b/douyin/detection/pixel/stage.py @@ -0,0 +1,436 @@ +# pyright: reportGeneralTypeIssues=false +# pyright: reportAttributeAccessIssue=false +# pyright: reportMissingModuleSource=false +# pyright: reportUnknownMemberType=false +# pyright: reportUnknownVariableType=false +# pylint: disable=no-member +"""pixel.stage - 阶段诊断相关像素检测。 + +- detect_live_room_icons_by_pixel 直播间图标检测 +- detect_fudai_detail_page 福袋详情页识别 +- read_pixel_stage_state_from_screenshot 综合多种检测,返回阶段状态 +""" + +import os +from concurrent.futures import ThreadPoolExecutor, as_completed +from typing import Any, Optional + +from PIL import Image + +from ._common import _safe_open_screenshot_rgb + + +def detect_live_room_icons_by_pixel(pic_rgb: Image.Image) -> tuple[bool, float]: + """通过像素检测多边形区域内的白色像素,判断是否在直播间。 + + 检测指定多边形区域内的白色像素比例,基于1080x2400分辨率: + - 多边形顶点坐标(12个点): + (1002,172), (999,174), (1011,186), (999,199), (1002,202), (1013,190), + (1025,201), (1028,198), (1017,187), (1028,174), (1025,172), (1014,184) + - 白色像素比例 >= 0.8 则认为在直播间内 + + 返回: + (detected, confidence) 其中 confidence 为白色像素比例(0.0~1.0) + """ + try: + from PIL import ImageDraw + import numpy as np + + width, height = pic_rgb.size + # 缩放坐标到实际尺寸(基于1080x2400) + scale_x = width / 1080.0 + scale_y = height / 2400.0 + + # 多边形顶点坐标(基于1080x2400基准分辨率) + polygon_base_points = [ + (1002, 172), + (999, 174), + (1011, 186), + (999, 199), + (1002, 202), + (1013, 190), + (1025, 201), + (1028, 198), + (1017, 187), + (1028, 174), + (1025, 172), + (1014, 184), + ] + + # 缩放多边形坐标到实际截图尺寸 + polygon_points = [ + (int(x * scale_x), int(y * scale_y)) for x, y in polygon_base_points + ] + + # 创建多边形mask + mask = Image.new("L", (width, height), 0) + draw = ImageDraw.Draw(mask) + + # 绘制多边形(填充为白色) + draw.polygon(polygon_points, fill=255) + + # 将mask转换为numpy数组 + mask_array = np.asarray(mask) + + # 获取原始图像的像素数据 + img_array = np.asarray(pic_rgb) + + # 计算多边形内部的总像素数 + total_pixels = np.sum(mask_array > 0) + + if total_pixels == 0: + return False, 0.0 + + # 计算多边形内部的白色像素(RGB值都>=240)——使用 numpy 向量化计算以加速 + # mask_array 为 uint8,mask_bool 为布尔数组 + mask_bool = mask_array > 0 + # 将 img_array 转为 numpy 数组并选择掩膜区域内的像素,shape -> (N, 3) + masked_pixels = img_array[mask_bool] + if masked_pixels.size == 0: + white_count = 0 + else: + # 检查 RGB 三通道是否都 >= 240 + white_flags = ( + (masked_pixels[:, 0] >= 240) + & (masked_pixels[:, 1] >= 240) + & (masked_pixels[:, 2] >= 240) + ) + white_count = int(np.count_nonzero(white_flags)) + + white_ratio = white_count / total_pixels if total_pixels > 0 else 0.0 + + # 白色像素比例 >= 0.8 则认为在直播间内 + detected = white_ratio >= 0.8 + confidence = white_ratio # 置信度就是白色比例 + + # 计算多边形边界框(用于调试图像) + if polygon_points: + x_coords = [p[0] for p in polygon_points] + y_coords = [p[1] for p in polygon_points] + x1 = max(0, min(x_coords)) + y1 = max(0, min(y_coords)) + x2 = min(width, max(x_coords)) + y2 = min(height, max(y_coords)) + else: + x1, y1, x2, y2 = 0, 0, 0, 0 + + # 保存调试图像(检测结果) + try: + from douyin.handlers import douyin_screenshot_manager + + debug_path = douyin_screenshot_manager.save_debug_image( + pic_rgb, # 使用全屏截图 + operation_type="pixel_detect", + picname="live_room_icons", + step="detected" if detected else "not_detected", + coords=(x1, y1, x2, y2), # 标注识别区域边界框 + extra_info={ + "white_ratio": round(white_ratio, 4), + "white_count": white_count, + "total_pixels": total_pixels, + "confidence": round(confidence, 4), + "threshold": 0.8, + "result": "success" if detected else "failed", + "polygon_points": len(polygon_points), + }, + ) + if debug_path: + try: + import logging + + logging.info( + "[调试图像] 直播间图标检测保存: %s, white_ratio=%.4f, white_count=%d, total_pixels=%d, confidence=%.4f, result=%s", + debug_path, + white_ratio, + white_count, + total_pixels, + confidence, + "success" if detected else "failed", + ) + except Exception as e: # type: ignore[general-exception] + pass + except Exception as e: # type: ignore[general-exception] + pass + + return bool(detected), float(confidence) + except Exception as e: # type: ignore[general-exception] + return False, 0.0 + + + + +def detect_fudai_detail_page( + _pic_rgb: Image.Image, + _resolution_ratio_x: int = 1080, + _resolution_ratio_y: int = 2400, + _y_pianyi: int = 0, + _template_threshold: float = 0.7, + return_info: bool = False, + analyser: Any = None, +) -> bool | tuple[bool, dict]: + """检测是否处于福袋详情页。 + + 检测策略:任务数量检测 + 1. 通过检测任务数量来判断是否在福袋详情页 + 2. 如果任务数量 <= 0,则认为不在福袋详情页 + 3. 如果任务数量 >= 1,则认为在福袋详情页,并返回任务数量 + + 参数: + pic_rgb: RGB格式的整屏截图(保留用于兼容性,实际不使用) + resolution_ratio_x: 分辨率X比例(默认1080,保留用于兼容性) + resolution_ratio_y: 分辨率Y比例(默认2400,保留用于兼容性) + y_pianyi: Y轴偏移(默认0,保留用于兼容性) + template_threshold: 模板匹配阈值(保留用于兼容性,不再使用) + return_info: 是否返回详细信息(包含任务数量) + analyser: 分析器对象(必需,用于任务数量检测) + + 返回: + bool: 是否检测到福袋详情页(return_info=False时) + tuple[bool, dict]: (检测结果, 详细信息)(return_info=True时) + 详细信息包含: + - method: 检测方法("task_count") + - task_count: 任务数量(0/1/2/3) + """ + # ---------- 任务数量检测 ---------- + if analyser is None: + # analyser 参数必需 + if return_info: + return False, { + "method": "task_count", + "task_count": 0, + "reason": "analyser_required", + } + return False + + try: + from .douyin_detection import check_detail_height + + # 执行任务数量检测 + task_count = int(check_detail_height(analyser) or 0) + + # 判断是否在福袋详情页 + # 如果任务数量 <= 0,则认为不在福袋详情页 + # 如果任务数量 >= 1,则认为在福袋详情页 + is_in_detail = task_count >= 1 + + if return_info: + return is_in_detail, { + "method": "task_count", + "task_count": task_count, + } + return is_in_detail + except Exception as e: # type: ignore[general-exception] + # 检测异常 + if return_info: + return False, { + "method": "task_count", + "task_count": 0, + "reason": f"detection_exception: {str(e)}", + } + return False + + + + +def read_pixel_stage_state_from_screenshot( + pic_path: str, + resolution_ratio_x: int = 1080, + resolution_ratio_y: int = 2400, + y_pianyi: int = 0, + lucky_draw_red_ratio_threshold: float = 0.8, + fudai_template_threshold: float = 0.7, + return_details: bool = False, + analyser: Any = None, +) -> dict[str, bool] | tuple[dict[str, bool], dict[str, dict]]: + """从 screenshot.png 读取"阶段诊断"需要的多区域像素特征(不调用 OCR)。 + + 使用多线程并行执行所有检测函数以提高性能。 + + 参数: + pic_path: 截图路径 + resolution_ratio_x: 分辨率X比例(默认1080) + resolution_ratio_y: 分辨率Y比例(默认2400) + y_pianyi: Y轴偏移(默认0) + lucky_draw_red_ratio_threshold: 抽奖结果红色比例阈值(默认0.8) + fudai_template_threshold: 福袋详情模板匹配阈值(默认0.7) + return_details: 是否返回每个检测步骤的详细信息(包括分数) + + 返回: + dict[str, bool]: 检测结果(return_details=False时) + tuple[dict[str, bool], dict[str, dict]]: (检测结果, 详细信息)(return_details=True时) + """ + state: dict[str, bool] = { + "has_coupon_bg": False, + "has_redpacket_bg": False, + "has_lucky_draw_result": False, + "has_attend_button_red": False, + "has_fansclub_join_red": False, + "has_fudai_detail": False, + "has_live_room_pixel": False, # 直播间图标像素检测(多边形区域白色像素检测) + } + details: dict[str, dict] = {} + + pic_rgb = _safe_open_screenshot_rgb(pic_path) + if pic_rgb is None: + if return_details: + return state, details + return state + + # 并行执行所有检测函数 + # 注意:优惠券检测需要额外参数,使用lambda包装 + # 注意:福袋详情检测使用任务数量检测,需要analyser参数 + # 从analyser读取阈值(如果可用) + coupon_threshold = None + attend_button_threshold = None + fansclub_threshold = None + redpacket_threshold = None + live_room_pixel_threshold = None + if analyser is not None: + try: + coupon_threshold = float( + getattr(analyser, "coupon_template_match_threshold", None) or 0.75 + ) + except Exception as e: # type: ignore[general-exception] + pass + try: + attend_button_threshold = float( + getattr(analyser, "attend_button_red_ratio_threshold", None) or 0.25 + ) + except Exception as e: # type: ignore[general-exception] + pass + try: + fansclub_threshold = float( + getattr(analyser, "fansclub_join_red_ratio_threshold", None) or 0.25 + ) + except Exception as e: # type: ignore[general-exception] + pass + try: + redpacket_threshold = float( + getattr(analyser, "shopping_redpacket_red_ratio_threshold", None) + or 0.15 + ) + except Exception as e: # type: ignore[general-exception] + pass + try: + live_room_pixel_threshold = float( + getattr(analyser, "live_room_match_threshold", None) or 0.8 + ) + except Exception as e: # type: ignore[general-exception] + pass + + # 定义直播间图标像素检测包装函数 + def _detect_live_room_pixel_wrapper(pic: Image.Image) -> tuple[bool, dict]: + """直播间图标像素检测包装函数,返回 (bool, dict) 格式""" + try: + detected, confidence = detect_live_room_icons_by_pixel(pic) + threshold = live_room_pixel_threshold or 0.8 + is_detected = detected and confidence >= threshold + return is_detected, { + "method": "pixel_detection", + "confidence": confidence, + "effective_threshold": threshold, + } + except Exception as e: # type: ignore[general-exception] + return False, {"method": "pixel_detection", "error": str(e)} + + # 模板匹配已删除,只使用像素检测 + + detection_tasks = { + "has_coupon_bg": lambda pic: detect_coupon_popup_background( + pic, + return_info=True, + template_threshold=coupon_threshold, + analyser=analyser, + ), + "has_redpacket_bg": lambda pic: detect_redpacket_popup_background( + pic, + return_info=True, + red_ratio_threshold=redpacket_threshold, + analyser=analyser, + ), + "has_lucky_draw_result": lambda pic: detect_lucky_draw_result_popup( + pic, lucky_draw_red_ratio_threshold, return_info=True + ), + "has_attend_button_red": lambda pic: detect_attend_button_red_area( + pic, + return_info=True, + red_ratio_threshold=attend_button_threshold, + analyser=analyser, + ), + "has_fansclub_join_red": lambda pic: detect_fansclub_join_red_area( + pic, + return_info=True, + red_ratio_threshold=fansclub_threshold, + analyser=analyser, + ), + "has_fudai_detail": lambda pic: detect_fudai_detail_page( + pic, + resolution_ratio_x, + resolution_ratio_y, + y_pianyi, + fudai_template_threshold, + return_info=True, + analyser=analyser, + ), + "has_live_room_pixel": _detect_live_room_pixel_wrapper, # 直播间图标像素检测(多边形区域白色像素检测) + } + + # 使用线程池并行执行检测(增加线程数以支持直播间图标检测) + with ThreadPoolExecutor(max_workers=8) as executor: + # 提交所有任务,并记录开始时间 + import time + + future_to_key = {} + for key, detect_func in detection_tasks.items(): + start_time = time.time() + future = executor.submit(detect_func, pic_rgb) + future_to_key[future] = (key, start_time) + + # 收集结果 + for future in as_completed(future_to_key): + key, start_time = future_to_key[future] + elapsed_ms = (time.time() - start_time) * 1000 + try: + result = future.result() + + # 处理结果 + if isinstance(result, tuple): + # 检测函数返回 (bool, dict) 格式 + is_detected, info = result + state[key] = bool(is_detected) + if return_details: + details[key] = { + "detected": bool(is_detected), + "method": info.get("method", "unknown"), + "template_score": info.get("template_score"), + "confidence": info.get( + "confidence" + ), # 添加confidence支持(用于像素检测) + "task_count": info.get("task_count"), # 添加task_count支持 + "red_ratio": info.get("red_ratio"), # 添加red_ratio支持 + "effective_threshold": info.get("effective_threshold"), + "reason": info.get("reason"), + "elapsed_ms": round(elapsed_ms, 1), # 添加耗时 + } + else: + # 其他检测返回 bool(不应该发生,因为所有检测都支持return_info) + state[key] = bool(result) + if return_details: + details[key] = { + "detected": bool(result), + "method": "unknown", + "elapsed_ms": round(elapsed_ms, 1), # 添加耗时 + } + except Exception as e: # type: ignore[general-exception] + # 如果某个检测失败,保持默认值 False + state[key] = False + if return_details: + details[key] = { + "detected": False, + "error": str(e), + "elapsed_ms": round(elapsed_ms, 1), # 添加耗时 + } + + if return_details: + return state, details + return state