474 lines
17 KiB
Python
474 lines
17 KiB
Python
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
|
||
|
||
|
||
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,
|
||
analyser: Any = None,
|
||
) -> 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
|
||
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.info(
|
||
"[调试图像] 像素检测前保存: %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
|
||
|
||
# 保存调试图像(检测结果)- 使用全屏截图,用框框选出识别区域
|
||
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.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,
|
||
"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
|
||
|
||
|