Compare commits

..

No commits in common. "9a646dbb3ceac4908f468cfb436ed8695ac0cc12" and "bbc0291fedcf01d0e421eb45a59e30f43240a7f6" have entirely different histories.

2 changed files with 72 additions and 83 deletions

View File

@ -27,7 +27,6 @@ namespace Wpf_AiSportsMicrospace.Common
IPointTracker _rightTracker; IPointTracker _rightTracker;
IPointTracker _leftElbow; IPointTracker _leftElbow;
IPointTracker _rightElbow; IPointTracker _rightElbow;
WebcamClient _webcamClient;
public SportOperate() public SportOperate()
{ {
@ -43,8 +42,8 @@ namespace Wpf_AiSportsMicrospace.Common
} }
public WebcamClient CreateRTSP() public WebcamClient CreateRTSP()
{ {
_webcamClient = WebcamClient.CreateRTSP("192.168.3.64", "admin", "yd708090", 554u); WebcamClient webcamClient = WebcamClient.CreateRTSP("192.168.3.64", "admin", "yd708090", 554u);
return _webcamClient; return webcamClient;
} }
/// <summary> /// <summary>

View File

@ -1,7 +1,6 @@
 
using Microsoft.ML.Runtime; using Microsoft.ML.Runtime;
using System; using System;
using System.Collections.Concurrent;
using System.Collections.Generic; using System.Collections.Generic;
using System.Collections.ObjectModel; using System.Collections.ObjectModel;
using System.ComponentModel; using System.ComponentModel;
@ -37,8 +36,7 @@ namespace Wpf_AiSportsMicrospace
private SportBase _sport; private SportBase _sport;
private readonly SportDetectionQueue _detectQueue; private readonly SportDetectionQueue _detectQueue;
private WebcamClient _webcamClient; private WebcamClient _webcamClient;
private ConcurrentQueue<VideoFrame> _frameQueue = new();
private CancellationTokenSource _cts = new();
private SportOperate _sportOperate; private SportOperate _sportOperate;
public Home() public Home()
@ -78,36 +76,17 @@ namespace Wpf_AiSportsMicrospace
_sportOperate = new SportOperate(); _sportOperate = new SportOperate();
_webcamClient = _sportOperate.CreateRTSP(); _webcamClient = _sportOperate.CreateRTSP();
_webcamClient.OnExtractFrame += frame => //处理抽帧回调
{ _webcamClient.OnExtractFrame += this.OnFrameExtracted;
if (frame != null) _webcamClient.StartExtract();//开始抽帧
_frameQueue.Enqueue(frame);
};
_webcamClient.StartExtract();
StartFrameProcessing();
} }
private void StartFrameProcessing() private void OnFrameExtracted(VideoFrame frame)
{ {
if (frame == null) return;
// 抽帧和人体识别全部在后台线程
Task.Run(() => Task.Run(() =>
{
while (!_cts.Token.IsCancellationRequested)
{
if (_frameQueue.TryDequeue(out var frame))
{
ProcessFrame(frame);
}
else
{
Thread.Sleep(5);
}
}
}, _cts.Token);
}
private void ProcessFrame(VideoFrame frame)
{ {
try try
{ {
@ -154,20 +133,31 @@ namespace Wpf_AiSportsMicrospace
if (liftHandAction == 1) if (liftHandAction == 1)
{ {
// 启动进度条动画; // 启动进度条动画;
Dispatcher.BeginInvoke(() => coverFlow.StartSelectedProgress()); SlideCoverFlow(coverFlow.StartSelectedProgress);
return;
} }
} }
catch (Exception ex) catch (Exception ex)
{ {
Console.WriteLine("OnFrameExtracted error: " + ex.Message); Console.WriteLine("OnFrameExtracted error: " + ex.Message);
} }
});
} }
protected override void OnClosed(EventArgs e) // 抽象 UI 更新到方法中
private void SlideCoverFlow(Action slideAction)
{ {
base.OnClosed(e); Application.Current?.Dispatcher?.BeginInvoke(new Action(() =>
_cts.Cancel(); {
_webcamClient?.StopExtract(); try
{
slideAction?.Invoke();
}
catch (Exception ex)
{
Console.WriteLine("UI update error: " + ex.Message);
}
}), System.Windows.Threading.DispatcherPriority.Background);
} }
} }
} }