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