From 1862aeb35225e154b3fcbe4fab44e18558ddc9eb Mon Sep 17 00:00:00 2001 From: tfarcraw <123211521@163.com> Date: Thu, 19 Nov 2026 01:47:09 +0800 Subject: [PATCH] =?UTF-8?q?=E9=99=90=E5=88=B6=E6=97=A0=E5=85=B3=E5=8E=86?= =?UTF-8?q?=E5=8F=B2=E8=A1=A5=E6=9F=A5=E5=B9=B6=E4=BF=9D=E7=95=99=E8=B7=A8?= =?UTF-8?q?=E8=B7=B3=E8=BF=87=E6=AD=A5=E9=AA=A4=E7=9A=84=E8=AF=81=E6=8D=AE?= =?UTF-8?q?=E9=A1=BA=E5=BA=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Sop/InspectionFrameSelector.cs | 9 ++++++++- .../Sop/SopEngine.ContinuousChecks.cs | 15 ++++++++++----- .../VisionDecision.Smoke/ContinuousCheckTests.cs | 2 ++ 3 files changed, 20 insertions(+), 6 deletions(-) diff --git a/src/SopHelper.CrossPlatform.Engine/Sop/InspectionFrameSelector.cs b/src/SopHelper.CrossPlatform.Engine/Sop/InspectionFrameSelector.cs index 2ebbd28..b1f9853 100644 --- a/src/SopHelper.CrossPlatform.Engine/Sop/InspectionFrameSelector.cs +++ b/src/SopHelper.CrossPlatform.Engine/Sop/InspectionFrameSelector.cs @@ -4,13 +4,20 @@ namespace SopHelper.Engine.Sop; public static class InspectionFrameSelector { public static VisionEvidenceFrame? Select(IReadOnlyList candidates, - IReadOnlyDictionary inspected, IReadOnlyList boundaries) + IReadOnlyDictionary inspected, IReadOnlyList boundaries, + double bufferedSpanSeconds = double.PositiveInfinity) { 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) + { + var start = ObservationEvidenceWindow.Time(available[0]); + var end = ObservationEvidenceWindow.Time(available[^1]); + if (end - start >= bufferedSpanSeconds) + return available.MinBy(f => Math.Abs(ObservationEvidenceWindow.Time(f) - (start + end) / 2)); 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(); diff --git a/src/SopHelper.CrossPlatform.Engine/Sop/SopEngine.ContinuousChecks.cs b/src/SopHelper.CrossPlatform.Engine/Sop/SopEngine.ContinuousChecks.cs index 1e9a91f..d447f65 100644 --- a/src/SopHelper.CrossPlatform.Engine/Sop/SopEngine.ContinuousChecks.cs +++ b/src/SopHelper.CrossPlatform.Engine/Sop/SopEngine.ContinuousChecks.cs @@ -5,6 +5,9 @@ namespace SopHelper.Engine.Sop; public partial class SopEngine { private readonly Dictionary _continuousChecks = []; + public event Action>? DecisionCandidatesObserved; + private double PriorCompletedEvidenceTime() => _acceptedEvidenceTimes.Where(p => p.Key < _index) + .Select(p => p.Value).DefaultIfEmpty(double.NegativeInfinity).Max(); private sealed class ContinuousCheck(StepDef step, CaptureGate gate) { public StepDef Step = step; @@ -159,8 +162,10 @@ public partial class SopEngine var sinceRequest = state.Attempts == 0 ? state.ElapsedMs : Environment.TickCount64 - state.LastSubmittedAt; var firstWait = Math.Min(interval, Math.Max(500, options.CheckSettleMs)); var timeToCheck = state.Attempts == 0 ? quiet || sinceRequest >= firstWait : sinceRequest >= interval; - var frames = state.Window.Frames.Where(f => !_acceptedEvidenceTimes.TryGetValue(_index - 1, out var prior) - || ObservationEvidenceWindow.Time(f) >= prior).ToArray(); + var prior = PriorCompletedEvidenceTime(); + if (options.EvidenceMode == VisionEvidenceMode.SingleFrame && state.Attempts > 0) + prior = Math.Max(prior, state.LatestSeconds - options.CheckWindowMs / 1000d); + var frames = state.Window.Frames.Where(f => ObservationEvidenceWindow.Time(f) >= prior).ToArray(); if (!timeToCheck) return new() { Message = $"等待下一次检查时机(已送检 {state.Attempts}/{options.MaxChecks} 次)" }; if (options.EvidenceMode == VisionEvidenceMode.SingleFrame) { @@ -170,9 +175,9 @@ public partial class SopEngine 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); + var valid = candidates.Where(f => ObservationEvidenceWindow.Time(f) >= prior).ToArray(); + DecisionCandidatesObserved?.Invoke(step.Id, valid); + var selected = InspectionFrameSelector.Select(valid, state.Inspected, state.Boundaries, options.ObservationMinMs / 1000d); frames = selected is null ? [] : [selected]; } if (frames.Length == 0 || state.Inspected.ContainsKey(frames[^1].FrameId) diff --git a/tests/VisionDecision.Smoke/ContinuousCheckTests.cs b/tests/VisionDecision.Smoke/ContinuousCheckTests.cs index b83384f..2cc7f9e 100644 --- a/tests/VisionDecision.Smoke/ContinuousCheckTests.cs +++ b/tests/VisionDecision.Smoke/ContinuousCheckTests.cs @@ -38,6 +38,8 @@ internal static class ContinuousCheckTests public static async Task RunAsync() { var source = Enumerable.Range(0, 7).Select(i => new VisionEvidenceFrame(i, DateTime.UnixEpoch.AddSeconds(i), [(byte)i], i)).ToArray(); + Check(InspectionFrameSelector.Select(source, new Dictionary(), [source[^1]], 2.4)?.FrameId == 3, + "已缓存较长操作片段的首次检查应覆盖中段,避免只检查动作结束后的尾图"); var coverage = new Dictionary { [6] = 6 }; var recovered = InspectionFrameSelector.Select(source, coverage, []); Check(recovered?.FrameId == 3, "已经检查过较晚帧后,重试须能补查缓存中间帧,不能永远只检查更晚画面");