官方OCR
AScript iOS 内置多套免费 OCR 引擎:百度 PaddleOCR V5、Apple Vision、Google MLKit。无需额外配置即可使用。
from ascript.ios.screen import Ocr
识别方法
PaddleOCR 识别
Ocr.paddleocr(rect=None, pattern=None, confidence=0.1,
max_side_len=1200, precision=16, bitmap=None,
file=None, binary=-1, language="chinese", image=None)
- 参数
| 参数 | 类型 | 是否必填 | 说明 |
|---|---|---|---|
| rect | list | 否 | 圈定屏幕范围 |
| pattern | str | 否 | 正则表达式,过滤匹配的文字 |
| confidence | float | 否 | 可信度,默认0.1 |
| max_side_len | int | 否 | 最大边长,默认1200 |
| precision | int | 否 | 推理精度,默认16 |
| bitmap | PIL.Image | 否 | 要识别的 图片对象 |
| image | PIL.Image | 否 | 要识别的图片,默认屏幕截图 |
| file | str | 否 | 要识别的图片路径 |
| binary | int | 否 | 二值化阈值,-1为不启用(默认) |
| language | str | 否 | 识别语言,默认 chinese |
- 返回值
文字结果字典列表,每条结果包含:
{
"text": "识别到的文本",
"rect": (x1, y1, x2, y2), # 识别到的范围
"center_x": 155.5, # 文字中心点 X
"center_y": 44.0, # 文字中心点 Y
"confidence": 0.95 # 可信度
}
- 示例
from ascript.ios.screen import Ocr
# 全屏识别
res = Ocr.paddleocr()
if res:
for r in res:
print(r['text'])
# 指定区域识别
res = Ocr.paddleocr(rect=[42, 153, 641, 694])
# 正则过滤
res = Ocr.paddleocr(pattern="\\d+") # 只匹配数字
Apple Vision 识别
基于 iOS 原生 Vision 框架,iOS 16+ 支持中文识别。
Ocr.vision(rect=None, pattern=None, confidence=0.1,
bitmap=None, file=None, image=None, binary=-1)
- 参数
| 参数 | 类型 | 是否必填 | 说明 |
|---|---|---|---|
| rect | list | 否 | 圈定屏幕范围 |
| pattern | str | 否 | 正则表达式 |
| confidence | float | 否 | 可信度,默认0.1 |
| bitmap | PIL.Image | 否 | 要识别的图片对象 |
| image | PIL.Image | 否 | 要识别的图片,默认屏幕截图 |
| file | str | 否 | 要识别的图片路径 |
| binary | int | 否 | 二值化阈值,-1为不启用(默认) |
- 示例
from ascript.ios.screen import Ocr
res = Ocr.vision(rect=[0, 0, 500, 200])
for r in res:
print(r['text'])
MLKit 识别
基于 Google MLKit OCR。
Ocr.mlkitocr_v2(rect=None, pattern=None, confidence=0.5,
bitmap=None, binary=-1, image=None)
- 参数
| 参数 | 类型 | 是否必填 | 说明 |
|---|---|---|---|
| rect | list | 否 | 圈定屏幕范围 |
| pattern | str | 否 | 正则表达式 |
| confidence | float | 否 | 可信度,默认0.5 |
| bitmap | PIL.Image | 否 | 要识别的图片对象 |
| image | PIL.Image | 否 | 要识别的图片,默认屏幕截图 |
| binary | int | 否 | 二值化阈值,-1为不启用(默认) |
- 示例
from ascript.ios.screen import Ocr
res = Ocr.mlkitocr_v2(rect=[0, 0, 500, 200])
for r in res:
print(r['text'])
设置默认引擎
设置全局默认 OCR 引擎,设置后 find/exists/click 等便捷方法将使用该引擎。
Ocr.set_engine(engine)
| 参数 | 类型 | 说明 |
|---|---|---|
| engine | str/int | "paddle" / "vision" / "mlkit" 或 int 常量 |
引擎常量:
| 常量 | 值 | 说明 |
|---|---|---|
| Ocr.MODE_PADDLE_V5 | 6 | PaddleOCR V5(默认) |
| Ocr.MODE_VISION | 5 | Apple Vision |
| Ocr.MODE_MLK | 1 | Google MLKit |
from ascript.ios.screen import Ocr
Ocr.set_engine("vision") # 切换到 Apple Vision
便捷方法
以下方法使用默认引擎(可通过 set_engine 切换),提供常用的查找和操作功能。
查找文字
查找第一个匹配的文字结果。
Ocr.find(text, rect=None, mode=None, binary=-1, confidence=0.1, image=None)
| 参数 | 类型 | 是否必填 | 说明 |
|---|---|---|---|
| text | str | 是 | 要查找的文字(正则表达式 ) |
| rect | list | 否 | 圈定屏幕范围 |
| mode | int | 否 | OCR 引擎模式,不传则使用默认引擎 |
| binary | int | 否 | 二值化阈值,-1为不启用 |
| confidence | float | 否 | 可信度,默认0.1 |
| image | PIL.Image | 否 | 要识别的图片,默认屏幕截图 |
- 返回值:匹配的结果字典,未找到返回 None
from ascript.ios.screen import Ocr
r = Ocr.find("设置")
if r:
print(r['text'], r['center_x'], r['center_y'])
查找全部文字
Ocr.find_all(text=None, rect=None, mode=None, binary=-1, confidence=0.1, image=None)
| 参数 | 类型 | 是否必填 | 说明 |
|---|---|---|---|
| text | str | 否 | 正则表达式过滤,None 返回全部 |
| rect | list | 否 | 圈定屏幕范围 |
| mode | int | 否 | OCR 引擎模式,不传则使用默认引擎 |
| binary | int | 否 | 二值化阈值,-1为不启用 |
| confidence | float | 否 | 可信度,默认0.1 |
| image | PIL.Image | 否 | 要识别的图片 |
- 返回值:结果字典列表
from ascript.ios.screen import Ocr
results = Ocr.find_all("\\d+", rect=[0, 0, 500, 500])
for r in results:
print(r['text'])
判断文字是否存在
Ocr.exists(text, rect=None, mode=None, binary=-1, confidence=0.1, image=None)
- 返回值:bool
from ascript.ios.screen import Ocr
if Ocr.exists("确定"):
print("找到了")
查找文字并点击
Ocr.click(text, rect=None, mode=None, binary=-1, confidence=0.1, timeout=0, image=None)
找到文字后自动点击其中心点。
- 返回值:bool,True 表示找到并点击成功
from ascript.ios.screen import Ocr
Ocr.click("确定")
等待文字出现
Ocr.wait(text, timeout=10, rect=None, mode=None, binary=-1, confidence=0.1, image=None)
| 参数 | 类型 | 是否必填 | 说明 |
|---|---|---|---|
| text | str | 是 | 要等待的文字 |
| timeout | float | 否 | 超时时间(秒),默认10秒 |
| rect | list | 否 | 圈定屏幕范围 |
| mode | int | 否 | OCR 引擎模式,不传则使用默认引擎 |
| binary | int | 否 | 二值化阈值,-1为不启用 |
| confidence | float | 否 | 可信度,默认0.1 |
| image | PIL.Image | 否 | 要识别的图片 |
- 返回值:匹配的结果字典,超时返回 None
from ascript.ios.screen import Ocr
r = Ocr.wait("加载完成", timeout=15)
if r:
print("页面已加载")
等待文字并点击
Ocr.wait_click(text, timeout=10, rect=None, mode=None, binary=-1, confidence=0.1, image=None)
等待文字出现后自动点击。
- 返回值:bool,True 表示找到并点击成功
from ascript.ios.screen import Ocr
Ocr.wait_click("同意", timeout=5)
点阵字体识别
Ocr.matrix(font_lib, rect=None, region=0.9, image=None)
| 参数 | 类型 | 是否必填 | 说明 |
|---|---|---|---|
| font_lib | str | 是 | 点阵字库路径 |
| rect | list | 否 | 圈定屏幕范围 |
| region | float | 否 | 匹配精度,默认0.9 |
| image | PIL.Image | 否 | 要识别的图片 |
from ascript.ios.screen import Ocr
from ascript.ios.system import R
text = Ocr.matrix(R.res("font.ini"), rect=[100, 200, 300, 250])
print(text)