This commit is contained in:
tanglong 2025-10-13 20:12:20 +08:00
parent a59d6096a1
commit 694b046e50
4 changed files with 84 additions and 64 deletions

View File

@ -35,6 +35,8 @@ namespace Wpf_AiSportsMicrospace.Views
{ {
private Main _mainWin => Application.Current.MainWindow as Main; private Main _mainWin => Application.Current.MainWindow as Main;
SportOperate sportOperate = new SportOperate();
public String _nowSelect = "test"; //测试吧test 游戏吧play public String _nowSelect = "test"; //测试吧test 游戏吧play
public String NowSelect public String NowSelect
{ {
@ -44,7 +46,7 @@ namespace Wpf_AiSportsMicrospace.Views
if (_nowSelect != value) if (_nowSelect != value)
{ {
_nowSelect = value; _nowSelect = value;
InitImg(); //InitImg();
} }
} }
} }
@ -52,11 +54,20 @@ namespace Wpf_AiSportsMicrospace.Views
public CenterHome() public CenterHome()
{ {
InitializeComponent(); 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; Loaded += Window_Loaded;
Unloaded += CenterHome_Unloaded; Unloaded += CenterHome_Unloaded;
//AnimationBehavior.SetSourceUri(LoadingImage, loadingImage); //AnimationBehavior.SetSourceUri(LoadingImage, loadingImage);
} }
private void Window_Loaded(object sender, RoutedEventArgs e) private void Window_Loaded(object sender, RoutedEventArgs e)
@ -76,7 +87,7 @@ namespace Wpf_AiSportsMicrospace.Views
if (human == null) return; if (human == null) return;
//检测挥手动作 //检测挥手动作
var wavingaction = _mainWin.SportOperate.VerifyWavingAction(human); var wavingaction = sportOperate.VerifyWavingAction(human);
switch (wavingaction) switch (wavingaction)
{ {
@ -100,11 +111,12 @@ namespace Wpf_AiSportsMicrospace.Views
break; break;
default: // 没有动作 → 取消进度 default: // 没有动作 → 取消进度
Application.Current.Dispatcher.BeginInvoke(() => coverFlow.CancelSelectedProgress()); //Application.Current.Dispatcher.BeginInvoke(() => coverFlow.CancelSelectedProgress());
break; break;
} }
} }
private void CoverFlow_ProgressCompleted(CoverFlowItem item) private void CoverFlow_ProgressCompleted(CoverFlowItem item)
{ {
try try

View File

@ -100,13 +100,11 @@ namespace Wpf_AiSportsMicrospace
} }
} }
private void CoverFlow_ProgressCompleted(CoverFlowItem item) private void CoverFlow_ProgressCompleted(CoverFlowItem item)
{ {
// 停止抽帧线程/释放资源 // 停止抽帧线程/释放资源
try try
{ {
//Dispose();
RouterGoNew(); RouterGoNew();
} }
catch (Exception ex) catch (Exception ex)

View File

@ -17,6 +17,7 @@ using System.Windows.Input;
using System.Windows.Media; using System.Windows.Media;
using System.Windows.Media.Animation; using System.Windows.Media.Animation;
using System.Windows.Media.Imaging; using System.Windows.Media.Imaging;
using System.Windows.Media.Media3D;
using System.Windows.Shapes; using System.Windows.Shapes;
using System.Windows.Threading; using System.Windows.Threading;
using Wpf_AiSportsMicrospace.Common; using Wpf_AiSportsMicrospace.Common;
@ -143,7 +144,6 @@ namespace Wpf_AiSportsMicrospace.Views
}); });
} }
private void MediaPlayer_MediaEnded(object sender, EventArgs e) private void MediaPlayer_MediaEnded(object sender, EventArgs e)
{ {
// 音乐播放完成后的逻辑 // 音乐播放完成后的逻辑
@ -158,11 +158,15 @@ namespace Wpf_AiSportsMicrospace.Views
{ {
if (!IsGameStarted) if (!IsGameStarted)
{ {
var human = humans.LastOrDefault(); if (humans == null || humans.Count == 0) return;
if (human == null) return;
//检测挥手动作 int wavingaction = 0;
var wavingaction = DetectRightHandRaise(human); foreach (var human in humans)
{
wavingaction = DetectRightHandRaise(human);
if (wavingaction < 3)
continue;
switch (wavingaction) switch (wavingaction)
{ {
@ -189,6 +193,7 @@ namespace Wpf_AiSportsMicrospace.Views
break; break;
} }
} }
}
else else
{ {
UpdateCircleCounts(humans); UpdateCircleCounts(humans);
@ -205,8 +210,10 @@ namespace Wpf_AiSportsMicrospace.Views
private void StartCountdown(int start = 3) private void StartCountdown(int start = 3)
{ {
_currentCountdown = start; _currentCountdown = start;
countdownText.Text = _currentCountdown.ToString(); countdownText.Text = _currentCountdown.ToString();
countdownGrid.Visibility = Visibility.Visible; countdownGrid.Visibility = Visibility.Visible;
_lastUpdateTime = DateTime.Now; _lastUpdateTime = DateTime.Now;
Utils.PlayBackgroundMusic("countdown_3.mp3", false); Utils.PlayBackgroundMusic("countdown_3.mp3", false);
@ -232,16 +239,10 @@ namespace Wpf_AiSportsMicrospace.Views
private async void FinishCountdown() private async void FinishCountdown()
{ {
//foreach (var item in userList)
//{
// item.ImageState = "2";
//}
// 举手完成,显示 ✔
countdownText.Text = "GO!"; countdownText.Text = "GO!";
countdownGrid.Visibility = Visibility.Hidden; countdownGrid.Visibility = Visibility.Hidden;
IsGameStarted = true; IsGameStarted = true;
// 播放背景音乐(循环) // 播放背景音乐(循环)
Utils.PlayBackgroundMusic("homeprojectselected1.mp3", true); Utils.PlayBackgroundMusic("homeprojectselected1.mp3", true);
@ -260,8 +261,12 @@ namespace Wpf_AiSportsMicrospace.Views
countdownGrid.Visibility = Visibility.Hidden; countdownGrid.Visibility = Visibility.Hidden;
IsGameStarted = false; userList.ForEach(x =>
{
x.ImageState = "1";
});
IsGameStarted = false;
Utils.StopBackgroundMusic(); Utils.StopBackgroundMusic();
} }
@ -276,7 +281,7 @@ namespace Wpf_AiSportsMicrospace.Views
var rightElbow = human.Keypoints.FirstOrDefault(k => k.Name == "right_elbow"); var rightElbow = human.Keypoints.FirstOrDefault(k => k.Name == "right_elbow");
const double raiseThreshold = 60; // 举手阈值 const double raiseThreshold = 60; // 举手阈值
const double holdDuration = 2; // 持续 2 秒触发倒计时 const double holdDuration = 1; // 持续 2 秒触发倒计时
const int countdownSeconds = 3; // 倒计时长度 const int countdownSeconds = 3; // 倒计时长度
bool handRaised = false; bool handRaised = false;
@ -340,9 +345,9 @@ namespace Wpf_AiSportsMicrospace.Views
_firstHandTriggered = false; _firstHandTriggered = false;
_lastCountdownSecond = 3; _lastCountdownSecond = 3;
} }
private Dictionary<int, DateTime> _lastJumpUpdateTime = new(); // 用于存储最后更新时间
private Dictionary<int, DateTime> _lastJumpUpdateTime = new Dictionary<int, DateTime>(); private Dictionary<int, DateTime> _lastUIUpdateTime = new(); // 用于防抖处理
public Human LocateHuman(List<Human> humans, double begin, double end, double frameWidth, double frameHeight, int circleIndex) public Human LocateHuman(List<Human> humans, double frameWidth, double frameHeight, int circleIndex)
{ {
if (humans == null || humans.Count == 0) if (humans == null || humans.Count == 0)
{ {
@ -383,16 +388,17 @@ namespace Wpf_AiSportsMicrospace.Views
{ {
inCircleHuman = hu; inCircleHuman = hu;
} }
} }
if (isOut) if (isOut)
userList[circleIndex].ImageState = "3"; userList[circleIndex].ImageState = "3";
else else
{ {
if (userList[circleIndex].ImageState == "3") //if (userList[circleIndex].ImageState == "3")
{ //{
userList[circleIndex].ImageState = "1"; // userList[circleIndex].ImageState = "1";
} //}
// 检查当前编号是否未变化超过 2 秒 // 检查当前编号是否未变化超过 2 秒
if (userNumberList[circleIndex] == userList[circleIndex].NumberText) if (userNumberList[circleIndex] == userList[circleIndex].NumberText)
@ -403,11 +409,10 @@ namespace Wpf_AiSportsMicrospace.Views
var elapsed = (DateTime.Now - lastTime).TotalSeconds; var elapsed = (DateTime.Now - lastTime).TotalSeconds;
if (elapsed > 0.8) if (elapsed > 1)
{ {
// 2 秒未变化,更新状态为禁止
userList[circleIndex].ImageState = "1"; userList[circleIndex].ImageState = "1";
_lastJumpUpdateTime[circleIndex] = DateTime.Now; // 重置时间 _lastJumpUpdateTime[circleIndex] = DateTime.Now;
} }
// 否则2 秒内仍在变化,不更新状态 // 否则2 秒内仍在变化,不更新状态
} }
@ -418,7 +423,6 @@ namespace Wpf_AiSportsMicrospace.Views
_lastJumpUpdateTime[circleIndex] = DateTime.Now; _lastJumpUpdateTime[circleIndex] = DateTime.Now;
} }
} }
return inCircleHuman; return inCircleHuman;
} }
@ -462,35 +466,39 @@ namespace Wpf_AiSportsMicrospace.Views
// 订阅事件 // 订阅事件
sport.OnTicked += (count, times) => sport.OnTicked += (count, times) =>
{
currentItem.ImageState = "2";
Application.Current.Dispatcher.BeginInvoke(() =>
{ {
currentItem.NumberText = count.ToString(); currentItem.NumberText = count.ToString();
});
};
if (currentItem.ImageState != "2")
{
currentItem.ImageState = "2";
}
};
sport.PointThreshold = 0.03f;
sport.Start(); sport.Start();
sports.Add(sport); sports.Add(sport);
} }
} }
private void UpdateCircleCounts(List<Human> humans) private void UpdateCircleCounts(List<Human> humans)
{ {
for (int i = 0; i < circlePositionsX.Length; i++) // 在后台线程运行检测逻辑
{ //Parallel.For(0, circlePositions.Count, i =>
double center = circlePositionsX[i]; //{
double range = 0.07; // var human = LocateHuman(humans, userBox.ActualWidth, userBox.ActualHeight, i);
double begin = center - range; // sports[i].Pushing(human);
double end = center + range; //});
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) if (human != null)
{ {
sports[i].Pushing(human); sports[i].Pushing(human);
} }
} }
} }
/// <summary> /// <summary>
/// 添加带渐变光的圆圈(中心红色,边缘蓝色) /// 添加带渐变光的圆圈(中心红色,边缘蓝色)
/// </summary> /// </summary>

