This commit is contained in:
tanglong 2026-03-11 11:26:29 +08:00
parent 33e2addc03
commit cbf1728489
3 changed files with 57 additions and 59 deletions

View File

@ -31,10 +31,9 @@ namespace Wpf_AiSportsMicrospace.Common
IPointTracker _rightTracker;
private int _leftWaveCount = 0;
private int _rightWaveCount = 0;
private const int WaveThreshold = 4;
private const int WaveThreshold =1;
WebcamClient _webcamClient;
private DateTime? _raiseStartTime = null;
private DateTime? _wristStartTime = null;
private bool _firstHandTriggered = false;
@ -75,7 +74,8 @@ namespace Wpf_AiSportsMicrospace.Common
/// <summary>
/// 验证右手动作(右手举手优先,然后左右挥手)
/// </summary>
public int VerifyRightWavingAction(List<Human> humans)
public int
VerifyRightWavingAction(List<Human> humans)
{
if (humans == null || humans.Count == 0)
return (int)WavingAction.None;
@ -148,7 +148,6 @@ namespace Wpf_AiSportsMicrospace.Common
if (!pass)
{
_raiseStartTime = null;
_wristStartTime = null;
_firstHandTriggered = false;
return (int)WavingAction.None;
@ -156,23 +155,31 @@ namespace Wpf_AiSportsMicrospace.Common
var now = DateTime.Now;
_raiseStartTime ??= now;
//_raiseStartTime ??= now;
_wristStartTime ??= now;
double wristElapsed = (now - _wristStartTime.Value).TotalSeconds;
double raiseElapsed = (now - _raiseStartTime.Value).TotalSeconds;
//double raiseElapsed = (now - _raiseStartTime.Value).TotalSeconds;
// 第一步:触发倒计时 / 举手开始
if (!_firstHandTriggered && wristElapsed >= 1)
if (!_firstHandTriggered && wristElapsed >= 0)
{
_firstHandTriggered = true;
return (int)WavingAction.FirstHand;
}
//if (wristElapsed <= 3)
//{
// return (int)WavingAction.Raising;
//}
//_wristStartTime = null;
//_firstHandTriggered = false;
//return (int)WavingAction.RaiseHand;
// 第二步:举手完成
if (raiseElapsed >= 1)
if (wristElapsed >= 3)
{
_raiseStartTime = null;
_wristStartTime = null;
_firstHandTriggered = false;
return (int)WavingAction.RaiseHand;

View File

@ -57,6 +57,7 @@ namespace Wpf_AiSportsMicrospace
ImageUri = GetResourceUri("play_img/play_music.png"),
ProgressColor1 = "#e73d42",
ProgressColor2 = "#fd8212"
});
coverFlow.Images.Add(new CoverFlowItem
{
@ -96,8 +97,8 @@ namespace Wpf_AiSportsMicrospace
{
if (!IsGestureActive) return; // 手势识别已停止,直接返回
var human = humans.FirstOrDefault();
if (human == null) return;
//var human = humans.FirstOrDefault();
//if (human == null) return;
// 检测挥手动作
var wavingaction = _mainWin.SportOperate.VerifyRightWavingAction(humans);
@ -105,29 +106,31 @@ namespace Wpf_AiSportsMicrospace
switch (wavingaction)
{
case (int)WavingAction.LeftWave: // 左手挥动
Application.Current.Dispatcher.BeginInvoke(() => coverFlow.SlideRight());
Application.Current.Dispatcher.Invoke(() => coverFlow.SlideRight());
break;
case (int)WavingAction.RightWave: // 右手挥动
Application.Current.Dispatcher.BeginInvoke(() => coverFlow.SlideLeft());
Application.Current.Dispatcher.Invoke(() => coverFlow.SlideLeft());
break;
case (int)WavingAction.FirstHand: // 举手开始
Application.Current.Dispatcher.BeginInvoke(() => coverFlow.StartSelectedProgress());
//Application.Current.Dispatcher.BeginInvoke(() => coverFlow.StartSelectedProgress());
RouterGoNew();
break;
case (int)WavingAction.Raising: // 举手中,实时更新进度
break;
case (int)WavingAction.RaiseHand: // 举手完成
coverFlow.ProgressCompleted += CoverFlow_ProgressCompleted;
// 设置标记,停止后续手势识别
IsGestureActive = false;
//IsGestureActive = false;
//RouterGoNew();
//coverFlow.ProgressCompleted += CoverFlow_ProgressCompleted;
break;
default: // 没有动作 → 取消进度
Application.Current.Dispatcher.BeginInvoke(() => coverFlow.CancelSelectedProgress());
//Application.Current.Dispatcher.BeginInvoke(() => coverFlow.CancelSelectedProgress());
break;
}
}
@ -272,7 +275,7 @@ namespace Wpf_AiSportsMicrospace
var baseData = await db.BasicConfig.FirstOrDefaultAsync(x => x.Code == AppSettings.Code);
if (baseData == null)
{
baseData = new BasicConfig { Code = AppSettings.Code, Host = host.Text, UserName = name.Text, Password = password.Text , HomeWidth = int.Parse(homeWidth.Text)};
baseData = new BasicConfig { Code = AppSettings.Code, Host = host.Text, UserName = name.Text, Password = password.Text, HomeWidth = int.Parse(homeWidth.Text) };
db.BasicConfig.Add(baseData);
db.SaveChanges();
}
@ -326,7 +329,7 @@ namespace Wpf_AiSportsMicrospace
//var db = scope.ServiceProvider.GetRequiredService<AppDbContext>();
//var mainWindow = new MainWindow(db);
//mainWindow.Show();
var mainWin = Application.Current.MainWindow as Main;
mainWin.SwitchPageWithMaskAnimation(new MultiJumpRope(), true);
}

View File

@ -207,8 +207,8 @@ namespace Wpf_AiSportsMicrospace.Views
int leftWaving = sportOperate.VerifyLeftWavingAction(humans);
if (leftWaving == 5)
{
_mainWin.HumanFrameUpdated -= OnHumanFrameUpdated;
_mainWin.WebcamClient.StopExtract();
//_mainWin.HumanFrameUpdated -= OnHumanFrameUpdated;
//_mainWin.WebcamClient.StopExtract();
// 举左手逻辑,例如结束动画或退出
var newPage = new Home();
@ -223,16 +223,10 @@ namespace Wpf_AiSportsMicrospace.Views
switch (rightWaving)
{
case (int)WavingAction.FirstHand:
_currentGameState = GameState.Running;
StartCountdown(3);
_isCountdownStarted = true; // 倒计时开始,停止左手检测
break;
//case (int)WavingAction.Raising:
// UpdateCountdown();
// break;
//case (int)WavingAction.RaiseHand:
// FinishCountdown();
// _currentGameState = GameState.Running; // 倒计时完成 → 游戏开始
// break;
}
}
break;
@ -249,50 +243,44 @@ namespace Wpf_AiSportsMicrospace.Views
private async void StartCountdown(int start = 3)
{
_mainWin.ShowCountDownAnimation();
//_currentCountdown = start;
//countdownText.Text = _currentCountdown.ToString();
//countdownGrid.Visibility = Visibility.Visible;
//_lastUpdateTime = DateTime.Now;
Utils.PlayBackgroundMusic("countdown_3.mp3", false);
//开始人脸识别=============================================
Action<VideoFrame> handler = null;
//Action<VideoFrame> handler = null;
handler = async (frame) =>
{
var buffer = frame.GetImageBuffer(ImageFormat.Jpeg).ToArray();
double[] xValues = [.. _groupJumpRopeContext.CirclePositions.Select(p => p.XNorm)];
//handler = async (frame) =>
//{
// var buffer = frame.GetImageBuffer(ImageFormat.Jpeg).ToArray();
// double[] xValues = [.. _groupJumpRopeContext.CirclePositions.Select(p => p.XNorm)];
var studentList = Utils.FacialRecognition(buffer, xValues);
// var studentList = Utils.FacialRecognition(buffer, xValues);
if (studentList != null && studentList.Count > 0)
{
// 解绑事件
_mainWin.FacialRecognitionEvent -= handler;
// if (studentList != null && studentList.Count > 0)
// {
// // 解绑事件
// _mainWin.FacialRecognitionEvent -= handler;
for (int i = 0; i < studentList.Count; i++)
{
var student = studentList[i];
if (student != null)
{
_groupJumpRopeContext.UserList[i].DisplayText = student.StudentName;
}
}
// for (int i = 0; i < studentList.Count; i++)
// {
// var student = studentList[i];
// if (student != null)
// {
// _groupJumpRopeContext.UserList[i].DisplayText = student.StudentName;
// }
// }
}
// }
// 这里可以继续处理 studentList
};
// // 这里可以继续处理 studentList
//};
// 绑定事件
_mainWin.FacialRecognitionEvent += handler;
//// 绑定事件
//_mainWin.FacialRecognitionEvent += handler;
await Task.Delay(3000);
FinishCountdown();
_currentGameState = GameState.Running;
//_currentGameState = GameState.Running;
}
private void UpdateCountdown()