43 lines
2.8 KiB
C#
43 lines
2.8 KiB
C#
using OpenCvSharp;
|
|
using SopHelper.Engine.Sop;
|
|
|
|
internal static class VisualEvidenceTests
|
|
{
|
|
public static void Run()
|
|
{
|
|
using var dark = new Mat(32, 32, MatType.CV_8UC3, Scalar.Black);
|
|
using var bright = new Mat(32, 32, MatType.CV_8UC3, Scalar.White);
|
|
var normal = VisionDecisionImage.Encode(dark, null); var transient = VisionDecisionImage.Encode(bright, null);
|
|
var frames = Enumerable.Range(0, 4).Select(i => new VisionEvidenceFrame(i, DateTime.UnixEpoch.AddSeconds(i * .11),
|
|
i == 1 ? transient : normal, i * .11)).ToArray();
|
|
var compact = VisualEvidenceSampler.Select(frames, 3);
|
|
if (!compact.Select(f => f.FrameId).SequenceEqual(new long[] { 0, 1, 3 }))
|
|
throw new Exception("重复背景不能挤掉短暂出现的不同视觉状态,且须保留真实首尾帧");
|
|
var window = new ObservationEvidenceWindow(new() { ObservationMaxFrames = 3, ObservationSampleMs = 100 });
|
|
window.Open(0);
|
|
foreach (var frame in frames) window.Add(frame);
|
|
if (!window.Frames.Select(f => f.FrameId).SequenceEqual(new long[] { 0, 1, 3 }))
|
|
throw new Exception("缓存压缩与请求采样均须保护瞬态关键画面");
|
|
using var blue = new Mat(32, 32, MatType.CV_8UC3, new Scalar(255, 0, 0));
|
|
var baseline = VisionDecisionImage.Encode(blue, null);
|
|
using var baselineGray = Cv2.ImDecode(baseline, ImreadModes.Grayscale);
|
|
byte[]? colorChange = null;
|
|
for (var value = 30; value < 100 && colorChange is null; value++)
|
|
{
|
|
using var green = new Mat(32, 32, MatType.CV_8UC3, new Scalar(0, value, 0));
|
|
var jpeg = VisionDecisionImage.Encode(green, null);
|
|
using var gray = Cv2.ImDecode(jpeg, ImreadModes.Grayscale);
|
|
if (Cv2.Mean(gray).Val0 == Cv2.Mean(baselineGray).Val0) colorChange = jpeg;
|
|
}
|
|
if (colorChange is null) throw new Exception("无法构造同亮度异色回归样本");
|
|
var colors = frames.Select(f => f with { Jpeg = f.FrameId == 1 ? colorChange : baseline }).ToArray();
|
|
if (!VisualEvidenceSampler.Select(colors, 3).Any(f => f.FrameId == 1))
|
|
throw new Exception("同亮度的短暂颜色变化也必须保留,不能只依赖灰度变化");
|
|
var many = Enumerable.Range(0, 12).Select(i => new VisionEvidenceFrame(i, DateTime.UnixEpoch.AddSeconds(i), normal, i)).ToArray();
|
|
var snapshot = VisualEvidenceSampler.Select(many, 4);
|
|
if (snapshot.Length != 4 || snapshot[0].FrameId != 0 || snapshot[^1].FrameId != 11 || many.Length != 12)
|
|
throw new Exception("请求图片预算须独立于缓存容量,并保持时间顺序及快照隔离");
|
|
Console.WriteLine("PASS: 视觉冗余压缩、瞬态画面保护、首尾与顺序、独立请求图片预算");
|
|
}
|
|
}
|