移除AI确认勾选并强制所有步骤经AI确认后推进

This commit is contained in:
tfarcraw 2026-11-13 09:24:01 +08:00
parent cf7090cdd8
commit 337827a0b2
8 changed files with 46 additions and 23 deletions

View File

@ -23,7 +23,9 @@ pwsh -File tools/Start-HandAiD3.ps1 -CheckOnly
脚本只在 SOP 子进程使用 D3 配置,返回时恢复调用窗口原有环境变量。服务关闭界面后继续保留,启动日志在 `artifacts/d3-integration/`。要连接其他机器上已启动的 D3 服务,可设置 `SOPHELPER_VISION_PROVIDER=d3-lite` 和完整的 `SOPHELPER_VISION_ENDPOINT`;这不会自动部署远端服务。
步骤编辑器中勾选“手部动作触发后由 AI 确认完成”,填写自然语言完成条件,例如“盒子里面放着零件”。选择“送检区域”后,只发送该区域;留空才发送整帧。按原始相机坐标先裁剪,再把长边缩至最多 512 像素,不放大小图。区域不存在、坐标越界或编码失败会阻止发送,不会回退整图。
每个步骤默认必须经过 AI 确认,编辑器不再提供启用/关闭勾选。直接填写自然语言完成条件,例如“盒子里面放着零件”。选择“送检区域”后,只发送该区域;留空才发送整帧。按原始相机坐标先裁剪,再把长边缩至最多 512 像素,不放大小图。区域不存在、坐标越界或编码失败会阻止发送,不会回退整图。
旧配方缺少 `ai_confirmation`、显式为空或未填写 `criteria` 时,会提示“请先在步骤中填写 AI 完成条件”并阻止推进,不能通过省略字段恢复规则直通。打开旧配方的步骤页补齐完成条件并选择支持的手部触发规则,保存、重新应用后生效。
每步支持一条触发规则:
@ -95,7 +97,7 @@ dotnet run --project src/SopHelper.Avalonia -p:BundlePythonRuntime=false
1. 新建配方,在“区域”页按相机原始像素坐标标定取料、装配、成品区域。
2. 步骤的判定方法选“手在区域内”、“手进入后离开区域”或“手从起点移到终点”,设置手和区域;区域进入/离开可设置稳定帧防抖。
3. 勾选“手部动作触发后由 AI 确认完成”,选择送检区域,填写图中能够验证的完成条件,设置最低完成概率、重试间隔和请求超时。
3. 在始终显示的“AI 完成确认(每步必填)”中选择送检区域,填写图中能够验证的完成条件,设置最低完成概率、重试间隔和请求超时。
4. 保存并应用,开始采集。日志显示取图帧号与 AI 判断,步骤卡显示等待、未确认或完成状态。
5. 保持“保存结束图”开启,完成后图片保存至当前配方的 `completion-images/`;界面和磁盘使用的都是 AI 实际收到的判定图。

View File