View File

@ -42,7 +42,7 @@ namespace Wpf_AiSportsMicrospace.Views
}; };
Yztob.AiSports.Common.SportAppSettingService.Set("inferences", options); Yztob.AiSports.Common.SportAppSettingService.Set("inferences", options);
_humanPredictor = HumanPredictorFactory.Create(HumanPredictorType.MultiLow); _humanPredictor = HumanPredictorFactory.Create(HumanPredictorType.MultiMedium);
_humanGraphicsRenderer = new HumanGraphicsRenderer(); _humanGraphicsRenderer = new HumanGraphicsRenderer();
_humanGraphicsRenderer.DrawLabel = false; _humanGraphicsRenderer.DrawLabel = false;
@ -85,6 +85,9 @@ namespace Wpf_AiSportsMicrospace.Views
{ {
try try
{ {
//if (frame.Number % 2 != 0)
// return;
var buffer = frame.GetImageBuffer(ImageFormat.Jpeg).ToArray(); var buffer = frame.GetImageBuffer(ImageFormat.Jpeg).ToArray();
var humanResult = _humanPredictor.Predicting(buffer, frame.Number); var humanResult = _humanPredictor.Predicting(buffer, frame.Number);
@ -122,7 +125,6 @@ namespace Wpf_AiSportsMicrospace.Views
// 切换到只有新页面 // 切换到只有新页面
MainContent.Content = newPage; MainContent.Content = newPage;
// 清理资源 // 清理资源
if (oldPage is IDisposable disposable) if (oldPage is IDisposable disposable)
disposable.Dispose(); disposable.Dispose();