Skip to content

Commit 0d3d8c8

Browse files
authored
Merge pull request #9 from overflow65537/fix/expression-recognition-docs
fix(customs): 对齐 ExpressionRecognition 注册方式与 README 格式
2 parents 7c6f179 + cb0176e commit 0d3d8c8

2 files changed

Lines changed: 49 additions & 61 deletions

File tree

Storage/customs/overflow65537/expression-recognition/ExpressionRecognition.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import re
55
from typing import Any
66

7+
from maa.agent.agent_server import AgentServer
78
from maa.custom_recognition import CustomRecognition
89
from maa.define import OCRResult
910

@@ -19,6 +20,7 @@ def __init__(self, message: str, payload: dict[str, Any]):
1920
self.payload = payload
2021

2122

23+
@AgentServer.custom_recognition("ExpressionRecognition")
2224
class ExpressionRecognition(CustomRecognition):
2325

2426
def analyze(

Storage/customs/overflow65537/expression-recognition/README.md

Lines changed: 47 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -2,90 +2,76 @@
22

33
自定义识别器:在 pipeline 里用 `{节点名}` 引用其他节点的 OCR 数值,通过表达式做算术与逻辑比较,结果为 `true` 时识别命中。
44

5-
适用场景:当前值是否达标、多路 OCR 数值加权求和后再判断等,不必为每种组合单独写节点。
5+
## 功能
66

7-
## 文件说明
7+
- 引用其他 pipeline 节点的 OCR 数值参与计算
8+
- 支持算术、比较与逻辑表达式
9+
- 适用场景:当前值是否达标、多路 OCR 数值加权求和后再判断等
810

9-
| 文件 | 说明 |
10-
|------|------|
11-
| `ExpressionRecognition.py` | 识别器实现 |
12-
| `pipeline.json` | 最小 pipeline 示例 |
13-
| `maahub_meta.json` | MaaHub 元信息 |
11+
## 文件说明
1412

15-
## 注册方式
13+
- `ExpressionRecognition.py`:识别器实现
14+
- `pipeline.json`:最小 pipeline 示例
1615

17-
`ExpressionRecognition.py` 放入 agent 工程,在 agent 入口注册:
16+
## 使用方式
1817

19-
```python
20-
from maa.agent.agent_server import AgentServer
21-
from ExpressionRecognition import ExpressionRecognition
18+
先准备 OCR 数值节点,再在 Custom 识别节点中引用:
2219

23-
@AgentServer.custom_recognition("ExpressionRecognition")
24-
class Agent_ExpressionRecognition(ExpressionRecognition):
25-
pass
2620
```
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
4426
},
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+
}
4741
```
4842

4943
多节点加权示例:
5044

51-
```json
45+
```
5246
"加权分数计算": {
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-目标数值}"
6051
}
6152
}
62-
}
6353
```
6454

65-
完整可运行示例见同目录 `pipeline.json`
66-
67-
## 表达式语法
55+
`custom_recognition_param` 参数:
6856

69-
- **占位符**`{节点名}`,运行时对该节点执行识别,从 OCR 结果中提取第一个数字(支持整数与小数)
70-
- **运算符**`+` `-` `*` `/` `%`,比较 `>` `>=` `<` `<=` `==` `!=`,逻辑 `&&` `||` `!`(会转换为 Python 的 `and` / `or` / `not`
71-
- **返回值**:表达式必须求值为 **布尔值**;为 `true` 时返回固定 box `(0, 0, 100, 100)` 表示命中
57+
- `expression`:表达式字符串,`{节点名}` 为占位符,运行时对该节点 OCR 并提取第一个数字
7258

73-
被引用的节点需能 OCR 出数字文本。若节点使用 `And` 等多结果识别,可在该节点配置 `box_index` 指定取第几个子结果。
59+
表达式语法:
7460

75-
## 识别结果 detail
61+
- **占位符**`{节点名}`,支持整数与小数
62+
- **运算符**`+` `-` `*` `/` `%`,比较 `>` `>=` `<` `<=` `==` `!=`,逻辑 `&&` `||` `!`
63+
- **返回值**:表达式必须求值为布尔值;为 `true` 时识别命中
7664

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`
8766

8867
## 依赖
8968

9069
- Python 3.12+
9170
- 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

Comments
 (0)