@ -487,12 +487,12 @@ public class RecipeNode : EditorNode
StandardDurationMs = step.StandardDurationMs,
SaveCompletionImage = step.SaveCompletionImage,
CompletionImageDirectory = step.CompletionImageDirectory,
AiConfirmation = step.RequireAiConfirmation ? new AiConfirmation
AiConfirmation = new AiConfirmation
{
Criteria = step.AiCriteria, MinConfidence = step.AiMinConfidence,
ImageRegion = step.AiImageRegion,
RetryDelayMs = step.AiRetryDelayMs, TimeoutSeconds = step.AiTimeoutSeconds,
} : null,
},
};
def.MustKeep.AddRange(step.MustKeep.Items.Select(i => i.Text).Where(s => !string.IsNullOrWhiteSpace(s)));
@ -784,12 +784,10 @@ public class RegionNode : EditorNode
/// </summary>
public class StepNode : EditorNode
{
private bool _requireAiConfirmation;
private string _aiCriteria = "";
private string _aiImageRegion = "";
private double _aiMinConfidence = 0.85;
private int _aiRetryDelayMs = 3000, _aiTimeoutSeconds = 30;
public bool RequireAiConfirmation { get => _requireAiConfirmation; set => SetProperty(ref _requireAiConfirmation, value); }
public string AiCriteria { get => _aiCriteria; set => SetProperty(ref _aiCriteria, value); }
public string AiImageRegion { get => _aiImageRegion; set => SetProperty(ref _aiImageRegion, value); }
public double AiMinConfidence { get => _aiMinConfidence; set => SetProperty(ref _aiMinConfidence, value); }
@ -799,7 +797,6 @@ public class StepNode : EditorNode
{
set
{
RequireAiConfirmation = value is not null;
if (value is null) return;
AiCriteria = value.Criteria; AiMinConfidence = value.MinConfidence;
AiImageRegion = value.ImageRegion;

View File

@ -148,13 +148,13 @@
<TextBlock Text="说明" Classes="Field" />
<TextBox Grid.Column="1" Text="{Binding Description, Mode=TwoWay}" />
</Grid>
<CheckBox Content="手部动作触发后由 AI 确认完成" IsChecked="{Binding RequireAiConfirmation, Mode=TwoWay}" Margin="0,10,0,4" />
<StackPanel IsVisible="{Binding RequireAiConfirmation}" Spacing="6">
<TextBlock Text="AI 完成确认(每步必填)" Classes="Title" Margin="0,10,0,4" />
<StackPanel Spacing="6">
<TextBlock Text="规则满足后取图,由模型检查下方完成条件。选择“手进入后离开区域”可减少手部遮挡。" TextWrapping="Wrap" Foreground="{StaticResource FgDim}" />
<TextBlock Text="送检区域(留空使用整张画面)" />
<ComboBox x:Name="AiImageRegionPicker" ItemsSource="{Binding DataContext.Recipe.RegionNames, RelativeSource={RelativeSource AncestorType=UserControl}}" SelectedItem="{Binding AiImageRegion, Mode=TwoWay, Converter={StaticResource SelectionConverter}}" />
<TextBlock Text="完成条件(用日常语言描述图片中应看到的结果)" />
<TextBox Text="{Binding AiCriteria, Mode=TwoWay}" AcceptsReturn="True" TextWrapping="Wrap" MinHeight="64" />
<TextBox Text="{Binding AiCriteria, Mode=TwoWay}" PlaceholderText="例如:盒子里面放着连接器。未填写时不会推进步骤。" AcceptsReturn="True" TextWrapping="Wrap" MinHeight="64" />
<Grid ColumnDefinitions="*,*" RowDefinitions="Auto,Auto,Auto,Auto">
<TextBlock Text="最低完成概率(0–1)" VerticalAlignment="Center" />
<NumericUpDown Grid.Column="1" Value="{Binding AiMinConfidence, Mode=TwoWay}" Minimum="0" Maximum="1" Increment="0.05" FormatString="0.00" />

View File

@ -148,9 +148,9 @@ public class StepDef
/// <summary>本步判定条件(AND,全部满足才算完成)</summary>
public StepCondition? Detection { get; set; }
/// <summary>非空时,手部条件仅触发取图,AI 确认后才推进。</summary>
/// <summary>规则仅触发取图,必须经过 AI 确认才推进;缺失完成条件时阻止推进。</summary>
[YamlMember(DefaultValuesHandling = DefaultValuesHandling.OmitNull)]
public AiConfirmation? AiConfirmation { get; set; }
public AiConfirmation? AiConfirmation { get; set; } = new();
/// <summary>本步期间必须常在的物体,被拿走即违规</summary>
public List<string> MustKeep { get; set; } = [];

View File

@ -104,6 +104,7 @@ public static partial class SopConfigLoader
foreach (var step in ordered)
{
step.AiConfirmation ??= new AiConfirmation();
if (step.AiConfirmation is { } ai)
{
if (!ConditionMethod.SupportsAiTrigger(step.Detection?.Method))
@ -114,7 +115,9 @@ public static partial class SopConfigLoader
if (region is null || region.X1 < 0 || region.Y1 < 0 || region.X2 <= region.X1 || region.Y2 <= region.Y1)
config.Warnings.Add($"步骤 {step.Id} 的 AI 取图区域 '{ai.ImageRegion}' 不存在或坐标无效");
}
if (string.IsNullOrWhiteSpace(ai.Criteria) || !double.IsFinite(ai.MinConfidence) || ai.MinConfidence is < 0 or > 1
if (string.IsNullOrWhiteSpace(ai.Criteria))
config.Warnings.Add($"步骤 {step.Id}({step.Name})请填写 AI 完成条件,填写前不会自动推进");
if (!double.IsFinite(ai.MinConfidence) || ai.MinConfidence is < 0 or > 1
|| ai.RetryDelayMs < 0 || ai.TimeoutSeconds <= 0)
config.Warnings.Add($"步骤 {step.Id} 的 AI 判据、置信度、重试间隔或超时配置无效");
if (step.MustKeep.Count > 0 || step.RequiredObjects.Count > 0)

View File

@ -222,9 +222,7 @@ public class SopEngine
// 1. 判定完成条件(稳定帧门控在 evaluator 里)
byte[]? evidence = null;
var result = step.AiConfirmation is null
? _evaluator.EvaluateStep(step, frame)
: EvaluateAiStep(step, frame, captureJpeg, captureDecisionImage, out evidence);
var result = EvaluateAiStep(step, frame, captureJpeg, captureDecisionImage, out evidence);
// 2. 违规:超时 / 必留物被拿走
var violation = _violations.CheckTimeout(step, _stepEnteredAt)
@ -285,7 +283,8 @@ public class SopEngine
private ConditionResult EvaluateAiStep(StepDef step, FrameResult frame, Func<byte[]>? captureJpeg, Func<RegionDef?, byte[]>? captureDecisionImage, out byte[]? evidence)
{
evidence = null;
var options = step.AiConfirmation!;
if (step.AiConfirmation is not { } options || string.IsNullOrWhiteSpace(options.Criteria))
return new() { Message = "请先在步骤中填写 AI 完成条件" };
if (!ConditionMethod.SupportsAiTrigger(step.Detection?.Method))
return new() { Message = "AI 确认需要手进入、离开区域或区域间移动触发" };
if (string.IsNullOrWhiteSpace(options.Criteria) || !double.IsFinite(options.MinConfidence) || options.MinConfidence is < 0 or > 1

View File

@ -786,18 +786,18 @@ internal static class Smoke
var dialog=new Window{Content=editor,Width=1040,Height=900};dialog.Show();await Task.Delay(80);
try
{
var toggle=editor.GetVisualDescendants().OfType<CheckBox>().Single(c=>c.Content?.ToString()=="手部动作触发后由 AI 确认完成");
Check(toggle.IsChecked==true,"Editor exposes AI confirmation switch");
Check(!editor.GetVisualDescendants().OfType<CheckBox>().Any(c=>c.Content?.ToString()=="手部动作触发后由 AI 确认完成"),"AI confirmation no longer has an opt-out checkbox");
var picker=editor.FindControl<ComboBox>("AiImageRegionPicker")!;
Check(picker.SelectedItem?.ToString()=="work","Editor exposes configured image region");
picker.SelectedItem="";await Task.Delay(20);
Check(editorVm.Recipe.ToConfig().Sop.Steps[0].AiConfirmation?.ImageRegion=="","Editor can switch to full-frame capture");
picker.SelectedItem="work";await Task.Delay(20);
Check(editorVm.Recipe.ToConfig().Sop.Steps[0].AiConfirmation?.ImageRegion=="work","Editor persists selected crop region");
toggle.IsChecked=false;await Task.Delay(20);
Check(editorVm.Recipe.ToConfig().Sop.Steps[0].AiConfirmation is null,"Disabling AI restores direct condition evaluation");
toggle.IsChecked=true;await Task.Delay(20);
Check(editorVm.Recipe.ToConfig().Sop.Steps[0].AiConfirmation?.Criteria==ai!.Criteria,"Re-enabling AI retains completion criteria");
Check(editorVm.Recipe.ToConfig().Sop.Steps[0].AiConfirmation?.Criteria==ai!.Criteria,"AI confirmation is always saved with the step");
var added=editorVm.Recipe.AddStep();
Check(editorVm.Recipe.ToConfig().Sop.Steps.Last().AiConfirmation is {Criteria:""},"New steps default to AI confirmation and require explicit completion criteria");
editorVm.Recipe.Steps.Remove(added);
editorVm.Recipe.SelectedStep=editorVm.Recipe.Steps[0];await Task.Delay(20);
Save(dialog,"hand-ai-confirmation-settings");
}
finally{await editorVm.ShutdownAsync();dialog.Close();}

View File

@ -24,6 +24,28 @@ sop:
""");
Check(config.Warnings.Count == 0, "有效的纯手部 AI 配方不应要求 YOLO");
Check(SopConfigLoader.Parse(SopConfigLoader.ToYaml(config)).Sop.Steps[0].AiConfirmation?.Criteria == "零件已安装到位", "AI 配方应往返保存");
// 旧配方缺字段、显式 null 和空判据都不能退回“规则即完成”。
var legacy = SopConfigLoader.Parse("""
sop:
name: 旧配方
settings: { auto_reset_on_complete: false }
regions: { work: { x1: 0, y1: 0, x2: 100, y2: 100 } }
steps:
- id: 1
name: 放置盒子
detection: { method: hand_in_region, region: work, stable_frames: 1 }
""");
Check(legacy.Sop.Steps[0].AiConfirmation is not null && legacy.Warnings.Any(w=>w.Contains("请填写 AI 完成条件")), "旧配方必须默认 AI 确认并提示补充完成条件");
foreach (var missing in new AiConfirmation?[] { null, new(), new() { Criteria = " " } })
{
legacy.Sop.Steps[0].AiConfirmation = missing;
var guarded = new SopEngine(); guarded.Load(legacy); guarded.Start();
string? message = null;
guarded.StepSnapshotChanged += s => message = s.Message;
for (int i = 0; i < 10; i++) guarded.ProcessFrame(new() { Hands = [new() { Rect = new(20,20,10,10) }] }, () => throw new Exception("空判据不应取图"));
Check(guarded.State == EngineState.Running && guarded.CurrentStep?.Id == 1 && message!.Contains("填写 AI 完成条件"), "AI 配置缺失不得仅凭手部规则推进");
guarded.Stop();
}
var fake = new FakeDecision();
var engine = new SopEngine { VisionDecisionClient = fake };
engine.Load(config); engine.Start();