使用手部关键点实际范围判定接触和离开区域

This commit is contained in:
tfarcraw 2026-11-18 09:17:15 +08:00
parent 31a3e8327d
commit 76267751c4
5 changed files with 59 additions and 7 deletions

View File

@ -122,6 +122,19 @@ public class Hand
/// <summary>21 个关键点,已映射回原图像素坐标</summary>
public List<HandPoint> Keypoints { get; set; } = [];
/// <summary>关键点实际覆盖的手部范围;Rect 可能是推理所用的放大裁剪框,不能当作实际接触范围。</summary>
public DetRect ContactRect
{
get
{
var points = Keypoints.Where(p => float.IsFinite(p.X) && float.IsFinite(p.Y)).ToArray();
if (points.Length < 3) return Rect;
var x1 = points.Min(p => p.X); var y1 = points.Min(p => p.Y);
var x2 = points.Max(p => p.X); var y2 = points.Max(p => p.Y);
return x2 > x1 && y2 > y1 ? new(x1, y1, x2 - x1, y2 - y1) : Rect;
}
}
/// <summary>手掌检测置信度</summary>
public float Confidence { get; set; }

View File

@ -129,7 +129,7 @@ public class ConditionEvaluator
"right" or "右手" => h.HandType == HandType.Right,
_ => true,
});
if (selected.Any(h => h.Rect.Intersects(region.Value, tolerance)))
if (selected.Any(h => h.ContactRect.Intersects(region.Value, tolerance)))
{
_leaveRegionVisited.Add(step.Id);
return No($"等待手离开 {condition.Region}");

View File

@ -27,16 +27,16 @@ public sealed class HandImageRegionTracker(TimeProvider? timeProvider = null)
"left" or "左手" => h.HandType == HandType.Left,
"right" or "右手" => h.HandType == HandType.Right,
_ => true,
}).Where(h => float.IsFinite(h.Rect.X) && float.IsFinite(h.Rect.Y)
&& float.IsFinite(h.Rect.Right) && float.IsFinite(h.Rect.Bottom)
&& h.Rect.W > 0 && h.Rect.H > 0 && h.Rect.Intersects(new(0, 0, frame.Size.W, frame.Size.H))).ToList();
}).Select(h => h.ContactRect).Where(rect => float.IsFinite(rect.X) && float.IsFinite(rect.Y)
&& float.IsFinite(rect.Right) && float.IsFinite(rect.Bottom)
&& rect.W > 0 && rect.H > 0 && rect.Intersects(new(0, 0, frame.Size.W, frame.Size.H))).ToList();
var target = condition.Method.ToLowerInvariant() == ConditionMethod.HandMove ? condition.ToRegion : condition.Region;
if (!regions.TryGetValue(target, out var region)) return;
// 离开触发只记离开前接触区域的位置;不追随已经移开的手。
candidates = candidates.Where(h => h.Rect.Intersects(region, Math.Max(0, condition.Tolerance))).ToList();
candidates = candidates.Where(rect => rect.Intersects(region, Math.Max(0, condition.Tolerance))).ToList();
if (candidates.Count == 0) return;
var x1 = candidates.Min(h => h.Rect.X); var y1 = candidates.Min(h => h.Rect.Y);
var x2 = candidates.Max(h => h.Rect.Right); var y2 = candidates.Max(h => h.Rect.Bottom);
var x1 = candidates.Min(rect => rect.X); var y1 = candidates.Min(rect => rect.Y);
var x2 = candidates.Max(rect => rect.Right); var y2 = candidates.Max(rect => rect.Bottom);
_lastRect = new(x1, y1, x2 - x1, y2 - y1);
_lastSeen = _clock.GetTimestamp();
}

View File

@ -0,0 +1,38 @@
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<string, DetRect> { ["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: 实际手部接触框、离开稳定帧、放大裁剪框分离、跟手位置保留、无关键点兼容");
}
}

View File

@ -149,6 +149,7 @@ finally { for (int i = 0; i < names.Length; i++) Environment.SetEnvironmentVaria
await LayaChecks.RunAsync(args);
await D3Checks.RunAsync(args);
await BufferedEvidenceChecks.RunAsync();
HandContactChecks.Run();
await TriggerImageChecks.RunAsync();
await DynamicRegionChecks.RunAsync();
Console.WriteLine("PASS: 手部触发、单请求、AI 门控、证据帧、限流、超时、旧结果失效、配方往返、兼容 HTTP 消息及错误响应、Laya 原生适配与复核语义");