Skip to content

feat: 为两个类型神经网络识别算法的expected字段增加str类型支持 - #1474

Open
laoyijiehe wants to merge 2 commits into
MaaXYZ:mainfrom
laoyijiehe:feature/support-expected-string
Open

feat: 为两个类型神经网络识别算法的expected字段增加str类型支持#1474
laoyijiehe wants to merge 2 commits into
MaaXYZ:mainfrom
laoyijiehe:feature/support-expected-string

Conversation

@laoyijiehe

@laoyijiehe laoyijiehe commented Sep 6, 2026

Copy link
Copy Markdown
Contributor

概述

NeuralNetworkClassifyNeuralNetworkDetect 任务中,expected 字段只能使用数字索引,现为该字段增加str类型支持。

Closes #1462

改动内容

C++核心

  • expected 类型从 std::vector<int> 改为 std::vector<std::variant<int, std::string>>
  • PipelineParser 支持解析字符串类型的 expected
  • 在NeuralNetworkClassifier和NeuralNetworkDetector中增加expected_indices存储expected对应的索引,使用Init_expected初始化

Python Binding

  • expected 类型改为 List[Union[int, str]]

TypeScript Binding

  • expected 类型改为 MaybeArray<number | string, Mode>

修改的文件

  • source/MaaFramework/Resource/PipelineParser.cpp
  • source/MaaFramework/Resource/PipelineTypesV2.h
  • source/MaaFramework/Vision/NeuralNetworkClassifier.cpp
  • source/MaaFramework/Vision/NeuralNetworkClassifier.h
  • source/MaaFramework/Vision/NeuralNetworkDetector.cpp
  • source/MaaFramework/Vision/NeuralNetworkDetector.h
  • source/MaaFramework/Vision/VisionTypes.h
  • source/binding/NodeJS/src/apis/pipeline.d.ts
  • source/binding/Python/maa/pipeline.py

Sourcery 总结

允许神经网络分类和检测流水线通过数字索引或标签名称选择预期结果。

新功能:

  • 支持在神经网络分类和检测流水线的 expected 字段中使用字符串标签名称,同时支持数字索引。
  • 在 Python 和 TypeScript 绑定中公开扩展后的 expected 类型。

增强功能:

  • 将预期标签解析为模型索引,用于筛选、阈值匹配和预期顺序排序;对于无效索引或未知标签发出警告。
Original summary in English

Sourcery 摘要

扩展神经网络分类和检测任务中的 expected 字段,使其支持按索引或标签名称选择预期结果。

新功能:

  • 支持神经网络分类和检测任务通过数字索引或字符串标签指定 expected 结果。

增强:

  • expected 标签统一解析为模型索引,并用于结果筛选、阈值匹配和预期顺序排序,同时对无效索引和未知标签发出警告。

维护:

  • 扩展 Python 和 TypeScript 绑定中的 expected 类型定义,以支持数字或字符串。
Original summary in English

Summary by Sourcery

为神经网络分类和检测任务扩展 expected 字段,使其支持按索引或标签名称选择预期结果。

New Features:

  • 支持神经网络分类和检测任务通过数字索引或字符串标签指定 expected 结果。

Enhancements:

  • 将 expected 标签统一解析为模型索引,并用于结果筛选、阈值匹配和预期顺序排序,同时对无效索引和未知标签发出警告。

Chores:

  • 扩展 Python 和 TypeScript 绑定中的 expected 类型定义以支持数字或字符串。

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

您好——我发现了 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>

Sourcery 对开源项目免费——如果您喜欢我们的审查,请考虑分享它们 ✨
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>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨

Comment thread source/MaaFramework/Vision/NeuralNetworkClassifier.cpp
Comment on lines +367 to +383
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);
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Feature Request] 为两个类型神经网络识别算法的expected字段新增str类型支持

1 participant