feat: 为两个类型神经网络识别算法的expected字段增加str类型支持 - #1474
Conversation
There was a problem hiding this comment.
您好——我发现了 2 个问题
AI Agent 提示词
请处理此次代码审查中的评论:
## 具体评论
### 评论 1
<location path="source/MaaFramework/Vision/NeuralNetworkClassifier.cpp" line_range="174-180" />
<code_context>
+ if (std::holds_alternative<int>(item)) {
+ int idx = std::get<int>(item);
+ // 校验索引有效性
+ if (idx >= 0 && idx < static_cast<int>(param_.labels.size())) {
+ expected_indices_.push_back(idx);
+ }
+ else {
+ LogWarn << "Invalid index in expected" << VAR(idx) << VAR(param_.labels.size());
+ }
+ }
</code_context>
<issue_to_address>
**issue (broader_impact):** 当 `labels` 为空或短于请求的索引时,数值型的 `expected` 值会被丢弃。这会使之前的行为发生回归,因为文档说明 `labels` 仅用于输出/调试;因此,在省略 labels 时,有效的数值类索引将无法通过筛选,也无法用于 expected 顺序排序。
**触发条件:** 分类器包含整数型 `expected` 值但没有 labels,或者检测器使用整数索引而其元数据没有提供对应的 labels。
**建议修复:** 不要根据可选的 labels 向量验证整数索引;保留有效的非负模型索引,并且仅通过 labels 解析字符串值。
```suggestion
// 校验索引有效性
if (idx >= 0) {
expected_indices_.push_back(idx);
}
else {
LogWarn << "Invalid index in expected" << VAR(idx);
}
```
</issue_to_address>
### 评论 2
<location path="source/MaaFramework/Vision/NeuralNetworkDetector.cpp" line_range="367-383" />
<code_context>
+ if (std::holds_alternative<int>(item)) {
+ int idx = std::get<int>(item);
+ // 校验索引有效性
+ if (idx >= 0 && idx < static_cast<int>(labels.size())) {
+ expected_indices_.push_back(idx);
+ }
+ else {
+ LogWarn << "Invalid index in expected" << VAR(idx) << VAR(labels.size());
</code_context>
<issue_to_address>
**issue (bug_risk):** 无效的整数索引和未知的字符串 labels 会从 `expected_indices_` 中被静默移除,但 `thresholds` 仍然与原始的 `expected` 数组保持对齐。因此,后续的大小检查会失败,`add_results` 会直接返回而不生成筛选结果,即使剩余的 expected 条目及其 thresholds 都是有效的。
**触发条件:** 当 `expected` 包含超出范围的索引或不在 `labels` 中的字符串时,尤其是在混合的 expected 列表中。
**建议修复:** 在解析期间拒绝无效的 expected 条目,或者保留其位置并报告无效配置,避免静默改变 expected 与 threshold 的数量对应关系;同时针对解析后的列表一致地比较并扩展 thresholds。
</issue_to_address>Original comment in English
Hey - I've found 2 issues
Prompt for AI Agents
Please address the comments from this code review:
## Individual Comments
### Comment 1
<location path="source/MaaFramework/Vision/NeuralNetworkClassifier.cpp" line_range="174-180" />
<code_context>
+ if (std::holds_alternative<int>(item)) {
+ int idx = std::get<int>(item);
+ // 校验索引有效性
+ if (idx >= 0 && idx < static_cast<int>(param_.labels.size())) {
+ expected_indices_.push_back(idx);
+ }
+ else {
+ LogWarn << "Invalid index in expected" << VAR(idx) << VAR(param_.labels.size());
+ }
+ }
</code_context>
<issue_to_address>
**issue (broader_impact):** Numeric `expected` values are discarded whenever `labels` is empty or shorter than the requested index. This regresses the previous behavior because `labels` is documented as output/debug-only, so a valid numeric class index no longer passes filtering or expected-order sorting when labels are omitted.
**Triggers:** When a classifier has an integer `expected` value but no labels, or a detector uses integer indices while its metadata provides no corresponding labels.
**Suggested fix:** Do not validate integer indices against the optional labels vector; preserve valid non-negative model indices, and only resolve string values through labels.
```suggestion
// 校验索引有效性
if (idx >= 0) {
expected_indices_.push_back(idx);
}
else {
LogWarn << "Invalid index in expected" << VAR(idx);
}
```
</issue_to_address>
### Comment 2
<location path="source/MaaFramework/Vision/NeuralNetworkDetector.cpp" line_range="367-383" />
<code_context>
+ if (std::holds_alternative<int>(item)) {
+ int idx = std::get<int>(item);
+ // 校验索引有效性
+ if (idx >= 0 && idx < static_cast<int>(labels.size())) {
+ expected_indices_.push_back(idx);
+ }
+ else {
+ LogWarn << "Invalid index in expected" << VAR(idx) << VAR(labels.size());
</code_context>
<issue_to_address>
**issue (bug_risk):** Invalid integer indices and unknown string labels are silently removed from `expected_indices_`, but `thresholds` remains aligned to the original `expected` array. The subsequent size check therefore fails and `add_results` returns without producing filtered results, even when the remaining expected entries and thresholds are valid.
**Triggers:** When `expected` contains an out-of-range index or a string not present in `labels`, especially in a mixed expected list.
**Suggested fix:** Reject invalid expected entries during parsing, or retain positional entries and report the invalid configuration without silently changing the expected/threshold cardinality; compare and expand thresholds against the resolved list consistently.
</issue_to_address>| if (idx >= 0 && idx < static_cast<int>(labels.size())) { | ||
| expected_indices_.push_back(idx); | ||
| } | ||
| else { | ||
| LogWarn << "Invalid index in expected" << VAR(idx) << VAR(labels.size()); | ||
| } | ||
| } | ||
| else if (std::holds_alternative<std::string>(item)) { | ||
| const std::string& label = std::get<std::string>(item); | ||
| auto it = std::find(labels.begin(), labels.end(), label); | ||
| if (it != labels.end()) { | ||
| int idx = static_cast<int>(std::distance(labels.begin(), it)); | ||
| expected_indices_.push_back(idx); | ||
| } | ||
| else { | ||
| LogWarn << "Label not found in labels list" << VAR(label); | ||
| } |
There was a problem hiding this comment.
issue (bug_risk): 无效的整数索引和未知的字符串 labels 会从 expected_indices_ 中被静默移除,但 thresholds 仍然与原始的 expected 数组保持对齐。因此,后续的大小检查会失败,add_results 会直接返回而不生成筛选结果,即使剩余的 expected 条目及其 thresholds 都是有效的。
触发条件: 当 expected 包含超出范围的索引或不在 labels 中的字符串时,尤其是在混合的 expected 列表中。
建议修复: 在解析期间拒绝无效的 expected 条目,或者保留其位置并报告无效配置,避免静默改变 expected 与 threshold 的数量对应关系;同时针对解析后的列表一致地比较并扩展 thresholds。
Original comment in English
issue (bug_risk): Invalid integer indices and unknown string labels are silently removed from expected_indices_, but thresholds remains aligned to the original expected array. The subsequent size check therefore fails and add_results returns without producing filtered results, even when the remaining expected entries and thresholds are valid.
Triggers: When expected contains an out-of-range index or a string not present in labels, especially in a mixed expected list.
Suggested fix: Reject invalid expected entries during parsing, or retain positional entries and report the invalid configuration without silently changing the expected/threshold cardinality; compare and expand thresholds against the resolved list consistently.
概述
在
NeuralNetworkClassify和NeuralNetworkDetect任务中,expected字段只能使用数字索引,现为该字段增加str类型支持。Closes #1462
改动内容
C++核心
expected类型从std::vector<int>改为std::vector<std::variant<int, std::string>>PipelineParser支持解析字符串类型的expected值expected对应的索引,使用Init_expected初始化Python Binding
expected类型改为List[Union[int, str]]TypeScript Binding
expected类型改为MaybeArray<number | string, Mode>修改的文件
source/MaaFramework/Resource/PipelineParser.cppsource/MaaFramework/Resource/PipelineTypesV2.hsource/MaaFramework/Vision/NeuralNetworkClassifier.cppsource/MaaFramework/Vision/NeuralNetworkClassifier.hsource/MaaFramework/Vision/NeuralNetworkDetector.cppsource/MaaFramework/Vision/NeuralNetworkDetector.hsource/MaaFramework/Vision/VisionTypes.hsource/binding/NodeJS/src/apis/pipeline.d.tssource/binding/Python/maa/pipeline.pySourcery 总结
允许神经网络分类和检测流水线通过数字索引或标签名称选择预期结果。
新功能:
expected字段中使用字符串标签名称,同时支持数字索引。expected类型。增强功能:
Original summary in English
Sourcery 摘要
扩展神经网络分类和检测任务中的
expected字段,使其支持按索引或标签名称选择预期结果。新功能:
expected结果。增强:
expected标签统一解析为模型索引,并用于结果筛选、阈值匹配和预期顺序排序,同时对无效索引和未知标签发出警告。维护:
expected类型定义,以支持数字或字符串。Original summary in English
Summary by Sourcery
为神经网络分类和检测任务扩展 expected 字段,使其支持按索引或标签名称选择预期结果。
New Features:
Enhancements:
Chores: