using SopHelper.Engine.Common; using SopHelper.Engine.Sop; internal static class HandContactChecks { public static void Run() { static void Check(bool value, string message) { if (!value) throw new Exception(message); } Hand At(float x, float y) => new() { HandType = HandType.Right, Rect = new(-100, -100, 400, 400), // 始终压着区域的推理放大框。 Keypoints = [new() { X = x, Y = y }, new() { X = x + 20, Y = y }, new() { X = x + 20, Y = y + 30 }, new() { X = x, Y = y + 30 }] }; var hand = At(20, 30); Check(hand.ContactRect is { X: 20, Y: 30, W: 20, H: 30 }, "实际接触范围必须来自图像坐标下的关键点,不能采用推理放大框"); var regions = new Dictionary { ["work"] = new(0, 0, 100, 100) }; var step = new StepDef { Id = 1, Detection = new() { Method = ConditionMethod.HandLeaveRegion, Region = "work", StableFrames = 2, Tolerance = 1 }, AiConfirmation = new() { ImageMode = VisionImageMode.Hand, HandPadding = 0, HandMinSize = 32 } }; var evaluator = new ConditionEvaluator(new(), regions); FrameResult Frame(Hand h) => new() { Size = (400, 300), Hands = [h] }; Check(!evaluator.EvaluateStep(step, Frame(hand)).IsMet, "手在区域内时必须等待离开"); var departed = At(150, 150); Check(!evaluator.EvaluateStep(step, Frame(departed)).IsMet && evaluator.EvaluateStep(step, Frame(departed)).IsMet, "关键点稳定移开后即应触发,不能被残留放大框阻塞"); var tracker = new HandImageRegionTracker(); tracker.Observe(step, Frame(hand), regions); tracker.Observe(step, Frame(departed), regions); Check(tracker.Resolve(step.AiConfirmation, Frame(departed)) is { X1: 14, Y1: 29, X2: 46, Y2: 61 }, "跟手取图必须保留实际接触位置,离开后的放大框不能覆盖它"); var synthetic = new Hand { Rect = new(10, 10, 30, 40) }; Check(synthetic.ContactRect.Equals(synthetic.Rect), "无关键点的历史或合成检测结果必须保持原框兼容"); synthetic.Keypoints = [new() { X = float.NaN, Y = 10 }, new() { X = 10, Y = float.PositiveInfinity }]; Check(synthetic.ContactRect.Equals(synthetic.Rect), "无效关键点不能生成无效接触框"); Console.WriteLine("PASS: 实际手部接触框、离开稳定帧、放大裁剪框分离、跟手位置保留、无关键点兼容"); } }