复检补查缓存时间缺口而非只追逐最新画面
This commit is contained in:
parent
3ace098f21
commit
3027db91f7
@ -0,0 +1,27 @@
|
||||
namespace SopHelper.Engine.Sop;
|
||||
|
||||
/// <summary>在真实已采集画面中补查时间覆盖缺口;不依据视频时间表或画面语义判定完成。</summary>
|
||||
public static class InspectionFrameSelector
|
||||
{
|
||||
public static VisionEvidenceFrame? Select(IReadOnlyList<VisionEvidenceFrame> candidates,
|
||||
IReadOnlyDictionary<long, double> inspected, IReadOnlyList<VisionEvidenceFrame> 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];
|
||||
}
|
||||
}
|
||||
@ -21,6 +21,7 @@ public partial class SopEngine
|
||||
public bool Triggered, SawQuiet, BudgetStarted, StoredBoundary, StoredOutside;
|
||||
public double? OutsideSince;
|
||||
public List<VisionEvidenceFrame> Boundaries = [];
|
||||
public Dictionary<long, double> 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<VisionDecisionResult> CheckAsync()
|
||||
{
|
||||
|
||||
@ -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<long, double> { [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<StepSnapshot>(); int evaluated = 0, alarms = 0; bool? passed = null;
|
||||
engine.StepSnapshotChanged += snapshots.Add; engine.DecisionEvaluated += (_, _, _) => evaluated++;
|
||||
|
||||
Loading…
Reference in New Issue
Block a user