|
2 | 2 |
|
3 | 3 | 自定义识别器:在 pipeline 里用 `{节点名}` 引用其他节点的 OCR 数值,通过表达式做算术与逻辑比较,结果为 `true` 时识别命中。 |
4 | 4 |
|
5 | | -适用场景:当前值是否达标、多路 OCR 数值加权求和后再判断等,不必为每种组合单独写节点。 |
| 5 | +## 功能 |
6 | 6 |
|
7 | | -## 文件说明 |
| 7 | +- 引用其他 pipeline 节点的 OCR 数值参与计算 |
| 8 | +- 支持算术、比较与逻辑表达式 |
| 9 | +- 适用场景:当前值是否达标、多路 OCR 数值加权求和后再判断等 |
8 | 10 |
|
9 | | -| 文件 | 说明 | |
10 | | -|------|------| |
11 | | -| `ExpressionRecognition.py` | 识别器实现 | |
12 | | -| `pipeline.json` | 最小 pipeline 示例 | |
13 | | -| `maahub_meta.json` | MaaHub 元信息 | |
| 11 | +## 文件说明 |
14 | 12 |
|
15 | | -## 注册方式 |
| 13 | +- `ExpressionRecognition.py`:识别器实现 |
| 14 | +- `pipeline.json`:最小 pipeline 示例 |
16 | 15 |
|
17 | | -将 `ExpressionRecognition.py` 放入 agent 工程,在 agent 入口注册: |
| 16 | +## 使用方式 |
18 | 17 |
|
19 | | -```python |
20 | | -from maa.agent.agent_server import AgentServer |
21 | | -from ExpressionRecognition import ExpressionRecognition |
| 18 | +先准备 OCR 数值节点,再在 Custom 识别节点中引用: |
22 | 19 |
|
23 | | -@AgentServer.custom_recognition("ExpressionRecognition") |
24 | | -class Agent_ExpressionRecognition(ExpressionRecognition): |
25 | | - pass |
26 | 20 | ``` |
27 | | - |
28 | | -注册名 `"ExpressionRecognition"` 需与 pipeline 中 `custom_recognition` 字段一致。 |
29 | | - |
30 | | -## Pipeline 用法 |
31 | | - |
32 | | -在节点上使用 `Custom` 识别,通过 `custom_recognition_param.expression` 传入表达式: |
33 | | - |
34 | | -```json |
35 | | -"数值达标判断": { |
36 | | - "recognition": { |
37 | | - "type": "Custom", |
38 | | - "param": { |
39 | | - "custom_recognition": "ExpressionRecognition", |
40 | | - "custom_recognition_param": { |
41 | | - "expression": "{OCR-当前数值}>={OCR-目标数值}" |
42 | | - } |
43 | | - } |
| 21 | +"OCR-当前数值": { |
| 22 | + "recognition": "OCR", |
| 23 | + "expected": ["^\\d*$"], |
| 24 | + "roi": [100, 200, 80, 40], |
| 25 | + "only_rec": true |
44 | 26 | }, |
45 | | - "next": ["下一步节点"] |
46 | | -} |
| 27 | + "OCR-目标数值": { |
| 28 | + "recognition": "OCR", |
| 29 | + "expected": ["^\\d*$"], |
| 30 | + "roi": [100, 100, 80, 40], |
| 31 | + "only_rec": true |
| 32 | + }, |
| 33 | + "数值达标判断": { |
| 34 | + "recognition": "Custom", |
| 35 | + "custom_recognition": "ExpressionRecognition", |
| 36 | + "custom_recognition_param": { |
| 37 | + "expression": "{OCR-当前数值}>={OCR-目标数值}" |
| 38 | + }, |
| 39 | + "next": ["下一步节点"] |
| 40 | + } |
47 | 41 | ``` |
48 | 42 |
|
49 | 43 | 多节点加权示例: |
50 | 44 |
|
51 | | -```json |
| 45 | +``` |
52 | 46 | "加权分数计算": { |
53 | | - "recognition": { |
54 | | - "type": "Custom", |
55 | | - "param": { |
56 | | - "custom_recognition": "ExpressionRecognition", |
57 | | - "custom_recognition_param": { |
58 | | - "expression": "{OCR-当前数值}*{OCR-倍率A}+{OCR-目标数值}*{OCR-倍率B}>={OCR-目标数值}" |
59 | | - } |
| 47 | + "recognition": "Custom", |
| 48 | + "custom_recognition": "ExpressionRecognition", |
| 49 | + "custom_recognition_param": { |
| 50 | + "expression": "{OCR-当前数值}*{OCR-倍率A}+{OCR-目标数值}*{OCR-倍率B}>={OCR-目标数值}" |
60 | 51 | } |
61 | 52 | } |
62 | | -} |
63 | 53 | ``` |
64 | 54 |
|
65 | | -完整可运行示例见同目录 `pipeline.json`。 |
66 | | - |
67 | | -## 表达式语法 |
| 55 | +`custom_recognition_param` 参数: |
68 | 56 |
|
69 | | -- **占位符**:`{节点名}`,运行时对该节点执行识别,从 OCR 结果中提取第一个数字(支持整数与小数) |
70 | | -- **运算符**:`+` `-` `*` `/` `%`,比较 `>` `>=` `<` `<=` `==` `!=`,逻辑 `&&` `||` `!`(会转换为 Python 的 `and` / `or` / `not`) |
71 | | -- **返回值**:表达式必须求值为 **布尔值**;为 `true` 时返回固定 box `(0, 0, 100, 100)` 表示命中 |
| 57 | +- `expression`:表达式字符串,`{节点名}` 为占位符,运行时对该节点 OCR 并提取第一个数字 |
72 | 58 |
|
73 | | -被引用的节点需能 OCR 出数字文本。若节点使用 `And` 等多结果识别,可在该节点配置 `box_index` 指定取第几个子结果。 |
| 59 | +表达式语法: |
74 | 60 |
|
75 | | -## 识别结果 detail |
| 61 | +- **占位符**:`{节点名}`,支持整数与小数 |
| 62 | +- **运算符**:`+` `-` `*` `/` `%`,比较 `>` `>=` `<` `<=` `==` `!=`,逻辑 `&&` `||` `!` |
| 63 | +- **返回值**:表达式必须求值为布尔值;为 `true` 时识别命中 |
76 | 64 |
|
77 | | -`detail` 中包含调试信息,便于排查: |
78 | | - |
79 | | -| 字段 | 含义 | |
80 | | -|------|------| |
81 | | -| `status` | `success` / `expression evaluated to false` / `invalid expression` 等 | |
82 | | -| `expression` | 原始表达式 | |
83 | | -| `resolved_expression` | 占位符替换后的表达式 | |
84 | | -| `resolved_values` | 各占位符解析出的数值 | |
85 | | -| `node_results` | 各引用节点的识别摘要 | |
86 | | -| `summary` | 上述信息的可读文本 | |
| 65 | +完整示例见同目录 `pipeline.json`。 |
87 | 66 |
|
88 | 67 | ## 依赖 |
89 | 68 |
|
90 | 69 | - Python 3.12+ |
91 | 70 | - MaaFramework Agent SDK(`maa.custom_recognition`) |
| 71 | + |
| 72 | +## 注意事项 |
| 73 | + |
| 74 | +- `custom_recognition` 需与 agent 中注册的识别名一致 |
| 75 | +- 被引用的节点需能 OCR 出数字文本 |
| 76 | +- 若节点使用 `And` 等多结果识别,可在该节点配置 `box_index` 指定取第几个子结果 |
| 77 | +- 识别 `detail` 中包含 `status`、`resolved_expression`、`resolved_values` 等调试信息 |
0 commit comments