Files
douyi_guaji_main/douyin/detection/detection_pkg/finder_numpy_scan.py
T

170 lines
6.0 KiB
Python

"""douyin/detection/detection_pkg/finder_numpy_scan - numpy 加速福袋图标像素扫描
把 check_have_fudai_with_ready_screenshot 中"numpy 加速扫描水平带"段抽出来。
逻辑:在 ROI 内根据预设颜色范围生成像素掩码,按列聚合,从最优起始列向两侧
交替搜索第一个匹配列,命中即返回检测到的 x 坐标(1080 基准)。
入口:scan_with_numpy(analyser, pic_new, rx, ry, y_pianyi_val, optimal_x_scaled)
返回:(detected: bool, detected_x: Optional[int])
"""
from __future__ import annotations
import time
from typing import Any, Optional, Tuple
def scan_with_numpy(
analyser: Any,
pic_new,
rx: int,
ry: int,
y_pianyi_val: int,
optimal_x_scaled: int,
) -> Tuple[bool, Optional[int]]:
"""numpy 加速扫描福袋图标的水平像素带。
Args:
analyser: 可读 last_fudai_x / 写 last_fudai_x / last_find_fudai_time
pic_new: PIL.Image,已加载的当前截图
rx, ry: 目标分辨率宽/高(默认 1080x2400 基准)
y_pianyi_val: Y 偏移
optimal_x_scaled: 最优起始列(设备分辨率坐标)
Returns:
(detected, detected_x) — 命中时 detected_x 是 1080 基准坐标;
失败/异常返回 (False, None),由 caller 切换到逐像素扫描。
"""
try:
import numpy as _np
except (ImportError, ValueError, IndexError, AttributeError):
return False, None
width, height = pic_new.size
try:
# 计算检测区域,考虑分辨率缩放和偏移。
# 新版福袋 icon 可能更靠左,numpy 快速扫描区域向左扩大。
y0 = 354 * ry // 2400 + y_pianyi_val
x1 = max(0, 120 * rx // 1080)
x2 = min(width, 410 * rx // 1080)
y_top = max(0, y0 - 3)
y_bottom = min(height, y0 + 4)
if not (x1 < x2 and y_top < y_bottom):
return False, None
roi = pic_new.crop((x1, y_top, x2, y_bottom)).convert("RGB")
# 双截图记录:保存实时操作截图(像素检测区域)
try:
from douyin.handlers import douyin_screenshot_manager
douyin_screenshot_manager.save_latest_attempt(
roi, operation_type="pixel_detect"
)
except Exception:
pass
arr = _np.array(roi)
if arr.size == 0:
return False, None
r = arr[:, :, 0] # type: ignore[index]
g = arr[:, :, 1] # type: ignore[index]
b = arr[:, :, 2] # type: ignore[index]
# 精度优化:扩大颜色范围,提高容错性(考虑不同屏幕色温和亮度)
# 原来的范围:R[193-203], G[176-193], B[237-247]
# 优化后:适度放宽范围,但保持中心值权重
mask_center = (
(r >= 190) & (r <= 206)
& (g >= 173) & (g <= 196)
& (b >= 234) & (b <= 250)
) # type: ignore[operator]
mask = mask_center
# 检查每一列是否有足够多的匹配像素(提高准确性,避免单个噪点误判)
cols = _np.sum(mask, axis=0)
# 至少需要 2 个像素匹配(避免单个噪点误判)
valid_cols = cols >= 2
if not valid_cols.any():
return False, None
# 从最优位置开始向左右两侧交替搜索
roi_width = x2 - x1
optimal_col = optimal_x_scaled - x1
optimal_col = max(0, min(optimal_col, roi_width - 1))
found_col: Optional[int] = 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:
# 兜底:从左到右第一个匹配
found_col = int(_np.argmax(valid_cols))
col_idx = found_col
# 1080 基准坐标
detected_x = int((x1 + col_idx) * 1080.0 / max(1, rx))
# 保存找到的位置作为下次的最优起点
try:
analyser.last_fudai_x = detected_x
except Exception:
pass
# 双截图记录:成功识别存档
try:
from douyin.handlers import douyin_screenshot_manager
douyin_screenshot_manager.save_last_success(
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=(x1, y_top, x2, y_bottom),
extra_info={
"detected_x": detected_x,
"detected_x_scaled": x1 + col_idx,
"scan_range": f"x:{x1}-{x2}, y:{y_top}-{y_bottom}",
"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
return True, detected_x
except (ImportError, ValueError, IndexError, AttributeError):
return False, None