From 9e5143c26abe53572f483c9bda407a4c10ff896a Mon Sep 17 00:00:00 2001 From: tfarcraw <123211521@163.com> Date: Fri, 13 Nov 2026 11:03:04 +0800 Subject: [PATCH] =?UTF-8?q?=E7=9B=B4=E6=8E=A5=E4=BD=BF=E7=94=A8=E6=AD=A5?= =?UTF-8?q?=E9=AA=A4=E8=AF=B4=E6=98=8E=E4=BD=9C=E4=B8=BAAI=E5=AE=8C?= =?UTF-8?q?=E6=88=90=E6=9D=A1=E4=BB=B6=E5=B9=B6=E7=A7=BB=E9=99=A4=E9=87=8D?= =?UTF-8?q?=E5=A4=8D=E8=BE=93=E5=85=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- HAND_AI.md | 8 ++++---- configs/sop/d3-workflow.yaml | 6 ++---- configs/sop/hand-ai.yaml | 2 -- .../VVMs/RecipeEditor/EditorNodes.cs | 8 +++----- .../VVMs/RecipeEditor/RecipeEditor.axaml | 8 +++----- .../Sop/SopConfigLoader.cs | 4 ++-- src/SopHelper.CrossPlatform.Engine/Sop/SopEngine.cs | 9 +++++---- .../Sop/VisionDecision.cs | 1 - tests/Avalonia.Smoke/Program.cs | 13 ++++++++----- tests/VisionDecision.Smoke/Program.cs | 12 +++++++----- tests/VisionDecision.Smoke/TriggerImageChecks.cs | 3 ++- 11 files changed, 36 insertions(+), 38 deletions(-) diff --git a/HAND_AI.md b/HAND_AI.md index d32b005..9dc4d99 100644 --- a/HAND_AI.md +++ b/HAND_AI.md @@ -23,9 +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 确认,编辑器不再提供启用/关闭勾选。直接使用原来的步骤“说明”(YAML `description`)作为完成条件,例如“把连接器放进盒子里”;不再单独填写或保存 `ai_confirmation.criteria`。选择“送检区域”后,只发送该区域;留空才发送整帧。按原始相机坐标先裁剪,再把长边缩至最多 512 像素,不放大小图。区域不存在、坐标越界或编码失败会阻止发送,不会回退整图。 -旧配方缺少 `ai_confirmation`、显式为空或未填写 `criteria` 时,会提示“请先在步骤中填写 AI 完成条件”并阻止推进,不能通过省略字段恢复规则直通。打开旧配方的步骤页补齐完成条件并选择支持的手部触发规则,保存、重新应用后生效。 +步骤说明为空时,会提示“请先填写步骤说明,AI 将按说明判断是否完成”并阻止推进。旧配方缺少 `ai_confirmation` 或显式为空时使用默认 AI 参数,仍然必须送图确认;旧 `criteria` 字段不会覆盖步骤说明。打开旧配方补齐原来的“说明”并选择支持的手部触发规则,保存、重新应用后生效。 每步支持一条触发规则: @@ -35,7 +35,7 @@ pwsh -File tools/Start-HandAiD3.ps1 -CheckOnly 新模板 `configs/sop/d3-workflow.yaml` 提供“放入零件 → 取走零件”两步,均在手移开后检查盒子区域。首次启动会导入尚不存在的模板;请在“运行设置 → 配方编辑”中选择它,按真实画面重画区域,再保存、应用并开始采集。坐标和文字都是示例,不会自动标定,也没有预设现场合格阈值。第一版暂不提供同一步内多规则 AND/OR 组合。 -协议使用 `images` 中的 JPEG data URL 和 `questions.step_completed` 的 `noul` 问题。自然语言完成条件直接构成问题,步骤名和说明作为上下文;从 `answers.step_completed.noul` 读取正例概率,异常响应、超时和低概率都不能放行。服务附带图片 SHA-256 时会逐次核对。运行日志显示帧号、送检区域、完成概率和耗时;卡片及磁盘结束图保留实际送检裁剪图。 +协议使用 `images` 中的 JPEG data URL 和 `questions.step_completed` 的 `noul` 问题。原始步骤说明直接构成判定条件,步骤名作为上下文;从 `answers.step_completed.noul` 读取正例概率,异常响应、超时和低概率都不能放行。服务附带图片 SHA-256 时会逐次核对。运行日志显示帧号、送检区域、完成概率和耗时;卡片及磁盘结束图保留实际送检裁剪图。 ### 可选:本机 E:\LayaAI @@ -97,7 +97,7 @@ dotnet run --project src/SopHelper.Avalonia -p:BundlePythonRuntime=false 1. 新建配方,在“区域”页按相机原始像素坐标标定取料、装配、成品区域。 2. 步骤的判定方法选“手在区域内”、“手进入后离开区域”或“手从起点移到终点”,设置手和区域;区域进入/离开可设置稳定帧防抖。 -3. 在始终显示的“AI 完成确认(每步必填)”中选择送检区域,填写图中能够验证的完成条件,设置最低完成概率、重试间隔和请求超时。 +3. 在步骤“说明”中描述要完成的操作;在“AI 完成确认”中选择送检区域,设置最低完成概率、重试间隔和请求超时。 4. 保存并应用,开始采集。日志显示取图帧号与 AI 判断,步骤卡显示等待、未确认或完成状态。 5. 保持“保存结束图”开启,完成后图片保存至当前配方的 `completion-images/`;界面和磁盘使用的都是 AI 实际收到的判定图。 diff --git a/configs/sop/d3-workflow.yaml b/configs/sop/d3-workflow.yaml index 2f05bd2..8f99104 100644 --- a/configs/sop/d3-workflow.yaml +++ b/configs/sop/d3-workflow.yaml @@ -14,21 +14,19 @@ sop: steps: - id: 1 name: 放入零件 - description: 放好零件并把手移开后,检查盒子内部。 + description: 把零件放进盒子,盒子里面能看到零件。 detection: { method: hand_leave_region, region: work, hand: any, stable_frames: 3, tolerance: 10 } ai_confirmation: image_region: work - criteria: 盒子里面放着零件。 min_confidence: 0.85 retry_delay_ms: 1000 timeout_seconds: 5 - id: 2 name: 取走零件 - description: 取走零件并把手移开后,检查盒子是否已经清空。 + description: 取走盒子里的零件,盒子里没有剩余零件。 detection: { method: hand_leave_region, region: work, hand: any, stable_frames: 3, tolerance: 10 } ai_confirmation: image_region: work - criteria: 盒子里面是空的,没有零件。 min_confidence: 0.85 retry_delay_ms: 1000 timeout_seconds: 5 diff --git a/configs/sop/hand-ai.yaml b/configs/sop/hand-ai.yaml index a4218f1..561e6ff 100644 --- a/configs/sop/hand-ai.yaml +++ b/configs/sop/hand-ai.yaml @@ -39,7 +39,6 @@ sop: hand: right tolerance: 10 ai_confirmation: - criteria: 装配区定位夹具中可见一个完整底座,底座平放、轮廓与夹具贴合;若手遮挡关键部位则不能确认完成。 min_confidence: 0.85 retry_delay_ms: 3000 timeout_seconds: 30 @@ -53,7 +52,6 @@ sop: stable_frames: 3 tolerance: 10 ai_confirmation: - criteria: 成品区内可见装配完整的成品,底座与上盖贴合且没有明显缺件;若画面无法确定装配情况则不能确认完成。 min_confidence: 0.9 retry_delay_ms: 3000 timeout_seconds: 30 diff --git a/src/SopHelper.Avalonia/VVMs/RecipeEditor/EditorNodes.cs b/src/SopHelper.Avalonia/VVMs/RecipeEditor/EditorNodes.cs index 36064de..5963f08 100644 --- a/src/SopHelper.Avalonia/VVMs/RecipeEditor/EditorNodes.cs +++ b/src/SopHelper.Avalonia/VVMs/RecipeEditor/EditorNodes.cs @@ -427,7 +427,7 @@ public class RecipeNode : EditorNode CompletionImageDirectory = step.CompletionImageDirectory, AiConfirmation = step.AiConfirmation is null ? null : new AiConfirmation { - Criteria = step.AiConfirmation.Criteria, MinConfidence = step.AiConfirmation.MinConfidence, + MinConfidence = step.AiConfirmation.MinConfidence, ImageRegion = step.AiConfirmation.ImageRegion, RetryDelayMs = step.AiConfirmation.RetryDelayMs, TimeoutSeconds = step.AiConfirmation.TimeoutSeconds, }, @@ -489,7 +489,7 @@ public class RecipeNode : EditorNode CompletionImageDirectory = step.CompletionImageDirectory, AiConfirmation = new AiConfirmation { - Criteria = step.AiCriteria, MinConfidence = step.AiMinConfidence, + MinConfidence = step.AiMinConfidence, ImageRegion = step.AiImageRegion, RetryDelayMs = step.AiRetryDelayMs, TimeoutSeconds = step.AiTimeoutSeconds, }, @@ -784,11 +784,9 @@ public class RegionNode : EditorNode /// public class StepNode : EditorNode { - private string _aiCriteria = ""; private string _aiImageRegion = ""; private double _aiMinConfidence = 0.85; private int _aiRetryDelayMs = 3000, _aiTimeoutSeconds = 30; - 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); } public int AiRetryDelayMs { get => _aiRetryDelayMs; set => SetProperty(ref _aiRetryDelayMs, value); } @@ -798,7 +796,7 @@ public class StepNode : EditorNode set { if (value is null) return; - AiCriteria = value.Criteria; AiMinConfidence = value.MinConfidence; + AiMinConfidence = value.MinConfidence; AiImageRegion = value.ImageRegion; AiRetryDelayMs = value.RetryDelayMs; AiTimeoutSeconds = value.TimeoutSeconds; } diff --git a/src/SopHelper.Avalonia/VVMs/RecipeEditor/RecipeEditor.axaml b/src/SopHelper.Avalonia/VVMs/RecipeEditor/RecipeEditor.axaml index 5889ab9..ad298ba 100644 --- a/src/SopHelper.Avalonia/VVMs/RecipeEditor/RecipeEditor.axaml +++ b/src/SopHelper.Avalonia/VVMs/RecipeEditor/RecipeEditor.axaml @@ -146,15 +146,13 @@ - + - + - + - - diff --git a/src/SopHelper.CrossPlatform.Engine/Sop/SopConfigLoader.cs b/src/SopHelper.CrossPlatform.Engine/Sop/SopConfigLoader.cs index 9545400..162867c 100644 --- a/src/SopHelper.CrossPlatform.Engine/Sop/SopConfigLoader.cs +++ b/src/SopHelper.CrossPlatform.Engine/Sop/SopConfigLoader.cs @@ -115,8 +115,8 @@ 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)) - config.Warnings.Add($"步骤 {step.Id}({step.Name})请填写 AI 完成条件,填写前不会自动推进"); + if (string.IsNullOrWhiteSpace(step.Description)) + 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 判据、置信度、重试间隔或超时配置无效"); diff --git a/src/SopHelper.CrossPlatform.Engine/Sop/SopEngine.cs b/src/SopHelper.CrossPlatform.Engine/Sop/SopEngine.cs index 9f6cad6..5d60ea7 100644 --- a/src/SopHelper.CrossPlatform.Engine/Sop/SopEngine.cs +++ b/src/SopHelper.CrossPlatform.Engine/Sop/SopEngine.cs @@ -283,11 +283,12 @@ public class SopEngine private ConditionResult EvaluateAiStep(StepDef step, FrameResult frame, Func? captureJpeg, Func? captureDecisionImage, out byte[]? evidence) { evidence = null; - if (step.AiConfirmation is not { } options || string.IsNullOrWhiteSpace(options.Criteria)) - return new() { Message = "请先在步骤中填写 AI 完成条件" }; + if (string.IsNullOrWhiteSpace(step.Description)) + return new() { Message = "请先填写步骤说明,AI 将按说明判断是否完成" }; + var options = step.AiConfirmation ?? new AiConfirmation(); 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 + if (!double.IsFinite(options.MinConfidence) || options.MinConfidence is < 0 or > 1 || options.RetryDelayMs < 0 || options.TimeoutSeconds <= 0) return new() { Message = "AI 确认配置无效" }; if (_pendingDecision is { } pending) @@ -321,7 +322,7 @@ public class SopEngine var jpeg = captureDecisionImage is not null ? captureDecisionImage(region) : captureJpeg!(); if (jpeg.Length == 0) return new() { Message = "AI 判定取图失败" }; var request = new VisionDecisionRequest(_config.Sop.Name, step.Id, step.Name, step.Description, - options.Criteria, frame.FrameId, frame.Timestamp, jpeg); + step.Description, frame.FrameId, frame.Timestamp, jpeg); var cancellation = new CancellationTokenSource(TimeSpan.FromSeconds(options.TimeoutSeconds)); _pendingDecision = new(RunDecisionAsync(client, request, cancellation.Token), jpeg, cancellation); Log("info", $"步骤 {step.Id} 手部触发取图,提交 AI 判定(帧 {frame.FrameId},区域:{(region is null ? "整帧" : options.ImageRegion)})"); diff --git a/src/SopHelper.CrossPlatform.Engine/Sop/VisionDecision.cs b/src/SopHelper.CrossPlatform.Engine/Sop/VisionDecision.cs index 94bd08f..f79e24a 100644 --- a/src/SopHelper.CrossPlatform.Engine/Sop/VisionDecision.cs +++ b/src/SopHelper.CrossPlatform.Engine/Sop/VisionDecision.cs @@ -8,7 +8,6 @@ public sealed class AiConfirmation { /// 送检区域名;留空发送整帧。坐标使用相机原始像素。 public string ImageRegion { get; set; } = ""; - public string Criteria { get; set; } = ""; public double MinConfidence { get; set; } = 0.85; public int RetryDelayMs { get; set; } = 3000; public int TimeoutSeconds { get; set; } = 30; diff --git a/tests/Avalonia.Smoke/Program.cs b/tests/Avalonia.Smoke/Program.cs index 4d704f9..8939d61 100644 --- a/tests/Avalonia.Smoke/Program.cs +++ b/tests/Avalonia.Smoke/Program.cs @@ -774,11 +774,11 @@ internal static class Smoke var config=new SopHelper.Engine.Sop.SopConfig();config.Sop.Name=name; config.Sop.Settings.AutoResetOnComplete=false; config.Sop.Regions["work"]=new(){X1=0,Y1=0,X2=64,Y2=48}; - config.Sop.Steps.Add(new(){Id=1,Name="装配",Detection=new(){Method="hand_in_region",Region="work",StableFrames=1}, - AiConfirmation=new(){Criteria="红色零件安装到位",ImageRegion="work",MinConfidence=.9,RetryDelayMs=4500,TimeoutSeconds=20}}); + config.Sop.Steps.Add(new(){Id=1,Name="装配",Description="红色零件安装到位",Detection=new(){Method="hand_in_region",Region="work",StableFrames=1}, + AiConfirmation=new(){ImageRegion="work",MinConfidence=.9,RetryDelayMs=4500,TimeoutSeconds=20}}); var roundTrip=RecipeNode.From(config,file,false).ToConfig(); var ai=roundTrip.Sop.Steps[0].AiConfirmation; - Check(ai is {Criteria:"红色零件安装到位",ImageRegion:"work",MinConfidence:.9,RetryDelayMs:4500,TimeoutSeconds:20},"AI settings survive editor mapping"); + Check(ai is {ImageRegion:"work",MinConfidence:.9,RetryDelayMs:4500,TimeoutSeconds:20} && roundTrip.Sop.Steps[0].Description=="红色零件安装到位","AI settings and step description survive editor mapping"); Directory.CreateDirectory(store.DirectoryFor(name));SopHelper.Engine.Sop.SopConfigLoader.Save(roundTrip,store.ConfigPath(name)); var editorVm=new RecipeEditorViewModel(); editorVm.OnDialogOpened(new DialogParameters{{"directory",store.RecipesDirectory},{"path",store.ConfigPath(name)},{"config",roundTrip},{"new",false}}); @@ -793,9 +793,12 @@ internal static class Smoke 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"); - Check(editorVm.Recipe.ToConfig().Sop.Steps[0].AiConfirmation?.Criteria==ai!.Criteria,"AI confirmation is always saved with the step"); + var description=editor.FindControl("StepDescriptionBox")!; + Check(description.Text=="红色零件安装到位","Step description is the only editable completion condition"); + description.Text="把红色零件放到盒子里";await Task.Delay(20); + Check(editorVm.Recipe.ToConfig().Sop.Steps[0].Description==description.Text,"Editing original step description updates the AI completion condition"); 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"); + Check(editorVm.Recipe.ToConfig().Sop.Steps.Last().AiConfirmation is not null && added.Description=="","New steps default to AI confirmation and require a step description"); editorVm.Recipe.Steps.Remove(added); editorVm.Recipe.SelectedStep=editorVm.Recipe.Steps[0];await Task.Delay(20); Save(dialog,"hand-ai-confirmation-settings"); diff --git a/tests/VisionDecision.Smoke/Program.cs b/tests/VisionDecision.Smoke/Program.cs index 7517044..6ed5157 100644 --- a/tests/VisionDecision.Smoke/Program.cs +++ b/tests/VisionDecision.Smoke/Program.cs @@ -19,11 +19,12 @@ sop: steps: - id: 1 name: 装配 + description: 把零件安装到位 detection: { method: hand_in_region, region: work, stable_frames: 2, tolerance: 1 } - ai_confirmation: { criteria: 零件已安装到位, retry_delay_ms: 0, timeout_seconds: 1 } + ai_confirmation: { criteria: 旧条件不能覆盖步骤说明, retry_delay_ms: 0, timeout_seconds: 1 } """); Check(config.Warnings.Count == 0, "有效的纯手部 AI 配方不应要求 YOLO"); -Check(SopConfigLoader.Parse(SopConfigLoader.ToYaml(config)).Sop.Steps[0].AiConfirmation?.Criteria == "零件已安装到位", "AI 配方应往返保存"); +Check(SopConfigLoader.Parse(SopConfigLoader.ToYaml(config)).Sop.Steps[0].Description == "把零件安装到位" && !SopConfigLoader.ToYaml(config).Contains("criteria:"), "AI 使用步骤说明且不再保存独立完成条件"); // 旧配方缺字段、显式 null 和空判据都不能退回“规则即完成”。 var legacy = SopConfigLoader.Parse(""" sop: @@ -35,15 +36,15 @@ sop: 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 = " " } }) +Check(legacy.Sop.Steps[0].AiConfirmation is not null && legacy.Warnings.Any(w=>w.Contains("请填写步骤说明")), "旧配方必须默认 AI 确认并提示补充步骤说明"); +foreach (var missing in new AiConfirmation?[] { null, new() }) { 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 配置缺失不得仅凭手部规则推进"); + Check(guarded.State == EngineState.Running && guarded.CurrentStep?.Id == 1 && message!.Contains("填写步骤说明"), "步骤说明缺失不得仅凭手部规则推进"); guarded.Stop(); } var fake = new FakeDecision(); @@ -58,6 +59,7 @@ Check(captures == 0, "稳定帧未满足不能取图"); engine.ProcessFrame(frame, Capture); await fake.Called.Task.WaitAsync(TimeSpan.FromSeconds(5)); Check(captures == 1 && fake.Request?.FrameId == 42, "应提交动作触发的准确帧"); +Check(fake.Request?.Criteria == "把零件安装到位", "发给模型的完成条件必须来自步骤说明,不接受旧 criteria 覆盖"); for (int i = 0; i < 10; i++) engine.ProcessFrame(frame, Capture); Check(captures == 1 && engine.State == EngineState.Running, "AI 等待期间不得重复请求或推进"); byte[]? evidence = null; diff --git a/tests/VisionDecision.Smoke/TriggerImageChecks.cs b/tests/VisionDecision.Smoke/TriggerImageChecks.cs index d3b3dd2..2e17190 100644 --- a/tests/VisionDecision.Smoke/TriggerImageChecks.cs +++ b/tests/VisionDecision.Smoke/TriggerImageChecks.cs @@ -17,8 +17,9 @@ internal static class TriggerImageChecks steps: - id: 1 name: 放好零件 + description: 零件放在盒子里 detection: { method: hand_leave_region, region: work, hand: any, stable_frames: 2, tolerance: 1 } - ai_confirmation: { image_region: inspect, criteria: 零件放在盒子里, min_confidence: 0.9 } + ai_confirmation: { image_region: inspect, min_confidence: 0.9 } """); Check(config.Warnings.Count == 0, "手离开与裁剪配方必须有效且不要求 YOLO"); Check(SopConfigLoader.Parse(SopConfigLoader.ToYaml(config)).Sop.Steps[0].AiConfirmation!.ImageRegion == "inspect", "裁剪区域必须随配方保存");