Remove wrong-order violation detection

This commit is contained in:
sophelper 2026-11-07 23:29:19 +08:00
parent 3cdb71bb28
commit ad1c2a4bb7
8 changed files with 10 additions and 102 deletions

View File

@ -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 秒冷却。
### 配方编辑器

View File

@ -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

View File

@ -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<RegionNode>(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); }
}
/// <summary>启用跳步/错序违规</summary>
public bool EnableSkipDetection
{
get { return _enableSkipDetection; }
set { SetProperty(ref _enableSkipDetection, value); }
}
/// <summary>整轮完成后自动复位</summary>
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;

View File

@ -524,7 +524,6 @@
<StackPanel Orientation="Horizontal" Margin="0,10,0,0">
<CheckBox Content="超时违规" IsChecked="{Binding EnableTimeoutDetection}" Margin="0,0,20,0" />
<CheckBox Content="错序违规" IsChecked="{Binding EnableSkipDetection}" Margin="0,0,20,0" />
<CheckBox Content="完成后自动复位" IsChecked="{Binding AutoResetOnComplete}" />
</StackPanel>
</StackPanel>

View File

@ -63,9 +63,6 @@ public class SopSettings
/// <summary>启用超时违规</summary>
public bool EnableTimeoutDetection { get; set; } = true;
/// <summary>启用跳步/错序违规</summary>
public bool EnableSkipDetection { get; set; } = true;
/// <summary>整轮完成后自动复位</summary>
public bool AutoResetOnComplete { get; set; } = true;
@ -226,7 +223,7 @@ public static class ConditionMethod
/// </summary>
public class ViolationRule
{
/// <summary>违规类型(timeout / wrong_order / object_removed)</summary>
/// <summary>违规类型(timeout / object_removed)</summary>
public string Type { get; set; } = "";
/// <summary>描述(显示在告警横幅上)</summary>
@ -244,9 +241,6 @@ public static class ViolationType
/// <summary>本步超时</summary>
public const string Timeout = "timeout";
/// <summary>错序 / 跳步</summary>
public const string WrongOrder = "wrong_order";
/// <summary>must_keep 的物体被拿走</summary>
public const string ObjectRemoved = "object_removed";
}

View File

@ -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)
{

View File

@ -24,7 +24,7 @@ public class ViolationEvent
}
/// <summary>
/// 违规判定:超时 / 跳步(错序)/ 必留物被拿走。
/// 违规判定:超时 / 必留物被拿走。
/// 每类违规带冷却,避免逐帧刷屏(参考项目用 3s,这里保持一致)。
/// </summary>
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<string, DetRect> _regions = [];
private readonly Dictionary<string, DateTime> _lastFired = [];
/// <summary>每个必留物体最近一次被检出的时刻(判"被拿走"用)</summary>
@ -72,25 +71,6 @@ public class ViolationDetector
return Fire(rule, step.Id, $"步骤 {step.Id}「{step.Name}」超时 {elapsed.TotalSeconds:F0}s(限 {step.Timeout}s)");
}
/// <summary>
/// 跳步:下一部的条件已满足,但当前步骤还没完成。
/// 只看紧邻的下一步(参考项目的实测结论:看更远会大量误报)。
/// </summary>
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}」");
}
/// <summary>
/// 必留物被拿走:本步期间 must_keep 的物体连续若干帧消失
/// </summary>
@ -126,54 +106,6 @@ public class ViolationDetector
return null;
}
/// <summary>
/// 轻量判定(只为跳步服务,不参与稳定帧门控,否则跳步永远不触发)
/// </summary>
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<string, DetRect> Regions() => _regions;
/// <summary>
/// 区域字典由引擎注入(跳步判定要看区域坐标)
/// </summary>
public void SetRegions(Dictionary<string, DetRect> 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));

View File

@ -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<Button>().Single(b =>
ReferenceEquals(b.DataContext, rule) && Equals(b.Content, "删"));