underlying_operations 第三轮拆分(2434 → 2351,再瘦身 83 行)
新增 2 个子模块:
- device_select (新, 73) select_device 设备选择交互(无 self.X)
- ocr_digits (新, 49) analyse_pic_digits 数字 OCR 识别
(保留 analyser 参数以调 analyser.analyse_pic_word)
策略:
- select_device 的 self.X 全是 _adb_path / _adb_args,已经是 module-level,
搬出后改用 from .adb_helpers import _adb_path, _adb_args 即可。
- analyse_pic_digits 用 self.analyse_pic_word(递归调用类内大方法)。
搬到 module-level 后第一参数从 self 改名 analyser,类内 forwarding
传 self 即可,外部 op.analyse_pic_digits(picname) 完全兼容。
回归:
- 4 个新/改文件 compileall 通过
- pytest 353 passed / 1 skipped
- underlying_operations.select_device / analyse_pic_digits 类方法和 module-level
函数都可访问
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
1 parent
57eacb3219
commit
a337b4b93d
4 files changed
+122
-87
No files matched your search
@@ -88,6 +88,8 @@ from douyin.core.underlying_operations_pkg import (
|
||||
save_reward_pic as _impl_save_reward_pic,
|
||||
set_clipboard_text as _impl_set_clipboard_text,
|
||||
_swipe_with_sendevent_improved as _impl_swipe_sendevent_improved,
|
||||
select_device as _impl_select_device,
|
||||
analyse_pic_digits as _impl_analyse_pic_digits,
|
||||
)
|
||||
class underlying_operations:
|
||||
"""底层操作类"""
|
||||
@@ -205,58 +207,7 @@ class underlying_operations:
|
||||
self.ocr_type = None
|
||||
|
||||
def select_device(self):
|
||||
"""选择需要连接的设备"""
|
||||
result = subprocess.run(
|
||||
[self._adb_path(), "devices", "-l"],
|
||||
stdout=subprocess.PIPE,
|
||||
stderr=subprocess.DEVNULL,
|
||||
text=True,
|
||||
encoding="utf-8",
|
||||
errors="replace",
|
||||
check=False,
|
||||
)
|
||||
totalstring = result.stdout or ""
|
||||
# print(totalstring)
|
||||
devicelist = []
|
||||
for line in totalstring.splitlines()[1:]:
|
||||
parts = line.split()
|
||||
if len(parts) >= 2 and parts[1] == "device":
|
||||
devicelist.append(parts[0])
|
||||
# devicelist = re.compile(r'(\w*)\s*device\b').findall(totalstring)
|
||||
devicenum = len(devicelist)
|
||||
if devicenum == 0:
|
||||
print("当前无设备连接电脑,请检查设备连接情况!")
|
||||
return False
|
||||
elif devicenum == 1:
|
||||
print("当前有一台设备连接,编号:%s." % devicelist[0])
|
||||
return devicelist[0]
|
||||
else:
|
||||
print("当前存在多台设备连接! 输入数字选择对应设备:")
|
||||
dictdevice = {}
|
||||
for i in range(devicenum):
|
||||
prop = subprocess.run(
|
||||
self._adb_args(devicelist[i], "shell", "getprop", "ro.product.device"),
|
||||
stdout=subprocess.PIPE,
|
||||
stderr=subprocess.DEVNULL,
|
||||
text=True,
|
||||
encoding="utf-8",
|
||||
errors="replace",
|
||||
check=False,
|
||||
)
|
||||
modestring = (prop.stdout or "").strip()
|
||||
print("%s:%s---%s" % (i + 1, devicelist[i], modestring))
|
||||
dictdevice[i + 1] = devicelist[i]
|
||||
if not sys.stdin.isatty():
|
||||
print("检测到多台设备且当前不是交互式终端,请通过 --device-id 明确指定设备。")
|
||||
return False
|
||||
num = input()
|
||||
num = int(num)
|
||||
while not num in dictdevice.keys():
|
||||
print("输入不正确,请重新输入:")
|
||||
num = input()
|
||||
num = int(num)
|
||||
return dictdevice[num]
|
||||
|
||||
return _impl_select_device()
|
||||
def _find_pic_path(self, picname: str, for_reading: bool = True):
|
||||
return _impl_find_pic_path(picname, for_reading)
|
||||
def get_screenshot(self, device_id, path="pic", use_service: bool = False):
|
||||
@@ -1628,41 +1579,7 @@ class underlying_operations:
|
||||
return text
|
||||
|
||||
def analyse_pic_digits(self, picname, change_color=1, pic_path=None):
|
||||
"""轻量数字OCR识别函数,专门用于识别数字(0-9)。
|
||||
|
||||
策略:
|
||||
1. 复用 analyse_pic_word 的核心路径(缓存 / 预处理 / OCR 调用 / 调试图保存)
|
||||
2. change_color=1/2 触发二值化(数字框背景简单时有效)
|
||||
3. 在拿到原始文本后做数字过滤与智能段提取(保留 . 用于小数)
|
||||
"""
|
||||
text = self.analyse_pic_word(
|
||||
picname, change_color=change_color, pic_path=pic_path
|
||||
)
|
||||
if not text:
|
||||
return ""
|
||||
|
||||
# 规范化常见全角/中文标点 -> 英文点
|
||||
text_norm = (
|
||||
str(text).replace("。", ".").replace(",", ".").replace(".", ".")
|
||||
)
|
||||
digits_only = "".join(ch for ch in text_norm if ch.isdigit())
|
||||
digits = "".join(ch for ch in text_norm if ch.isdigit() or ch == ".")
|
||||
|
||||
# 智能过滤:识别出的数字过多通常意味着混入了非目标内容
|
||||
# (例如把文件名/坐标也识别进来),尝试提取合理段
|
||||
if len(digits_only) > 10:
|
||||
digit_segments = re.findall(r"\d+(?:\.\d+)?", text_norm)
|
||||
if digit_segments:
|
||||
# 价值类:1-6 位最合理;倒计时:1-2 位最合理
|
||||
reasonable_segments = [
|
||||
s for s in digit_segments if 1 <= len(s.replace(".", "")) <= 6
|
||||
]
|
||||
if reasonable_segments:
|
||||
return max(reasonable_segments, key=len)
|
||||
return min(digit_segments, key=len) if digit_segments else digits[:6]
|
||||
|
||||
return digits
|
||||
|
||||
return _impl_analyse_pic_digits(self, picname, change_color, pic_path)
|
||||
def extract_ocr_content(self, content=None, return_confidence: bool = False):
|
||||
return _impl_extract_ocr_content(content, return_confidence)
|
||||
def delay(self, seconds: float = 1):
|
||||
|
||||
@@ -20,6 +20,9 @@ from .bounds import _parse_bounds
|
||||
from .reward_pic import save_reward_pic
|
||||
from .swipe_sendevent import _swipe_with_sendevent_improved
|
||||
from .clipboard import set_clipboard_text
|
||||
from .device_select import select_device
|
||||
from .ocr_digits import analyse_pic_digits
|
||||
|
||||
from .adb_helpers import _adb_args
|
||||
|
||||
|
||||
@@ -34,4 +37,6 @@ __all__ = [
|
||||
"_swipe_with_sendevent_improved",
|
||||
"set_clipboard_text",
|
||||
"_adb_args",
|
||||
"select_device",
|
||||
"analyse_pic_digits",
|
||||
]
|
||||
@@ -0,0 +1,66 @@
|
||||
"""underlying_operations_pkg.device_select - 设备选择交互。
|
||||
|
||||
select_device(): 列出 adb devices,单设备直接返回,多设备时让用户选。
|
||||
保留交互 print(CLI 用户在 stdout 看提示),无设备返回 False。
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import subprocess
|
||||
import sys
|
||||
|
||||
from .adb_helpers import _adb_args, _adb_path
|
||||
|
||||
|
||||
def select_device():
|
||||
"""选择需要连接的设备"""
|
||||
result = subprocess.run(
|
||||
[_adb_path(), "devices", "-l"],
|
||||
stdout=subprocess.PIPE,
|
||||
stderr=subprocess.DEVNULL,
|
||||
text=True,
|
||||
encoding="utf-8",
|
||||
errors="replace",
|
||||
check=False,
|
||||
)
|
||||
totalstring = result.stdout or ""
|
||||
# print(totalstring)
|
||||
devicelist = []
|
||||
for line in totalstring.splitlines()[1:]:
|
||||
parts = line.split()
|
||||
if len(parts) >= 2 and parts[1] == "device":
|
||||
devicelist.append(parts[0])
|
||||
# devicelist = re.compile(r'(\w*)\s*device\b').findall(totalstring)
|
||||
devicenum = len(devicelist)
|
||||
if devicenum == 0:
|
||||
print("当前无设备连接电脑,请检查设备连接情况!")
|
||||
return False
|
||||
elif devicenum == 1:
|
||||
print("当前有一台设备连接,编号:%s." % devicelist[0])
|
||||
return devicelist[0]
|
||||
else:
|
||||
print("当前存在多台设备连接! 输入数字选择对应设备:")
|
||||
dictdevice = {}
|
||||
for i in range(devicenum):
|
||||
prop = subprocess.run(
|
||||
_adb_args(devicelist[i], "shell", "getprop", "ro.product.device"),
|
||||
stdout=subprocess.PIPE,
|
||||
stderr=subprocess.DEVNULL,
|
||||
text=True,
|
||||
encoding="utf-8",
|
||||
errors="replace",
|
||||
check=False,
|
||||
)
|
||||
modestring = (prop.stdout or "").strip()
|
||||
print("%s:%s---%s" % (i + 1, devicelist[i], modestring))
|
||||
dictdevice[i + 1] = devicelist[i]
|
||||
if not sys.stdin.isatty():
|
||||
print("检测到多台设备且当前不是交互式终端,请通过 --device-id 明确指定设备。")
|
||||
return False
|
||||
num = input()
|
||||
num = int(num)
|
||||
while not num in dictdevice.keys():
|
||||
print("输入不正确,请重新输入:")
|
||||
num = input()
|
||||
num = int(num)
|
||||
return dictdevice[num]
|
||||
|
||||
@@ -0,0 +1,47 @@
|
||||
"""underlying_operations_pkg.ocr_digits - 数字 OCR 识别。
|
||||
|
||||
analyse_pic_digits(analyser, picname, change_color=1, pic_path=None):
|
||||
轻量数字 OCR 识别。复用 analyser.analyse_pic_word 的核心路径,
|
||||
拿到原始文本后做数字过滤与智能段提取(保留 . 用于小数)。
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import re
|
||||
|
||||
|
||||
def analyse_pic_digits(analyser, picname, change_color=1, pic_path=None):
|
||||
"""轻量数字OCR识别函数,专门用于识别数字(0-9)。
|
||||
|
||||
策略:
|
||||
1. 复用 analyse_pic_word 的核心路径(缓存 / 预处理 / OCR 调用 / 调试图保存)
|
||||
2. change_color=1/2 触发二值化(数字框背景简单时有效)
|
||||
3. 在拿到原始文本后做数字过滤与智能段提取(保留 . 用于小数)
|
||||
"""
|
||||
text = analyser.analyse_pic_word(
|
||||
picname, change_color=change_color, pic_path=pic_path
|
||||
)
|
||||
if not text:
|
||||
return ""
|
||||
|
||||
# 规范化常见全角/中文标点 -> 英文点
|
||||
text_norm = (
|
||||
str(text).replace("。", ".").replace(",", ".").replace(".", ".")
|
||||
)
|
||||
digits_only = "".join(ch for ch in text_norm if ch.isdigit())
|
||||
digits = "".join(ch for ch in text_norm if ch.isdigit() or ch == ".")
|
||||
|
||||
# 智能过滤:识别出的数字过多通常意味着混入了非目标内容
|
||||
# (例如把文件名/坐标也识别进来),尝试提取合理段
|
||||
if len(digits_only) > 10:
|
||||
digit_segments = re.findall(r"\d+(?:\.\d+)?", text_norm)
|
||||
if digit_segments:
|
||||
# 价值类:1-6 位最合理;倒计时:1-2 位最合理
|
||||
reasonable_segments = [
|
||||
s for s in digit_segments if 1 <= len(s.replace(".", "")) <= 6
|
||||
]
|
||||
if reasonable_segments:
|
||||
return max(reasonable_segments, key=len)
|
||||
return min(digit_segments, key=len) if digit_segments else digits[:6]
|
||||
|
||||
return digits
|
||||
|
||||
Reference in new issue
Block a user