aisop/tests/VideoFlowTest.Smoke/ReferenceEvidenceChecks.cs

53 lines
3.8 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using OpenCvSharp;
using SopHelper.Engine.Sop;
using SopHelper.Engine.SopAgent;
using SopHelper.Training;
internal static class ReferenceEvidenceChecks
{
public static void Run(string videoPath)
{
var video = SopVideoSampler.ReadInfo(videoPath, default);
video = video with { Frames = SopVideoSampler.ReadFrames(video, [.5, 1.5], 0, default) };
var config = new SopConfig();
config.Sop.Regions["left"] = new() { X2 = 32, Y2 = 48 };
config.Sop.Regions["right"] = new() { X1 = 32, X2 = 64, Y2 = 48 };
config.Sop.Steps = [new() { Id = 1, Name = "前序", Description = "可见状态", AiConfirmation = new() { ImageMode = "fixed_region", ImageRegion = "left", MinConfidence = .95 } },
new() { Id = 2, Name = "后续", Description = "另一可见状态", AiConfirmation = new() { ImageMode = "fixed_region", ImageRegion = "right", MinConfidence = .85 } }];
var draft = new SopVideoDraft { Steps = [new() { EvidenceFrame = 0 }, new() { EvidenceFrame = 1 }] };
var before = SopConfigLoader.ToYaml(config);
var probe = new ProbeClient();
var checks = new SopDraftReferenceVerifier(probe).CheckAsync(config, draft, video, 1, default).GetAwaiter().GetResult();
Check(checks.Count == 4 && checks[0].Accepted == false && checks[2].Accepted == true && checks[1].Kind == "previous_state", "参考检查必须使用各步骤原有AI阈值并包含前序对照,不得降低阈值凑通过");
Check(probe.FrameIds.SequenceEqual(new long[] { 5, 0, 15, 5 }) && checks[0].EvidenceFrame == 0 && checks[0].Seconds == .5,
"参考证据索引、视频原始帧号和时间须区分");
Check(before == SopConfigLoader.ToYaml(config), "参考判据检查不修改原配置或推进流程");
Check(checks[0].Contradicts && checks[3].Contradicts && !checks[1].Contradicts && !checks[2].Contradicts,
"正例失败或前序状态也通过均须标记矛盾,不能把前序误判为本步完成");
config.Sop.Steps[0].AiConfirmation!.ImageMode = "hand";
checks = new SopDraftReferenceVerifier(probe).CheckAsync(config, draft, video, 1, default).GetAwaiter().GetResult();
Check(checks[0].Accepted is null && checks[0].Jpeg.Length == 0, "跟随手部范围不能用伪造静态范围替代实际跟踪");
config.Sop.Steps[0].AiConfirmation!.EvidenceMode = VisionEvidenceMode.Sequence;
checks = new SopDraftReferenceVerifier(probe).CheckAsync(config, draft, video, 1, default).GetAwaiter().GetResult();
Check(checks[0].Accepted is null && checks[0].Reason.Contains("过程时序"), "时序操作不得用单帧参考结果代判");
using var cancellation = new CancellationTokenSource(); cancellation.Cancel();
var cancelled = false;
try { new SopDraftReferenceVerifier(probe).CheckAsync(config, draft, video, 2, cancellation.Token).GetAwaiter().GetResult(); }
catch (OperationCanceledException) { cancelled = true; }
Check(cancelled, "取消应停止参考检查而不伪报结果");
Console.WriteLine("PASS: 完成证据同服务/同裁剪参考检查、原阈值保护、帧号/证据索引、跟手边界与取消");
}
private static void Check(bool value, string message) { if (!value) throw new Exception(message); }
private sealed class ProbeClient : IVisionDecisionClient
{
public List<long> FrameIds { get; } = [];
public Task<VisionDecisionResult> DecideAsync(VisionDecisionRequest request, CancellationToken token)
{
using var image = Cv2.ImDecode(request.Jpeg, ImreadModes.Color);
Check(image.Width == 32 && image.Height == 48, "参考图片必须按实际送检区域裁剪");
FrameIds.Add(request.FrameId);
return Task.FromResult(new VisionDecisionResult(true, .9, "参考判据结果"));
}
}
}