From 694b046e507cca6f2f0e2c1103c084fde0a68eaa Mon Sep 17 00:00:00 2001 From: tanglong <842690096@qq.com> Date: Mon, 13 Oct 2025 20:12:20 +0800 Subject: [PATCH] ddd --- .../Views/CenterHome.xaml.cs | 20 ++- Wpf_AiSportsMicrospace/Views/Home.xaml.cs | 2 - .../Views/JumpRope/GroupJumpRope.xaml.cs | 120 ++++++++++-------- Wpf_AiSportsMicrospace/Views/Main.xaml.cs | 6 +- 4 files changed, 84 insertions(+), 64 deletions(-) diff --git a/Wpf_AiSportsMicrospace/Views/CenterHome.xaml.cs b/Wpf_AiSportsMicrospace/Views/CenterHome.xaml.cs index ac01d92..ec7a6f3 100644 --- a/Wpf_AiSportsMicrospace/Views/CenterHome.xaml.cs +++ b/Wpf_AiSportsMicrospace/Views/CenterHome.xaml.cs @@ -35,6 +35,8 @@ namespace Wpf_AiSportsMicrospace.Views { private Main _mainWin => Application.Current.MainWindow as Main; + SportOperate sportOperate = new SportOperate(); + public String _nowSelect = "test"; //测试吧:test 游戏吧:play public String NowSelect { @@ -44,7 +46,7 @@ namespace Wpf_AiSportsMicrospace.Views if (_nowSelect != value) { _nowSelect = value; - InitImg(); + //InitImg(); } } } @@ -52,11 +54,20 @@ namespace Wpf_AiSportsMicrospace.Views public CenterHome() { InitializeComponent(); - InitImg(); + //InitImg(); + + string projectRoot = Path.Combine(AppContext.BaseDirectory, @"..\..\.."); + string imgPath = Path.Combine(projectRoot, "Resources", "Img", "play_img"); + coverFlow.Images.Add(new CoverFlowItem { ImageUri = new Uri(Path.Combine(imgPath, "play_vs.png")), ProgressColor1 = "#215bc7", ProgressColor2 = "#fc640e" }); + coverFlow.Images.Add(new CoverFlowItem { ImageUri = new Uri(Path.Combine(imgPath, "play_jump.png")), ProgressColor1 = "#e73d42", ProgressColor2 = "#fd8212" }); + coverFlow.Images.Add(new CoverFlowItem { ImageUri = new Uri(Path.Combine(imgPath, "play_music.png")), ProgressColor1 = "#e73d42", ProgressColor2 = "#215bc7" }); + + coverFlow.SelectedIndex = 0; Loaded += Window_Loaded; Unloaded += CenterHome_Unloaded; //AnimationBehavior.SetSourceUri(LoadingImage, loadingImage); + } private void Window_Loaded(object sender, RoutedEventArgs e) @@ -76,7 +87,7 @@ namespace Wpf_AiSportsMicrospace.Views if (human == null) return; //检测挥手动作 - var wavingaction = _mainWin.SportOperate.VerifyWavingAction(human); + var wavingaction = sportOperate.VerifyWavingAction(human); switch (wavingaction) { @@ -100,11 +111,12 @@ namespace Wpf_AiSportsMicrospace.Views break; default: // 没有动作 → 取消进度 - Application.Current.Dispatcher.BeginInvoke(() => coverFlow.CancelSelectedProgress()); + //Application.Current.Dispatcher.BeginInvoke(() => coverFlow.CancelSelectedProgress()); break; } } + private void CoverFlow_ProgressCompleted(CoverFlowItem item) { try diff --git a/Wpf_AiSportsMicrospace/Views/Home.xaml.cs b/Wpf_AiSportsMicrospace/Views/Home.xaml.cs index eb6a3a6..417e071 100644 --- a/Wpf_AiSportsMicrospace/Views/Home.xaml.cs +++ b/Wpf_AiSportsMicrospace/Views/Home.xaml.cs @@ -100,13 +100,11 @@ namespace Wpf_AiSportsMicrospace } } - private void CoverFlow_ProgressCompleted(CoverFlowItem item) { // 停止抽帧线程/释放资源 try { - //Dispose(); RouterGoNew(); } catch (Exception ex) diff --git a/Wpf_AiSportsMicrospace/Views/JumpRope/GroupJumpRope.xaml.cs b/Wpf_AiSportsMicrospace/Views/JumpRope/GroupJumpRope.xaml.cs index 59851fd..3dff199 100644 --- a/Wpf_AiSportsMicrospace/Views/JumpRope/GroupJumpRope.xaml.cs +++ b/Wpf_AiSportsMicrospace/Views/JumpRope/GroupJumpRope.xaml.cs @@ -17,6 +17,7 @@ using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Animation; using System.Windows.Media.Imaging; +using System.Windows.Media.Media3D; using System.Windows.Shapes; using System.Windows.Threading; using Wpf_AiSportsMicrospace.Common; @@ -143,7 +144,6 @@ namespace Wpf_AiSportsMicrospace.Views }); } - private void MediaPlayer_MediaEnded(object sender, EventArgs e) { // 音乐播放完成后的逻辑 @@ -158,35 +158,40 @@ namespace Wpf_AiSportsMicrospace.Views { if (!IsGameStarted) { - var human = humans.LastOrDefault(); - if (human == null) return; + if (humans == null || humans.Count == 0) return; - //检测挥手动作 - var wavingaction = DetectRightHandRaise(human); - - switch (wavingaction) + int wavingaction = 0; + foreach (var human in humans) { - case (int)WavingAction.FirstHand: - // 第一次举手,初始化倒计时 - StartCountdown(3); - break; + wavingaction = DetectRightHandRaise(human); - case (int)WavingAction.Raising: - // 持续倒计时中 - UpdateCountdown(); - break; + if (wavingaction < 3) + continue; - case (int)WavingAction.RaiseHand: - // 举手完成,倒计时结束 - FinishCountdown(); - break; + switch (wavingaction) + { + case (int)WavingAction.FirstHand: + // 第一次举手,初始化倒计时 + StartCountdown(3); + break; - default: - // 没检测到动作,重置倒计时显示 - Utils.StopBackgroundMusic(); - countdownText.Text = "3"; - countdownGrid.Visibility = Visibility.Hidden; - break; + case (int)WavingAction.Raising: + // 持续倒计时中 + UpdateCountdown(); + break; + + case (int)WavingAction.RaiseHand: + // 举手完成,倒计时结束 + FinishCountdown(); + break; + + default: + // 没检测到动作,重置倒计时显示 + Utils.StopBackgroundMusic(); + countdownText.Text = "3"; + countdownGrid.Visibility = Visibility.Hidden; + break; + } } } else @@ -205,8 +210,10 @@ namespace Wpf_AiSportsMicrospace.Views private void StartCountdown(int start = 3) { _currentCountdown = start; + countdownText.Text = _currentCountdown.ToString(); countdownGrid.Visibility = Visibility.Visible; + _lastUpdateTime = DateTime.Now; Utils.PlayBackgroundMusic("countdown_3.mp3", false); @@ -232,16 +239,10 @@ namespace Wpf_AiSportsMicrospace.Views private async void FinishCountdown() { - //foreach (var item in userList) - //{ - // item.ImageState = "2"; - //} - // 举手完成,显示 ✔ countdownText.Text = "GO!"; countdownGrid.Visibility = Visibility.Hidden; IsGameStarted = true; - // 播放背景音乐(循环) Utils.PlayBackgroundMusic("homeprojectselected1.mp3", true); @@ -260,8 +261,12 @@ namespace Wpf_AiSportsMicrospace.Views countdownGrid.Visibility = Visibility.Hidden; - IsGameStarted = false; + userList.ForEach(x => + { + x.ImageState = "1"; + }); + IsGameStarted = false; Utils.StopBackgroundMusic(); } @@ -276,7 +281,7 @@ namespace Wpf_AiSportsMicrospace.Views var rightElbow = human.Keypoints.FirstOrDefault(k => k.Name == "right_elbow"); const double raiseThreshold = 60; // 举手阈值 - const double holdDuration = 2; // 持续 2 秒触发倒计时 + const double holdDuration = 1; // 持续 2 秒触发倒计时 const int countdownSeconds = 3; // 倒计时长度 bool handRaised = false; @@ -340,9 +345,9 @@ namespace Wpf_AiSportsMicrospace.Views _firstHandTriggered = false; _lastCountdownSecond = 3; } - - private Dictionary _lastJumpUpdateTime = new Dictionary(); - public Human LocateHuman(List humans, double begin, double end, double frameWidth, double frameHeight, int circleIndex) + private Dictionary _lastJumpUpdateTime = new(); // 用于存储最后更新时间 + private Dictionary _lastUIUpdateTime = new(); // 用于防抖处理 + public Human LocateHuman(List humans, double frameWidth, double frameHeight, int circleIndex) { if (humans == null || humans.Count == 0) { @@ -383,16 +388,17 @@ namespace Wpf_AiSportsMicrospace.Views { inCircleHuman = hu; } + } if (isOut) userList[circleIndex].ImageState = "3"; else { - if (userList[circleIndex].ImageState == "3") - { - userList[circleIndex].ImageState = "1"; - } + //if (userList[circleIndex].ImageState == "3") + //{ + // userList[circleIndex].ImageState = "1"; + //} // 检查当前编号是否未变化超过 2 秒 if (userNumberList[circleIndex] == userList[circleIndex].NumberText) @@ -403,11 +409,10 @@ namespace Wpf_AiSportsMicrospace.Views var elapsed = (DateTime.Now - lastTime).TotalSeconds; - if (elapsed > 0.8) + if (elapsed > 1) { - // 2 秒未变化,更新状态为禁止 userList[circleIndex].ImageState = "1"; - _lastJumpUpdateTime[circleIndex] = DateTime.Now; // 重置时间 + _lastJumpUpdateTime[circleIndex] = DateTime.Now; } // 否则:2 秒内仍在变化,不更新状态 } @@ -418,7 +423,6 @@ namespace Wpf_AiSportsMicrospace.Views _lastJumpUpdateTime[circleIndex] = DateTime.Now; } } - return inCircleHuman; } @@ -463,34 +467,38 @@ namespace Wpf_AiSportsMicrospace.Views // 订阅事件 sport.OnTicked += (count, times) => { - currentItem.ImageState = "2"; + currentItem.NumberText = count.ToString(); - Application.Current.Dispatcher.BeginInvoke(() => + if (currentItem.ImageState != "2") { - currentItem.NumberText = count.ToString(); - }); + currentItem.ImageState = "2"; + } }; - + sport.PointThreshold = 0.03f; sport.Start(); sports.Add(sport); } } private void UpdateCircleCounts(List humans) { - for (int i = 0; i < circlePositionsX.Length; i++) - { - double center = circlePositionsX[i]; - double range = 0.07; - double begin = center - range; - double end = center + range; + // 在后台线程运行检测逻辑 + //Parallel.For(0, circlePositions.Count, i => + //{ + // var human = LocateHuman(humans, userBox.ActualWidth, userBox.ActualHeight, i); + // sports[i].Pushing(human); + //}); - var human = LocateHuman(humans, begin, end, userBox.ActualWidth, userBox.ActualHeight, i); + for (int i = 0; i < circlePositions.Count; i++) + { + var human = LocateHuman(humans, userBox.ActualWidth, userBox.ActualHeight, i); if (human != null) { sports[i].Pushing(human); } } } + + /// /// 添加带渐变光的圆圈(中心红色,边缘蓝色) /// diff --git a/Wpf_AiSportsMicrospace/Views/Main.xaml.cs b/Wpf_AiSportsMicrospace/Views/Main.xaml.cs index be63a00..5fcb2e2 100644 --- a/Wpf_AiSportsMicrospace/Views/Main.xaml.cs +++ b/Wpf_AiSportsMicrospace/Views/Main.xaml.cs @@ -42,7 +42,7 @@ namespace Wpf_AiSportsMicrospace.Views }; Yztob.AiSports.Common.SportAppSettingService.Set("inferences", options); - _humanPredictor = HumanPredictorFactory.Create(HumanPredictorType.MultiLow); + _humanPredictor = HumanPredictorFactory.Create(HumanPredictorType.MultiMedium); _humanGraphicsRenderer = new HumanGraphicsRenderer(); _humanGraphicsRenderer.DrawLabel = false; @@ -85,6 +85,9 @@ namespace Wpf_AiSportsMicrospace.Views { try { + //if (frame.Number % 2 != 0) + // return; + var buffer = frame.GetImageBuffer(ImageFormat.Jpeg).ToArray(); var humanResult = _humanPredictor.Predicting(buffer, frame.Number); @@ -122,7 +125,6 @@ namespace Wpf_AiSportsMicrospace.Views // 切换到只有新页面 MainContent.Content = newPage; - // 清理资源 if (oldPage is IDisposable disposable) disposable.Dispose();