diff --git a/douyin/detection/detection_pkg/finder.py b/douyin/detection/detection_pkg/finder.py index f75e3e6..8495c03 100644 --- a/douyin/detection/detection_pkg/finder.py +++ b/douyin/detection/detection_pkg/finder.py @@ -269,133 +269,15 @@ def check_have_fudai_with_ready_screenshot( pass # 先尝试 numpy 加速扫描水平带(性能优化:批量处理) - try: - import numpy as _np - - # 计算检测区域,考虑分辨率缩放和偏移 - # 将像素检测的扫描带左上角改为图片坐标 x=157, y=354(右下不变) - y0 = 354 * ry // 2400 + y_pianyi_val - x1 = max(0, 157 * rx // 1080) - x2 = min(width, 410 * rx // 1080) - y_top = max(0, y0 - 3) - y_bottom = min(height, y0 + 4) - - # 边界检查 - if x1 < x2 and y_top < y_bottom: - 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: - 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 valid_cols.any(): - # 优化:从最优位置开始向左右两侧搜索 - roi_width = x2 - x1 - optimal_col = optimal_x_scaled - x1 # 转换为ROI内的列索引 - 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: - found_col = int(_np.argmax(valid_cols)) - - col_idx = found_col - # 计算检测到的x坐标:转换为1080基准坐标,确保精度 - detected_x = int((x1 + col_idx) * 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 - - 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 - except (ImportError, ValueError, IndexError, AttributeError): - # numpy不可用或计算错误,回退到逐像素检测 - detected = False + # 先尝试 numpy 加速扫描水平带(性能优化:批量处理) + from douyin.detection.detection_pkg.finder_numpy_scan import ( + numpy_band_scan, + ) + detected, detected_x = numpy_band_scan( + analyser, detected, detected_x, pic_new, + width, height, rx, ry, y_pianyi_val, optimal_x_scaled, + ) # numpy 未命中/不可用时回退到原始逐像素检查 from douyin.detection.detection_pkg.finder_pixel_scan import ( pixel_scan_fallback, diff --git a/douyin/detection/detection_pkg/finder_numpy_scan.py b/douyin/detection/detection_pkg/finder_numpy_scan.py new file mode 100644 index 0000000..1f630cd --- /dev/null +++ b/douyin/detection/detection_pkg/finder_numpy_scan.py @@ -0,0 +1,156 @@ +"""find_fudai 辅助:numpy 加速水平带扫描 + +从 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 numpy_band_scan( + analyser: Any, + detected: bool, + detected_x, + pic_new, + width: int, + height: int, + rx: int, + ry: int, + y_pianyi_val: int, + optimal_x_scaled: int, +) -> Tuple[bool, Any]: + """numpy 加速扫描水平带,查找福袋图标的特征颜色。 + + 返回更新后的 (detected, detected_x)。 + """ + try: + import numpy as _np + + # 计算检测区域,考虑分辨率缩放和偏移 + # 将像素检测的扫描带左上角改为图片坐标 x=157, y=354(右下不变) + y0 = 354 * ry // 2400 + y_pianyi_val + x1 = max(0, 157 * rx // 1080) + x2 = min(width, 410 * rx // 1080) + y_top = max(0, y0 - 3) + y_bottom = min(height, y0 + 4) + + # 边界检查 + if x1 < x2 and y_top < y_bottom: + 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: + 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 valid_cols.any(): + # 优化:从最优位置开始向左右两侧搜索 + roi_width = x2 - x1 + optimal_col = optimal_x_scaled - x1 # 转换为ROI内的列索引 + 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: + found_col = int(_np.argmax(valid_cols)) + + col_idx = found_col + # 计算检测到的x坐标:转换为1080基准坐标,确保精度 + detected_x = int((x1 + col_idx) * 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 + + 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 + except (ImportError, ValueError, IndexError, AttributeError): + # numpy不可用或计算错误,回退到逐像素检测 + detected = False + + + return detected, detected_x