fix(pil): 补充修复剩余 PIL Image 资源泄漏
覆盖 14 个文件中的 Image.open() 未正确关闭问题: - fansclub_handler: 5 处(pic/pic_rgb/img 未 close) - image_ops: 2 处(crop 后 pic 未 close) - douyin_popups: 2 处(pic.convert 重赋值泄漏原始句柄) - douyin_fudai: 1 处(open().convert() 链式调用泄漏) - douyin_robot: 2 处(同上) - save_files/direct_attend/finder/stage/find_open/detail/_common/ocr_preprocess: 各 1-2 处 统一修复模式:分离 open 与 convert,try/finally 确保原始句柄关闭。 Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
c3746b0207
commit
bb90e2983f
14 files changed
+370
-283
No files matched your search
@@ -291,9 +291,12 @@ def find_and_open_fudai_detail(
|
||||
|
||||
screenshot_path = path_config.get_screenshot_path()
|
||||
if os.path.exists(screenshot_path):
|
||||
img = Image.open(screenshot_path)
|
||||
img.load()
|
||||
pic_rgb_full = img.convert("RGB")
|
||||
_raw = Image.open(screenshot_path)
|
||||
try:
|
||||
_raw.load()
|
||||
pic_rgb_full = _raw.convert("RGB")
|
||||
finally:
|
||||
_raw.close()
|
||||
try:
|
||||
box_r = 30
|
||||
coord_box = (
|
||||
|
||||
@@ -754,6 +754,7 @@ class fudai_analyse:
|
||||
bottom = max(1, min(bottom, h))
|
||||
|
||||
roi = img.crop((left, top, right, bottom))
|
||||
img.close()
|
||||
# 保存到 pic 目录,使用 label 区分
|
||||
out_dir = path_config.get_pic_dir()
|
||||
os.makedirs(out_dir, exist_ok=True)
|
||||
@@ -794,8 +795,14 @@ class fudai_analyse:
|
||||
|
||||
# use module-level `Image` and `np` imports
|
||||
|
||||
roi_img = Image.open(roi_path).convert("RGB")
|
||||
tpl_img = Image.open(template_path).convert("RGB")
|
||||
_roi_raw = Image.open(roi_path)
|
||||
_tpl_raw = Image.open(template_path)
|
||||
try:
|
||||
roi_img = _roi_raw.convert("RGB")
|
||||
tpl_img = _tpl_raw.convert("RGB")
|
||||
finally:
|
||||
_roi_raw.close()
|
||||
_tpl_raw.close()
|
||||
|
||||
rw, rh = roi_img.size
|
||||
tw, th = tpl_img.size
|
||||
|
||||
@@ -161,10 +161,13 @@ def cut_pic(
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
_save_cut_debug_images(
|
||||
self, cropped_img, pic, name,
|
||||
"screenshot.png", left, top, right, bottom,
|
||||
)
|
||||
try:
|
||||
_save_cut_debug_images(
|
||||
self, cropped_img, pic, name,
|
||||
"screenshot.png", left, top, right, bottom,
|
||||
)
|
||||
finally:
|
||||
pic.close()
|
||||
return True
|
||||
|
||||
else:
|
||||
@@ -204,59 +207,62 @@ def cut_pic(
|
||||
else:
|
||||
cut_pic_path = path_target + "/" + name + ".png"
|
||||
|
||||
if not _validate_coords(left_up, right_down, pic.size):
|
||||
error_msg = (
|
||||
f"裁剪坐标无效: left_up={left_up}, right_down={right_down}, "
|
||||
f"image_size={pic.size}"
|
||||
)
|
||||
if attempt == max_retries - 1:
|
||||
_uo_log(f"[错误] {error_msg}")
|
||||
return False
|
||||
continue
|
||||
|
||||
left, top = left_up
|
||||
right, bottom = right_down
|
||||
assert cut_pic_path is not None
|
||||
cropped_img = pic.crop((left, top, right, bottom))
|
||||
try:
|
||||
_ensure_parent_dir(cut_pic_path)
|
||||
cropped_img.save(cut_pic_path)
|
||||
except Exception:
|
||||
logging.exception("保存裁剪图片失败: %s", cut_pic_path)
|
||||
return False
|
||||
if not _validate_coords(left_up, right_down, pic.size):
|
||||
error_msg = (
|
||||
f"裁剪坐标无效: left_up={left_up}, right_down={right_down}, "
|
||||
f"image_size={pic.size}"
|
||||
)
|
||||
if attempt == max_retries - 1:
|
||||
_uo_log(f"[错误] {error_msg}")
|
||||
return False
|
||||
continue
|
||||
|
||||
if name:
|
||||
left, top = left_up
|
||||
right, bottom = right_down
|
||||
assert cut_pic_path is not None
|
||||
cropped_img = pic.crop((left, top, right, bottom))
|
||||
try:
|
||||
with self._ocr_coords_lock:
|
||||
self._ocr_coords_cache[name] = (left, top, right, bottom)
|
||||
_ensure_parent_dir(cut_pic_path)
|
||||
cropped_img.save(cut_pic_path)
|
||||
except Exception:
|
||||
logging.exception("保存裁剪图片失败: %s", cut_pic_path)
|
||||
return False
|
||||
|
||||
if name:
|
||||
try:
|
||||
with self._ocr_coords_lock:
|
||||
self._ocr_coords_cache[name] = (left, top, right, bottom)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
# 命名分支总是保存 cut_pic 调试图(与原版行为一致 — 没有开关检查)
|
||||
try:
|
||||
from douyin.handlers import douyin_screenshot_manager
|
||||
|
||||
pic_rgb = pic.convert("RGB")
|
||||
debug_path = douyin_screenshot_manager.save_debug_image(
|
||||
pic_rgb,
|
||||
operation_type="cut_pic",
|
||||
picname=name if name else "cut",
|
||||
step="before_ocr",
|
||||
coords=(left, top, right, bottom),
|
||||
extra_info={
|
||||
"source": target + "/screenshot.png",
|
||||
"size": f"{right-left}x{bottom-top}",
|
||||
},
|
||||
)
|
||||
if debug_path:
|
||||
try:
|
||||
logging.info("[调试图像] cut_pic保存: %s", debug_path)
|
||||
except Exception:
|
||||
pass
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
# 命名分支总是保存 cut_pic 调试图(与原版行为一致 — 没有开关检查)
|
||||
try:
|
||||
from douyin.handlers import douyin_screenshot_manager
|
||||
|
||||
pic_rgb = pic.convert("RGB")
|
||||
debug_path = douyin_screenshot_manager.save_debug_image(
|
||||
pic_rgb,
|
||||
operation_type="cut_pic",
|
||||
picname=name if name else "cut",
|
||||
step="before_ocr",
|
||||
coords=(left, top, right, bottom),
|
||||
extra_info={
|
||||
"source": target + "/screenshot.png",
|
||||
"size": f"{right-left}x{bottom-top}",
|
||||
},
|
||||
)
|
||||
if debug_path:
|
||||
try:
|
||||
logging.info("[调试图像] cut_pic保存: %s", debug_path)
|
||||
except Exception:
|
||||
pass
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
return True
|
||||
return True
|
||||
finally:
|
||||
pic.close()
|
||||
|
||||
except (SyntaxError, IOError, OSError, FileNotFoundError, ValueError) as e:
|
||||
if attempt == max_retries - 1:
|
||||
|
||||
@@ -280,8 +280,11 @@ def check_in_fudai_detail(analyser: Any) -> bool:
|
||||
return False
|
||||
|
||||
pic = Image.open(pic_path)
|
||||
pic.load()
|
||||
pic_rgb = pic.convert("RGB")
|
||||
try:
|
||||
pic.load()
|
||||
pic_rgb = pic.convert("RGB")
|
||||
finally:
|
||||
pic.close()
|
||||
|
||||
# 获取分辨率比例和Y偏移
|
||||
rx = int(getattr(analyser, "resolution_ratio_x", 1080) or 1080)
|
||||
|
||||
@@ -197,6 +197,7 @@ def check_have_fudai_with_ready_screenshot(
|
||||
continue
|
||||
|
||||
pic_new = pic.convert("RGBA")
|
||||
pic.close()
|
||||
width, height = pic_new.size
|
||||
try:
|
||||
rx = int(getattr(analyser, "resolution_ratio_x", 1080) or 1080)
|
||||
|
||||
@@ -21,8 +21,11 @@ def _safe_open_screenshot_rgb(pic_path: str) -> Optional[Image.Image]:
|
||||
if not pic_path or (not os.path.exists(pic_path)):
|
||||
return None
|
||||
pic = Image.open(pic_path)
|
||||
pic.load() # Force load to catch truncated files early
|
||||
return pic.convert("RGB")
|
||||
try:
|
||||
pic.load()
|
||||
return pic.convert("RGB")
|
||||
finally:
|
||||
pic.close()
|
||||
except (OSError, IOError, SyntaxError):
|
||||
# Handle truncated image files
|
||||
return None
|
||||
|
||||
@@ -29,29 +29,35 @@ def handle_fansclub_branch(analyser: Any, attend_button_text: str) -> bool:
|
||||
from PIL import Image
|
||||
|
||||
pic = Image.open(pic_path)
|
||||
pic.load()
|
||||
pic_rgb = pic.convert("RGB")
|
||||
width, height = pic_rgb.size
|
||||
rx = int(getattr(analyser, "resolution_ratio_x", 1080) or 1080)
|
||||
ry = int(getattr(analyser, "resolution_ratio_y", 2400) or 2400)
|
||||
# 缩放OCR区域坐标到实际分辨率
|
||||
ocr_x1 = int(306 * width / rx)
|
||||
ocr_y1 = int(2030 * height / ry)
|
||||
ocr_x2 = int(780 * width / rx)
|
||||
ocr_y2 = int(2110 * height / ry)
|
||||
# 保存debug截图
|
||||
debug_path = douyin_screenshot_manager.save_debug_image(
|
||||
pic_rgb,
|
||||
operation_type="ocr",
|
||||
picname="参与按钮",
|
||||
step="识别参与按钮",
|
||||
coords=(ocr_x1, ocr_y1, ocr_x2, ocr_y2),
|
||||
extra_info={
|
||||
"ocr_text": attend_button_text[:50],
|
||||
"region": "(306,2030)-(780,2110)",
|
||||
"result": "需要粉丝团",
|
||||
},
|
||||
)
|
||||
try:
|
||||
pic.load()
|
||||
pic_rgb = pic.convert("RGB")
|
||||
finally:
|
||||
pic.close()
|
||||
try:
|
||||
width, height = pic_rgb.size
|
||||
rx = int(getattr(analyser, "resolution_ratio_x", 1080) or 1080)
|
||||
ry = int(getattr(analyser, "resolution_ratio_y", 2400) or 2400)
|
||||
# 缩放OCR区域坐标到实际分辨率
|
||||
ocr_x1 = int(306 * width / rx)
|
||||
ocr_y1 = int(2030 * height / ry)
|
||||
ocr_x2 = int(780 * width / rx)
|
||||
ocr_y2 = int(2110 * height / ry)
|
||||
# 保存debug截图
|
||||
debug_path = douyin_screenshot_manager.save_debug_image(
|
||||
pic_rgb,
|
||||
operation_type="ocr",
|
||||
picname="参与按钮",
|
||||
step="识别参与按钮",
|
||||
coords=(ocr_x1, ocr_y1, ocr_x2, ocr_y2),
|
||||
extra_info={
|
||||
"ocr_text": attend_button_text[:50],
|
||||
"region": "(306,2030)-(780,2110)",
|
||||
"result": "需要粉丝团",
|
||||
},
|
||||
)
|
||||
finally:
|
||||
pic_rgb.close()
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
@@ -100,10 +106,14 @@ def handle_fansclub_branch(analyser: Any, attend_button_text: str) -> bool:
|
||||
from PIL import Image
|
||||
|
||||
img = Image.open(pic_path)
|
||||
img = img.convert("RGB")
|
||||
try:
|
||||
img.load()
|
||||
img_rgb = img.convert("RGB")
|
||||
finally:
|
||||
img.close()
|
||||
import numpy as np
|
||||
|
||||
img_np = np.array(img)
|
||||
img_np = np.array(img_rgb)
|
||||
|
||||
# 调用OCR获取原始结果
|
||||
if getattr(analyser.operation, "ocr_type", None) == "easyocr":
|
||||
@@ -184,36 +194,42 @@ def handle_fansclub_branch(analyser: Any, attend_button_text: str) -> bool:
|
||||
from PIL import Image
|
||||
|
||||
pic = Image.open(pic_path)
|
||||
pic.load()
|
||||
pic_rgb = pic.convert("RGB")
|
||||
width, height = pic_rgb.size
|
||||
# 缩放OCR区域坐标到实际分辨率
|
||||
ocr_x1 = int(ocr_region_x1 * width / rx)
|
||||
ocr_y1 = int(ocr_region_y1 * height / ry)
|
||||
ocr_x2 = int(ocr_region_x2 * width / rx)
|
||||
ocr_y2 = int(ocr_region_y2 * height / ry)
|
||||
# 保存debug截图
|
||||
debug_path = douyin_screenshot_manager.save_debug_image(
|
||||
pic_rgb,
|
||||
operation_type="ocr",
|
||||
picname="加入粉丝团按钮",
|
||||
step="识别加入粉丝团按钮",
|
||||
coords=(ocr_x1, ocr_y1, ocr_x2, ocr_y2),
|
||||
extra_info={
|
||||
"ocr_text": fansclub_text[:50] if fansclub_text else "",
|
||||
"region": f"({ocr_region_x1},{ocr_region_y1})-({ocr_region_x2},{ocr_region_y2})",
|
||||
"result": (
|
||||
"识别成功"
|
||||
if fansclub_text
|
||||
and (
|
||||
"加入粉丝团" in fansclub_text
|
||||
or "粉丝团" in fansclub_text
|
||||
)
|
||||
else "识别失败"
|
||||
),
|
||||
"has_bbox": fansclub_bbox is not None,
|
||||
},
|
||||
)
|
||||
try:
|
||||
pic.load()
|
||||
pic_rgb = pic.convert("RGB")
|
||||
finally:
|
||||
pic.close()
|
||||
try:
|
||||
width, height = pic_rgb.size
|
||||
# 缩放OCR区域坐标到实际分辨率
|
||||
ocr_x1 = int(ocr_region_x1 * width / rx)
|
||||
ocr_y1 = int(ocr_region_y1 * height / ry)
|
||||
ocr_x2 = int(ocr_region_x2 * width / rx)
|
||||
ocr_y2 = int(ocr_region_y2 * height / ry)
|
||||
# 保存debug截图
|
||||
debug_path = douyin_screenshot_manager.save_debug_image(
|
||||
pic_rgb,
|
||||
operation_type="ocr",
|
||||
picname="加入粉丝团按钮",
|
||||
step="识别加入粉丝团按钮",
|
||||
coords=(ocr_x1, ocr_y1, ocr_x2, ocr_y2),
|
||||
extra_info={
|
||||
"ocr_text": fansclub_text[:50] if fansclub_text else "",
|
||||
"region": f"({ocr_region_x1},{ocr_region_y1})-({ocr_region_x2},{ocr_region_y2})",
|
||||
"result": (
|
||||
"识别成功"
|
||||
if fansclub_text
|
||||
and (
|
||||
"加入粉丝团" in fansclub_text
|
||||
or "粉丝团" in fansclub_text
|
||||
)
|
||||
else "识别失败"
|
||||
),
|
||||
"has_bbox": fansclub_bbox is not None,
|
||||
},
|
||||
)
|
||||
finally:
|
||||
pic_rgb.close()
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
@@ -412,10 +428,14 @@ def handle_fansclub_branch(analyser: Any, attend_button_text: str) -> bool:
|
||||
from PIL import Image
|
||||
|
||||
img = Image.open(pic_path)
|
||||
img = img.convert("RGB")
|
||||
try:
|
||||
img.load()
|
||||
img_rgb = img.convert("RGB")
|
||||
finally:
|
||||
img.close()
|
||||
import numpy as np
|
||||
|
||||
img_np = np.array(img)
|
||||
img_np = np.array(img_rgb)
|
||||
|
||||
if (
|
||||
getattr(
|
||||
@@ -785,10 +805,14 @@ def handle_fansclub_branch(analyser: Any, attend_button_text: str) -> bool:
|
||||
from PIL import Image
|
||||
|
||||
img = Image.open(pic_path)
|
||||
img = img.convert("RGB")
|
||||
try:
|
||||
img.load()
|
||||
img_rgb = img.convert("RGB")
|
||||
finally:
|
||||
img.close()
|
||||
import numpy as np
|
||||
|
||||
img_np = np.array(img)
|
||||
img_np = np.array(img_rgb)
|
||||
|
||||
if (
|
||||
getattr(
|
||||
|
||||
@@ -114,8 +114,11 @@ def safe_image_load(image_path: str) -> Optional[Image.Image]:
|
||||
|
||||
try:
|
||||
pic = Image.open(image_path)
|
||||
pic.load() # 确保图像数据已加载
|
||||
return pic.convert("RGB")
|
||||
try:
|
||||
pic.load()
|
||||
return pic.convert("RGB")
|
||||
finally:
|
||||
pic.close()
|
||||
except UnidentifiedImageError as e:
|
||||
# 图像格式不支持
|
||||
raise ValueError(f"无法识别图像格式: {image_path}") from e
|
||||
@@ -180,154 +183,159 @@ def save_full_screenshot_with_ocr_region(
|
||||
return
|
||||
|
||||
# 安全加载图像
|
||||
pic_rgb: Optional[Image.Image] = None
|
||||
try:
|
||||
pic_rgb = safe_image_load(pic_path)
|
||||
if pic_rgb is None:
|
||||
return
|
||||
except ValueError as e:
|
||||
log_debug_error(
|
||||
analyser,
|
||||
e,
|
||||
{**context, "stage": "加载图像"},
|
||||
level="ERROR",
|
||||
)
|
||||
return
|
||||
except OSError as e:
|
||||
log_debug_error(
|
||||
analyser,
|
||||
e,
|
||||
{**context, "stage": "读取图像文件"},
|
||||
level="ERROR",
|
||||
)
|
||||
return
|
||||
except Exception as e:
|
||||
log_debug_error(
|
||||
analyser,
|
||||
e,
|
||||
{**context, "stage": "加载图像(未知错误)"},
|
||||
level="ERROR",
|
||||
)
|
||||
return
|
||||
|
||||
# 获取分辨率信息
|
||||
width, height = pic_rgb.size
|
||||
try:
|
||||
rx = int(getattr(analyser, "resolution_ratio_x", 1080) or 1080)
|
||||
ry = int(getattr(analyser, "resolution_ratio_y", 2400) or 2400)
|
||||
except Exception as e:
|
||||
log_debug_error(
|
||||
analyser,
|
||||
e,
|
||||
{**context, "stage": "获取分辨率", "image_size": f"{width}x{height}"},
|
||||
level="WARNING",
|
||||
)
|
||||
rx, ry = 1080, 2400 # 使用默认值
|
||||
|
||||
context.update({
|
||||
"image_size": f"{width}x{height}",
|
||||
"base_resolution": f"{rx}x{ry}",
|
||||
})
|
||||
|
||||
# 缩放坐标到实际分辨率
|
||||
try:
|
||||
x1_base, y1_base, x2_base, y2_base = coords
|
||||
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)
|
||||
|
||||
scaled_coords = (x1, y1, x2, y2)
|
||||
except (ValueError, ZeroDivisionError) as e:
|
||||
log_debug_error(
|
||||
analyser,
|
||||
e,
|
||||
{**context, "stage": "坐标缩放"},
|
||||
level="ERROR",
|
||||
)
|
||||
return
|
||||
|
||||
# 验证和修正坐标
|
||||
is_valid, error_msg, corrected_coords = validate_coords(
|
||||
scaled_coords, width, height
|
||||
)
|
||||
|
||||
if not is_valid:
|
||||
log_debug_error(
|
||||
analyser,
|
||||
ValueError(f"坐标无效: {error_msg}"),
|
||||
{**context, "stage": "坐标验证", "scaled_coords": scaled_coords},
|
||||
level="WARNING",
|
||||
)
|
||||
return
|
||||
|
||||
x1, y1, x2, y2 = corrected_coords
|
||||
|
||||
# 如果坐标被修正,记录警告
|
||||
if error_msg:
|
||||
try:
|
||||
analyser._log_kv(
|
||||
"验证",
|
||||
"OCR区域坐标超出屏幕范围,已修正",
|
||||
original_coords=f"({x1_base},{y1_base})-({x2_base},{y2_base})",
|
||||
corrected_coords=f"({x1},{y1})-({x2},{y2})",
|
||||
screen_size=f"{width}x{height}",
|
||||
base_coords=coords,
|
||||
pic_rgb = safe_image_load(pic_path)
|
||||
if pic_rgb is None:
|
||||
return
|
||||
except ValueError as e:
|
||||
log_debug_error(
|
||||
analyser,
|
||||
e,
|
||||
{**context, "stage": "加载图像"},
|
||||
level="ERROR",
|
||||
)
|
||||
return
|
||||
except OSError as e:
|
||||
log_debug_error(
|
||||
analyser,
|
||||
e,
|
||||
{**context, "stage": "读取图像文件"},
|
||||
level="ERROR",
|
||||
)
|
||||
return
|
||||
except Exception as e:
|
||||
log_debug_error(
|
||||
analyser,
|
||||
e,
|
||||
{**context, "stage": "加载图像(未知错误)"},
|
||||
level="ERROR",
|
||||
)
|
||||
return
|
||||
|
||||
# 获取分辨率信息
|
||||
width, height = pic_rgb.size
|
||||
try:
|
||||
rx = int(getattr(analyser, "resolution_ratio_x", 1080) or 1080)
|
||||
ry = int(getattr(analyser, "resolution_ratio_y", 2400) or 2400)
|
||||
except Exception as e:
|
||||
log_debug_error(
|
||||
analyser,
|
||||
e,
|
||||
{**context, "stage": "获取分辨率", "image_size": f"{width}x{height}"},
|
||||
level="WARNING",
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
rx, ry = 1080, 2400 # 使用默认值
|
||||
|
||||
# 保存调试图片
|
||||
try:
|
||||
debug_path = douyin_screenshot_manager.save_debug_image(
|
||||
pic_rgb,
|
||||
operation_type="ocr",
|
||||
picname=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,
|
||||
},
|
||||
context.update({
|
||||
"image_size": f"{width}x{height}",
|
||||
"base_resolution": f"{rx}x{ry}",
|
||||
})
|
||||
|
||||
# 缩放坐标到实际分辨率
|
||||
try:
|
||||
x1_base, y1_base, x2_base, y2_base = coords
|
||||
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)
|
||||
|
||||
scaled_coords = (x1, y1, x2, y2)
|
||||
except (ValueError, ZeroDivisionError) as e:
|
||||
log_debug_error(
|
||||
analyser,
|
||||
e,
|
||||
{**context, "stage": "坐标缩放"},
|
||||
level="ERROR",
|
||||
)
|
||||
return
|
||||
|
||||
# 验证和修正坐标
|
||||
is_valid, error_msg, corrected_coords = validate_coords(
|
||||
scaled_coords, width, height
|
||||
)
|
||||
|
||||
if debug_path:
|
||||
if not is_valid:
|
||||
log_debug_error(
|
||||
analyser,
|
||||
ValueError(f"坐标无效: {error_msg}"),
|
||||
{**context, "stage": "坐标验证", "scaled_coords": scaled_coords},
|
||||
level="WARNING",
|
||||
)
|
||||
return
|
||||
|
||||
x1, y1, x2, y2 = corrected_coords
|
||||
|
||||
# 如果坐标被修正,记录警告
|
||||
if error_msg:
|
||||
try:
|
||||
analyser._log_kv(
|
||||
"[调试图像]",
|
||||
f"OCR识别前保存完整截图({picname})",
|
||||
path=debug_path,
|
||||
region=f"({x1_base},{y1_base})-({x2_base},{y2_base})",
|
||||
)
|
||||
except Exception as e:
|
||||
log_debug_error(
|
||||
analyser,
|
||||
e,
|
||||
{**context, "stage": "记录成功日志", "debug_path": debug_path},
|
||||
"验证",
|
||||
"OCR区域坐标超出屏幕范围,已修正",
|
||||
original_coords=f"({x1_base},{y1_base})-({x2_base},{y2_base})",
|
||||
corrected_coords=f"({x1},{y1})-({x2},{y2})",
|
||||
screen_size=f"{width}x{height}",
|
||||
base_coords=coords,
|
||||
level="WARNING",
|
||||
)
|
||||
except PermissionError as e:
|
||||
log_debug_error(
|
||||
analyser,
|
||||
e,
|
||||
{**context, "stage": "保存调试图像(权限错误)"},
|
||||
level="ERROR",
|
||||
)
|
||||
except OSError as e:
|
||||
log_debug_error(
|
||||
analyser,
|
||||
e,
|
||||
{**context, "stage": "保存调试图像(系统错误)"},
|
||||
level="ERROR",
|
||||
)
|
||||
except Exception as e:
|
||||
log_debug_error(
|
||||
analyser,
|
||||
e,
|
||||
{**context, "stage": "保存调试图像(未知错误)"},
|
||||
level="ERROR",
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
# 保存调试图片
|
||||
try:
|
||||
debug_path = douyin_screenshot_manager.save_debug_image(
|
||||
pic_rgb,
|
||||
operation_type="ocr",
|
||||
picname=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:
|
||||
analyser._log_kv(
|
||||
"[调试图像]",
|
||||
f"OCR识别前保存完整截图({picname})",
|
||||
path=debug_path,
|
||||
region=f"({x1_base},{y1_base})-({x2_base},{y2_base})",
|
||||
)
|
||||
except Exception as e:
|
||||
log_debug_error(
|
||||
analyser,
|
||||
e,
|
||||
{**context, "stage": "记录成功日志", "debug_path": debug_path},
|
||||
level="WARNING",
|
||||
)
|
||||
except PermissionError as e:
|
||||
log_debug_error(
|
||||
analyser,
|
||||
e,
|
||||
{**context, "stage": "保存调试图像(权限错误)"},
|
||||
level="ERROR",
|
||||
)
|
||||
except OSError as e:
|
||||
log_debug_error(
|
||||
analyser,
|
||||
e,
|
||||
{**context, "stage": "保存调试图像(系统错误)"},
|
||||
level="ERROR",
|
||||
)
|
||||
except Exception as e:
|
||||
log_debug_error(
|
||||
analyser,
|
||||
e,
|
||||
{**context, "stage": "保存调试图像(未知错误)"},
|
||||
level="ERROR",
|
||||
)
|
||||
finally:
|
||||
if pic_rgb is not None:
|
||||
pic_rgb.close()
|
||||
|
||||
|
||||
# 向后兼容:保留原函数名
|
||||
|
||||
@@ -138,8 +138,11 @@ def save_attempt_from_file(source_path: str, operation_type: str = "") -> bool:
|
||||
try:
|
||||
if not os.path.exists(source_path):
|
||||
return False
|
||||
image = Image.open(source_path)
|
||||
return save_latest_attempt(image, operation_type)
|
||||
_raw = Image.open(source_path)
|
||||
try:
|
||||
return save_latest_attempt(_raw, operation_type)
|
||||
finally:
|
||||
_raw.close()
|
||||
except Exception as e:
|
||||
try:
|
||||
_mgr_log(f"[截图管理] 从文件保存 latest_attempt.png 失败: {e}")
|
||||
@@ -163,8 +166,11 @@ def save_success_from_file(source_path: str, operation_type: str = "") -> bool:
|
||||
try:
|
||||
if not os.path.exists(source_path):
|
||||
return False
|
||||
image = Image.open(source_path)
|
||||
return save_last_success(image, operation_type)
|
||||
_raw = Image.open(source_path)
|
||||
try:
|
||||
return save_last_success(_raw, operation_type)
|
||||
finally:
|
||||
_raw.close()
|
||||
except Exception as e:
|
||||
try:
|
||||
_mgr_log(f"[截图管理] 从文件保存 last_success.png 失败: {e}")
|
||||
|
||||
@@ -36,9 +36,12 @@ def check_coupon_popup(analyser) -> bool:
|
||||
|
||||
pic_path = path_config.get_screenshot_path()
|
||||
pic = Image.open(pic_path)
|
||||
pic.load() # Force load to catch truncated files early
|
||||
pic = pic.convert("RGB")
|
||||
template_matched = bool(douyin_pixel.detect_coupon_popup_background(pic))
|
||||
try:
|
||||
pic.load() # Force load to catch truncated files early
|
||||
pic_rgb = pic.convert("RGB")
|
||||
finally:
|
||||
pic.close()
|
||||
template_matched = bool(douyin_pixel.detect_coupon_popup_background(pic_rgb))
|
||||
except (OSError, IOError, SyntaxError) as e:
|
||||
# Handle truncated image files
|
||||
try:
|
||||
@@ -207,8 +210,11 @@ def check_shopping_redpacket_popup(analyser) -> bool:
|
||||
|
||||
pic_path = path_config.get_screenshot_path()
|
||||
pic = Image.open(pic_path)
|
||||
pic.load() # Force load to catch truncated files early
|
||||
pic = pic.convert("RGB")
|
||||
try:
|
||||
pic.load() # Force load to catch truncated files early
|
||||
pic_rgb = pic.convert("RGB")
|
||||
finally:
|
||||
pic.close()
|
||||
try:
|
||||
redpacket_threshold = float(
|
||||
getattr(analyser, "shopping_redpacket_red_ratio_threshold", None)
|
||||
@@ -218,7 +224,7 @@ def check_shopping_redpacket_popup(analyser) -> bool:
|
||||
redpacket_threshold = 0.15
|
||||
has_popup_pixel = bool(
|
||||
douyin_pixel.detect_redpacket_popup_background(
|
||||
pic,
|
||||
pic_rgb,
|
||||
red_ratio_threshold=redpacket_threshold,
|
||||
analyser=analyser,
|
||||
)
|
||||
|
||||
@@ -133,7 +133,11 @@ def deal_robot_pic_change_color(analyser) -> None:
|
||||
path = path_config.get_robot_verify_pic_path(save_subdir=True)
|
||||
pic = path
|
||||
|
||||
img = Image.open(pic).convert("RGB")
|
||||
_raw = Image.open(pic)
|
||||
try:
|
||||
img = _raw.convert("RGB")
|
||||
finally:
|
||||
_raw.close()
|
||||
width, height = img.size
|
||||
for x in range(5, width - 40):
|
||||
for y in range(20, height - 30):
|
||||
@@ -156,7 +160,11 @@ def check_robot_pic_distance(analyser) -> int:
|
||||
if not pic_path or not os.path.exists(pic_path):
|
||||
raise FileNotFoundError(f"机器人验证图片不存在: {pic_path}")
|
||||
|
||||
img = Image.open(pic_path).convert("RGB")
|
||||
_raw = Image.open(pic_path)
|
||||
try:
|
||||
img = _raw.convert("RGB")
|
||||
finally:
|
||||
_raw.close()
|
||||
width, height = img.size
|
||||
|
||||
first_x = 0
|
||||
|
||||
@@ -209,9 +209,12 @@ def handle_reservation_direct_attend(analyser: Any) -> None:
|
||||
|
||||
from douyin.detection import douyin_pixel
|
||||
|
||||
button_result = douyin_pixel.detect_attend_button_red_area(
|
||||
pic_rgb, return_info=True, analyser=analyser
|
||||
)
|
||||
try:
|
||||
button_result = douyin_pixel.detect_attend_button_red_area(
|
||||
pic_rgb, return_info=True, analyser=analyser
|
||||
)
|
||||
finally:
|
||||
pic_rgb.close()
|
||||
if isinstance(button_result, tuple):
|
||||
has_button, button_info = button_result
|
||||
else:
|
||||
@@ -304,9 +307,12 @@ def handle_reservation_direct_attend(analyser: Any) -> None:
|
||||
)
|
||||
screenshot_path2 = path_config.get_screenshot_path()
|
||||
pic_rgb2 = Image.open(screenshot_path2) # type: ignore[attr-defined]
|
||||
button_result2 = douyin_pixel.detect_attend_button_red_area(
|
||||
pic_rgb2, return_info=True, analyser=analyser
|
||||
)
|
||||
try:
|
||||
button_result2 = douyin_pixel.detect_attend_button_red_area(
|
||||
pic_rgb2, return_info=True, analyser=analyser
|
||||
)
|
||||
finally:
|
||||
pic_rgb2.close()
|
||||
if isinstance(button_result2, tuple):
|
||||
has_button2, button_info2 = button_result2
|
||||
else:
|
||||
|
||||
@@ -633,9 +633,12 @@ def diagnose_stage_from_current_screen(analyser, trigger_reason="unknown"): # t
|
||||
|
||||
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")
|
||||
_raw = Image.open(pic_path)
|
||||
try:
|
||||
_raw.load()
|
||||
pic_rgb = _raw.convert("RGB")
|
||||
finally:
|
||||
_raw.close()
|
||||
|
||||
width, height = pic_rgb.size
|
||||
rx = int(
|
||||
|
||||
@@ -32,10 +32,13 @@ def preprocess_for_ocr(analyser: Any, cut_pic_name: str) -> str:
|
||||
try:
|
||||
if not os.path.exists(src):
|
||||
return cut_pic_name
|
||||
img = Image.open(src)
|
||||
img.load()
|
||||
# 转灰度
|
||||
img = img.convert("L")
|
||||
_raw = Image.open(src)
|
||||
try:
|
||||
_raw.load()
|
||||
# 转灰度
|
||||
img = _raw.convert("L")
|
||||
finally:
|
||||
_raw.close()
|
||||
# 放大两倍(Lanczos)
|
||||
img = img.resize((img.width * 2, img.height * 2), Image.LANCZOS)
|
||||
# 自动对比并锐化
|
||||
|
||||
Reference in new issue
Block a user