aisop/tests/SopVideoAgent.Smoke/TriggerDiagnosticsChecks.cs

62 lines
4.9 KiB
C#

using System.Text.Json;
using SopHelper.Engine.Common;
using SopHelper.Engine.Sop;
using SopHelper.Engine.SopAgent;
internal static class TriggerDiagnosticsChecks
{
public static void Run()
{
var config = new SopConfig();
config.Sop.Regions["target"] = new() { X1 = 40, Y1 = 40, X2 = 60, Y2 = 60 };
config.Sop.Steps = [new() { Id = 1, Name = "前序", Detection = new() { Method = "hand_in_region", Region = "target", Hand = "any", StableFrames = 2, Tolerance = 1 } },
new() { Id = 2, Name = "当前", Detection = new() { Method = "hand_in_region", Region = "target", Hand = "any", StableFrames = 2, Tolerance = 1 } }];
var draft = new SopVideoDraft { Steps = [new() { Name = "前序", StartSeconds = 0, EndSeconds = .4 }, new() { Name = "当前", StartSeconds = 0, EndSeconds = .4 }] };
var diagnostics = new SopTriggerDiagnosticsCollector(config, draft, 2);
var actual = new ConditionEvaluator(config.Sop.Settings, new() { ["target"] = new(40, 40, 20, 20) });
var control = new ConditionEvaluator(config.Sop.Settings, new() { ["target"] = new(40, 40, 20, 20) });
for (var i = 0; i <= 20; i++)
{
// 右手始终可见,左右手轮流命中,确保任意手模式能跨手连续累计稳定帧。
var frame = new FrameResult { FrameId = i, Hands = [new() { HandType = HandType.Left, Rect = i is 0 or 2 or 4 or 8 ? new(48, 48, 4, 4) : new(80, 80, 4, 4) },
new() { HandType = HandType.Right, Rect = i is 3 or 9 ? new(48, 48, 4, 4) : new(80, 80, 4, 4) }] };
diagnostics.ObserveFrame(frame, i / 10d);
if (i >= 6)
{
var probe = actual.EvaluateStep(config.Sop.Steps[1], frame);
var expected = control.EvaluateStep(config.Sop.Steps[1], frame);
diagnostics.ObserveTrigger(2, frame, probe, "following");
Check(probe.IsMet == expected.IsMet && probe.Message == expected.Message, "旁路诊断不能改变真实门控");
if (i == 9) diagnostics.Captured(2, .9);
}
if (i == 10) diagnostics.Evaluated(2, 9, .9, false, "结果未形成");
if (i == 11) diagnostics.Captured(2, 1.1, 11);
if (i == 14) diagnostics.Submitted(2, 11, 1.1);
if (i == 20) diagnostics.Evaluated(2, 11, 1.1, true, "可见结果已形成");
}
var result = diagnostics.Build();
var step = result.Steps[1];
Check(step.WindowFrames == 5 && step.WindowRawMatches == 4 && step.WindowMaxConsecutiveMatches == 3 && step.WindowFirstReadySeconds == .3,
"独立窗口按任意手命中计数,并在两手均离开区域时中断稳定帧");
Check(step.ArmedSeconds == .6 && step.CapturedSeconds == .9 && step.SubmittedAtSeconds == 1.4 && step.ConfirmedAtSeconds == 2,
"实际启用、取图、发送、确认时间须区分,不能把延迟发送时间当成取图时间");
Check(step.ActualRawMatches == 2 && step.ActualMaxStableFrames == 2 && result.ObservedFrames == 21,
"真实门控统计必须只统计启用后的处理帧");
Check(step.ImageEvents.Any(e => e.Event == "evaluated" && e.ImageSeconds == .9 && e.Accepted == false)
&& step.ImageEvents.Last().ImageSeconds == 1.1 && step.ImageEvents.Last().AtVideoSeconds == 2,
"多次取图和判定须逐次记录,不能用首次取图或最新送检覆盖历史失败证据");
Check(result.Trajectory.Count <= 9 && result.Trajectory[0].Hands.Count == 2, "手部轨迹应有界抽样并保存左右手坐标");
var compact = JsonSerializer.Serialize(SopTriggerDiagnosticsCollector.ForModel(result, new HashSet<int> { 2 }));
Check(compact.Contains("ArmedSeconds") && compact.Contains("WindowFirstReadySeconds") && compact.Contains("StepId\":1"),
"失败反馈必须包含阻塞步骤及其前序的轨迹和时序,明确区分反事实匹配与实际触发");
var decoded = JsonDocument.Parse(compact).RootElement.GetProperty("semantics").GetString()!;
Check(decoded.Contains("反事实") && decoded.Contains("不得用视频时间点代替触发"), "统计的适用范围和单帧安全约束须明确告知模型");
var detail = JsonDocument.Parse(compact).RootElement.GetProperty("failure_details")[0];
var hands = detail.GetProperty("hands_in_expected_window")[0].GetProperty("Hands")[0];
Check(hands.GetProperty("Center").GetArrayLength() == 2 && hands.GetProperty("Contact").GetArrayLength() == 4
&& result.Trajectory[0].Hands[0].X == 50, "精简反馈须保留可解释的中心与接触框且不修改原始轨迹");
Console.WriteLine("PASS: 实际手部轨迹、any选手、稳定帧、门控启用与取图/发送/确认时间、诊断旁路和前序阻塞反馈");
}
private static void Check(bool condition, string message) { if (!condition) throw new Exception(message); }
}