From ad1c2a4bb7c74daa05a01c7158614695679ebd43 Mon Sep 17 00:00:00 2001 From: sophelper <123211521@163.com> Date: Sat, 7 Nov 2026 23:29:19 +0800 Subject: [PATCH] Remove wrong-order violation detection --- README.md | 2 +- configs/sop/demo.yaml | 4 -- .../VVMs/RecipeEditor/EditorNodes.cs | 12 +--- .../VVMs/RecipeEditor/RecipeEditor.xaml | 1 - src/SopHelper.Engine/Sop/SopConfig.cs | 8 +-- src/SopHelper.Engine/Sop/SopEngine.cs | 9 +-- src/SopHelper.Engine/Sop/ViolationDetector.cs | 70 +------------------ tests/RegionEditorSmoke/Program.cs | 6 +- 8 files changed, 10 insertions(+), 102 deletions(-) diff --git a/README.md b/README.md index 9aac7cd..2f85d71 100644 --- a/README.md +++ b/README.md @@ -157,7 +157,7 @@ python/python.exe -X utf8 -m unittest discover -s tests -p test_worker.py -v 步骤里写了没实现的方法,**加载配方时就会报出来**——否则现场表现是"这一步永远不推进", 极难定位。 -违规:`timeout` / `wrong_order`(只看紧邻下一步)/ `object_removed`,同类 3 秒冷却。 +违规:`timeout` / `object_removed`,同类 3 秒冷却。 ### 配方编辑器 diff --git a/configs/sop/demo.yaml b/configs/sop/demo.yaml index bb19d3e..67f844c 100644 --- a/configs/sop/demo.yaml +++ b/configs/sop/demo.yaml @@ -14,7 +14,6 @@ sop: stable_frames: 3 position_tolerance: 30 enable_timeout_detection: true - enable_skip_detection: true auto_reset_on_complete: true reset_delay_sec: 2 frame_stride: 2 @@ -103,6 +102,3 @@ sop: - type: timeout description: 放入超时 severity: 1 - - type: wrong_order - description: 没有按顺序放入 - severity: 2 diff --git a/src/SopHelper.App/VVMs/RecipeEditor/EditorNodes.cs b/src/SopHelper.App/VVMs/RecipeEditor/EditorNodes.cs index 9bf3b13..9aece6e 100644 --- a/src/SopHelper.App/VVMs/RecipeEditor/EditorNodes.cs +++ b/src/SopHelper.App/VVMs/RecipeEditor/EditorNodes.cs @@ -116,7 +116,7 @@ public class RecipeNode : EditorNode Hands = ["any", "left", "right"]; Actions = ["", "pickup", "putdown"]; - ViolationTypes = [ViolationType.Timeout, ViolationType.WrongOrder, ViolationType.ObjectRemoved]; + ViolationTypes = [ViolationType.Timeout, ViolationType.ObjectRemoved]; AddRegionCommand = new DelegateCommand(() => { AddRegion(); Changed?.Invoke(); }); RemoveRegionCommand = new DelegateCommand(node => { RemoveRegion(node); Changed?.Invoke(); }, node => node is not null); @@ -514,7 +514,6 @@ public class RecipeSettingsNode : EditorNode private int _stableFrames = 5; private float _positionTolerance = 20f; private bool _enableTimeoutDetection = true; - private bool _enableSkipDetection = true; private bool _autoResetOnComplete = true; private int _resetDelaySec = 1; private int _frameStride = 1; @@ -555,13 +554,6 @@ public class RecipeSettingsNode : EditorNode set { SetProperty(ref _enableTimeoutDetection, value); } } - /// 启用跳步/错序违规 - public bool EnableSkipDetection - { - get { return _enableSkipDetection; } - set { SetProperty(ref _enableSkipDetection, value); } - } - /// 整轮完成后自动复位 public bool AutoResetOnComplete { @@ -591,7 +583,6 @@ public class RecipeSettingsNode : EditorNode StableFrames = from.StableFrames; PositionTolerance = from.PositionTolerance; EnableTimeoutDetection = from.EnableTimeoutDetection; - EnableSkipDetection = from.EnableSkipDetection; AutoResetOnComplete = from.AutoResetOnComplete; ResetDelaySec = from.ResetDelaySec; FrameStride = from.FrameStride; @@ -605,7 +596,6 @@ public class RecipeSettingsNode : EditorNode to.StableFrames = StableFrames; to.PositionTolerance = PositionTolerance; to.EnableTimeoutDetection = EnableTimeoutDetection; - to.EnableSkipDetection = EnableSkipDetection; to.AutoResetOnComplete = AutoResetOnComplete; to.ResetDelaySec = ResetDelaySec; to.FrameStride = FrameStride; diff --git a/src/SopHelper.App/VVMs/RecipeEditor/RecipeEditor.xaml b/src/SopHelper.App/VVMs/RecipeEditor/RecipeEditor.xaml index 3f1139d..273f88c 100644 --- a/src/SopHelper.App/VVMs/RecipeEditor/RecipeEditor.xaml +++ b/src/SopHelper.App/VVMs/RecipeEditor/RecipeEditor.xaml @@ -524,7 +524,6 @@ - diff --git a/src/SopHelper.Engine/Sop/SopConfig.cs b/src/SopHelper.Engine/Sop/SopConfig.cs index 92e0e74..93ec458 100644 --- a/src/SopHelper.Engine/Sop/SopConfig.cs +++ b/src/SopHelper.Engine/Sop/SopConfig.cs @@ -63,9 +63,6 @@ public class SopSettings /// 启用超时违规 public bool EnableTimeoutDetection { get; set; } = true; - /// 启用跳步/错序违规 - public bool EnableSkipDetection { get; set; } = true; - /// 整轮完成后自动复位 public bool AutoResetOnComplete { get; set; } = true; @@ -226,7 +223,7 @@ public static class ConditionMethod /// public class ViolationRule { - /// 违规类型(timeout / wrong_order / object_removed) + /// 违规类型(timeout / object_removed) public string Type { get; set; } = ""; /// 描述(显示在告警横幅上) @@ -244,9 +241,6 @@ public static class ViolationType /// 本步超时 public const string Timeout = "timeout"; - /// 错序 / 跳步 - public const string WrongOrder = "wrong_order"; - /// must_keep 的物体被拿走 public const string ObjectRemoved = "object_removed"; } diff --git a/src/SopHelper.Engine/Sop/SopEngine.cs b/src/SopHelper.Engine/Sop/SopEngine.cs index f48e5b7..d8bc7f7 100644 --- a/src/SopHelper.Engine/Sop/SopEngine.cs +++ b/src/SopHelper.Engine/Sop/SopEngine.cs @@ -87,7 +87,6 @@ public class SopEngine _evaluator = new ConditionEvaluator(config.Sop.Settings, _regions); _violations = new ViolationDetector(config.Sop.Settings); - _violations.SetRegions(_regions); State = EngineState.Idle; _index = 0; @@ -166,14 +165,12 @@ public class SopEngine var step = StepAt(_index); if (step is null) return; - // 1. 先判定完成条件(稳定帧门控在 evaluator 里)—— - // 必须先判:本帧当前步也可能刚好达成,那样就不算跳步 + // 1. 判定完成条件(稳定帧门控在 evaluator 里) var result = _evaluator.EvaluateStep(step, frame); - // 2. 违规:超时 / 必留物被拿走 / 跳步 + // 2. 违规:超时 / 必留物被拿走 var violation = _violations.CheckTimeout(step, _stepEnteredAt) - ?? _violations.CheckMustKeep(step, frame) - ?? (result.IsMet ? null : _violations.CheckSkip(step, StepAt(_index + 1), result.IsMet, frame)); + ?? _violations.CheckMustKeep(step, frame); if (violation is not null) { diff --git a/src/SopHelper.Engine/Sop/ViolationDetector.cs b/src/SopHelper.Engine/Sop/ViolationDetector.cs index 7f0956d..cf59154 100644 --- a/src/SopHelper.Engine/Sop/ViolationDetector.cs +++ b/src/SopHelper.Engine/Sop/ViolationDetector.cs @@ -24,7 +24,7 @@ public class ViolationEvent } /// -/// 违规判定:超时 / 跳步(错序)/ 必留物被拿走。 +/// 违规判定:超时 / 必留物被拿走。 /// 每类违规带冷却,避免逐帧刷屏(参考项目用 3s,这里保持一致)。 /// public class ViolationDetector @@ -33,7 +33,6 @@ public class ViolationDetector private static readonly TimeSpan Cooldown = TimeSpan.FromSeconds(3); private readonly SopSettings _settings; - private readonly Dictionary _regions = []; private readonly Dictionary _lastFired = []; /// 每个必留物体最近一次被检出的时刻(判"被拿走"用) @@ -72,25 +71,6 @@ public class ViolationDetector return Fire(rule, step.Id, $"步骤 {step.Id}「{step.Name}」超时 {elapsed.TotalSeconds:F0}s(限 {step.Timeout}s)"); } - /// - /// 跳步:下一部的条件已满足,但当前步骤还没完成。 - /// 只看紧邻的下一步(参考项目的实测结论:看更远会大量误报)。 - /// - public ViolationEvent? CheckSkip(StepDef current, StepDef? next, bool currentMet, FrameResult frame) - { - if (!_settings.EnableSkipDetection) return null; - if (next is null) return null; - if (currentMet) return null; - if (next.Detection is null) return null; - - var rule = FindRule(current, ViolationType.WrongOrder); - - var nextMet = QuickEvaluate(next, frame); - if (!nextMet) return null; - - return Fire(rule, current.Id, $"跳步:步骤 {current.Id}「{current.Name}」未完成,却已满足步骤 {next.Id}「{next.Name}」"); - } - /// /// 必留物被拿走:本步期间 must_keep 的物体连续若干帧消失 /// @@ -126,54 +106,6 @@ public class ViolationDetector return null; } - /// - /// 轻量判定(只为跳步服务,不参与稳定帧门控,否则跳步永远不触发) - /// - private bool QuickEvaluate(StepDef step, FrameResult frame) - { - var condition = step.Detection!; - var conf = condition.MinConfidence > 0 ? condition.MinConfidence : _settings.ConfidenceThreshold; - var tolerance = condition.Tolerance > 0 ? condition.Tolerance : _settings.PositionTolerance; - - switch (condition.Method.ToLowerInvariant()) - { - case ConditionMethod.ObjectInZone: - { - var hit = frame.Detections.FirstOrDefault(d => - d.Label.Equals(condition.TargetObject, StringComparison.OrdinalIgnoreCase) && d.Confidence >= conf); - - if (hit is null) return false; - - var (cx, cy) = hit.Rect.Center; - return Regions().TryGetValue(condition.Region, out var rect) && rect.Contains(cx, cy, tolerance); - } - - case ConditionMethod.HandInRegion: - { - if (frame.Hands.Count == 0) return false; - var (hx, hy) = frame.Hands[0].Center; - - return Regions().TryGetValue(condition.Region, out var rect) && rect.Contains(hx, hy, tolerance); - } - - default: - return false; - } - } - - private Dictionary Regions() => _regions; - - /// - /// 区域字典由引擎注入(跳步判定要看区域坐标) - /// - public void SetRegions(Dictionary regions) - { - _regions.Clear(); - - foreach (var (key, value) in regions) - _regions[key] = value; - } - private static ViolationRule? FindRule(StepDef step, string type) { return step.ViolationRules.FirstOrDefault(r => r.Type.Equals(type, StringComparison.OrdinalIgnoreCase)); diff --git a/tests/RegionEditorSmoke/Program.cs b/tests/RegionEditorSmoke/Program.cs index b7554ea..5603313 100644 --- a/tests/RegionEditorSmoke/Program.cs +++ b/tests/RegionEditorSmoke/Program.cs @@ -133,12 +133,12 @@ internal static class Program addRule.Command.Execute(addRule.CommandParameter); stepView.UpdateLayout(); var rule = reopenedVm.Recipe.SelectedStep!.ViolationRules.Items.Single(); - rule.Type = ViolationType.WrongOrder; - rule.Description = "请按顺序操作"; + rule.Type = ViolationType.ObjectRemoved; + rule.Description = "必留物品被拿走"; rule.Severity = 2; reopenedVm.SaveCommand.Execute(); var savedRule = SopConfigLoader.Load(savePath).Sop.Steps.Single().ViolationRules.Single(); - if (savedRule.Type != ViolationType.WrongOrder || savedRule.Description != "请按顺序操作" || savedRule.Severity != 2) + if (savedRule.Type != ViolationType.ObjectRemoved || savedRule.Description != "必留物品被拿走" || savedRule.Severity != 2) throw new Exception("新增违规规则未正确保存"); var removeRule = Descendants(stepView).OfType