refactor(gui): 抽 do_screenshot + OCR_REGIONS 到 components/screenshot_action (4635 → 4463)

This commit is contained in:
Your Name committed 2026-05-30 14:03:31 +08:00
1 parent a07ef00b5e
commit 8ea676562c
2 files changed
+210 -186

No files matched your search

+196
View File
@@ -0,0 +1,196 @@
"""截图 + OCR 识别按钮的执行逻辑。
从 ``gui/gui_app.py`` 抽出。原本是 ``main()`` 内的闭包 ``do_screenshot``
(以及与之配套的 ``OCR_REGIONS`` 区域定义)。
依赖通过显式参数注入:设备 ID、Y 偏移、当前预览图片名(StringVar)、
日志/OCR 结果/预览刷新回调、tk root(用于线程间 ``after``)。
"""
from __future__ import annotations
import os
import threading
import tkinter as tk
from typing import Callable
from PIL import Image # type: ignore[import]
# OCR 区域配置:定义所有需要识别的区域(坐标基于1080x2400基准)
OCR_REGIONS = [
# (区域名称, (x1, y1, x2, y2), 描述)
("福袋内容", (389, 1273, 1000, 1534), "福袋内容详情"),
("福袋倒计时", (390, 1470, 980, 1580), "福袋倒计时"),
("福袋价值", (390, 1440, 1000, 1580), "奖品价值/价格"),
("抽奖结果", (380, 660, 720, 755), "抽奖结果弹窗"),
("推荐页标签栏", (400, 140, 900, 200), "推荐/关注/直播标签"),
("推荐页标签栏快速检测", (400, 140, 900, 200), "推荐页快速检测"),
("参与按钮", (306, 2030, 780, 2110), "参与福袋抽奖按钮"),
("无奖励确认", (340, 1195, 740, 1355), "无奖励确认弹窗"),
("奖励通知确认", (370, 1350, 680, 1440), "奖励通知确认"),
("订单确认", (370, 120, 700, 220), "订单确认页面"),
("关注列表标签", (658, 147, 765, 194), "关注列表标签"),
("直播列表标签", (400, 140, 900, 200), "直播列表标签"),
("直播广场", (400, 140, 900, 200), "直播广场标题"),
("直播间标题_中", (400, 140, 900, 200), "直播间标题中部"),
("诊断优惠券标题", (60, 1260, 1020, 1480), "优惠券弹窗标题"),
("诊断优惠券按钮", (400, 1600, 680, 1700), "优惠券按钮"),
("诊断购物红包", (400, 1400, 900, 1600), "购物红包弹窗"),
]
def do_screenshot(
*,
device_id: str,
y_offset: int,
image_var: tk.StringVar,
on_line: Callable[[str], None],
root: tk.Misc,
on_ocr_results: Callable[[list], None],
on_refresh_preview: Callable[[], None],
) -> None:
"""触发设备截图并对预设区域做 OCR 识别。"""
if not device_id:
on_line("[截图] 请先选择设备")
return
# 在后台线程中执行截图和OCR识别,避免阻塞GUI
def screenshot_thread():
try:
from douyin.core.underlying_operations import underlying_operations
# 使用模块级别导入的 Image 与 os,避免重复导入导致 Pylance 警告
# 使用 root.after 在主线程中更新日志
def log_msg(msg: str) -> Callable[[], None]:
def wrapper() -> None:
on_line(msg)
return wrapper
root.after(0, log_msg("[截图] 正在截图...")) # type: ignore[arg-type]
ops = underlying_operations()
result = ops.get_screenshot(device_id, use_service=False)
if not result:
root.after(0, log_msg("[截图] 截图失败")) # type: ignore[arg-type]
return
root.after(0, log_msg("[截图] 截图成功,已保存到 pic/screenshot.png")) # type: ignore[arg-type]
# 读取截图以获取实际尺寸
try:
from douyin.core.path_config import get_screenshot_path
screenshot_path = get_screenshot_path()
except Exception:
# 回退到旧路径(向后兼容)
from douyin.core import path_config
screenshot_path = path_config.get_screenshot_path()
if not os.path.exists(screenshot_path):
root.after(0, log_msg("[OCR] 截图文件不存在")) # type: ignore[arg-type]
return
try:
img = Image.open(screenshot_path)
img.load() # Force load to catch truncated files early
img_width, img_height = img.size
# 计算缩放比例(基于1080x2400基准)
scale_x = img_width / 1080.0
scale_y = img_height / 2400.0
except (OSError, IOError, SyntaxError) as e:
# Handle truncated image files
root.after(0, log_msg(f"图片文件损坏或截断: {e}")) # type: ignore[arg-type]
return
except Exception as e:
root.after(0, log_msg(f"[OCR] 读取截图失败: {e}")) # type: ignore[arg-type]
return
# 获取Y偏移
# (来自函数参数 y_offset)
# 执行所有OCR区域的识别
ocr_results = []
total_regions = len(OCR_REGIONS)
root.after(0, log_msg(f"[OCR] 开始识别 {total_regions} 个区域...")) # type: ignore[arg-type]
for idx, (region_name, (x1, y1, x2, y2), desc) in enumerate(
OCR_REGIONS, 1
):
try:
# 缩放坐标到实际截图尺寸
actual_x1 = int(x1 * scale_x)
actual_y1 = int(y1 * scale_y) + y_offset
actual_x2 = int(x2 * scale_x)
actual_y2 = int(y2 * scale_y) + y_offset
# 确保坐标在图片范围内
actual_x1 = max(0, min(actual_x1, img_width))
actual_y1 = max(0, min(actual_y1, img_height))
actual_x2 = max(0, min(actual_x2, img_width))
actual_y2 = max(0, min(actual_y2, img_height))
if actual_x1 >= actual_x2 or actual_y1 >= actual_y2:
root.after(0, log_msg(f"[OCR] {region_name}: 坐标无效,跳过")) # type: ignore[arg-type]
continue
# 截取区域
cut_result = ops.cut_pic(
(actual_x1, actual_y1),
(actual_x2, actual_y2),
"",
region_name,
)
if not cut_result:
root.after(0, log_msg(f"[OCR] {region_name}: 截图失败")) # type: ignore[arg-type]
ocr_results.append((region_name, desc, "", "截图失败"))
continue
# OCR识别(先尝试模式0,如果为空再尝试模式1)
text_result = ""
ocr_mode = 0
for mode in [0, 1]:
try:
text = str(
ops.analyse_pic_word(region_name, mode) or ""
).strip()
if text:
text_result = text
ocr_mode = mode
break
except Exception:
continue
ocr_results.append(
(region_name, desc, text_result, f"模式{ocr_mode}")
)
status_msg = f"[OCR] ({idx}/{total_regions}) {region_name}: {text_result[:50] if text_result else '(空)'}"
root.after(0, log_msg(status_msg)) # type: ignore[arg-type]
except Exception as e:
error_msg = f"[OCR] {region_name}: 异常 - {e}"
root.after(0, log_msg(error_msg)) # type: ignore[arg-type]
ocr_results.append((region_name, desc, "", f"异常: {e}"))
# 更新OCR结果标签页
def update_ocr_wrapper() -> None:
on_ocr_results(ocr_results)
root.after(0, update_ocr_wrapper) # type: ignore[arg-type]
root.after(0, log_msg(f"[OCR] 完成!共识别 {len(ocr_results)} 个区域")) # type: ignore[arg-type]
# 刷新截图预览(如果当前显示的是 screenshot.png)
def refresh_if_screenshot() -> None:
if image_var.get() == "screenshot.png":
on_refresh_preview()
root.after(0, refresh_if_screenshot) # type: ignore[arg-type]
except Exception as e:
import traceback
error_detail = traceback.format_exc()
root.after(0, log_msg(f"[截图] 截图异常: {e}\n{error_detail}")) # type: ignore[arg-type]
thread = threading.Thread(target=screenshot_thread, daemon=True)
thread.start()
+14 -186
View File
@@ -49,6 +49,7 @@ from gui.components.wifi_adb_scan_dialog import open_scan_dialog
from gui.components.wifi_adb_dialog import open_wifi_adb_dialog as _open_wifi_adb_dialog
from gui.components.reservations_view import refresh_reservations as _refresh_reservations_impl
from gui.components.high_value_view import refresh_high_value_rooms as _refresh_high_value_rooms_impl
from gui.components.screenshot_action import do_screenshot as _do_screenshot_impl
# 导入日志颜色配置模块
try:
@@ -2836,193 +2837,20 @@ def main():
)
# 截图+OCR 按钮也放在顶部,方便随时使用
# 具体回调在后文定义,这里先占位,通过 lambda 延迟绑定
screenshot_button_var = {"command": None}
def _bind_screenshot_button(btn):
# 在 do_screenshot 定义后调用,将真正的 command 绑定到按钮
screenshot_button_var["button"] = btn
screenshot_btn = ttk.Button(top_btns, text="截图+OCR")
screenshot_btn = ttk.Button(
top_btns,
text="截图+OCR",
command=lambda: _do_screenshot_impl(
device_id=device_var.get().strip(),
y_offset=int(y_var.get() or 0),
image_var=image_var,
on_line=on_line,
root=root,
on_ocr_results=update_ocr_results,
on_refresh_preview=refresh_preview,
),
)
screenshot_btn.pack(side=LEFT, padx=(0, 8))
_bind_screenshot_button(screenshot_btn)
# OCR区域配置:定义所有需要识别的区域(坐标基于1080x2400基准)
OCR_REGIONS = [
# (区域名称, (x1, y1, x2, y2), 描述)
("福袋内容", (389, 1273, 1000, 1534), "福袋内容详情"),
("福袋倒计时", (390, 1470, 980, 1580), "福袋倒计时"),
("福袋价值", (390, 1440, 1000, 1580), "奖品价值/价格"),
("抽奖结果", (380, 660, 720, 755), "抽奖结果弹窗"),
("推荐页标签栏", (400, 140, 900, 200), "推荐/关注/直播标签"),
("推荐页标签栏快速检测", (400, 140, 900, 200), "推荐页快速检测"),
("参与按钮", (306, 2030, 780, 2110), "参与福袋抽奖按钮"),
("无奖励确认", (340, 1195, 740, 1355), "无奖励确认弹窗"),
("奖励通知确认", (370, 1350, 680, 1440), "奖励通知确认"),
("订单确认", (370, 120, 700, 220), "订单确认页面"),
("关注列表标签", (658, 147, 765, 194), "关注列表标签"),
("直播列表标签", (400, 140, 900, 200), "直播列表标签"),
("直播广场", (400, 140, 900, 200), "直播广场标题"),
("直播间标题_中", (400, 140, 900, 200), "直播间标题中部"),
("诊断优惠券标题", (60, 1260, 1020, 1480), "优惠券弹窗标题"),
("诊断优惠券按钮", (400, 1600, 680, 1700), "优惠券按钮"),
("诊断购物红包", (400, 1400, 900, 1600), "购物红包弹窗"),
]
# 截图按钮
def do_screenshot():
device_id = device_var.get().strip()
if not device_id:
on_line("[截图] 请先选择设备")
return
# 在后台线程中执行截图和OCR识别,避免阻塞GUI
def screenshot_thread():
try:
from douyin.core.underlying_operations import underlying_operations
# 使用模块级别导入的 Image 与 os,避免重复导入导致 Pylance 警告
# 使用 root.after 在主线程中更新日志
def log_msg(msg: str) -> Callable[[], None]:
def wrapper() -> None:
on_line(msg)
return wrapper
root.after(0, log_msg("[截图] 正在截图...")) # type: ignore[arg-type]
ops = underlying_operations()
result = ops.get_screenshot(device_id, use_service=False)
if not result:
root.after(0, log_msg("[截图] 截图失败")) # type: ignore[arg-type]
return
root.after(0, log_msg("[截图] 截图成功,已保存到 pic/screenshot.png")) # type: ignore[arg-type]
# 读取截图以获取实际尺寸
try:
from douyin.core.path_config import get_screenshot_path
screenshot_path = get_screenshot_path()
except Exception:
# 回退到旧路径(向后兼容)
from douyin.core import path_config
screenshot_path = path_config.get_screenshot_path()
if not os.path.exists(screenshot_path):
root.after(0, log_msg("[OCR] 截图文件不存在")) # type: ignore[arg-type]
return
try:
img = Image.open(screenshot_path)
img.load() # Force load to catch truncated files early
img_width, img_height = img.size
# 计算缩放比例(基于1080x2400基准)
scale_x = img_width / 1080.0
scale_y = img_height / 2400.0
except (OSError, IOError, SyntaxError) as e:
# Handle truncated image files
root.after(0, log_msg(f"图片文件损坏或截断: {e}")) # type: ignore[arg-type]
return
except Exception as e:
root.after(0, log_msg(f"[OCR] 读取截图失败: {e}")) # type: ignore[arg-type]
return
# 获取Y偏移
y_offset = int(y_var.get() or 0)
# 执行所有OCR区域的识别
ocr_results = []
total_regions = len(OCR_REGIONS)
root.after(0, log_msg(f"[OCR] 开始识别 {total_regions} 个区域...")) # type: ignore[arg-type]
for idx, (region_name, (x1, y1, x2, y2), desc) in enumerate(
OCR_REGIONS, 1
):
try:
# 缩放坐标到实际截图尺寸
actual_x1 = int(x1 * scale_x)
actual_y1 = int(y1 * scale_y) + y_offset
actual_x2 = int(x2 * scale_x)
actual_y2 = int(y2 * scale_y) + y_offset
# 确保坐标在图片范围内
actual_x1 = max(0, min(actual_x1, img_width))
actual_y1 = max(0, min(actual_y1, img_height))
actual_x2 = max(0, min(actual_x2, img_width))
actual_y2 = max(0, min(actual_y2, img_height))
if actual_x1 >= actual_x2 or actual_y1 >= actual_y2:
root.after(0, log_msg(f"[OCR] {region_name}: 坐标无效,跳过")) # type: ignore[arg-type]
continue
# 截取区域
cut_result = ops.cut_pic(
(actual_x1, actual_y1),
(actual_x2, actual_y2),
"",
region_name,
)
if not cut_result:
root.after(0, log_msg(f"[OCR] {region_name}: 截图失败")) # type: ignore[arg-type]
ocr_results.append((region_name, desc, "", "截图失败"))
continue
# OCR识别(先尝试模式0,如果为空再尝试模式1)
text_result = ""
ocr_mode = 0
for mode in [0, 1]:
try:
text = str(
ops.analyse_pic_word(region_name, mode) or ""
).strip()
if text:
text_result = text
ocr_mode = mode
break
except Exception:
continue
ocr_results.append(
(region_name, desc, text_result, f"模式{ocr_mode}")
)
status_msg = f"[OCR] ({idx}/{total_regions}) {region_name}: {text_result[:50] if text_result else '(空)'}"
root.after(0, log_msg(status_msg)) # type: ignore[arg-type]
except Exception as e:
error_msg = f"[OCR] {region_name}: 异常 - {e}"
root.after(0, log_msg(error_msg)) # type: ignore[arg-type]
ocr_results.append((region_name, desc, "", f"异常: {e}"))
# 更新OCR结果标签页
def update_ocr_wrapper() -> None:
update_ocr_results(ocr_results)
root.after(0, update_ocr_wrapper) # type: ignore[arg-type]
root.after(0, log_msg(f"[OCR] 完成!共识别 {len(ocr_results)} 个区域")) # type: ignore[arg-type]
# 刷新截图预览(如果当前显示的是 screenshot.png)
def refresh_if_screenshot() -> None:
if image_var.get() == "screenshot.png":
refresh_preview()
root.after(0, refresh_if_screenshot) # type: ignore[arg-type]
except Exception as e:
import traceback
error_detail = traceback.format_exc()
root.after(0, log_msg(f"[截图] 截图异常: {e}\n{error_detail}")) # type: ignore[arg-type]
thread = threading.Thread(target=screenshot_thread, daemon=True)
thread.start()
# 将顶部“截图+OCR”按钮的回调绑定到 do_screenshot
try:
btn = screenshot_button_var.get("button")
if btn is not None:
btn.configure(command=do_screenshot)
except Exception:
pass
# 右侧:操作按钮区域(先创建,放在底部)
actions = ttk.Frame(right)