线程阻塞
This commit is contained in:
parent
1b66fae16c
commit
9a646dbb3c
@ -1,6 +1,7 @@
|
|||||||
|
|
||||||
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;
|
||||||
@ -36,7 +37,8 @@ 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()
|
||||||
@ -76,88 +78,96 @@ namespace Wpf_AiSportsMicrospace
|
|||||||
_sportOperate = new SportOperate();
|
_sportOperate = new SportOperate();
|
||||||
_webcamClient = _sportOperate.CreateRTSP();
|
_webcamClient = _sportOperate.CreateRTSP();
|
||||||
|
|
||||||
//处理抽帧回调
|
_webcamClient.OnExtractFrame += frame =>
|
||||||
_webcamClient.OnExtractFrame += this.OnFrameExtracted;
|
{
|
||||||
_webcamClient.StartExtract();//开始抽帧
|
if (frame != null)
|
||||||
|
_frameQueue.Enqueue(frame);
|
||||||
|
};
|
||||||
|
_webcamClient.StartExtract();
|
||||||
|
|
||||||
|
StartFrameProcessing();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void OnFrameExtracted(VideoFrame frame)
|
private void StartFrameProcessing()
|
||||||
{
|
{
|
||||||
if (frame == null) return;
|
|
||||||
|
|
||||||
// 抽帧和人体识别全部在后台线程
|
|
||||||
Task.Run(() =>
|
Task.Run(() =>
|
||||||
{
|
{
|
||||||
try
|
while (!_cts.Token.IsCancellationRequested)
|
||||||
{
|
{
|
||||||
var buffer = frame.GetImageBuffer(ImageFormat.Jpeg).ToArray();
|
if (_frameQueue.TryDequeue(out var frame))
|
||||||
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)
|
|
||||||
{
|
{
|
||||||
// 启动进度条动画;
|
ProcessFrame(frame);
|
||||||
SlideCoverFlow(coverFlow.StartSelectedProgress);
|
}
|
||||||
return;
|
else
|
||||||
|
{
|
||||||
|
Thread.Sleep(5);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
}, _cts.Token);
|
||||||
{
|
|
||||||
Console.WriteLine("OnFrameExtracted error: " + ex.Message);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 抽象 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)
|
}
|
||||||
{
|
catch (Exception ex)
|
||||||
Console.WriteLine("UI update error: " + ex.Message);
|
{
|
||||||
}
|
Console.WriteLine("OnFrameExtracted error: " + ex.Message);
|
||||||
}), System.Windows.Threading.DispatcherPriority.Background);
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
protected override void OnClosed(EventArgs e)
|
||||||
|
{
|
||||||
|
base.OnClosed(e);
|
||||||
|
_cts.Cancel();
|
||||||
|
_webcamClient?.StopExtract();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user