Files
douyin_guaji-main/douyin_fudai_finder.py
T
Your Name f92933641f feat: Enhance documentation and clean up code structure
- Added comprehensive documentation for the unified screenshot and score recording process in the new markdown file.
- Improved existing markdown files by adding spacing for better readability.
- Cleaned up code by removing unnecessary blank lines across various scripts, enhancing overall code clarity.
- Updated configuration settings to reflect recent changes and ensure consistency across the project.
- Enhanced logging mechanisms to support better debugging and monitoring.
2025-12-25 16:11:25 +08:00

1687 lines
71 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
from __future__ import annotations
"""
优化的福袋查找器模块
==================
功能:
1. 模块化设计:图像获取、识别器、决策器、执行器分离
2. 智能搜索策略:根据热点区域优化搜索顺序
3. 验证机制:点击后验证是否成功打开详情页
4. 重试机制:失败时自动重试
5. 详细日志:记录每个步骤的耗时和结果
"""
import os
import time
from typing import Any, Optional, Tuple, List, Dict
from PIL import Image
import path_config
class FudaiImageAcquirer:
"""图像获取器:负责截图和图像预处理"""
def __init__(self, analyser: Any):
self.analyser = analyser
self.screenshot_path = path_config.get_screenshot_path()
def acquire_screenshot(self, force_refresh: bool = False) -> Optional[Image.Image]:
"""获取截图
参数:
force_refresh: 是否强制刷新截图
返回:
PIL Image对象,失败返回None
"""
start_time = time.time()
try:
if force_refresh or not os.path.exists(self.screenshot_path):
try:
self.analyser._shot_and_diagnose(
"找福袋图标",
"获取截图",
self.analyser.device_id,
diagnose=False,
)
except Exception:
try:
self.analyser.operation.get_screenshot(self.analyser.device_id)
except Exception:
return None
# 读取截图
try:
pic = Image.open(self.screenshot_path)
pic.load()
elapsed = (time.time() - start_time) * 1000
try:
self.analyser._log_kv(
"[福袋查找] 图像获取",
"成功",
elapsed_ms=round(elapsed, 2),
size=f"{pic.size[0]}x{pic.size[1]}",
)
except Exception:
pass
return pic.convert("RGBA")
except Exception:
return None
except Exception as e:
try:
self.analyser._log_kv(
"[福袋查找] 图像获取",
"失败",
error=str(e),
error_type=type(e).__name__,
)
except Exception:
pass
return None
class FudaiDetector:
"""福袋识别器:负责检测福袋图标位置"""
def __init__(self, analyser: Any):
self.analyser = analyser
self._init_resolution()
self._init_search_regions()
def _init_resolution(self):
"""初始化分辨率信息"""
try:
self.rx = int(getattr(self.analyser, "resolution_ratio_x", 1080) or 1080)
self.ry = int(getattr(self.analyser, "resolution_ratio_y", 2400) or 2400)
self.y_pianyi = int(getattr(self.analyser, "y_pianyi", 0) or 0)
except Exception:
self.rx, self.ry, self.y_pianyi = 1080, 2400, 0
def _init_search_regions(self):
"""初始化搜索区域(按优先级排序,基于日志分析和实际区域优化)
基于日志分析结果(1158条记录):
- X=163 出现1060次(91.54%)
- X=166 出现48次(4.15%)
- 左上角区域(41-410)占100%
- 中间区域(156-269)占96.03%
- 平均位置:X=169.8,标准差:35.5
- 最热门区间:150-199(96.03%)
实际福袋出现区域:(34, 363) 到 (608, 451) [基于1080x2400基准]
优化后的优先级:
1. 实际福袋区域(X=34-608, Y=363-451):覆盖所有可能的福袋位置
2. 最热门区域(X=98-240,覆盖91%+):优先检测,最快命中
3. 扩展热门区域(X=41-410,覆盖100%):兜底检测
4. 右上角(X=670-1039):较少见,最后检测
"""
base_y = 403 * self.ry // 2400 + self.y_pianyi
# 优先级1:实际福袋区域(X=34-608, Y=363-451)
# 这是用户确认的福袋实际出现区域,优先检测确保覆盖所有可能位置
self.search_regions = [
{
"name": "实际福袋区域",
"priority": 1,
"x1": 34 * self.rx // 1080,
"x2": 608 * self.rx // 1080,
"y_top": 363 * self.ry // 2400 + self.y_pianyi,
"y_bottom": 451 * self.ry // 2400 + self.y_pianyi,
"y": (363 + 451) // 2 * self.ry // 2400 + self.y_pianyi, # 中心Y坐标
"y_range": (451 - 363) // 2 + 1, # 覆盖整个Y范围
},
# 优先级2:最热门区域(X=98-240,基于日志分析,覆盖91%+的情况)
# 这是最集中的区域,优先检测可以最快命中
{
"name": "最热门区域",
"priority": 2,
"x1": 98 * self.rx // 1080,
"x2": 240 * self.rx // 1080,
"y": base_y,
"y_range": 7, # 上下3-4行
},
# 优先级3:扩展热门区域(X=41-410,覆盖100%的情况)
# 如果最热门区域没找到,扩大范围搜索
{
"name": "扩展热门区域",
"priority": 3,
"x1": 41 * self.rx // 1080,
"x2": 410 * self.rx // 1080,
"y": base_y,
"y_range": 7,
},
# 优先级4:右上角(较少见)
{
"name": "右上角",
"priority": 4,
"x1": 670 * self.rx // 1080,
"x2": 1039 * self.rx // 1080,
"y": base_y,
"y_range": 7,
},
# 优先级5:中间区域(模板匹配兜底,用于特殊情况)
{
"name": "中间区域",
"priority": 5,
"x1": 156 * self.rx // 1080,
"x2": 269 * self.rx // 1080,
"y_top": 351 * self.ry // 2400 + self.y_pianyi,
"y_bottom": 451 * self.ry // 2400 + self.y_pianyi,
},
]
def detect_by_pixel(self, pic: Image.Image, region: Dict) -> Optional[int]:
"""使用像素检测在指定区域查找福袋图标
参数:
pic: PIL Image对象
region: 搜索区域配置
返回:
检测到的X坐标(1080基准),未找到返回None
"""
start_time = time.time()
width, height = pic.size
# 提取区域参数
x1 = max(0, min(region.get("x1", 0), width))
x2 = max(0, min(region.get("x2", width), width))
# 支持两种区域配置方式:
# 1. 使用 y_top 和 y_bottom(精确范围)
# 2. 使用 y 和 y_range(中心点+范围)
if "y_top" in region and "y_bottom" in region:
y_top = max(0, min(int(region.get("y_top", 0)), height))
y_bottom = max(0, min(int(region.get("y_bottom", height)), height))
else:
scan_y = region.get("y", 0)
y_range = region.get("y_range", 7)
if not (0 <= scan_y < height):
# 记录无效区域配置
try:
elapsed = (time.time() - start_time) * 1000
self.analyser._log_kv(
"[福袋查找] 像素检测",
f"{region['name']}区域无效(Y坐标超出范围)",
elapsed_ms=round(elapsed, 2),
region=region["name"],
scan_y=scan_y,
height=height,
)
except Exception:
pass
return None
y_top = max(0, scan_y - y_range // 2)
y_bottom = min(height, scan_y + y_range // 2 + 1)
if x1 >= x2 or y_top >= y_bottom:
# 记录无效区域配置
try:
elapsed = (time.time() - start_time) * 1000
self.analyser._log_kv(
"[福袋查找] 像素检测",
f"{region['name']}区域无效(坐标范围错误)",
elapsed_ms=round(elapsed, 2),
region=region["name"],
x1=x1,
x2=x2,
y_top=y_top,
y_bottom=y_bottom,
)
except Exception:
pass
return None
try:
import numpy as np
# 使用numpy加速
roi = pic.crop((x1, y_top, x2, y_bottom)).convert("RGB")
arr = np.array(roi)
if arr.size == 0:
return None
r = arr[:, :, 0] # type: ignore[index]
g = arr[:, :, 1] # type: ignore[index]
b = arr[:, :, 2] # type: ignore[index]
# 福袋图标颜色范围(优化后的范围)
mask = (
(r >= 190) # type: ignore[operator]
& (r <= 206) # type: ignore[operator]
& (g >= 173) # type: ignore[operator]
& (g <= 196) # type: ignore[operator]
& (b >= 234) # type: ignore[operator]
& (b <= 250) # type: ignore[operator]
)
# 检查每一列是否有足够多的匹配像素
cols = np.sum(mask, axis=0)
valid_cols = cols >= 2 # 至少2个像素匹配
if valid_cols.any():
# 优化:从最优位置开始向左右两侧搜索
roi_width = x2 - x1
# 获取最优搜索起始位置(使用历史记录或默认值)
try:
# 尝试获取上次找到的福袋位置(1080基准)
last_fudai_x = int(getattr(self.analyser, "last_fudai_x", 0) or 0)
if last_fudai_x > 0:
optimal_x_scaled = last_fudai_x * self.rx // 1080
optimal_col = optimal_x_scaled - x1
if optimal_col < 0 or optimal_col >= roi_width:
# 如果历史位置不在当前区域内,使用区域中心
optimal_col = roi_width // 2
else:
# 使用区域中心作为最优位置
optimal_col = roi_width // 2
except Exception:
# 使用区域中心作为最优位置
optimal_col = roi_width // 2
optimal_col = max(0, min(optimal_col, roi_width - 1))
# 从最优位置开始,向左右两侧交替搜索
found_col = None
search_range = max(optimal_col, roi_width - optimal_col)
# 先检查最优位置本身
if valid_cols[optimal_col]:
found_col = optimal_col
else:
# 向左右两侧交替搜索
for offset in range(1, search_range + 1):
# 先检查右侧
right_col = optimal_col + offset
if right_col < roi_width and valid_cols[right_col]:
found_col = right_col
break
# 再检查左侧
left_col = optimal_col - offset
if left_col >= 0 and valid_cols[left_col]:
found_col = left_col
break
# 如果没找到,回退到原来的方法(从右往左)
if found_col is None:
valid_cols_reversed = valid_cols[::-1]
col_idx_reversed = int(np.argmax(valid_cols_reversed))
found_col = len(valid_cols) - 1 - col_idx_reversed
detected_x = int((x1 + found_col) * 1080.0 / max(1, self.rx))
# 保存找到的位置作为下次的最优位置
try:
self.analyser.last_fudai_x = detected_x
except Exception:
pass
elapsed = (time.time() - start_time) * 1000
try:
self.analyser._log_kv(
"[福袋查找] 像素检测",
f"{region['name']}命中",
elapsed_ms=round(elapsed, 2),
x=detected_x,
region=f"({x1},{y_top})-({x2},{y_bottom})",
optimal_col=optimal_col,
found_col=found_col,
)
except Exception:
pass
return detected_x
except ImportError:
# numpy不可用,回退到逐像素检测
pass
except Exception:
pass
# 回退到逐像素检测(从最优位置开始向左右两侧搜索)
try:
pix = pic.load()
y_range_int = int(y_range) if y_range is not None else 7
# 获取最优搜索起始位置(使用历史记录或默认值)
try:
# 尝试获取上次找到的福袋位置(1080基准)
last_fudai_x = int(getattr(self.analyser, "last_fudai_x", 0) or 0)
if last_fudai_x > 0:
optimal_x_scaled = last_fudai_x * self.rx // 1080
if optimal_x_scaled < x1 or optimal_x_scaled >= x2:
# 如果历史位置不在当前区域内,使用区域中心
optimal_x_scaled = (x1 + x2) // 2
else:
# 使用区域中心作为最优位置
optimal_x_scaled = (x1 + x2) // 2
except Exception:
# 使用区域中心作为最优位置
optimal_x_scaled = (x1 + x2) // 2
optimal_x_scaled = max(x1, min(optimal_x_scaled, x2 - 1))
for offset_y in range(-y_range_int // 2, y_range_int // 2 + 1):
check_y = scan_y + offset_y
if not (0 <= check_y < height):
continue
# 优化:从最优位置开始向左右两侧搜索
found_x = None
search_range = max(optimal_x_scaled - x1, x2 - optimal_x_scaled)
# 先检查最优位置本身
try:
px = pix[optimal_x_scaled, check_y]
if isinstance(px, tuple) and len(px) >= 3:
if (
190 <= px[0] <= 206
and 173 <= px[1] <= 196
and 234 <= px[2] <= 250
):
found_x = optimal_x_scaled
except (IndexError, TypeError):
pass
# 如果最优位置没找到,向左右两侧交替搜索
if found_x is None:
for offset in range(1, search_range + 1):
# 先检查右侧
right_x = optimal_x_scaled + offset
if right_x < x2:
try:
px = pix[right_x, check_y]
if isinstance(px, tuple) and len(px) >= 3:
if (
190 <= px[0] <= 206
and 173 <= px[1] <= 196
and 234 <= px[2] <= 250
):
found_x = right_x
break
except (IndexError, TypeError):
continue
# 再检查左侧
left_x = optimal_x_scaled - offset
if left_x >= x1:
try:
px = pix[left_x, check_y]
if isinstance(px, tuple) and len(px) >= 3:
if (
190 <= px[0] <= 206
and 173 <= px[1] <= 196
and 234 <= px[2] <= 250
):
found_x = left_x
break
except (IndexError, TypeError):
continue
# 如果还是没找到,回退到原来的方法(从右往左)
if found_x is None:
for x_scaled in range(x2 - 1, x1 - 1, -1):
try:
px = pix[x_scaled, check_y]
if isinstance(px, tuple) and len(px) >= 3:
if (
190 <= px[0] <= 206
and 173 <= px[1] <= 196
and 234 <= px[2] <= 250
):
found_x = x_scaled
break
except (IndexError, TypeError):
continue
if found_x is not None:
detected_x = int(found_x * 1080.0 / max(1, self.rx))
# 保存找到的位置作为下次的最优位置
try:
self.analyser.last_fudai_x = detected_x
except Exception:
pass
elapsed = (time.time() - start_time) * 1000
try:
self.analyser._log_kv(
"[福袋查找] 像素检测",
f"{region['name']}命中(回退模式,从最优位置搜索)",
elapsed_ms=round(elapsed, 2),
x=detected_x,
)
except Exception:
pass
return detected_x
if found_x is not None:
break # 找到后退出offset_y循环
except Exception:
pass
return None
def detect_by_template(self, pic: Image.Image, region: Dict) -> Optional[int]:
"""使用模板匹配在指定区域查找福袋图标(优化版:多方法+预处理)
参数:
pic: PIL Image对象
region: 搜索区域配置
返回:
检测到的X坐标(1080基准),未找到返回None
"""
start_time = time.time()
try:
import cv2
import numpy as np
except ImportError:
return None
# 获取模板路径
base_dir = os.path.dirname(__file__)
template_path = os.path.join(base_dir, "assets", "fudai.png")
if not os.path.exists(template_path):
return None
# 获取匹配阈值
try:
threshold = float(
getattr(self.analyser, "fudai_template_match_threshold", 0.6) or 0.6
)
except Exception:
threshold = 0.6 # 降低默认阈值,因为添加了多种匹配方法
# 读取模板(彩色和灰度)
template_color = cv2.imread(template_path, cv2.IMREAD_COLOR)
template_gray = cv2.imread(template_path, cv2.IMREAD_GRAYSCALE)
if template_color is None or template_gray is None:
return None
width, height = pic.size
pic_rgb = pic.convert("RGB")
screenshot_bgr = cv2.cvtColor(np.array(pic_rgb), cv2.COLOR_RGB2BGR)
# 提取搜索区域(扩大Y范围用于模板匹配,提高成功率)
if "y_top" in region:
# 中间区域(矩形)
x1 = max(0, min(region.get("x1", 0), width))
x2 = max(0, min(region.get("x2", width), width))
y_top = max(0, min(region.get("y_top", 0), height))
y_bottom = max(0, min(region.get("y_bottom", height), height))
else:
# 水平带区域:扩大Y范围用于模板匹配(从7像素扩大到30像素)
x1 = max(0, min(region.get("x1", 0), width))
x2 = max(0, min(region.get("x2", width), width))
scan_y = region.get("y", 0)
y_range = region.get("y_range", 7)
# 模板匹配需要更大的Y范围
template_match_y_range = max(30, y_range * 4) # 至少30像素
y_top = max(0, scan_y - template_match_y_range // 2)
y_bottom = min(height, scan_y + template_match_y_range // 2 + 1)
if x1 >= x2 or y_top >= y_bottom:
return None
search_roi_bgr = screenshot_bgr[y_top:y_bottom, x1:x2] # type: ignore[index]
if search_roi_bgr.size == 0:
return None
# 转换为灰度图(用于灰度匹配)
search_roi_gray = cv2.cvtColor(search_roi_bgr, cv2.COLOR_BGR2GRAY)
# 图像预处理:增强对比度和去噪
try:
# 直方图均衡化(增强对比度)
search_roi_gray_enhanced = cv2.equalizeHist(search_roi_gray)
template_gray_enhanced = cv2.equalizeHist(template_gray)
except Exception:
search_roi_gray_enhanced = search_roi_gray
template_gray_enhanced = template_gray
th, tw = template_color.shape[:2]
rh, rw = search_roi_bgr.shape[:2]
# 多尺度+多方法模板匹配
best_score = -1.0
best_x = None
best_scale = 1.0
best_method = ""
# 缩放比例范围:0.7 到 1.3(扩大范围)
scales = [1.0, 0.9, 1.1, 0.8, 1.2, 0.7, 1.3]
# 多种匹配方法(按优先级排序)
match_methods = [
(cv2.TM_CCOEFF_NORMED, "CCOEFF_NORMED"), # 归一化相关系数(最常用)
(cv2.TM_CCORR_NORMED, "CCORR_NORMED"), # 归一化相关匹配
(cv2.TM_SQDIFF_NORMED, "SQDIFF_NORMED"), # 归一化平方差(值越小越好)
]
for scale in scales:
scaled_tw = max(1, int(tw * scale))
scaled_th = max(1, int(th * scale))
# 如果缩放后的模板太大,跳过
if scaled_th > rh or scaled_tw > rw:
continue
# 缩放模板(彩色和灰度)
scaled_template_color = cv2.resize(template_color, (scaled_tw, scaled_th))
scaled_template_gray = cv2.resize(template_gray, (scaled_tw, scaled_th))
scaled_template_gray_enhanced = cv2.resize(template_gray_enhanced, (scaled_tw, scaled_th))
# 尝试多种匹配方法
for method, method_name in match_methods:
try:
# 方法1:彩色匹配
res_color = cv2.matchTemplate(search_roi_bgr, scaled_template_color, method)
if method == cv2.TM_SQDIFF_NORMED:
_min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res_color)
score = 1.0 - max_val # SQDIFF值越小越好,转换为越大越好
else:
_min_val, max_val, _min_loc, max_loc = cv2.minMaxLoc(res_color)
score = max_val
if score > best_score:
best_score = score
best_x = max_loc[0]
best_scale = scale
best_method = f"{method_name}_color"
# 方法2:灰度匹配(更稳定,不受光照影响)
res_gray = cv2.matchTemplate(search_roi_gray, scaled_template_gray, method)
if method == cv2.TM_SQDIFF_NORMED:
_min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res_gray)
score = 1.0 - max_val
else:
_min_val, max_val, _min_loc, max_loc = cv2.minMaxLoc(res_gray)
score = max_val
if score > best_score:
best_score = score
best_x = max_loc[0]
best_scale = scale
best_method = f"{method_name}_gray"
# 方法3:增强对比度后的灰度匹配
res_gray_enhanced = cv2.matchTemplate(search_roi_gray_enhanced, scaled_template_gray_enhanced, method)
if method == cv2.TM_SQDIFF_NORMED:
_min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res_gray_enhanced)
score = 1.0 - max_val
else:
_min_val, max_val, _min_loc, max_loc = cv2.minMaxLoc(res_gray_enhanced)
score = max_val
if score > best_score:
best_score = score
best_x = max_loc[0]
best_scale = scale
best_method = f"{method_name}_gray_enhanced"
except Exception:
continue
# 如果所有方法都失败,回退到单尺度单方法匹配
if best_score < 0:
# 如果模板太大,缩小模板
if th > rh or tw > rw:
scale = min(rh / max(th, 1), rw / max(tw, 1))
if scale > 0:
template_color = cv2.resize(
template_color, (max(1, int(tw * scale)), max(1, int(th * scale)))
)
template_gray = cv2.resize(
template_gray, (max(1, int(tw * scale)), max(1, int(th * scale)))
)
th, tw = template_color.shape[:2]
if th <= rh and tw <= rw:
try:
res = cv2.matchTemplate(search_roi_bgr, template_color, cv2.TM_CCOEFF_NORMED)
_min_val, max_val, _min_loc, max_loc = cv2.minMaxLoc(res)
best_score = max_val
best_x = max_loc[0]
best_scale = 1.0
best_method = "CCOEFF_NORMED_color_fallback"
except Exception:
best_score = -1.0
best_x = None
best_scale = 1.0
best_method = ""
else:
best_score = -1.0
best_x = None
best_scale = 1.0
best_method = ""
max_val = best_score
max_loc = (best_x, 0) if best_x is not None else (0, 0)
elapsed = (time.time() - start_time) * 1000
try:
self.analyser._log_kv(
"[福袋查找] 模板匹配",
f"{region['name']}",
elapsed_ms=round(elapsed, 2),
score=round(max_val, 4),
threshold=threshold,
passed=bool(max_val >= threshold),
best_scale=round(best_scale, 2) if best_score >= 0 else None,
best_method=best_method if best_method else None,
)
except Exception:
pass
# 保存模板匹配的调试图像(无论成功与否)
try:
import douyin_screenshot_manager
from PIL import Image as PILImage
# 保存搜索区域图像
search_roi_pil = PILImage.fromarray(
cv2.cvtColor(search_roi_bgr, cv2.COLOR_BGR2RGB)
)
# 如果匹配成功,在图像上标注匹配位置
if max_val >= threshold and best_x is not None:
try:
from PIL import ImageDraw
annotated_roi = search_roi_pil.copy()
draw = ImageDraw.Draw(annotated_roi)
best_tw = int(tw * best_scale) if best_score >= 0 else tw
best_th = int(th * best_scale) if best_score >= 0 else th
# 绘制匹配框
match_x1 = best_x
match_y1 = 0
match_x2 = best_x + best_tw
match_y2 = best_th
rect_coords: Tuple[float, float, float, float] = (
float(match_x1), float(match_y1), float(match_x2), float(match_y2)
)
draw.rectangle(rect_coords, outline="green", width=2)
search_roi_pil = annotated_roi
except Exception:
pass
debug_path = douyin_screenshot_manager.save_debug_image(
search_roi_pil,
operation_type="template_match",
picname="fudai_icon",
step="matched" if max_val >= threshold else "failed",
coords=(x1, y_top, x2, y_bottom),
extra_info={
"region": region.get("name", "unknown"),
"score": round(max_val, 4),
"threshold": threshold,
"passed": bool(max_val >= threshold),
"best_scale": round(best_scale, 2) if best_score >= 0 else None,
"template_size": f"{tw}x{th}",
"search_size": f"{rw}x{rh}",
}
)
if debug_path:
try:
self.analyser._log_kv(
"[调试图像]",
f"模板匹配{'成功' if max_val >= threshold else '失败'}",
path=debug_path,
region=region.get("name", "unknown"),
score=round(max_val, 4),
threshold=threshold
)
except Exception:
pass
except Exception:
pass
if max_val >= threshold:
detected_x_in_roi = max_loc[0]
# 使用最佳匹配时的模板宽度计算中心点
best_tw = int(tw * best_scale) if best_score >= 0 else tw
detected_x = int(
(x1 + detected_x_in_roi + best_tw // 2) * 1080.0 / max(1, self.rx)
)
return detected_x
return None
def detect(self, pic: Image.Image) -> Tuple[Optional[int], str]:
"""检测福袋图标位置(按优先级搜索,像素检测后需模板匹配确认)
参数:
pic: PIL Image对象
返回:
(检测到的X坐标(1080基准),检测方法) 或 (None, "")
检测方法: "pixel" 或 "template"
"""
overall_start = time.time()
# 检查是否启用模板匹配
try:
template_match_enabled = bool(
getattr(self.analyser, "fudai_template_match_enabled", False)
)
except Exception:
template_match_enabled = False # 默认关闭模板匹配
# 按优先级搜索
regions_checked = []
for region in self.search_regions:
regions_checked.append(region["name"])
# 优先使用像素检测(更快)
detected_x = self.detect_by_pixel(pic, region)
if detected_x is not None:
# 如果模板匹配已关闭,直接返回像素检测结果
if not template_match_enabled:
overall_elapsed = (time.time() - overall_start) * 1000
try:
self.analyser._log_kv(
"[福袋查找] 检测完成",
"像素检测命中(模板匹配已关闭)",
total_elapsed_ms=round(overall_elapsed, 2),
region=region["name"],
x=detected_x,
method="pixel",
note="模板匹配已禁用,直接使用像素检测结果",
)
except Exception:
pass
return detected_x, "pixel"
# 像素检测命中后,使用模板匹配确认
# 在整个检测区域内进行模板匹配,而不是只在检测位置附近
# 这样可以处理像素检测位置偏差的情况
try:
width, height = pic.size
# 确定Y范围(与检测区域相同)
if "y_top" in region and "y_bottom" in region:
confirm_y_top = max(0, min(int(region.get("y_top", 0)), height))
confirm_y_bottom = max(0, min(int(region.get("y_bottom", height)), height))
else:
scan_y = region.get("y", 0)
y_range = region.get("y_range", 7)
confirm_y_top = max(0, scan_y - y_range // 2)
confirm_y_bottom = min(height, scan_y + y_range // 2 + 1)
# 使用整个检测区域进行模板匹配确认(而不是只在检测位置附近)
# 这样可以处理像素检测位置偏差的情况
confirm_region = {
"name": f"{region['name']}-模板确认",
"x1": max(0, min(region.get("x1", 0), width)),
"x2": max(0, min(region.get("x2", width), width)),
"y_top": confirm_y_top,
"y_bottom": confirm_y_bottom,
}
# 进行模板匹配确认(在整个检测区域内搜索)
template_confirmed_x = self.detect_by_template(pic, confirm_region)
if template_confirmed_x is not None:
# 模板匹配确认成功
overall_elapsed = (time.time() - overall_start) * 1000
try:
self.analyser._log_kv(
"[福袋查找] 检测完成",
"像素检测+模板确认命中",
total_elapsed_ms=round(overall_elapsed, 2),
region=region["name"],
pixel_x=detected_x,
template_x=template_confirmed_x,
method="pixel+template",
)
except Exception:
pass
# 使用模板匹配确认的X坐标(更准确)
return template_confirmed_x, "pixel+template"
else:
# 模板匹配未确认,检查是否应该信任像素检测结果
# 检查配置:是否允许在模板匹配失败时信任像素检测
try:
trust_pixel_when_template_fails = bool(
getattr(self.analyser, "fudai_trust_pixel_when_template_fails", True)
)
except Exception:
trust_pixel_when_template_fails = True # 默认信任
# 如果配置允许,且像素检测在可信区域命中,信任像素检测
if trust_pixel_when_template_fails:
if region.get("name") in ["实际福袋区域", "最热门区域", "扩展热门区域"]:
# 信任像素检测结果
overall_elapsed = (time.time() - overall_start) * 1000
try:
self.analyser._log_kv(
"[福袋查找] 检测完成",
"像素检测命中(模板确认失败但信任像素检测)",
total_elapsed_ms=round(overall_elapsed, 2),
region=region["name"],
x=detected_x,
method="pixel",
note="模板匹配失败但像素检测在可信区域命中,信任像素检测结果",
)
except Exception:
pass
return detected_x, "pixel"
# 不信任,继续搜索下一个区域
try:
self.analyser._log_kv(
"[福袋查找] 像素检测命中但模板确认失败",
f"{region['name']}",
pixel_x=detected_x,
confirm_region=f"({confirm_region['x1']},{confirm_y_top})-({confirm_region['x2']},{confirm_y_bottom})",
说明="在整个检测区域内未找到模板匹配,继续搜索",
)
except Exception:
pass
continue # 继续搜索下一个区域
except Exception as e:
# 模板确认过程异常,信任像素检测结果
try:
self.analyser._log_kv(
"[福袋查找] 模板确认异常",
f"{region['name']}",
pixel_x=detected_x,
error=str(e),
说明="信任像素检测结果",
)
except Exception:
pass
overall_elapsed = (time.time() - overall_start) * 1000
try:
self.analyser._log_kv(
"[福袋查找] 检测完成",
"像素检测命中(模板确认异常)",
total_elapsed_ms=round(overall_elapsed, 2),
region=region["name"],
x=detected_x,
method="pixel",
)
except Exception:
pass
return detected_x, "pixel"
# 像素检测未命中时,使用模板匹配(仅对中间区域,且模板匹配已启用)
if template_match_enabled and region.get("name") == "中间区域":
detected_x = self.detect_by_template(pic, region)
if detected_x is not None:
overall_elapsed = (time.time() - overall_start) * 1000
try:
self.analyser._log_kv(
"[福袋查找] 检测完成",
"模板匹配命中",
total_elapsed_ms=round(overall_elapsed, 2),
region=region["name"],
x=detected_x,
method="template",
)
except Exception:
pass
return detected_x, "template"
else:
# 记录像素检测未命中的区域(非中间区域)
try:
pixel_elapsed = (time.time() - overall_start) * 1000
self.analyser._log_kv(
"[福袋查找] 像素检测",
f"{region['name']}未命中",
elapsed_ms=round(pixel_elapsed, 2),
region=region["name"],
)
except Exception:
pass
overall_elapsed = (time.time() - overall_start) * 1000
try:
self.analyser._log_kv(
"[福袋查找] 检测完成",
"未找到",
total_elapsed_ms=round(overall_elapsed, 2),
checked_regions=", ".join(regions_checked),
total_regions=len(regions_checked),
)
except Exception:
pass
return None, ""
class FudaiClickExecutor:
"""福袋点击执行器:负责点击福袋图标"""
def __init__(self, analyser: Any):
self.analyser = analyser
self._init_resolution()
def _init_resolution(self):
"""初始化分辨率信息"""
try:
self.rx = int(getattr(self.analyser, "resolution_ratio_x", 1080) or 1080)
self.ry = int(getattr(self.analyser, "resolution_ratio_y", 2400) or 2400)
except Exception:
self.rx, self.ry = 1080, 2400
def click(self, fudai_x: int) -> bool:
"""点击福袋图标(点击前会检查是否是红包,避免误点)
参数:
fudai_x: 福袋图标的X坐标(1080基准)
返回:
是否点击成功
"""
start_time = time.time()
# 计算点击坐标
base_y = 403 # 与检测时使用的同一水平线
click_x = int(fudai_x * self.rx / 1080)
click_y = int(base_y * self.ry / 2400)
try:
self.analyser._log_kv(
"[福袋查找] 点击",
"开始",
fudai_x=fudai_x,
click_x=click_x,
click_y=click_y,
)
except Exception:
pass
# 点击前检查:避免误点红包
try:
# 检查是否有红包弹窗
import douyin_popups
has_redpacket = douyin_popups.check_shopping_redpacket_popup(self.analyser)
if has_redpacket:
try:
self.analyser._log_kv(
"[福袋查找] 点击",
"跳过(检测到红包弹窗)",
fudai_x=fudai_x,
note="检测到红包弹窗,避免误点红包",
)
except Exception:
pass
return False
# 快速检查点击位置附近是否有红包特征(通过OCR)
# 检查点击位置上方和下方的区域,看是否有红包关键词
try:
# 截图点击位置附近的区域(扩大范围以包含可能的红包文字)
check_x1 = max(0, click_x - 150)
check_x2 = min(self.rx, click_x + 150)
check_y1 = max(0, click_y - 200)
check_y2 = min(self.ry, click_y + 200)
# 转换为1080基准坐标
check_x1_base = int(check_x1 * 1080 / self.rx)
check_y1_base = int(check_y1 * 2400 / self.ry)
check_x2_base = int(check_x2 * 1080 / self.rx)
check_y2_base = int(check_y2 * 2400 / self.ry)
# 截图检查区域
self.analyser.cut_pic(
check_x1_base, check_y1_base,
check_x2_base, check_y2_base,
"", "檢查紅包特徵"
)
# OCR识别
check_text = str(
self.analyser.operation.analyse_pic_word("檢查紅包特徵", 0) or ""
).strip()
# 检查是否包含红包关键词
redpacket_keywords = ("最高金额", "开抢", "邀你抢", "抢红包", "购物红包", "手气红包")
if check_text and any(kw in check_text for kw in redpacket_keywords):
matched_keywords = [kw for kw in redpacket_keywords if kw in check_text]
try:
self.analyser._log_kv(
"[福袋查找] 点击",
"跳过(检测到红包特征)",
fudai_x=fudai_x,
matched_keywords=matched_keywords,
check_text_preview=check_text[:50],
note="点击位置附近检测到红包特征文字,避免误点红包",
)
except Exception:
pass
return False
except Exception as e:
# OCR检查失败不影响点击,继续执行
try:
self.analyser._log_kv(
"[福袋查找] 点击",
"红包特征检查异常,继续点击",
error=str(e),
error_type=type(e).__name__,
)
except Exception:
pass
except Exception:
# 红包检查失败不影响点击,继续执行
pass
# 执行点击
try:
device_id = getattr(self.analyser, "device_id", "")
self.analyser.operation.click(device_id, click_x, click_y)
# 点击后等待足够时间让详情页加载(避免连续点击导致详情页被关闭)
# 默认等待2.5秒,确保详情页完全打开并稳定
click_wait_time = float(
getattr(self.analyser, "fudai_click_wait_seconds", 2.5) or 2.5
)
if click_wait_time < 1.5:
click_wait_time = 1.5 # 最小1.5秒
self.analyser.operation.delay(click_wait_time)
elapsed = (time.time() - start_time) * 1000
try:
self.analyser._log_kv(
"[福袋查找] 点击",
"成功",
elapsed_ms=round(elapsed, 2),
wait_seconds=click_wait_time
)
except Exception:
pass
return True
except Exception:
# 回退到analyser.click
try:
self.analyser.click(fudai_x, base_y)
# 回退模式也需要等待
click_wait_time = float(
getattr(self.analyser, "fudai_click_wait_seconds", 1.5) or 1.5
)
if click_wait_time < 1.0:
click_wait_time = 1.0
self.analyser.operation.delay(click_wait_time)
elapsed = (time.time() - start_time) * 1000
try:
self.analyser._log_kv(
"[福袋查找] 点击",
"成功(回退模式)",
elapsed_ms=round(elapsed, 2),
wait_seconds=click_wait_time
)
except Exception:
pass
return True
except Exception:
elapsed = (time.time() - start_time) * 1000
try:
self.analyser._log_kv(
"[福袋查找] 点击",
"失败",
elapsed_ms=round(elapsed, 2),
error="点击异常",
)
except Exception:
pass
return False
class FudaiDetailVerifier:
"""福袋详情页验证器:验证是否成功打开详情页"""
def __init__(self, analyser: Any):
self.analyser = analyser
def verify(self, max_wait_seconds: float = 2.0) -> bool:
"""验证是否成功打开福袋详情页
参数:
max_wait_seconds: 最大等待时间(秒)
返回:
是否成功打开详情页
"""
start_time = time.time()
try:
import douyin_pixel
# 等待界面加载(增加等待时间,确保详情页完全加载)
# 点击后已经等待了2.5秒,这里再等待0.8秒,总共约3.3秒
verify_initial_wait = float(
getattr(self.analyser, "fudai_verify_initial_wait_seconds", 0.8) or 0.8
)
if verify_initial_wait < 0.5:
verify_initial_wait = 0.5 # 最小0.5秒
self.analyser.operation.delay(verify_initial_wait)
# 多次检查,直到超时或检测到详情页
check_interval = 0.3
checks = 0
max_checks = int(max_wait_seconds / check_interval) + 1
while checks < max_checks:
# 刷新截图
try:
self.analyser.operation.get_screenshot(self.analyser.device_id)
except Exception:
pass
# 检查是否在详情页(已移除任务数量检测,仅使用OCR检测)
# 额外检测:在指定区域检测"邀你抢红包"文字
# 区域:(238,454) 到 (847,545) - 基准坐标(1080x2400)
try:
y_pianyi = int(getattr(self.analyser, "y_pianyi", 0) or 0)
# 保存完整截图并标注OCR区域
try:
import douyin_screenshot_manager
pic_path = path_config.get_screenshot_path()
if os.path.exists(pic_path):
pic = Image.open(pic_path)
pic.load()
pic_rgb = pic.convert("RGB")
width, height = pic_rgb.size
rx = int(getattr(self.analyser, "resolution_ratio_x", 1080) or 1080)
ry = int(getattr(self.analyser, "resolution_ratio_y", 2400) or 2400)
x1_base, y1_base, x2_base, y2_base = 238, 454, 847, 545
x1 = int(x1_base * width / rx)
y1 = int((y1_base + y_pianyi) * height / ry)
x2 = int(x2_base * width / rx)
y2 = int((y2_base + y_pianyi) * 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,
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}",
"y_offset": y_pianyi,
}
)
if debug_path:
try:
self.analyser._log_kv(
"[调试图像]",
"OCR识别前保存完整截图(验证邀你抢红包区域)",
path=debug_path,
)
except Exception:
pass
except Exception:
pass
# cut_pic 方法内部会根据分辨率自动缩放,所以直接传入基准坐标
self.analyser.cut_pic(238, 454, 847, 545, "", "验证邀你抢红包区域", y_pianyi)
ocr_text = str(self.analyser.operation.analyse_pic_word("验证邀你抢红包区域", 0) or "")
# 如果模式0未识别到,尝试模式1
enable_ocr_mode1 = bool(getattr(self.analyser, "enable_ocr_mode1", False))
if enable_ocr_mode1 and (not ocr_text or not any(kw in ocr_text for kw in ("邀你抢", "抢红包", "抢手气"))):
ocr_text = str(self.analyser.operation.analyse_pic_word("验证邀你抢红包区域", 1) or "")
# 检测是否包含"邀你抢红包"相关关键词(支持变体)
# 关键词包括:"邀你抢红包"、"邀你抢手气红包"、"邀你抢"等
redpacket_keywords = ("邀你抢红包", "邀你抢手气红包", "邀你抢", "抢红包", "抢手气")
if ocr_text and any(kw in ocr_text for kw in redpacket_keywords):
elapsed = (time.time() - start_time) * 1000
try:
self.analyser._log_kv(
"[福袋查找] 验证",
"成功打开详情页(检测到邀你抢红包)",
elapsed_ms=round(elapsed, 2),
checks=checks + 1,
ocr_text=ocr_text[:50],
)
except Exception:
pass
# 额外等待,确保参与按钮加载完成
# 验证时只检测了详情页是否打开,但参与按钮可能需要额外时间加载
button_wait_time = float(
getattr(self.analyser, "fudai_button_load_wait_seconds", 0.5) or 0.5
)
if button_wait_time > 0:
try:
self.analyser.operation.delay(button_wait_time)
except Exception:
time.sleep(button_wait_time)
# 设置标志,表示详情页已经验证过,后续读取详情时可以缩短等待时间
# 同时设置重试标志,如果任务数为0可以重试一次
try:
self.analyser._fudai_detail_verified = True
self.analyser._fudai_detail_verified_retry = True
except Exception:
pass
return True
except Exception:
# OCR检测失败不影响其他检测方式
pass
checks += 1
if checks < max_checks:
self.analyser.operation.delay(check_interval)
elapsed = (time.time() - start_time) * 1000
try:
self.analyser._log_kv(
"[福袋查找] 验证",
"未检测到详情页",
elapsed_ms=round(elapsed, 2),
checks=checks,
)
except Exception:
pass
return False
except Exception as e:
elapsed = (time.time() - start_time) * 1000
try:
self.analyser._log_kv(
"[福袋查找] 验证",
"异常",
elapsed_ms=round(elapsed, 2),
error=str(e),
error_type=type(e).__name__,
)
except Exception:
pass
return False
class FudaiFinder:
"""福袋查找器:统一管理查找流程"""
def __init__(self, analyser: Any):
self.analyser = analyser
self.acquirer = FudaiImageAcquirer(analyser)
self.detector = FudaiDetector(analyser)
self.executor = FudaiClickExecutor(analyser)
self.verifier = FudaiDetailVerifier(analyser)
# 配置参数
self._init_config()
def _init_config(self):
"""初始化配置参数"""
try:
self.retry_delay_s = float(
getattr(self.analyser, "fudai_icon_retry_delay_seconds", 0.6) or 0.6
)
except Exception:
self.retry_delay_s = 0.6
try:
self.max_wait_s = float(
getattr(self.analyser, "fudai_icon_max_wait_seconds", 5.0) or 5.0
)
except Exception:
self.max_wait_s = 5.0
try:
self.verify_enabled = bool(
getattr(self.analyser, "fudai_click_verify_enabled", True)
)
except Exception:
self.verify_enabled = True
try:
self.max_retries = int(
getattr(self.analyser, "fudai_find_max_retries", 3) or 3
)
except Exception:
self.max_retries = 3
# 确保参数合理
if self.retry_delay_s < 0.1:
self.retry_delay_s = 0.1
if self.max_wait_s < 0:
self.max_wait_s = 0.0
# 防止无限等待:最大等待时间限制为30秒
if self.max_wait_s > 30.0:
self.max_wait_s = 30.0
def find_and_open(
self, screenshot_ready: bool = False, verify_after_click: bool = True
) -> Tuple[bool, Optional[int]]:
"""查找并打开福袋详情页
参数:
screenshot_ready: 是否已有可用截图
verify_after_click: 点击后是否验证
返回:
(是否成功, 福袋X坐标)
"""
overall_start = time.time()
try:
self.analyser._log_kv(
"[福袋查找] 开始",
"查找福袋图标",
max_wait_s=self.max_wait_s,
retry_delay_s=self.retry_delay_s,
screenshot_ready=screenshot_ready,
)
except Exception:
pass
# 主循环:按时间限制重试
loop = 0
max_loops = int(self.max_wait_s / self.retry_delay_s) + 10 # 额外的循环次数保护
consecutive_failures = 0 # 连续失败计数
max_consecutive_failures = 5 # 最大连续失败次数
while (time.time() - overall_start) < self.max_wait_s:
# 额外的循环次数保护:防止无限循环
if loop >= max_loops:
try:
self.analyser._log_kv(
"[福袋查找] 警告",
"达到最大循环次数限制,提前退出",
max_loops=max_loops,
elapsed_s=round(time.time() - overall_start, 2),
)
except Exception:
pass
break
# 1. 获取截图
pic = self.acquirer.acquire_screenshot(
force_refresh=not (screenshot_ready and loop == 0)
)
if pic is None:
loop += 1
consecutive_failures += 1
# 如果连续失败太多次,提前退出
if consecutive_failures >= max_consecutive_failures:
try:
self.analyser._log_kv(
"[福袋查找] 警告",
"连续截图失败次数过多,提前退出",
consecutive_failures=consecutive_failures,
elapsed_s=round(time.time() - overall_start, 2),
)
except Exception:
pass
break
elapsed = time.time() - overall_start
if elapsed < self.max_wait_s:
remaining_time = self.max_wait_s - elapsed
sleep_time = min(self.retry_delay_s, remaining_time)
if sleep_time > 0:
self.analyser.operation.delay(sleep_time)
continue
# 保存完整截图用于调试(检测前)
try:
import douyin_screenshot_manager
pic_rgb_full = pic.convert("RGB")
# 获取检测区域信息用于标注
try:
first_region = self.detector.search_regions[0] if self.detector.search_regions else None
if first_region:
if "y_top" in first_region:
coords = (
first_region.get("x1", 0),
first_region.get("y_top", 0),
first_region.get("x2", 0),
first_region.get("y_bottom", 0),
)
else:
scan_y = first_region.get("y", 0)
y_range = first_region.get("y_range", 7)
coords = (
first_region.get("x1", 0),
max(0, scan_y - y_range // 2),
first_region.get("x2", 0),
scan_y + y_range // 2 + 1,
)
else:
coords = None
except Exception:
coords = None
debug_path = douyin_screenshot_manager.save_debug_image(
pic_rgb_full,
operation_type="pixel_detect",
picname="fudai_icon",
step="before_detect",
coords=coords,
extra_info={
"loop": loop,
"method": "optimized_finder",
"resolution": f"{self.detector.rx}x{self.detector.ry}",
"y_offset": self.detector.y_pianyi,
}
)
if debug_path:
try:
self.analyser._log_kv(
"[调试图像]",
"福袋图标检测前保存完整截图(优化查找器)",
path=debug_path,
loop=loop
)
except Exception:
pass
except Exception:
pass
# 2. 检测福袋图标
detected_x, detect_method = self.detector.detect(pic)
if detected_x is not None:
# 重置连续失败计数
consecutive_failures = 0
# 保存完整截图(检测成功)
try:
import douyin_screenshot_manager
pic_rgb_full = pic.convert("RGB")
# 获取检测区域信息用于标注
try:
# 找到检测成功的区域
detected_region = None
for region in self.detector.search_regions:
if "y_top" in region:
x1 = region.get("x1", 0)
x2 = region.get("x2", 0)
y_top = region.get("y_top", 0)
y_bottom = region.get("y_bottom", 0)
else:
scan_y = region.get("y", 0)
y_range = region.get("y_range", 7)
x1 = region.get("x1", 0)
x2 = region.get("x2", 0)
y_top = max(0, scan_y - y_range // 2)
y_bottom = scan_y + y_range // 2 + 1
# 检查detected_x是否在这个区域内
detected_x_scaled = detected_x * self.detector.rx // 1080
if x1 <= detected_x_scaled <= x2:
detected_region = (x1, y_top, x2, y_bottom)
break
coords = detected_region
except Exception:
coords = None
debug_path = douyin_screenshot_manager.save_debug_image(
pic_rgb_full,
operation_type="pixel_detect",
picname="fudai_icon",
step="detected",
coords=coords,
extra_info={
"detected_x": detected_x,
"method": detect_method,
"loop": loop,
"resolution": f"{self.detector.rx}x{self.detector.ry}",
"result": "success"
}
)
if debug_path:
try:
self.analyser._log_kv(
"[调试图像]",
"福袋图标检测成功保存完整截图(优化查找器)",
path=debug_path,
detected_x=detected_x,
method=detect_method
)
except Exception:
pass
except Exception:
pass
# 3. 点击福袋图标
click_success = self.executor.click(detected_x)
if click_success:
# 点击成功后直接返回,不进行验证
# 让主循环进行阶段检测,判断是否成功打开详情页
overall_elapsed = (time.time() - overall_start) * 1000
try:
self.analyser._log_kv(
"[福袋查找] 完成",
"成功(已点击,等待阶段检测)",
total_elapsed_ms=round(overall_elapsed, 2),
loops=loop + 1,
x=detected_x,
method=detect_method,
)
except Exception:
pass
# 记录首次找到福袋的时间
self._record_find_fudai_time(detected_x)
return True, detected_x
loop += 1
consecutive_failures += 1
# 如果连续失败太多次,提前退出(避免无效循环)
if consecutive_failures >= max_consecutive_failures:
try:
overall_elapsed = (time.time() - overall_start) * 1000
self.analyser._log_kv(
"[福袋查找] 警告",
"连续检测失败次数过多,提前退出",
consecutive_failures=consecutive_failures,
loop=loop,
total_elapsed_ms=round(overall_elapsed, 2),
)
except Exception:
pass
break
# 检查是否还有剩余时间
elapsed = time.time() - overall_start
if elapsed < self.max_wait_s:
remaining_time = self.max_wait_s - elapsed
sleep_time = min(self.retry_delay_s, remaining_time)
if sleep_time > 0:
self.analyser.operation.delay(sleep_time)
else:
# 时间已到,退出循环
break
# 未找到
overall_elapsed = (time.time() - overall_start) * 1000
try:
self.analyser._log_kv(
"[福袋查找] 完成",
"未找到",
total_elapsed_ms=round(overall_elapsed, 2),
loops=loop,
max_wait_s=self.max_wait_s,
)
except Exception:
pass
return False, None
def _record_find_fudai_time(self, detected_x: int):
"""记录首次找到福袋的时间"""
try:
enter_ts = float(getattr(self.analyser, "_enter_live_ts", 0.0) or 0.0)
if enter_ts > 0:
dt = time.time() - enter_ts
if not getattr(self.analyser, "_enter_live_fudai_logged", False):
try:
self.analyser._enter_live_fudai_logged = True
except Exception:
pass
try:
reason = str(
getattr(self.analyser, "_enter_live_reason", "") or ""
)
except Exception:
reason = ""
try:
self.analyser._log_kv(
"性能",
"进入直播间后找到福袋耗时",
seconds=round(dt, 2),
enter_reason=reason,
fudai_x=detected_x,
)
except Exception:
pass
except Exception:
pass
def check_have_fudai_with_ready_screenshot_optimized(
analyser: Any, screenshot_ready: bool = False
) -> int:
"""优化的福袋查找函数(兼容原接口,仅检测不点击)
参数:
analyser: 分析器对象
screenshot_ready: 是否已有可用截图
返回:
福袋X坐标(1080基准),未找到返回False
"""
# 只检测,不点击(避免与 find_and_open_fudai_detail 重复点击)
try:
acquirer = FudaiImageAcquirer(analyser)
pic = acquirer.acquire_screenshot(force_refresh=not screenshot_ready)
if pic is None:
return False
detector = FudaiDetector(analyser)
detected_x, detect_method = detector.detect(pic)
if detected_x is not None:
return int(detected_x)
return False
except Exception:
return False