diff --git a/src/SopHelper.CrossPlatform.Engine/Sop/VisualEvidenceSampler.cs b/src/SopHelper.CrossPlatform.Engine/Sop/VisualEvidenceSampler.cs index 6e37499..0e8f195 100644 --- a/src/SopHelper.CrossPlatform.Engine/Sop/VisualEvidenceSampler.cs +++ b/src/SopHelper.CrossPlatform.Engine/Sop/VisualEvidenceSampler.cs @@ -34,11 +34,11 @@ public static class VisualEvidenceSampler if (jpeg.Length < 16) return null; try { - using var image = Cv2.ImDecode(jpeg, ImreadModes.Grayscale); + using var image = Cv2.ImDecode(jpeg, ImreadModes.Color); if (image.Empty()) return null; using var small = new Mat(); Cv2.Resize(image, small, new Size(32, 32), 0, 0, InterpolationFlags.Area); - var signature = new byte[1024]; + var signature = new byte[32 * 32 * 3]; System.Runtime.InteropServices.Marshal.Copy(small.Data, signature, 0, signature.Length); return signature; } diff --git a/tests/VisionDecision.Smoke/VisualEvidenceTests.cs b/tests/VisionDecision.Smoke/VisualEvidenceTests.cs index 8e0ed3e..7985a77 100644 --- a/tests/VisionDecision.Smoke/VisualEvidenceTests.cs +++ b/tests/VisionDecision.Smoke/VisualEvidenceTests.cs @@ -18,6 +18,21 @@ internal static class VisualEvidenceTests 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)