401 lines
15 KiB
Python
401 lines
15 KiB
Python
"""
|
||
优惠券弹窗检测模块
|
||
"""
|
||
|
||
from typing import Any
|
||
from douyin.fudai.core.coords import PARTICIPATE_BUTTON_ALT
|
||
import os
|
||
from PIL import Image
|
||
|
||
|
||
def is_wrong_coupon_popup(analyser: Any) -> bool:
|
||
"""在已点击疑似福袋后,判断当前弹窗是否为优惠券类,而非真正福袋详情。
|
||
|
||
只在"点击详情后"调用,不在平时循环中使用,避免额外开销。
|
||
逻辑:
|
||
1. 对屏幕中部区域做颜色统计
|
||
2. 如果同时检测到福袋详情和优惠券特征,需要OCR确认"开心收下"字样
|
||
3. 只有OCR确认是"开心收下"时,才判定为优惠券弹窗
|
||
"""
|
||
try:
|
||
analyser.operation.get_screenshot(analyser.device_id)
|
||
except Exception as e:
|
||
try:
|
||
analyser._log_kv(
|
||
"错误",
|
||
"获取截图失败(判断优惠券弹窗)",
|
||
log_type="ERR",
|
||
error=str(e),
|
||
error_type=type(e).__name__,
|
||
level="WARNING",
|
||
)
|
||
except Exception:
|
||
pass
|
||
return False
|
||
|
||
try:
|
||
# 使用统一的截图路径配置
|
||
try:
|
||
import path_config
|
||
|
||
path = path_config.get_screenshot_path()
|
||
except Exception:
|
||
# 回退到旧路径(向后兼容)
|
||
import path_config
|
||
|
||
path = path_config.get_screenshot_path()
|
||
|
||
try:
|
||
pic = Image.open(path)
|
||
pic.load() # Force load to catch truncated files early
|
||
except (OSError, IOError, SyntaxError):
|
||
# Handle truncated image files
|
||
return False
|
||
except Exception:
|
||
return False
|
||
|
||
pic = pic.convert("RGBA")
|
||
pix = pic.load()
|
||
w, h = pic.size
|
||
|
||
# 扫描区域:153,351 到 266,451(基于1080x2400基准坐标)
|
||
# 根据实际分辨率缩放坐标
|
||
scale_x = w / 1080.0
|
||
scale_y = h / 2400.0
|
||
x1 = int(153 * scale_x)
|
||
y1 = int(351 * scale_y)
|
||
x2 = int(266 * scale_x)
|
||
y2 = int(451 * scale_y)
|
||
|
||
# 边界检查
|
||
x1 = max(0, min(x1, w))
|
||
y1 = max(0, min(y1, h))
|
||
x2 = max(0, min(x2, w))
|
||
y2 = max(0, min(y2, h))
|
||
|
||
if x1 >= x2 or y1 >= y2:
|
||
return False
|
||
|
||
fudai_like = 0
|
||
coupon_like = 0
|
||
step_x = max(1, (x2 - x1) // 40)
|
||
step_y = max(1, (y2 - y1) // 40)
|
||
|
||
for yy in range(y1, y2, step_y):
|
||
for xx in range(x1, x2, step_x):
|
||
try:
|
||
p = pix[xx, yy]
|
||
except Exception as e:
|
||
# 像素访问失败,跳过该像素(可能是坐标越界)
|
||
continue
|
||
if not (isinstance(p, tuple) and len(p) >= 3):
|
||
continue
|
||
r, g, b = int(p[0]), int(p[1]), int(p[2])
|
||
# 福袋:粉/紫/粉蓝
|
||
if r >= 200 and b >= 210 and 80 <= g <= 230:
|
||
fudai_like += 1
|
||
# 券:偏橙色
|
||
if r >= 200 and 120 <= g <= 230 and b <= 180:
|
||
coupon_like += 1
|
||
|
||
try:
|
||
analyser._log_kv(
|
||
"弹窗",
|
||
"弹窗颜色统计",
|
||
log_type="DEBUG",
|
||
fudai_like=fudai_like,
|
||
coupon_like=coupon_like,
|
||
)
|
||
except Exception as e:
|
||
try:
|
||
analyser._log_kv(
|
||
"错误",
|
||
"记录弹窗颜色统计日志失败",
|
||
log_type="ERR",
|
||
error=str(e),
|
||
error_type=type(e).__name__,
|
||
level="WARNING",
|
||
)
|
||
except Exception:
|
||
pass
|
||
|
||
# 如果券像素明显多于粉袋像素,可能需要OCR确认
|
||
is_coupon_like_by_color = coupon_like > fudai_like * 1.2
|
||
|
||
# 检测是否同时有福袋详情特征(通过像素检测)
|
||
has_fudai_detail = False
|
||
try:
|
||
from douyin.detection import douyin_pixel
|
||
|
||
resolution_ratio_x = int(
|
||
getattr(analyser, "resolution_ratio_x", 1080) or 1080
|
||
)
|
||
resolution_ratio_y = int(
|
||
getattr(analyser, "resolution_ratio_y", 2400) or 2400
|
||
)
|
||
y_pianyi = int(getattr(analyser, "y_pianyi", 0) or 0)
|
||
pic_rgb = pic.convert("RGB")
|
||
# 获取模板匹配阈值(优先使用福袋详情页专用阈值)
|
||
template_threshold = float(
|
||
getattr(analyser, "fudai_detail_template_threshold", None)
|
||
or getattr(analyser, "fudai_template_match_threshold", 0.7)
|
||
or 0.7
|
||
)
|
||
result = douyin_pixel.detect_fudai_detail_page(
|
||
pic_rgb,
|
||
resolution_ratio_x,
|
||
resolution_ratio_y,
|
||
y_pianyi,
|
||
template_threshold,
|
||
return_info=True,
|
||
analyser=analyser,
|
||
)
|
||
if isinstance(result, tuple):
|
||
has_fudai_detail, detect_info = result
|
||
# 记录检测信息到日志
|
||
try:
|
||
analyser._log_kv(
|
||
"弹窗",
|
||
"福袋详情检测结果",
|
||
log_type="DEBUG",
|
||
has_fudai_detail=has_fudai_detail,
|
||
detect_info=detect_info if detect_info else {},
|
||
)
|
||
except Exception as e:
|
||
try:
|
||
analyser._log_kv(
|
||
"错误",
|
||
"记录福袋详情检测日志失败",
|
||
log_type="ERR",
|
||
error=str(e),
|
||
error_type=type(e).__name__,
|
||
level="WARNING",
|
||
)
|
||
except Exception:
|
||
pass
|
||
else:
|
||
has_fudai_detail = bool(result)
|
||
except Exception as e:
|
||
try:
|
||
analyser._log_kv(
|
||
"错误",
|
||
"福袋详情检测异常",
|
||
log_type="ERR",
|
||
error=str(e),
|
||
error_type=type(e).__name__,
|
||
level="WARNING",
|
||
)
|
||
except Exception:
|
||
pass
|
||
|
||
# 如果颜色不像优惠券,且没有福袋详情特征,直接判定为福袋详情页
|
||
if not is_coupon_like_by_color:
|
||
if not has_fudai_detail:
|
||
analyser._log_kv(
|
||
"弹窗",
|
||
"颜色统计不像优惠券,且未检测到福袋详情,判定为福袋详情页",
|
||
log_type="OP",
|
||
)
|
||
else:
|
||
analyser._log_kv(
|
||
"弹窗",
|
||
"颜色统计不像优惠券,但检测到福袋详情,判定为福袋详情页",
|
||
log_type="OP",
|
||
)
|
||
return False
|
||
|
||
# 如果颜色像优惠券,但没有福袋详情特征,可能只是误判
|
||
if not has_fudai_detail:
|
||
analyser._log_kv(
|
||
"弹窗",
|
||
"颜色统计像优惠券,但未检测到福袋详情,判定为福袋详情页(可能是误判)",
|
||
log_type="OP",
|
||
)
|
||
return False
|
||
|
||
# 步骤2:OCR确认"开心收下"按钮(模板匹配成功后)
|
||
if has_fudai_detail:
|
||
analyser._log_kv(
|
||
"弹窗", "模板匹配成功,但检测到福袋详情,需要OCR确认", log_type="OP"
|
||
)
|
||
else:
|
||
analyser._log_kv("弹窗", "模板匹配成功,使用OCR确认", log_type="OP")
|
||
|
||
# 使用参与按钮区域检测"开心收下"(与"粉丝团加入"相同的区域)
|
||
# 保存完整截图并标注OCR区域
|
||
try:
|
||
from douyin.handlers import douyin_screenshot_manager
|
||
|
||
# 使用统一的截图路径配置
|
||
try:
|
||
import path_config
|
||
|
||
pic_path = path_config.get_screenshot_path()
|
||
except Exception:
|
||
# 回退到旧路径(向后兼容)
|
||
import path_config
|
||
|
||
pic_path = path_config.get_screenshot_path()
|
||
if os.path.exists(pic_path):
|
||
pic_check = Image.open(pic_path)
|
||
pic_check.load()
|
||
pic_rgb_check = pic_check.convert("RGB")
|
||
|
||
width, height = pic_rgb_check.size
|
||
rx = int(getattr(analyser, "resolution_ratio_x", 1080) or 1080)
|
||
ry = int(getattr(analyser, "resolution_ratio_y", 2400) or 2400)
|
||
|
||
x1_base, y1_base, x2_base, y2_base = 200, 1940, 880, 2140
|
||
x1 = int(x1_base * width / rx)
|
||
y1 = int(y1_base * height / ry)
|
||
x2 = int(x2_base * width / rx)
|
||
y2 = int(y2_base * height / ry)
|
||
|
||
x1 = max(0, min(x1, width - 1))
|
||
y1 = max(0, min(y1, height - 1))
|
||
x2 = max(0, min(x2, width - 1))
|
||
y2 = max(0, min(y2, height - 1))
|
||
|
||
if x1 < x2 and y1 < y2:
|
||
debug_path = douyin_screenshot_manager.save_debug_image(
|
||
pic_rgb_check,
|
||
operation_type="ocr",
|
||
picname="参与按钮",
|
||
step="before_ocr",
|
||
coords=(x1, y1, x2, y2),
|
||
extra_info={
|
||
"region": f"({x1_base},{y1_base})-({x2_base},{y2_base})",
|
||
"size": f"{x2_base-x1_base}x{y2_base-y1_base}",
|
||
"purpose": "检测开心收下",
|
||
},
|
||
)
|
||
if debug_path:
|
||
try:
|
||
analyser._log_kv(
|
||
"[调试图像]",
|
||
"OCR识别前保存完整截图(参与按钮-检测开心收下)",
|
||
log_type="SCREENSHOT",
|
||
path=debug_path,
|
||
)
|
||
except Exception as e:
|
||
try:
|
||
analyser._log_kv(
|
||
"错误",
|
||
"记录调试图像日志失败",
|
||
log_type="ERR",
|
||
error=str(e),
|
||
error_type=type(e).__name__,
|
||
level="WARNING",
|
||
)
|
||
except Exception:
|
||
pass
|
||
except Exception as e:
|
||
try:
|
||
analyser._log_kv(
|
||
"错误",
|
||
"保存参与按钮调试截图失败",
|
||
log_type="ERR",
|
||
error=str(e),
|
||
error_type=type(e).__name__,
|
||
level="WARNING",
|
||
)
|
||
except Exception:
|
||
pass
|
||
|
||
try:
|
||
analyser.cut_pic(*PARTICIPATE_BUTTON_ALT, "", "参与按钮")
|
||
|
||
# 并行执行模式0和模式1的OCR(如果启用模式1)
|
||
enable_ocr_mode1 = bool(getattr(analyser, "enable_ocr_mode1", False))
|
||
coupon_text = ""
|
||
if enable_ocr_mode1:
|
||
# 并行执行两个OCR模式
|
||
from concurrent.futures import ThreadPoolExecutor
|
||
|
||
with ThreadPoolExecutor(max_workers=2) as executor:
|
||
future_mode0 = executor.submit(
|
||
analyser.operation.analyse_pic_word, "参与按钮", 0
|
||
)
|
||
future_mode1 = executor.submit(
|
||
analyser.operation.analyse_pic_word, "参与按钮", 1
|
||
)
|
||
text_mode0 = str(future_mode0.result() or "")
|
||
text_mode1 = str(future_mode1.result() or "")
|
||
# 优先使用模式0的结果,如果模式0没有"开心收下"且模式1有,则使用模式1
|
||
if text_mode0 and "开心收下" in text_mode0:
|
||
coupon_text = text_mode0
|
||
elif text_mode1 and "开心收下" in text_mode1:
|
||
coupon_text = text_mode1
|
||
elif text_mode0:
|
||
coupon_text = text_mode0
|
||
else:
|
||
coupon_text = text_mode1
|
||
else:
|
||
# 只使用模式0
|
||
coupon_text = str(
|
||
analyser.operation.analyse_pic_word("参与按钮", 0) or ""
|
||
)
|
||
|
||
if coupon_text and "开心收下" in coupon_text:
|
||
# 模板匹配 + OCR 都确认,判定为优惠券弹窗
|
||
analyser._log_kv(
|
||
"弹窗",
|
||
"模板匹配+OCR确认:检测到'开心收下',判定为优惠券弹窗",
|
||
log_type="OP",
|
||
OCR文本=coupon_text[:50],
|
||
)
|
||
return True
|
||
else:
|
||
# 模板匹配成功,但OCR未确认"开心收下"
|
||
# 如果同时检测到福袋详情,优先信任福袋详情
|
||
if has_fudai_detail:
|
||
analyser._log_kv(
|
||
"弹窗",
|
||
"模板匹配成功但OCR未确认'开心收下',且检测到福袋详情,判定为福袋详情页",
|
||
log_type="OP",
|
||
OCR文本=coupon_text[:50] if coupon_text else "空",
|
||
)
|
||
return False
|
||
else:
|
||
# 模板匹配成功但OCR未确认,可能是识别福袋位置失败
|
||
# 不判定为优惠券弹窗,避免误点返回
|
||
analyser._log_kv(
|
||
"弹窗",
|
||
"模板匹配成功但OCR未确认'开心收下',不判定为优惠券弹窗",
|
||
log_type="OP",
|
||
OCR文本=coupon_text[:50] if coupon_text else "空",
|
||
说明="可能是识别福袋位置失败,点击了错误位置",
|
||
)
|
||
return False
|
||
except Exception as e:
|
||
# 确认过程异常,不判定为优惠券弹窗,避免误判
|
||
try:
|
||
analyser._log_kv(
|
||
"错误",
|
||
"优惠券确认过程异常,不判定为优惠券弹窗",
|
||
log_type="ERR",
|
||
error=str(e),
|
||
error_type=type(e).__name__,
|
||
level="WARNING",
|
||
)
|
||
except Exception:
|
||
pass
|
||
return False
|
||
|
||
# 否则判定为福袋详情页
|
||
return False
|
||
except Exception as e:
|
||
# 整个函数异常处理
|
||
try:
|
||
analyser._log_kv(
|
||
"错误",
|
||
"优惠券弹窗检测过程异常",
|
||
log_type="ERR",
|
||
error=str(e),
|
||
error_type=type(e).__name__,
|
||
level="WARNING",
|
||
)
|
||
except Exception:
|
||
pass
|
||
return False
|