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()
@ -77,97 +75,89 @@ namespace Wpf_AiSportsMicrospace
{ {
_sportOperate = new SportOperate(); _sportOperate = new SportOperate();
_webcamClient = _sportOperate.CreateRTSP(); _webcamClient = _sportOperate.CreateRTSP();
_webcamClient.OnExtractFrame += frame =>
{
if (frame != null)
_frameQueue.Enqueue(frame);
};
_webcamClient.StartExtract();
StartFrameProcessing(); //处理抽帧回调
_webcamClient.OnExtractFrame += this.OnFrameExtracted;
_webcamClient.StartExtract();//开始抽帧
} }
private void StartFrameProcessing() private void OnFrameExtracted(VideoFrame frame)
{ {
if (frame == null) return;
// 抽帧和人体识别全部在后台线程
Task.Run(() => Task.Run(() =>
{ {
while (!_cts.Token.IsCancellationRequested) try
{ {
if (_frameQueue.TryDequeue(out var frame)) var buffer = frame.GetImageBuffer(ImageFormat.Jpeg).ToArray();
{ var humanResult = _humanPredictor.Predicting(buffer, frame.Number);
ProcessFrame(frame);
} var humans = humanResult?.Humans?.ToList();
else if (humans == null || humans.Count == 0)
{ return;
Thread.Sleep(5);
} //var human = humans
} // .Where(h =>
}, _cts.Token); // 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;
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;
// 启动进度条动画;
//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)
{
// 启动进度条动画; // 启动进度条动画;
Dispatcher.BeginInvoke(() => coverFlow.StartSelectedProgress()); //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)
{
// 启动进度条动画;
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);
} }
} }
} }