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;
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

View File

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

View File

@ -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,11 +158,15 @@ 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);
int wavingaction = 0;
foreach (var human in humans)
{
wavingaction = DetectRightHandRaise(human);
if (wavingaction < 3)
continue;
switch (wavingaction)
{
@ -189,6 +193,7 @@ namespace Wpf_AiSportsMicrospace.Views
break;
}
}
}
else
{
UpdateCircleCounts(humans);
@ -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<int, DateTime> _lastJumpUpdateTime = new Dictionary<int, DateTime>();
public Human LocateHuman(List<Human> humans, double begin, double end, double frameWidth, double frameHeight, int circleIndex)
private Dictionary<int, DateTime> _lastJumpUpdateTime = new(); // 用于存储最后更新时间
private Dictionary<int, DateTime> _lastUIUpdateTime = new(); // 用于防抖处理
public Human LocateHuman(List<Human> 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;
}
@ -462,35 +466,39 @@ namespace Wpf_AiSportsMicrospace.Views
// 订阅事件
sport.OnTicked += (count, times) =>
{
currentItem.ImageState = "2";
Application.Current.Dispatcher.BeginInvoke(() =>
{
currentItem.NumberText = count.ToString();
});
};
if (currentItem.ImageState != "2")
{
currentItem.ImageState = "2";
}
};
sport.PointThreshold = 0.03f;
sport.Start();
sports.Add(sport);
}
}
private void UpdateCircleCounts(List<Human> 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);
}
}
}
/// <summary>
/// 添加带渐变光的圆圈(中心红色,边缘蓝色)
/// </summary>

View File

@ -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();