BetterGenshinImpact/Docs/recognition-json.md
本文档说明各任务目录下 Assets\Recognition.json 的写法,用于将普通静态 RecognitionObject 配置化,并通过 RecognitionAssets.Get(...) 动态加载。
每个任务自己的识别配置文件放在:
BetterGenshinImpact\GameTask\<任务名>\Assets\Recognition.json
例如:
BetterGenshinImpact\GameTask\AutoSkip\Assets\Recognition.json
BetterGenshinImpact\GameTask\Common\Element\Assets\Recognition.json
调用方式通常是:
RecognitionAssets.Get("AutoSkip", "Collect", region);
RecognitionAssets.Get("AutoFight", "Confirm", width, height);
ElementRecognition.Get("PaimonMenu", region);
其中第二个参数就是 objects 下的对象名。
{
"version": 1,
"vars": {},
"regions": {},
"templates": {},
"objects": {}
}
各字段含义:
version:配置版本号,目前固定写 1vars:公共变量,供表达式复用regions:公共区域表达式,供 roi / reference.bbox 通过 @别名 引用templates:公共模板别名,供 template 通过 @别名 引用objects:识别对象定义集合{
"version": 1,
"regions": {
"topLeftQuarter": "rect(0, 0, cw / 4, ch / 4)"
},
"objects": {
"PaimonMenu": {
"type": "TemplateMatch",
"template": "paimon_menu.png",
"roi": "@topLeftQuarter",
"draw": false
}
}
}
说明:
PaimonMenu 是对象名,也是调用时的 objectNametype 必须与 RecognitionTypes 枚举文本完全一致template 是模板文件名roi 可以直接写表达式,也可以引用 regions 中的别名name,运行时名称默认使用模板名,因此通常可以省略roi、reference.bbox、vars 中的值都支持表达式,底层由 NCalc 解析。
cw:当前截图宽度ch:当前截图高度cx:当前截图区域左上角 Xcy:当前截图区域左上角 Ys:资源缩放系数
s = cw / 1920s = 1rect(x, y, w, h):创建矩形cutLeft(percent):截取左侧比例区域cutRight(percent):截取右侧比例区域cutTop(percent):截取上侧比例区域cutBottom(percent):截取下侧比例区域cutLeftTop(widthPercent, heightPercent):截取左上区域cutRightTop(widthPercent, heightPercent):截取右上区域cutLeftBottom(widthPercent, heightPercent):截取左下区域cutRightBottom(widthPercent, heightPercent):截取右下区域{
"vars": {
"topBarHeight": "100 * s"
},
"regions": {
"inventoryTopRight": "rect(cw * 3 / 4, 0, cw / 4, topBarHeight)",
"pageCloseWhite": "rect(cw - cw / 8, 0, cw / 8, ch / 8)",
"leftBottom": "cutLeftBottom(0.2, 0.2)"
}
}
在 regions 中先定义:
"regions": {
"option": "rect(cw / 2, ch / 12, cw - cw / 2 - cw / 6, ch - ch / 12 - 10)"
}
再在对象里引用:
"roi": "@option"
在 templates 中先定义:
"templates": {
"pageClose": "page_close.png"
}
再在对象里引用:
"template": "@pageClose"
适合多个对象共用同一模板,或者模板文件名比较长的时候。
下面只列常用和当前加载器支持的字段。
name
type
RecognitionTypes 一致TemplateMatchColorMatchOcrMatchOcrColorRangeAndOcrroi
Rect 的表达式,或 @区域别名draw
drawColor
#FF0000drawWidth
适用于 type = "TemplateMatch"。
template
@模板别名templateMode
ImreadModesColorthreshold
0.8use3Channels
templateMatchMode
TemplateMatchModesCCoeffNormedCCorrNormedSqDiffuseMask
maskColor
maxMatchCount
useBinaryMatch
binaryThreshold
示例:
"SubmitGoods": {
"type": "TemplateMatch",
"template": "submit_goods.png",
"roi": "rect(0, 0, cw / 2, ch / 3)",
"threshold": 0.9,
"use3Channels": true,
"templateMatchMode": "CCorrNormed",
"draw": true
}
适用于颜色范围识别相关场景。
colorCode
ColorConversionCodesBGR2RGBBGR2HSVBGR2GRAYlowerColor
upperColor
matchCount
示例:
"SomeColorMark": {
"type": "ColorMatch",
"roi": "rect(0, 0, cw / 4, ch / 4)",
"colorCode": "BGR2HSV",
"lowerColor": [90, 80, 80],
"upperColor": [130, 255, 255],
"matchCount": 10
}
适用于 type = "Ocr" 或 type = "OcrMatch"。
ocrEngine
OcrEngineTypesPaddletext
replace
allContains
oneContains
regex
示例:
"PlayingText": {
"type": "OcrMatch",
"roi": "rect(100 * s, 35 * s, 85 * s, 35 * s)",
"oneContains": [
"播放",
"暂停",
"继续"
],
"draw": true
}
replace 示例:
"replace": {
"播放": ["播故", "搰放"],
"继续": ["绫续"]
}
reference 用于记录模板截图来源信息,便于后续按参考尺寸或原始包围盒做定位扩展。
字段:
size
[width, height]bbox
Rect 的表达式示例:
"reference": {
"size": [1920, 1080],
"bbox": "rect(1680, 32, 180, 72)"
}
search 用于描述参考画布搜索框、响应式锚点和额外扩展区域。
仅当对象同时配置了 reference.size 和 reference.bbox,且没有显式 roi 时,参考画布搜索才会生效。
字段:
anchor
SearchAnchorMode 枚举名Auto、TopLeft、TopRight、BottomLeft、BottomRight、CenterAuto 根据 reference.bbox 所在区域模拟游戏 UI 的响应式布局box
Rect 的表达式,也可写成 @区域别名reference.size 对应的参考画布坐标系reference.bbox 使用完全相同的缩放和锚定转换reference.bbox 作为基础搜索框expand
[width, height]width 像素、上下扩展 height 像素expandPercent 时本字段和默认 10px 均不生效expandPercent
0.05 表示 5%Thickness
[all]:四边使用相同比例[horizontal, vertical]:左右、上下[left, top, right, bottom]:左、上、右、下1expand;显式写 [0] 表示不扩展示例:
"search": {
"anchor": "Center",
"expand": [120, 80]
}
带独立搜索框和非对称百分比扩展的示例:
"reference": {
"size": [1920, 1080],
"bbox": "rect(1680, 32, 48, 48)"
},
"search": {
"anchor": "TopRight",
"box": "rect(1540, 0, 380, 160)",
"expandPercent": [0.02, 0.01, 0.03, 0.01]
}
上例的 expandPercent 按“左、上、右、下”解释:左侧扩展截图宽度的 2%,上侧扩展截图高度的 1%,右侧扩展截图宽度的 3%,下侧扩展截图高度的 1%。
type、template、roi、thresholdregionstemplatesXXXAssets 字段里缓存,直接调用 RecognitionAssets.Get(...)name下面这些仍然更适合保留在 C# 里:
这类对象可以继续保留在对应任务类或专用 helper 中。
如果加载失败,当前日志会输出类似:
Recognition 加载失败: Collect @ 1920x1080, file=...
Recognition 构建失败: Collect @ 1920x1080
建议优先检查:
type 是否和枚举名完全一致templateMatchMode、templateMode、colorCode、ocrEngine、anchor 是否和对应枚举名完全一致template 引用的模板文件或模板别名是否存在roi / bbox 表达式是否能返回 Rect@区域别名、@模板别名、vars 引用的名字是否存在lowerColor / upperColor 数组长度是否在 1 到 4 之间{
"version": 1,
"vars": {
"topBarHeight": "100 * s"
},
"regions": {
"topLeftQuarter": "rect(0, 0, cw / 4, ch / 4)",
"inventoryTopRight": "rect(cw * 3 / 4, 0, cw / 4, topBarHeight)"
},
"templates": {
"menu": "paimon_menu.png"
},
"objects": {
"PaimonMenu": {
"type": "TemplateMatch",
"template": "@menu",
"roi": "@topLeftQuarter",
"threshold": 0.8,
"draw": false
},
"Inventory": {
"type": "TemplateMatch",
"template": "inventory.png",
"roi": "@inventoryTopRight",
"draw": false
},
"ConfirmText": {
"type": "OcrMatch",
"roi": "rect(cw / 2, ch / 2, 200 * s, 80 * s)",
"oneContains": ["确认", "确定"]
}
}
}