diff --git a/Wpf_AiSportsMicrospace/Home.xaml.cs b/Wpf_AiSportsMicrospace/Home.xaml.cs index 7c398d8..fb175ca 100644 --- a/Wpf_AiSportsMicrospace/Home.xaml.cs +++ b/Wpf_AiSportsMicrospace/Home.xaml.cs @@ -1,6 +1,7 @@  using Microsoft.ML.Runtime; using System; +using System.Collections.Concurrent; using System.Collections.Generic; using System.Collections.ObjectModel; using System.ComponentModel; @@ -36,7 +37,8 @@ namespace Wpf_AiSportsMicrospace private SportBase _sport; private readonly SportDetectionQueue _detectQueue; private WebcamClient _webcamClient; - + private ConcurrentQueue _frameQueue = new(); + private CancellationTokenSource _cts = new(); private SportOperate _sportOperate; public Home() @@ -75,89 +77,97 @@ namespace Wpf_AiSportsMicrospace { _sportOperate = new SportOperate(); _webcamClient = _sportOperate.CreateRTSP(); + + _webcamClient.OnExtractFrame += frame => + { + if (frame != null) + _frameQueue.Enqueue(frame); + }; + _webcamClient.StartExtract(); - //处理抽帧回调 - _webcamClient.OnExtractFrame += this.OnFrameExtracted; - _webcamClient.StartExtract();//开始抽帧 + StartFrameProcessing(); } - private void OnFrameExtracted(VideoFrame frame) + private void StartFrameProcessing() { - if (frame == null) return; - - // 抽帧和人体识别全部在后台线程 Task.Run(() => { - try + while (!_cts.Token.IsCancellationRequested) { - 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; - - - // 启动进度条动画; - //SlideCoverFlow(coverFlow.StartSelectedProgress); - - ////检测挥手动作 - //var wavingAction = _sportOperate.VerifyWavingAction(human); - //// 根据手势结果选择滑动方向 - //if (wavingAction == 1) - //{ - // SlideCoverFlow(coverFlow.SlideRight); - // return; - //} - //if (wavingAction == 2) - //{ - // SlideCoverFlow(coverFlow.SlideLeft); - // return; - //} - - //检测举手动作 - var liftHandAction = _sportOperate.VerifyLiftHandAction(human); - - // 根据手势结果 - if (liftHandAction == 1) + if (_frameQueue.TryDequeue(out var frame)) { - // 启动进度条动画; - SlideCoverFlow(coverFlow.StartSelectedProgress); - return; + ProcessFrame(frame); + } + else + { + Thread.Sleep(5); } } - catch (Exception ex) - { - Console.WriteLine("OnFrameExtracted error: " + ex.Message); - } - }); + }, _cts.Token); } - // 抽象 UI 更新到方法中 - private void SlideCoverFlow(Action slideAction) + + private void ProcessFrame(VideoFrame frame) { - Application.Current?.Dispatcher?.BeginInvoke(new Action(() => + try { - 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; + + + // 启动进度条动画; + //SlideCoverFlow(coverFlow.StartSelectedProgress); + + ////检测挥手动作 + //var wavingAction = _sportOperate.VerifyWavingAction(human); + //// 根据手势结果选择滑动方向 + //if (wavingAction == 1) + //{ + // SlideCoverFlow(coverFlow.SlideRight); + // return; + //} + //if (wavingAction == 2) + //{ + // SlideCoverFlow(coverFlow.SlideLeft); + // return; + //} + + //检测举手动作 + var liftHandAction = _sportOperate.VerifyLiftHandAction(human); + + // 根据手势结果 + if (liftHandAction == 1) { - slideAction?.Invoke(); + // 启动进度条动画; + Dispatcher.BeginInvoke(() => coverFlow.StartSelectedProgress()); } - catch (Exception ex) - { - Console.WriteLine("UI update error: " + ex.Message); - } - }), System.Windows.Threading.DispatcherPriority.Background); + } + catch (Exception ex) + { + Console.WriteLine("OnFrameExtracted error: " + ex.Message); + } + } + + protected override void OnClosed(EventArgs e) + { + base.OnClosed(e); + _cts.Cancel(); + _webcamClient?.StopExtract(); } } }