This commit is contained in:
tanglong 2025-10-15 10:01:58 +08:00
parent 027cffa506
commit 29360dcb96
4 changed files with 148 additions and 116 deletions

View File

@ -65,23 +65,34 @@ namespace Wpf_AiSportsMicrospace.Common
return _webcamClient;
}
public int VerifyWavingAction(Human human)
public int VerifyWavingAction(List<Human> humans)
{
var nose = human.Keypoints.FirstOrDefault(k => k.Name == "right_ankle");
if (nose == null)
if (humans == null || humans.Count == 0)
return (int)WavingAction.None;
double xNorm = nose.X / 1920;
double yNorm = nose.Y / 1080;
foreach (var human in humans)
{
var rightAnkle = human.Keypoints.FirstOrDefault(k => k.Name == "right_ankle");
if (rightAnkle == null)
continue;
double xNorm = rightAnkle.X / 1920;
double yNorm = rightAnkle.Y / 1080;
// 仅检测中心区域内的人
if (!(xNorm >= 0.44 && xNorm <= 0.57 && yNorm >= 0.81))
return (int)WavingAction.None;
continue;
var leftWrist = human.Keypoints.FirstOrDefault(x => x.Name == "left_wrist");
var leftElbow = human.Keypoints.FirstOrDefault(x => x.Name == "left_elbow");
var rightWrist = human.Keypoints.FirstOrDefault(x => x.Name == "right_wrist");
var rightElbow = human.Keypoints.FirstOrDefault(x => x.Name == "right_elbow");
// --- 右手举手优先 ---
int rightHandAction = DetectRightHandRaise(human);
if (rightHandAction != (int)WavingAction.None)
return rightHandAction;
// --- 左手挥手 ---
if (leftWrist != null && leftElbow != null)
{
@ -95,12 +106,7 @@ namespace Wpf_AiSportsMicrospace.Common
return leftResult;
}
// --- 右手举手优先判断 ---
int rightHandAction = DetectRightHandRaise(human);
if (rightHandAction != (int)WavingAction.None)
return rightHandAction;
// --- 右手挥手,仅当未举手时判断 ---
// --- 右手挥手 ---
if (rightWrist != null && rightElbow != null)
{
int rightWaveResult = DetectHorizontalWave(
@ -112,10 +118,12 @@ namespace Wpf_AiSportsMicrospace.Common
if (rightWaveResult != (int)WavingAction.None)
return rightWaveResult;
}
}
return (int)WavingAction.None;
}
/// <summary>
/// 检测右手举手状态
/// </summary>

View File

@ -87,7 +87,7 @@ namespace Wpf_AiSportsMicrospace.Views
if (human == null) return;
//检测挥手动作
var wavingaction = sportOperate.VerifyWavingAction(human);
var wavingaction = sportOperate.VerifyWavingAction(humans);
switch (wavingaction)
{

View File

@ -71,7 +71,7 @@ namespace Wpf_AiSportsMicrospace
if (human == null) return;
//检测挥手动作
var wavingaction = _mainWin.SportOperate.VerifyWavingAction(human);
var wavingaction = _mainWin.SportOperate.VerifyWavingAction(humans);
switch (wavingaction)
{

View File

@ -158,12 +158,11 @@ namespace Wpf_AiSportsMicrospace.Views
if (humans == null || humans.Count == 0) return;
int wavingaction = 0;
foreach (var human in humans)
{
wavingaction = DetectRightHandRaise(human);
wavingaction = DetectRightHandRaise(humans);
if (wavingaction < 3)
continue;
return;
switch (wavingaction)
{
@ -189,7 +188,7 @@ namespace Wpf_AiSportsMicrospace.Views
countdownGrid.Visibility = Visibility.Hidden;
break;
}
}
}
else
{
@ -273,13 +272,34 @@ namespace Wpf_AiSportsMicrospace.Views
private bool _firstHandTriggered;
private int _lastCountdownSecond = 3;
private DateTime? _countdownStartTime;
public int DetectRightHandRaise(Human human)
public int DetectRightHandRaise(List<Human> humans)
{
if (humans == null || humans.Count == 0)
return (int)WavingAction.None;
foreach (var human in humans)
{
if (human?.Keypoints == null)
continue;
// --- 筛选右脚踝坐标 ---
var rightAnkle = human.Keypoints.FirstOrDefault(k => k.Name == "right_ankle");
if (rightAnkle == null)
continue;
double xNorm = rightAnkle.X / 1920;
double yNorm = rightAnkle.Y / 1080;
// 仅检测中心区域内的人
if (!(xNorm >= 0.44 && xNorm <= 0.57 && yNorm >= 0.81))
continue;
// --- 获取右臂关键点 ---
var rightWrist = human.Keypoints.FirstOrDefault(k => k.Name == "right_wrist");
var rightElbow = human.Keypoints.FirstOrDefault(k => k.Name == "right_elbow");
const double raiseThreshold = 60; // 举手阈值
const double holdDuration = 1; // 持续 2 秒触发倒计时
const double holdDuration = 1; // 持续间(秒)
const int countdownSeconds = 3; // 倒计时长度
bool handRaised = false;
@ -299,7 +319,7 @@ namespace Wpf_AiSportsMicrospace.Views
var holdElapsed = (DateTime.Now - _raiseStartTime.Value).TotalSeconds;
if (holdElapsed >= holdDuration)
{
// 举手达到 2 秒,启动倒计时
// 举手达到指定时间,启动倒计时
_firstHandTriggered = true;
_countdownStartTime = DateTime.Now;
_lastCountdownSecond = countdownSeconds;
@ -312,10 +332,10 @@ namespace Wpf_AiSportsMicrospace.Views
_raiseStartTime = DateTime.Now;
}
return (int)WavingAction.None; // 倒计时未开始
continue; // 本人未触发,检测下一个人
}
// ---------- 第二步:倒计时逻辑(独立于手状态) ----------
// ---------- 第二步:倒计时逻辑 ----------
var countdownElapsed = (DateTime.Now - _countdownStartTime.Value).TotalSeconds;
int currentSecond = countdownSeconds - (int)Math.Floor(countdownElapsed);
@ -332,7 +352,11 @@ namespace Wpf_AiSportsMicrospace.Views
return (int)WavingAction.RaiseHand; // 举手完成
}
return (int)WavingAction.Raising; // 倒计时中
return (int)WavingAction.Raising;
}
// 没有任何中心区域内的人举手
return (int)WavingAction.None;
}
private void ResetRaiseState()