86 lines
6.3 KiB
C#
86 lines
6.3 KiB
C#
using OpenCvSharp;
|
||
using SopHelper.Engine.Sop;
|
||
using SopHelper.Engine.SopAgent;
|
||
using SopHelper.SopVideo;
|
||
|
||
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), "参考判据检查不修改原配置或推进流程");
|
||
var boundaryVideo = video with { Frames = [.. video.Frames, .. SopVideoSampler.ReadFrames(video, [.2], 2, default)] };
|
||
draft.Steps[1].IncompleteEvidenceFrame = 2;
|
||
var boundaryChecks = new SopDraftReferenceVerifier(probe).CheckAsync(config, draft, boundaryVideo, 2, default).GetAwaiter().GetResult();
|
||
Check(probe.FrameIds[^1] == 2 && boundaryChecks[^1].EvidenceFrame == 2, "显式未完成边界须替代较远的前序状态并使用真实帧检查");
|
||
draft.Steps[1].IncompleteEvidenceFrame = null;
|
||
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("过程时序"), "时序操作不得用单帧参考结果代判");
|
||
config.Sop.Steps[0].AiConfirmation!.ImageMode = "fixed_region";
|
||
var temporal = new TemporalProbeClient();
|
||
checks = new SopDraftReferenceVerifier(temporal).CheckAsync(config, draft, video, 1, default).GetAwaiter().GetResult();
|
||
Check(checks[0].Accepted == true && checks[0].Frames is { Count: >= 3 } frames
|
||
&& frames.Select(f => f.FrameId).Distinct().Count() == frames.Count
|
||
&& frames.Zip(frames.Skip(1)).All(p => p.First.SourceSeconds < p.Second.SourceSeconds),
|
||
"时序参考须发送真实不同且按序的过程帧,仍按原阈值验收");
|
||
Check(checks[1].Accepted is null && checks[1].Reason.Contains("不足三张"), "零时刻的前序片段不能复制图片冒充过程反例");
|
||
temporal.Fabricate = true;
|
||
checks = new SopDraftReferenceVerifier(temporal).CheckAsync(config, draft, video, 1, default).GetAwaiter().GetResult();
|
||
Check(checks[0].Accepted == false, "参考片段伪造完成帧号也必须拒绝");
|
||
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, "参考判据结果"));
|
||
}
|
||
}
|
||
private sealed class TemporalProbeClient : IVisionDecisionClient
|
||
{
|
||
public bool SupportsSequences => true;
|
||
public bool Fabricate;
|
||
public Task<VisionDecisionResult> DecideAsync(VisionDecisionRequest request, CancellationToken token)
|
||
{
|
||
if (request.EvidenceMode != "sequence") return Task.FromResult(new VisionDecisionResult(false, .1, "对照"));
|
||
foreach (var frame in request.Frames!)
|
||
{
|
||
using var image = Cv2.ImDecode(frame.Jpeg, ImreadModes.Color);
|
||
Check(image.Width == 32 && image.Height == 48, "时序参考必须逐帧使用实际送检裁剪");
|
||
}
|
||
var ids = request.Frames!.Select(f => f.FrameId).ToArray();
|
||
return Task.FromResult(new VisionDecisionResult(true, .99, "真实片段", VisionDecisionStatus.Completed,
|
||
Fabricate ? [long.MaxValue, ids[0]] : ids, Fabricate ? long.MaxValue : ids[^1]));
|
||
}
|
||
}
|
||
}
|