update:静态资源
BIN
Wpf_AiSportsMicrospace/Resources/Img/play_img/play_home_bg.png
Normal file
|
After Width: | Height: | Size: 422 KiB |
|
After Width: | Height: | Size: 14 KiB |
BIN
Wpf_AiSportsMicrospace/Resources/Img/play_img/play_jump.png
Normal file
|
After Width: | Height: | Size: 133 KiB |
BIN
Wpf_AiSportsMicrospace/Resources/Img/play_img/play_music.png
Normal file
|
After Width: | Height: | Size: 120 KiB |
BIN
Wpf_AiSportsMicrospace/Resources/Img/play_img/play_vs.png
Normal file
|
After Width: | Height: | Size: 218 KiB |
BIN
Wpf_AiSportsMicrospace/Resources/Img/test_img/test_home_bg.png
Normal file
|
After Width: | Height: | Size: 358 KiB |
|
After Width: | Height: | Size: 13 KiB |
BIN
Wpf_AiSportsMicrospace/Resources/Img/test_img/test_jump.png
Normal file
|
After Width: | Height: | Size: 242 KiB |
BIN
Wpf_AiSportsMicrospace/Resources/Img/test_img/test_rope.png
Normal file
|
After Width: | Height: | Size: 124 KiB |
BIN
Wpf_AiSportsMicrospace/Resources/Img/test_img/test_situp.png
Normal file
|
After Width: | Height: | Size: 154 KiB |
84
Wpf_AiSportsMicrospace/Views/CenterHome.xaml
Normal file
@ -0,0 +1,84 @@
|
||||
<UserControl x:Class="Wpf_AiSportsMicrospace.Views.CenterHome"
|
||||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||
xmlns:local="clr-namespace:Wpf_AiSportsMicrospace.MyUserControl"
|
||||
xmlns:gif="http://wpfanimatedgif.codeplex.com"
|
||||
Height="1080" Width="1920" >
|
||||
<Grid VerticalAlignment="Bottom">
|
||||
<Grid.Background>
|
||||
<ImageBrush ImageSource="/Resources/Img/play_img/play_home_bg.png" Stretch="UniformToFill"/>
|
||||
</Grid.Background>
|
||||
|
||||
<!-- 顶部图片 -->
|
||||
<Image
|
||||
Source="/Resources/Img/play_img/play_home_title.png"
|
||||
HorizontalAlignment="Center"
|
||||
VerticalAlignment="Top"
|
||||
Width="615"
|
||||
Margin="0,50,0,0"
|
||||
/>
|
||||
|
||||
<!-- CoverFlowControl,距离图片80 -->
|
||||
<local:CoverFlowControl
|
||||
x:Name="coverFlow"
|
||||
HorizontalAlignment="Center"
|
||||
VerticalAlignment="Top"
|
||||
Height="900"
|
||||
Width="1920"
|
||||
Margin="0,180,0,0"
|
||||
Padding="0,100"
|
||||
/>
|
||||
<Grid Margin="314,833,214,0" VerticalAlignment="Top" Height="182">
|
||||
<Image
|
||||
Source="/Resources/Img/Album/change_left.png"
|
||||
HorizontalAlignment="Left"
|
||||
VerticalAlignment="Top"
|
||||
Width="330"
|
||||
Margin="0,100,0,0"
|
||||
/>
|
||||
<Image
|
||||
gif:ImageBehavior.AnimatedSource="/Resources/Img/Album/3.gif"
|
||||
gif:ImageBehavior.RepeatBehavior="Forever"
|
||||
HorizontalAlignment="Left"
|
||||
VerticalAlignment="Top"
|
||||
Margin="-20,10,30,20"
|
||||
/>
|
||||
|
||||
|
||||
<Image
|
||||
Source="/Resources/Img/Album/change_back.png"
|
||||
HorizontalAlignment="Center"
|
||||
VerticalAlignment="Top"
|
||||
Width="330"
|
||||
Margin="0,97,0,0"
|
||||
/>
|
||||
<Image
|
||||
gif:ImageBehavior.AnimatedSource="/Resources/Img/Album/2.gif"
|
||||
gif:ImageBehavior.RepeatBehavior="Forever"
|
||||
HorizontalAlignment="Left"
|
||||
VerticalAlignment="Top"
|
||||
Margin="513,16,0,0" Height="184" Width="146"
|
||||
/>
|
||||
|
||||
|
||||
<Image
|
||||
Source="/Resources/Img/Album/change_right.png"
|
||||
HorizontalAlignment="right"
|
||||
VerticalAlignment="Top"
|
||||
Width="330"
|
||||
Margin="0,100,0,0"
|
||||
/>
|
||||
<Image
|
||||
gif:ImageBehavior.AnimatedSource="/Resources/Img/Album/1.gif"
|
||||
gif:ImageBehavior.RepeatBehavior="Forever"
|
||||
HorizontalAlignment="right"
|
||||
VerticalAlignment="Top"
|
||||
Margin="50,10,200,20"
|
||||
/>
|
||||
|
||||
|
||||
</Grid>
|
||||
|
||||
|
||||
</Grid>
|
||||
</UserControl>
|
||||
253
Wpf_AiSportsMicrospace/Views/CenterHome.xaml.cs
Normal file
@ -0,0 +1,253 @@
|
||||
using Microsoft.ML.Runtime;
|
||||
using System;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.ComponentModel;
|
||||
using System.Diagnostics;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows;
|
||||
using System.Windows.Controls;
|
||||
using System.Windows.Input;
|
||||
using System.Windows.Media;
|
||||
using System.Windows.Media.Animation;
|
||||
using Wpf_AiSportsMicrospace.Common;
|
||||
using Wpf_AiSportsMicrospace.Enum;
|
||||
using Wpf_AiSportsMicrospace.MyUserControl;
|
||||
using Wpf_AiSportsMicrospace.Views;
|
||||
using Yztob.AiSports.Inferences.Abstractions;
|
||||
using Yztob.AiSports.Postures;
|
||||
using Yztob.AiSports.Postures.Abstractions;
|
||||
using Yztob.AiSports.Postures.Sports;
|
||||
using Yztob.AiSports.Postures.Things;
|
||||
using Yztob.AiSports.Sensors.Abstractions;
|
||||
using Yztob.AiSports.Sensors.Things;
|
||||
namespace Wpf_AiSportsMicrospace.Views
|
||||
{
|
||||
/// <summary>
|
||||
/// Home.xaml 的交互逻辑
|
||||
/// </summary>
|
||||
public partial class CenterHome : UserControl
|
||||
{
|
||||
private IHumanPredictor _humanPredictor;
|
||||
private WebcamClient _webcamClient;
|
||||
private ConcurrentQueue<VideoFrame> _frameQueue = new();
|
||||
private CancellationTokenSource _cts = new();
|
||||
private SportOperate _sportOperate;
|
||||
|
||||
public String _nowSelect = "test"; //测试吧:test 游戏吧:play
|
||||
public String NowSelect
|
||||
{
|
||||
get => _nowSelect;
|
||||
set
|
||||
{
|
||||
if (_nowSelect != value)
|
||||
{
|
||||
_nowSelect = value;
|
||||
InitImg();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public CenterHome()
|
||||
{
|
||||
InitializeComponent();
|
||||
_humanPredictor = HumanPredictorFactory.Create(HumanPredictorType.SingleHigh);
|
||||
|
||||
//_sports = SportBase.GetSports();
|
||||
//_detectQueue = new SportDetectionQueue();
|
||||
|
||||
//string imgPath = Path.Combine(projectRoot, "Resources", "Img" , _nowSelect == "test" ? "test_img" : "play_img");
|
||||
|
||||
// 默认选中第1张
|
||||
coverFlow.SelectedIndex = 0;
|
||||
|
||||
InitImg();
|
||||
//AnimationBehavior.SetSourceUri(LoadingImage, loadingImage);
|
||||
}
|
||||
|
||||
private void Window_Loaded(object sender, RoutedEventArgs e)
|
||||
{
|
||||
_sportOperate = new SportOperate();
|
||||
_webcamClient = _sportOperate.CreateRTSP();
|
||||
|
||||
_webcamClient.OnExtractFrame += frame =>
|
||||
{
|
||||
if (frame != null)
|
||||
_frameQueue.Enqueue(frame);
|
||||
};
|
||||
_webcamClient.StartExtract();
|
||||
|
||||
StartFrameProcessing();
|
||||
|
||||
Utils.PlayBackgroundMusic("homeprojectselected.mp3");
|
||||
|
||||
//coverFlow.ProgressCompleted += CoverFlow_ProgressCompleted;
|
||||
}
|
||||
private void CoverFlow_ProgressCompleted(CoverFlowItem item)
|
||||
{
|
||||
// 停止抽帧线程/释放资源
|
||||
try
|
||||
{
|
||||
_cts.Cancel(); // 停止后台处理线程
|
||||
_webcamClient?.StopExtract(); // 停止摄像头抽帧
|
||||
_webcamClient = null;
|
||||
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Debug.WriteLine($"停止抽帧异常: {ex.Message}");
|
||||
}
|
||||
|
||||
// 解绑事件,防止重复触发
|
||||
//coverFlow.ProgressCompleted -= CoverFlow_ProgressCompleted;
|
||||
|
||||
// 根据图片跳转新窗口
|
||||
string uri = item.ImageUri.ToString();
|
||||
|
||||
//if (uri.EndsWith("1.jpg"))
|
||||
// newWindow = new GroupJumpRope();
|
||||
//else if (uri.EndsWith("2.jpg"))
|
||||
// newWindow = new GroupJumpRope();
|
||||
//else if (uri.EndsWith("3.jpg"))
|
||||
// newWindow = new GroupJumpRope();
|
||||
// 找到主窗口,切换内容到 GroupJumpRopePage
|
||||
|
||||
//var mainWin = Application.Current.MainWindow as Main;
|
||||
//if (mainWin != null)
|
||||
//{
|
||||
// Utils.PlayBackgroundMusic("musicjumprope1.mp3");
|
||||
// mainWin.SwitchPage(new GroupJumpRope(), true);
|
||||
//}
|
||||
|
||||
RouterGoNew();
|
||||
}
|
||||
|
||||
private void StartFrameProcessing()
|
||||
{
|
||||
Task.Run(() =>
|
||||
{
|
||||
while (!_cts.Token.IsCancellationRequested)
|
||||
{
|
||||
if (_frameQueue.TryDequeue(out var frame))
|
||||
{
|
||||
ProcessFrame(frame);
|
||||
}
|
||||
else
|
||||
{
|
||||
//_webcamClient.OnExtractFrame += frame =>
|
||||
//{
|
||||
// if (frame != null)
|
||||
// _frameQueue.Enqueue(frame);
|
||||
//};
|
||||
//_webcamClient.StartExtract();
|
||||
|
||||
_webcamClient.StartExtract();
|
||||
//Thread.Sleep(5);
|
||||
}
|
||||
}
|
||||
}, _cts.Token);
|
||||
}
|
||||
|
||||
private void ProcessFrame(VideoFrame frame)
|
||||
{
|
||||
try
|
||||
{
|
||||
var buffer = frame.GetImageBuffer(ImageFormat.Jpeg).ToArray();
|
||||
var humanResult = _humanPredictor.Predicting(buffer, frame.Number);
|
||||
|
||||
var humans = humanResult?.Humans?.ToList();
|
||||
if (humans == null || humans.Count == 0)
|
||||
return;
|
||||
|
||||
//var human = humans
|
||||
// .Where(h =>
|
||||
// h.Keypoints.Any(kp => kp.Name == "left_ankle" && kp.X < 1020 && kp.Y > 900 && kp.Y < 1020) &&
|
||||
// h.Keypoints.Any(kp => kp.Name == "right_ankle" && kp.X > 750 && kp.Y > 900 && kp.Y < 1020)
|
||||
// )
|
||||
// .FirstOrDefault();
|
||||
|
||||
var human = humans.FirstOrDefault();
|
||||
|
||||
if (human == null) return;
|
||||
|
||||
//检测挥手动作
|
||||
var wavingaction = _sportOperate.VerifyWavingAction(human);
|
||||
|
||||
// 把低 8 位作为动作类型,高 8 位作为进度
|
||||
//int actionType = wavingaction & 0xFF;
|
||||
//int progress = (wavingaction >> 8) & 0xFF;
|
||||
|
||||
switch (wavingaction)
|
||||
{
|
||||
case (int)WavingAction.LeftWave: // 左手挥动
|
||||
Dispatcher.BeginInvoke(() => coverFlow.SlideRight());
|
||||
break;
|
||||
|
||||
case (int)WavingAction.RightWave: // 右手挥动
|
||||
Dispatcher.BeginInvoke(() => coverFlow.SlideLeft());
|
||||
break;
|
||||
|
||||
case (int)WavingAction.FirstHand: // 举手开始
|
||||
Dispatcher.BeginInvoke(() => coverFlow.StartSelectedProgress());
|
||||
break;
|
||||
|
||||
case (int)WavingAction.Raising: // 举手中,实时更新进度
|
||||
break;
|
||||
|
||||
case (int)WavingAction.RaiseHand: // 举手完成
|
||||
_cts.Cancel();
|
||||
coverFlow.ProgressCompleted += CoverFlow_ProgressCompleted;
|
||||
break;
|
||||
|
||||
default: // 没有动作 → 取消进度
|
||||
Dispatcher.BeginInvoke(() => coverFlow.CancelSelectedProgress());
|
||||
break;
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine("OnFrameExtracted error: " + ex.Message);
|
||||
}
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public void InitImg()
|
||||
{
|
||||
string projectRoot = Path.Combine(AppContext.BaseDirectory, @"..\..\..");
|
||||
coverFlow.Images.Clear(); // 先清空原有图
|
||||
if (_nowSelect == "test")
|
||||
{
|
||||
string imgPath = Path.Combine(projectRoot, "Resources", "Img", "test_img");
|
||||
coverFlow.Images.Add(new CoverFlowItem { ImageUri = new Uri(Path.Combine(imgPath, "test_jump.png")), ProgressColor1 = "#215bc7", ProgressColor2 = "#fc640e" });
|
||||
coverFlow.Images.Add(new CoverFlowItem { ImageUri = new Uri(Path.Combine(imgPath, "test_situp.png")), ProgressColor1 = "#e73d42", ProgressColor2 = "#fd8212" });
|
||||
coverFlow.Images.Add(new CoverFlowItem { ImageUri = new Uri(Path.Combine(imgPath, "test_rope.png")), ProgressColor1 = "#e73d42", ProgressColor2 = "#215bc7" });
|
||||
}
|
||||
else
|
||||
{
|
||||
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" });
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
//跳转二级页面
|
||||
private void RouterGoNew()
|
||||
{
|
||||
// 跳转逻辑(如导航到新页面并传递参数)
|
||||
// 例如:RaiseEvent、调用外部委托、或使用导航框架
|
||||
var mainWin = Application.Current.MainWindow as Main;
|
||||
var newPage = new GroupJumpRope();
|
||||
mainWin?.SwitchPage(newPage, true);
|
||||
}
|
||||
}
|
||||
}
|
||||