将 183 行的"numpy 未命中时的原始逐像素检查"块抽到独立模块。 保留了原代码"找到后退出外层 while 循环"的 break 语义(在调用方 检查返回的 detected 后 break)。 finder.py: 726 → 553 行 (-173) 新增 finder_pixel_scan.py: 207 行 finder.py 跌破 600 行(1438 → 553,本会话累计削减 885 行 = -62%) 验证: - pytest 1013 passed - compile OK Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
208 lines
9.0 KiB
Python
208 lines
9.0 KiB
Python
"""find_fudai 辅助:逐像素扫描兜底
|
|
|
|
从 douyin/detection/detection_pkg/finder.py:check_have_fudai_with_ready_screenshot 中提取。
|
|
当 numpy 加速扫描未命中或不可用时,回退到原始逐像素检查。
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
import time
|
|
from typing import Any, Tuple
|
|
|
|
|
|
def pixel_scan_fallback(
|
|
analyser: Any,
|
|
detected: bool,
|
|
detected_x,
|
|
pic_new,
|
|
rx: int,
|
|
ry: int,
|
|
y_pianyi_val: int,
|
|
) -> Tuple[bool, Any]:
|
|
"""numpy 未命中或不可用时的原始逐像素检查兜底。
|
|
|
|
返回更新后的 (detected, detected_x)。
|
|
"""
|
|
if not detected:
|
|
try:
|
|
pix = pic_new.load()
|
|
pic_width, pic_height = pic_new.size
|
|
# 计算实际扫描范围,防止越界
|
|
scan_x_start = 41 * rx // 1080
|
|
scan_x_end = min(410 * rx // 1080, pic_width)
|
|
scan_y = 403 * ry // 2400 + y_pianyi_val
|
|
|
|
# 获取最优搜索起始位置(使用历史记录或默认值)
|
|
try:
|
|
# 尝试获取上次找到的福袋位置(1080基准)
|
|
last_fudai_x = int(getattr(analyser, "last_fudai_x", 0) or 0)
|
|
if (
|
|
last_fudai_x > 0
|
|
and scan_x_start <= last_fudai_x * rx // 1080 <= scan_x_end
|
|
):
|
|
optimal_x_scaled = last_fudai_x * rx // 1080
|
|
else:
|
|
# 使用默认最优位置:最热门区域中心(98-240的中心,约169)
|
|
optimal_x_scaled = 169 * rx // 1080
|
|
except Exception:
|
|
# 使用默认最优位置
|
|
optimal_x_scaled = 169 * rx // 1080
|
|
|
|
# 确保最优位置在搜索范围内
|
|
optimal_x_scaled = max(
|
|
scan_x_start, min(optimal_x_scaled, scan_x_end - 1)
|
|
)
|
|
|
|
# 确保y坐标在有效范围内
|
|
if 0 <= scan_y < pic_height:
|
|
# 精度优化:同时检查上下几行像素,提高检测准确性
|
|
for offset_y in [0, -1, 1, -2, 2]: # 检查当前行及上下2行
|
|
check_y = scan_y + offset_y
|
|
if not (0 <= check_y < pic_height):
|
|
continue
|
|
|
|
# 优化:从最优位置开始向左右两侧搜索
|
|
found_x = None
|
|
search_range = max(
|
|
optimal_x_scaled - scan_x_start,
|
|
scan_x_end - 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 < scan_x_end:
|
|
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 >= scan_x_start:
|
|
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(scan_x_start, scan_x_end):
|
|
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:
|
|
# 转换回1080基准坐标
|
|
detected_x = int(found_x * 1080.0 / max(1, rx))
|
|
detected = True
|
|
|
|
# 保存找到的位置作为下次的最优位置
|
|
try:
|
|
analyser.last_fudai_x = detected_x
|
|
except Exception:
|
|
pass
|
|
|
|
# 双截图记录:保存成功识别存档截图
|
|
try:
|
|
from douyin.handlers import douyin_screenshot_manager
|
|
|
|
detect_roi = pic_new.crop(
|
|
(
|
|
scan_x_start,
|
|
max(0, scan_y - 3),
|
|
scan_x_end,
|
|
min(pic_height, scan_y + 4),
|
|
)
|
|
).convert("RGB")
|
|
douyin_screenshot_manager.save_last_success(
|
|
detect_roi,
|
|
operation_type="pixel_detect",
|
|
)
|
|
|
|
# 保存完整截图(检测成功)
|
|
pic_rgb_full = pic_new.convert("RGB")
|
|
debug_path = douyin_screenshot_manager.save_debug_image(
|
|
pic_rgb_full,
|
|
operation_type="pixel_detect",
|
|
picname="fudai_icon",
|
|
step="detected",
|
|
coords=(
|
|
scan_x_start,
|
|
max(0, scan_y - 3),
|
|
scan_x_end,
|
|
min(pic_height, scan_y + 4),
|
|
),
|
|
extra_info={
|
|
"detected_x": detected_x,
|
|
"detected_x_scaled": found_x,
|
|
"scan_range": f"x:{scan_x_start}-{scan_x_end}, y:{scan_y}",
|
|
"resolution": f"{rx}x{ry}",
|
|
"result": "success",
|
|
},
|
|
)
|
|
if debug_path:
|
|
try:
|
|
analyser._log_kv(
|
|
"[调试图像]",
|
|
"福袋图标检测成功保存完整截图",
|
|
path=debug_path,
|
|
detected_x=detected_x,
|
|
)
|
|
except Exception:
|
|
pass
|
|
except Exception:
|
|
pass
|
|
|
|
try:
|
|
analyser.last_find_fudai_time = time.time()
|
|
except Exception:
|
|
pass
|
|
break # 找到后退出offset_y循环
|
|
|
|
except Exception:
|
|
# 像素检测失败,记录但不影响主流程
|
|
pass
|
|
|
|
|
|
return detected, detected_x
|