ms-vite-plugin 1.4.39 → 1.4.40
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/docs/api/dotocr.md +160 -0
- package/docs/api/opencv.md +1638 -0
- package/docs/apicn/dotocr.md +161 -0
- package/docs/apipython/dotocr.md +148 -0
- package/package.json +1 -1
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
# 点阵 OCR 模块(dotOcr)
|
|
2
|
+
|
|
3
|
+
`dotOcr` 使用图色工具生成的固定像素点阵字库识别文字。字库保存在项目的 `res/fonts` 目录,使用方式与 YOLO 模型一致:先加载资源取得字库 ID,再复用该 ID 进行识别,使用完毕后释放。
|
|
4
|
+
|
|
5
|
+
点阵模板按照制作时的原始像素尺寸匹配,不会自动缩放到其他分辨率。目标文字的字号、缩放比例或像素形状发生变化时,应制作对应的字库特征。
|
|
6
|
+
|
|
7
|
+
## 功能概览
|
|
8
|
+
|
|
9
|
+
- **字库管理**:加载、缓存和释放项目字库
|
|
10
|
+
- **文字识别**:返回按阅读顺序排列的识别结果数组
|
|
11
|
+
- **文本查找**:先识别再按行定位目标串,返回命中位置的绝对坐标
|
|
12
|
+
- **多源输入**:支持 `"screen"`、图片文件路径、URL 或 imageId
|
|
13
|
+
- **绝对坐标**:裁剪区域内的结果会映射回原图或全屏坐标
|
|
14
|
+
|
|
15
|
+
## 数据类型
|
|
16
|
+
|
|
17
|
+
### OCRChar
|
|
18
|
+
|
|
19
|
+
```typescript
|
|
20
|
+
interface OCRChar {
|
|
21
|
+
text: string;
|
|
22
|
+
confidence: number;
|
|
23
|
+
x: number;
|
|
24
|
+
y: number;
|
|
25
|
+
ex: number;
|
|
26
|
+
ey: number;
|
|
27
|
+
width: number;
|
|
28
|
+
height: number;
|
|
29
|
+
centerX: number;
|
|
30
|
+
centerY: number;
|
|
31
|
+
}
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
| 字段名 | 类型 | 描述 |
|
|
35
|
+
| -------------------- | ------ | -------------------------------------- |
|
|
36
|
+
| `OCRChar.text` | string | 识别文本;查找结果中为命中的目标字符串 |
|
|
37
|
+
| `confidence` | number | 置信度,范围为 0-1 |
|
|
38
|
+
| `x`, `y` | number | 结果区域左上角绝对坐标 |
|
|
39
|
+
| `ex`, `ey` | number | 结果区域右下角绝对坐标 |
|
|
40
|
+
| `width`, `height` | number | 结果区域宽度和高度 |
|
|
41
|
+
| `centerX`, `centerY` | number | 结果区域中心点绝对坐标 |
|
|
42
|
+
|
|
43
|
+
## API 参考
|
|
44
|
+
|
|
45
|
+
### 字库管理
|
|
46
|
+
|
|
47
|
+
#### loadFont - 加载并缓存字库
|
|
48
|
+
|
|
49
|
+
```typescript
|
|
50
|
+
function loadFont(fontPath: string): string | null;
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
| 参数名 | 类型 | 是否必填 | 默认值 | 描述 |
|
|
54
|
+
| ---------- | ------ | -------- | ------ | --------------------------------------------------------------------------- |
|
|
55
|
+
| `fontPath` | string | 是 | | 资源路径,运行时会自动处理 `res` 目录解析;常见写法如 `/fonts/default.json` |
|
|
56
|
+
|
|
57
|
+
**返回值:**加载成功返回 `fontId`,失败返回 `null`。加载后的字库 ID 会复用到后续识别与释放流程。
|
|
58
|
+
|
|
59
|
+
```javascript
|
|
60
|
+
const fontId = dotOcr.loadFont("/fonts/default.json");
|
|
61
|
+
if (!fontId) throw new Error("字库加载失败");
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
建议在脚本初始化时加载一次并保存 `fontId`,后续识别时复用,不要每次识别都重新加载。
|
|
65
|
+
|
|
66
|
+
### 文字识别
|
|
67
|
+
|
|
68
|
+
#### recognizeAbs - 识别文字并返回绝对坐标
|
|
69
|
+
|
|
70
|
+
```typescript
|
|
71
|
+
function recognizeAbs(
|
|
72
|
+
fontId: string,
|
|
73
|
+
input: string,
|
|
74
|
+
x?: number,
|
|
75
|
+
y?: number,
|
|
76
|
+
ex?: number,
|
|
77
|
+
ey?: number,
|
|
78
|
+
confidenceThreshold?: number,
|
|
79
|
+
): OCRChar[];
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
| 参数名 | 类型 | 是否必填 | 默认值 | 描述 |
|
|
83
|
+
| --------------------- | ------ | -------- | ------ | -------------------------------------------------------------------------------------------- |
|
|
84
|
+
| `fontId` | string | 是 | | `loadFont` 返回的字库 ID |
|
|
85
|
+
| `input` | string | 是 | | 输入源,支持 `"screen"`、图片文件路径、URL 或 imageId |
|
|
86
|
+
| `x`, `y` | number | 否 | 0 | 裁剪区域左上角坐标 |
|
|
87
|
+
| `ex`, `ey` | number | 否 | 0 | 裁剪区域右下角坐标;`ex` 或 `ey` 为 0 时分别使用图像宽度或高度 |
|
|
88
|
+
| `confidenceThreshold` | number | 否 | 0.8 | 相似度下限;`<=0` 时按 `0.8`;低于阈值的字符会被过滤;`>0.95` 时要求模板与图像像素完全一致 |
|
|
89
|
+
|
|
90
|
+
**返回值:**`OCRChar[]`。按行从上到下、同一行从左到右排列。颜色按字库各字自带的前景色/偏色匹配,无需额外传色。
|
|
91
|
+
|
|
92
|
+
```javascript
|
|
93
|
+
const chars = dotOcr.recognizeAbs(fontId, "screen", 100, 200, 600, 280, 0.8);
|
|
94
|
+
console.log(chars[0]?.text, chars[0]?.x, chars[0]?.width);
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
### 文本查找
|
|
98
|
+
|
|
99
|
+
#### findTextAbs - 查找指定字符串并返回绝对坐标
|
|
100
|
+
|
|
101
|
+
```typescript
|
|
102
|
+
function findTextAbs(
|
|
103
|
+
fontId: string,
|
|
104
|
+
input: string,
|
|
105
|
+
targetTexts: string[],
|
|
106
|
+
x?: number,
|
|
107
|
+
y?: number,
|
|
108
|
+
ex?: number,
|
|
109
|
+
ey?: number,
|
|
110
|
+
confidenceThreshold?: number,
|
|
111
|
+
exactMatch?: boolean,
|
|
112
|
+
): OCRChar[];
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
| 参数名 | 类型 | 是否必填 | 默认值 | 描述 |
|
|
116
|
+
| --------------------- | -------- | -------- | ------ | --------------------------------------------------------------------- |
|
|
117
|
+
| `fontId` | string | 是 | | `loadFont` 返回的字库 ID |
|
|
118
|
+
| `input` | string | 是 | | 输入源,支持 `"screen"`、图片文件路径、URL 或 imageId |
|
|
119
|
+
| `targetTexts` | string[] | 是 | | 要查找的字符串数组,例如 `["商店", "背包"]` |
|
|
120
|
+
| `x`, `y` | number | 否 | 0 | 裁剪区域左上角坐标 |
|
|
121
|
+
| `ex`, `ey` | number | 否 | 0 | 裁剪区域右下角坐标;`ex` 或 `ey` 为 0 时分别使用图像宽度或高度 |
|
|
122
|
+
| `confidenceThreshold` | number | 否 | 0.8 | 单字符识别相似度下限;语义同 `recognizeAbs` |
|
|
123
|
+
| `exactMatch` | boolean | 否 | false | `false` 为行内子串包含匹配;`true` 要求某一整行文本完全等于目标字符串 |
|
|
124
|
+
|
|
125
|
+
**返回值:**命中结果数组。每个目标最多返回一次命中(最先匹配到的行);每项的 `text` 是目标字符串。
|
|
126
|
+
|
|
127
|
+
```javascript
|
|
128
|
+
const hits = dotOcr.findTextAbs(
|
|
129
|
+
fontId,
|
|
130
|
+
"screen",
|
|
131
|
+
["商店", "背包"],
|
|
132
|
+
100,
|
|
133
|
+
200,
|
|
134
|
+
600,
|
|
135
|
+
280,
|
|
136
|
+
0.8,
|
|
137
|
+
false,
|
|
138
|
+
);
|
|
139
|
+
if (hits.length > 0) action.click(hits[0].centerX, hits[0].centerY);
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
### 资源管理
|
|
143
|
+
|
|
144
|
+
#### free - 释放指定字库
|
|
145
|
+
|
|
146
|
+
```typescript
|
|
147
|
+
function free(fontId: string): void;
|
|
148
|
+
```
|
|
149
|
+
|
|
150
|
+
#### freeAll - 释放全部点阵字库
|
|
151
|
+
|
|
152
|
+
```typescript
|
|
153
|
+
function freeAll(): void;
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
```javascript
|
|
157
|
+
dotOcr.free(fontId);
|
|
158
|
+
// 或在需要清理全部已加载的点阵字库时:
|
|
159
|
+
dotOcr.freeAll();
|
|
160
|
+
```
|