diff --git a/src/SopHelper.CrossPlatform.Engine/Sop/InspectionFrameSelector.cs b/src/SopHelper.CrossPlatform.Engine/Sop/InspectionFrameSelector.cs new file mode 100644 index 0000000..2ebbd28 --- /dev/null +++ b/src/SopHelper.CrossPlatform.Engine/Sop/InspectionFrameSelector.cs @@ -0,0 +1,27 @@ +namespace SopHelper.Engine.Sop; + +/// 在真实已采集画面中补查时间覆盖缺口;不依据视频时间表或画面语义判定完成。 +public static class InspectionFrameSelector +{ + public static VisionEvidenceFrame? Select(IReadOnlyList candidates, + IReadOnlyDictionary inspected, IReadOnlyList boundaries) + { + var available = candidates.DistinctBy(f => f.FrameId).Where(f => !inspected.ContainsKey(f.FrameId)) + .OrderBy(ObservationEvidenceWindow.Time).ToArray(); + if (available.Length == 0) return null; + if (inspected.Count == 0) + return available.LastOrDefault(f => boundaries.Any(b => b.FrameId == f.FrameId)) ?? available[^1]; + var first = candidates.Min(ObservationEvidenceWindow.Time); + var last = candidates.Max(ObservationEvidenceWindow.Time); + var times = inspected.Values.Where(t => t >= first && t <= last).Append(first).Append(last).Distinct().Order().ToArray(); + var gaps = times.Zip(times.Skip(1)).OrderByDescending(p => p.Second - p.First).ThenByDescending(p => p.Second); + foreach (var gap in gaps) + { + var middle = (gap.First + gap.Second) / 2; + var frame = available.Where(f => ObservationEvidenceWindow.Time(f) >= gap.First && ObservationEvidenceWindow.Time(f) <= gap.Second) + .MinBy(f => Math.Abs(ObservationEvidenceWindow.Time(f) - middle)); + if (frame is not null) return frame; + } + return available[^1]; + } +} diff --git a/src/SopHelper.CrossPlatform.Engine/Sop/SopEngine.ContinuousChecks.cs b/src/SopHelper.CrossPlatform.Engine/Sop/SopEngine.ContinuousChecks.cs index 7731136..1e9a91f 100644 --- a/src/SopHelper.CrossPlatform.Engine/Sop/SopEngine.ContinuousChecks.cs +++ b/src/SopHelper.CrossPlatform.Engine/Sop/SopEngine.ContinuousChecks.cs @@ -21,6 +21,7 @@ public partial class SopEngine public bool Triggered, SawQuiet, BudgetStarted, StoredBoundary, StoredOutside; public double? OutsideSince; public List Boundaries = []; + public Dictionary Inspected = []; public long LastFrame = -1, SentFrame = -1, StartedAt, LastSubmittedAt, FinishedAt; public double BudgetSeconds, LatestSeconds, LastMotionSeconds; public int Attempts, SentVersion; @@ -161,17 +162,20 @@ public partial class SopEngine var frames = state.Window.Frames.Where(f => !_acceptedEvidenceTimes.TryGetValue(_index - 1, out var prior) || ObservationEvidenceWindow.Time(f) >= prior).ToArray(); if (!timeToCheck) return new() { Message = $"等待下一次检查时机(已送检 {state.Attempts}/{options.MaxChecks} 次)" }; - var boundaryFrame = state.Boundaries.LastOrDefault(f => f.FrameId > state.SentFrame - && (!_acceptedEvidenceTimes.TryGetValue(_index - 1, out var prior) || ObservationEvidenceWindow.Time(f) >= prior)); - if (options.EvidenceMode == VisionEvidenceMode.SingleFrame && boundaryFrame is not null) - frames = [boundaryFrame]; - else if (options.EvidenceMode == VisionEvidenceMode.SingleFrame && (capture is not null || crop is not null)) + if (options.EvidenceMode == VisionEvidenceMode.SingleFrame) { - // 停稳事件可能处于抽样间隔之间;单帧检查必须取该事件当下的图。 - var current = CaptureDecision(step, frame, state.Gate.HandRegion, capture, crop, out _, notify: false); - if (current is not null) frames = [new(frame.FrameId, frame.Timestamp, current.Request.Jpeg, frame.SourceSeconds)]; + var candidates = frames.Concat(state.Boundaries); + if (capture is not null || crop is not null) + { + var current = CaptureDecision(step, frame, state.Gate.HandRegion, capture, crop, out _, notify: false); + if (current is not null) candidates = candidates.Append(new(frame.FrameId, frame.Timestamp, current.Request.Jpeg, frame.SourceSeconds)); + } + var valid = candidates.Where(f => !_acceptedEvidenceTimes.TryGetValue(_index - 1, out var prior) + || ObservationEvidenceWindow.Time(f) >= prior).ToArray(); + var selected = InspectionFrameSelector.Select(valid, state.Inspected, state.Boundaries); + frames = selected is null ? [] : [selected]; } - if (frames.Length == 0 || frames[^1].FrameId <= state.SentFrame + if (frames.Length == 0 || state.Inspected.ContainsKey(frames[^1].FrameId) || options.EvidenceMode == VisionEvidenceMode.Sequence && state.Window.Version <= state.SentVersion || options.EvidenceMode == VisionEvidenceMode.Sequence && (frames.Length < 3 || !state.Window.Ready)) return new() { Message = $"窗口内观察,优先在手部停稳或离开后检查(已送检 {state.Attempts}/{options.MaxChecks} 次)" }; @@ -182,6 +186,7 @@ public partial class SopEngine var remaining = Math.Max(1, options.CheckWindowMs - state.ElapsedMs); var cancellation = new CancellationTokenSource(TimeSpan.FromMilliseconds(Math.Min(options.TimeoutSeconds * 1000d, remaining))); state.Attempts++; state.SentVersion = state.Window.Version; state.SentFrame = last.FrameId; state.LastSubmittedAt = Environment.TickCount64; + state.Inspected[last.FrameId] = ObservationEvidenceWindow.Time(last); state.FinishedAt = long.MaxValue; async Task CheckAsync() { diff --git a/tests/VisionDecision.Smoke/ContinuousCheckTests.cs b/tests/VisionDecision.Smoke/ContinuousCheckTests.cs index ee2317d..b83384f 100644 --- a/tests/VisionDecision.Smoke/ContinuousCheckTests.cs +++ b/tests/VisionDecision.Smoke/ContinuousCheckTests.cs @@ -37,6 +37,12 @@ internal static class ContinuousCheckTests Hands = [new() { HandType = HandType.Right, Rect = new(x, 20, 10, 10) }] }; public static async Task RunAsync() { + var source = Enumerable.Range(0, 7).Select(i => new VisionEvidenceFrame(i, DateTime.UnixEpoch.AddSeconds(i), [(byte)i], i)).ToArray(); + var coverage = new Dictionary { [6] = 6 }; + var recovered = InspectionFrameSelector.Select(source, coverage, []); + Check(recovered?.FrameId == 3, "已经检查过较晚帧后,重试须能补查缓存中间帧,不能永远只检查更晚画面"); + coverage[3] = 3; + Check(InspectionFrameSelector.Select(source, coverage, [])?.FrameId != 3, "已检查的历史帧不能重复消耗次数"); var client = new Client(); var engine = new SopEngine { VisionDecisionClient = client }; engine.Load(Config()); engine.Start(); var snapshots = new List(); int evaluated = 0, alarms = 0; bool? passed = null; engine.StepSnapshotChanged += snapshots.Add; engine.DecisionEvaluated += (_, _, _) => evaluated++;