From e492e408eec9f37ff37ee4a20d140b0e19d7207d Mon Sep 17 00:00:00 2001 From: tanglong <842690096@qq.com> Date: Tue, 23 Sep 2025 16:04:47 +0800 Subject: [PATCH] =?UTF-8?q?=E5=B7=A6=E5=8F=B3=E6=BB=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Wpf_AiSportsMicrospace/Home.xaml | 2 +- Wpf_AiSportsMicrospace/Home.xaml.cs | 94 ++++++++++++++++++- .../MyUserControl/CoverFlowControl.xaml.cs | 12 +++ 3 files changed, 106 insertions(+), 2 deletions(-) diff --git a/Wpf_AiSportsMicrospace/Home.xaml b/Wpf_AiSportsMicrospace/Home.xaml index 8e8d7ab..5e8e195 100644 --- a/Wpf_AiSportsMicrospace/Home.xaml +++ b/Wpf_AiSportsMicrospace/Home.xaml @@ -2,7 +2,7 @@ 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" - Title="Home" Height="400" Width="800"> + Title="Home" Height="400" Width="800" Loaded="Window_Loaded"> diff --git a/Wpf_AiSportsMicrospace/Home.xaml.cs b/Wpf_AiSportsMicrospace/Home.xaml.cs index b24d4d1..c8db342 100644 --- a/Wpf_AiSportsMicrospace/Home.xaml.cs +++ b/Wpf_AiSportsMicrospace/Home.xaml.cs @@ -1,4 +1,5 @@ -using System; +using Microsoft.ML.Runtime; +using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.ComponentModel; @@ -10,7 +11,15 @@ using System.Windows; using System.Windows.Controls; using System.Windows.Media; using System.Windows.Media.Animation; +using Wpf_AiSportsMicrospace.Common; using Wpf_AiSportsMicrospace.MyUserControl; +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 { @@ -19,9 +28,24 @@ namespace Wpf_AiSportsMicrospace /// public partial class Home : Window { + private IHumanPredictor _humanPredictor; + private IObjectDetector _objectDetector; + private HumanGraphicsRenderer _humanGraphicsRenderer; + private readonly List _sports; + private SportBase _sport; + private readonly SportDetectionQueue _detectQueue; + private IPointTracker _tracker; + public Home() { InitializeComponent(); + _humanPredictor = HumanPredictorFactory.Create(HumanPredictorType.SingleHigh); + _objectDetector = ObjectDetectorFactory.CreateSportGoodsDetector(); + _humanGraphicsRenderer = new HumanGraphicsRenderer(); + _humanGraphicsRenderer.DrawLabel = false; + + _sports = SportBase.GetSports(); + _detectQueue = new SportDetectionQueue(); string projectRoot = Path.Combine(AppContext.BaseDirectory, @"..\..\.."); string albumPath = Path.Combine(projectRoot, "Resources", "Img", "Album"); @@ -36,5 +60,73 @@ namespace Wpf_AiSportsMicrospace // 默认选中第3张 coverFlow.SelectedIndex = 2; } + + private void Window_Loaded(object sender, RoutedEventArgs e) + { + _tracker = PostureCalculate.CreatePointTracker("left_wrist", 0); + + LoadRTSP(); + } + + private void LoadRTSP() + { + //_sport.Reset(); + //_sport.Start(); + //_detectQueue.Start(); + + var _webcamClient = WebcamClient.CreateRTSP("192.168.3.64", "admin", "yd708090", 554u); + + //处理抽帧回调 + _webcamClient.OnExtractFrame += this.OnFrameExtracted; + _webcamClient.StartExtract();//开始抽帧 + + + } + + private DateTime _lastSlideTime = DateTime.MinValue; + + private void OnFrameExtracted(VideoFrame frame) + { + if (frame == null) return; + + // 抽帧和人体识别全部在后台线程 + Task.Run(() => + { + try + { + var buffer = frame.GetImageBuffer(ImageFormat.Jpeg).ToArray(); + var humanResult = _humanPredictor.Predicting(buffer, frame.Number); + var human = humanResult?.Humans?.FirstOrDefault(); + if (human == null) return; + + var tr = _tracker.Tracking(human); // 0 左滑,1 右滑 + + // 节流:每 500ms 最多更新一次 UI + if ((DateTime.Now - _lastSlideTime).TotalMilliseconds < 300) return; + _lastSlideTime = DateTime.Now; + + Application.Current?.Dispatcher?.BeginInvoke(new Action(() => + { + try + { + if (tr == 0) + { + //coverFlow.SlideLeft(); + } + else if (tr == 1) + coverFlow.SlideRight(); + } + catch (Exception ex) + { + Console.WriteLine("UI update error: " + ex.Message); + } + }), System.Windows.Threading.DispatcherPriority.Background); + } + catch (Exception ex) + { + Console.WriteLine("OnFrameExtracted error: " + ex.Message); + } + }); + } } } diff --git a/Wpf_AiSportsMicrospace/MyUserControl/CoverFlowControl.xaml.cs b/Wpf_AiSportsMicrospace/MyUserControl/CoverFlowControl.xaml.cs index d3042d0..f5d88d8 100644 --- a/Wpf_AiSportsMicrospace/MyUserControl/CoverFlowControl.xaml.cs +++ b/Wpf_AiSportsMicrospace/MyUserControl/CoverFlowControl.xaml.cs @@ -147,5 +147,17 @@ namespace Wpf_AiSportsMicrospace.MyUserControl } return null; } + + // 外部调用:左滑(下一张) + public void SlideLeft() + { + SelectedIndex++; + } + + // 外部调用:右滑(上一张) + public void SlideRight() + { + SelectedIndex--; + } } }