refactor(finder): 抽 check_have_fudai_with_ready_screenshot 中"截图获取+加载+失败处理"块到 finder_screenshot
将循环每次的截图相关逻辑(约 78 行)抽到 3 个独立 helper: - capture_screenshot_for_loop:每次循环开始时获取截图(_shot_and_diagnose + 文件验证 + 兜底) - load_screenshot_with_retry:读取截图,失败时重新截图后重试,返回 PIL Image 或 None - handle_screenshot_load_failure:连续失败超阈值返回 "break";否则等待后返回 "continue" finder.py: 371 → 320 行 (-51) 新增 finder_screenshot.py: 111 行 finder.py 跌破 350 行(1438 → 320,本会话累计削减 1118 行 = -78%) 验证: - pytest 1013 passed - compile OK Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
7aeb0c881c
commit
273adbb677
2 files changed
+127
-67
No files matched your search
@@ -118,82 +118,31 @@ def check_have_fudai_with_ready_screenshot(
|
||||
break
|
||||
|
||||
# 每次循环都重新截图(除了第一次如果已经提供了截图)
|
||||
if not (screenshot_ready and loop == 0):
|
||||
try:
|
||||
analyser._shot_and_diagnose(
|
||||
"找福袋图标", "判定是否存在福袋", analyser.device_id, diagnose=False
|
||||
)
|
||||
except Exception:
|
||||
# 如果截图失败,尝试直接调用 get_screenshot 并验证文件可读性(避免半写入/截断图片导致后续 PIL 打开失败)
|
||||
try:
|
||||
analyser.operation.get_screenshot(
|
||||
analyser.device_id, use_service=False
|
||||
)
|
||||
# 简单验证:文件存在、大小足够且能由 PIL 打开/加载
|
||||
valid = False
|
||||
try:
|
||||
if (
|
||||
os.path.exists(pic1_path)
|
||||
and os.path.getsize(pic1_path) >= 1024
|
||||
):
|
||||
from PIL import Image as PILImage
|
||||
from douyin.detection.detection_pkg.finder_screenshot import (
|
||||
capture_screenshot_for_loop,
|
||||
handle_screenshot_load_failure,
|
||||
load_screenshot_with_retry,
|
||||
)
|
||||
|
||||
with PILImage.open(pic1_path) as _im:
|
||||
_im.verify()
|
||||
valid = True
|
||||
except Exception:
|
||||
valid = False
|
||||
if not valid:
|
||||
# 再尝试一次直接截图作为最后回退
|
||||
try:
|
||||
analyser.operation.get_screenshot(
|
||||
analyser.device_id, use_service=False
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
except Exception:
|
||||
pass
|
||||
capture_screenshot_for_loop(analyser, screenshot_ready, loop, pic1_path)
|
||||
|
||||
# 读取截图
|
||||
pic = None
|
||||
try:
|
||||
pic = Image.open(pic1_path)
|
||||
pic.load()
|
||||
except Exception:
|
||||
# 如果读取失败,尝试重新截图
|
||||
try:
|
||||
analyser.operation.get_screenshot(analyser.device_id, use_service=False)
|
||||
pic = Image.open(pic1_path)
|
||||
pic.load()
|
||||
except Exception:
|
||||
pass
|
||||
pic = load_screenshot_with_retry(analyser, pic1_path)
|
||||
|
||||
# 如果截图读取失败,等待后继续下一次循环
|
||||
if pic is None:
|
||||
loop += 1
|
||||
consecutive_failures += 1
|
||||
# 如果连续失败太多次,提前退出
|
||||
if consecutive_failures >= max_consecutive_failures:
|
||||
try:
|
||||
analyser._log_kv(
|
||||
"[找福袋图标] 警告",
|
||||
"连续截图失败次数过多,提前退出",
|
||||
consecutive_failures=consecutive_failures,
|
||||
elapsed_s=round(time.time() - func_start, 2),
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
action = handle_screenshot_load_failure(
|
||||
analyser,
|
||||
consecutive_failures,
|
||||
max_consecutive_failures,
|
||||
func_start,
|
||||
max_wait_s,
|
||||
retry_delay_s,
|
||||
)
|
||||
if action == "break":
|
||||
break
|
||||
# 检查是否还有剩余时间
|
||||
elapsed = time.time() - func_start
|
||||
if elapsed < max_wait_s:
|
||||
remaining_time = max_wait_s - elapsed
|
||||
sleep_time = min(retry_delay_s, remaining_time)
|
||||
if sleep_time > 0:
|
||||
try:
|
||||
analyser.operation.delay(sleep_time)
|
||||
except Exception:
|
||||
time.sleep(sleep_time)
|
||||
continue
|
||||
|
||||
pic_new = pic.convert("RGBA")
|
||||
|
||||
@@ -0,0 +1,111 @@
|
||||
"""find_fudai 辅助:截图获取 + 验证 + 重试
|
||||
|
||||
从 douyin/detection/detection_pkg/finder.py:check_have_fudai_with_ready_screenshot 中提取。
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import os
|
||||
import time
|
||||
from typing import Any, Optional, Tuple
|
||||
|
||||
|
||||
def capture_screenshot_for_loop(analyser: Any, screenshot_ready: bool, loop: int, pic1_path: str) -> None:
|
||||
"""每次循环开始时获取截图(除了第一次如果已提供)。
|
||||
|
||||
流程:
|
||||
1. 调用 _shot_and_diagnose 截图(diagnose=False)
|
||||
2. 失败时调用 get_screenshot 并用 PIL 验证文件完整性
|
||||
3. 如验证失败再尝试一次截图作为最后回退
|
||||
"""
|
||||
if screenshot_ready and loop == 0:
|
||||
return
|
||||
|
||||
try:
|
||||
analyser._shot_and_diagnose(
|
||||
"找福袋图标", "判定是否存在福袋", analyser.device_id, diagnose=False
|
||||
)
|
||||
except Exception:
|
||||
# 如果截图失败,尝试直接调用 get_screenshot 并验证文件可读性
|
||||
try:
|
||||
analyser.operation.get_screenshot(
|
||||
analyser.device_id, use_service=False
|
||||
)
|
||||
valid = False
|
||||
try:
|
||||
if (
|
||||
os.path.exists(pic1_path)
|
||||
and os.path.getsize(pic1_path) >= 1024
|
||||
):
|
||||
from PIL import Image as PILImage
|
||||
|
||||
with PILImage.open(pic1_path) as _im:
|
||||
_im.verify()
|
||||
valid = True
|
||||
except Exception:
|
||||
valid = False
|
||||
if not valid:
|
||||
# 再尝试一次直接截图作为最后回退
|
||||
try:
|
||||
analyser.operation.get_screenshot(
|
||||
analyser.device_id, use_service=False
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
|
||||
def load_screenshot_with_retry(analyser: Any, pic1_path: str):
|
||||
"""读取截图,失败时重新截图后重试。返回 PIL Image 或 None。"""
|
||||
from PIL import Image
|
||||
|
||||
pic = None
|
||||
try:
|
||||
pic = Image.open(pic1_path)
|
||||
pic.load()
|
||||
except Exception:
|
||||
try:
|
||||
analyser.operation.get_screenshot(analyser.device_id, use_service=False)
|
||||
pic = Image.open(pic1_path)
|
||||
pic.load()
|
||||
except Exception:
|
||||
pass
|
||||
return pic
|
||||
|
||||
|
||||
def handle_screenshot_load_failure(
|
||||
analyser: Any,
|
||||
consecutive_failures: int,
|
||||
max_consecutive_failures: int,
|
||||
func_start: float,
|
||||
max_wait_s: float,
|
||||
retry_delay_s: float,
|
||||
) -> str:
|
||||
"""截图读取失败时的处理:
|
||||
|
||||
- 连续失败超过阈值 → 写警告日志,返回 "break"(外层 while 应 break)
|
||||
- 否则等待 retry_delay_s 秒后返回 "continue"(外层 while 应 continue)
|
||||
"""
|
||||
if consecutive_failures >= max_consecutive_failures:
|
||||
try:
|
||||
analyser._log_kv(
|
||||
"[找福袋图标] 警告",
|
||||
"连续截图失败次数过多,提前退出",
|
||||
consecutive_failures=consecutive_failures,
|
||||
elapsed_s=round(time.time() - func_start, 2),
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
return "break"
|
||||
|
||||
# 检查是否还有剩余时间
|
||||
elapsed = time.time() - func_start
|
||||
if elapsed < max_wait_s:
|
||||
remaining_time = max_wait_s - elapsed
|
||||
sleep_time = min(retry_delay_s, remaining_time)
|
||||
if sleep_time > 0:
|
||||
try:
|
||||
analyser.operation.delay(sleep_time)
|
||||
except Exception:
|
||||
time.sleep(sleep_time)
|
||||
return "continue"
|
||||
Reference in new issue
Block a user