百度 PaddleOCR V5
内置免费 OCR 引擎,基于百度 PaddleOCR V5,无需额外配置即可使用。
from ascript.ios.screen import Ocr
识别方法
PaddleOCR 识别
Ocr.paddleocr(rect=None, pattern=None, confidence=0.1, image=None)
- 参数
| 参数 | 类型 | 是否必填 | 说明 |
|---|---|---|---|
| rect | list | 否 | 圈定屏幕范围 |
| pattern | str | 否 | 正则表达式,过滤匹配的文字 |
| confidence | float | 否 | 可信度,默认0.1 |
| image | PIL.Image | 否 | 要识别的图片,默认屏幕 截图 |
| file | str | 否 | 要识别的图片路径 |
| binary | int | 否 | 二值化阈值,-1为不启用(默认) |
- 返回值
文字结果字典列表,每条结果包含:
{
"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, image=None)
- 参数
| 参数 | 类型 | 是否必填 | 说明 |
|---|---|---|---|
| rect | list | 否 | 圈定屏幕范围 |
| pattern | str | 否 | 正则表达式 |
| confidence | float | 否 | 可信度,默认0.1 |
| image | PIL.Image | 否 | 要识别的图片,默认屏幕截图 |
| file | str | 否 | 要识别的图片路径 |
- 示例
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, image=None)
- 参数
| 参数 | 类型 | 是否必填 | 说明 |
|---|---|---|---|
| rect | list | 否 | 圈定屏幕范围 |
| pattern | str | 否 | 正则表达式 |
| confidence | float | 否 | 可信度,默认0.5 |
| 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, confidence=0.1, image=None)
| 参数 | 类型 | 是否必填 | 说明 |
|---|---|---|---|
| text | str | 是 | 要查找的文字(正则表达式) |
| rect | list | 否 | 圈定屏幕范围 |
| 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, confidence=0.1, image=None)
| 参数 | 类型 | 是否必填 | 说明 |
|---|---|---|---|
| text | str | 否 | 正则表达式过滤,None 返回全部 |
| rect | list | 否 | 圈定屏幕范围 |
- 返回值:结果字典列表
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, confidence=0.1, image=None)
- 返回值:bool
from ascript.ios.screen import Ocr
if Ocr.exists("确定"):
print("找到了")