This commit is contained in:
ltx 2026-03-24 14:39:03 +08:00
commit 0c62f23379
4 changed files with 36 additions and 22 deletions

View File

@ -1,8 +1,7 @@
using System; using System;
using System.Collections.Concurrent;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using Yztob.AiSports.Inferences.Things; using Yztob.AiSports.Inferences.Things;
using Yztob.AiSports.Postures.Sports; using Yztob.AiSports.Postures.Sports;
@ -10,7 +9,7 @@ using Yztob.AiSports.Postures.Sports;
namespace Wpf_AiSportsMicrospace.Common namespace Wpf_AiSportsMicrospace.Common
{ {
/// <summary> /// <summary>
/// 运动检后台线程顺序检测队列 /// 运动检后台线程顺序检测队列(线程安全)
/// </summary> /// </summary>
internal class SportDetectionQueue internal class SportDetectionQueue
{ {
@ -36,8 +35,8 @@ namespace Wpf_AiSportsMicrospace.Common
#endregion #endregion
#region 私有成员 #region 私有成员
private readonly Queue<QueueItem> _queues; private readonly ConcurrentQueue<QueueItem> _queues;
private int _signral = 0; //0-停止 1-执行中 2-停止信号 private volatile int _signral = 0; //0-停止 1-执行中 2-停止信号
#endregion #endregion
#region 构造方法 #region 构造方法
@ -46,7 +45,7 @@ namespace Wpf_AiSportsMicrospace.Common
/// </summary> /// </summary>
public SportDetectionQueue() public SportDetectionQueue()
{ {
_queues = new Queue<QueueItem>(); _queues = new ConcurrentQueue<QueueItem>();
} }
#endregion #endregion
@ -73,14 +72,12 @@ namespace Wpf_AiSportsMicrospace.Common
_signral = 1; _signral = 1;
while (_signral == 1) while (_signral == 1)
{ {
//无队列 if (!_queues.TryDequeue(out var item))
if (!_queues.Any())
{ {
Thread.Sleep(5); Thread.Sleep(1);
continue; continue;
} }
var item = _queues.Dequeue();
if (item == null) if (item == null)
continue; continue;
@ -129,7 +126,7 @@ namespace Wpf_AiSportsMicrospace.Common
return; return;
_signral = 1; _signral = 1;
_queues.Clear();//队列清空 DrainQueue();
var thread = new Thread(Detecting); var thread = new Thread(Detecting);
thread.IsBackground = true; thread.IsBackground = true;
thread.Start(); thread.Start();
@ -145,7 +142,7 @@ namespace Wpf_AiSportsMicrospace.Common
_signral = 2; _signral = 2;
_queues.Clear(); DrainQueue();
} }
/// <summary> /// <summary>
@ -153,7 +150,12 @@ namespace Wpf_AiSportsMicrospace.Common
/// </summary> /// </summary>
public void Clear() public void Clear()
{ {
_queues.Clear(); DrainQueue();
}
private void DrainQueue()
{
while (_queues.TryDequeue(out _)) { }
} }
#endregion #endregion
} }

View File

@ -1,4 +1,4 @@
using AiSportsMicrospaceDB.DBContext; using AiSportsMicrospaceDB.DBContext;
using Common; using Common;
using HandyControl.Controls; using HandyControl.Controls;
using Microsoft.EntityFrameworkCore.Storage.Json; using Microsoft.EntityFrameworkCore.Storage.Json;
@ -196,13 +196,11 @@ namespace Wpf_AiSportsMicrospace.Common
for (int i = 0; i < humans.Count; i++) for (int i = 0; i < humans.Count; i++)
{ {
var h = humans[i]; var h = humans[i];
if (IsAnyFootInCircle(h, cx, cy, halfW, halfH)) if (IsAnyFootInCircle(h, cx, cy, halfW, halfH))
{ {
target = h; target = h;
break;
} }
if (target != null) break;
} }
if (target == null) if (target == null)

View File

@ -1,4 +1,4 @@
using AiSportsMicrospaceDB.DBContext; using AiSportsMicrospaceDB.DBContext;
using AiSportsMicrospaceDB.Entities; using AiSportsMicrospaceDB.Entities;
using Common; using Common;
using Dto; using Dto;
@ -147,6 +147,10 @@ namespace Views.JumpLong
{ {
try try
{ {
// 仅在运动中进行跳远检测,避免等待举手时的重复推理
if (_sportType != 1)
return;
var buffer = frame.GetImageBuffer(ImageFormat.Jpeg).ToArray(); var buffer = frame.GetImageBuffer(ImageFormat.Jpeg).ToArray();
var humanResult = await Task.Run<HumanInferenceResult>(() => _humanPredictor.Predicting(buffer, frame.Number)); var humanResult = await Task.Run<HumanInferenceResult>(() => _humanPredictor.Predicting(buffer, frame.Number));
var objects = _sport.Equipment ? await Task.Run<List<BoundingBox>>(() => _objectDetector.Detecting(buffer)) : new List<BoundingBox>(); var objects = _sport.Equipment ? await Task.Run<List<BoundingBox>>(() => _objectDetector.Detecting(buffer)) : new List<BoundingBox>();

View File

@ -1,4 +1,4 @@

using SharpDX.Direct3D9; using SharpDX.Direct3D9;
using System.Collections.Concurrent; using System.Collections.Concurrent;
using System.Drawing; using System.Drawing;
@ -32,6 +32,8 @@ namespace Wpf_AiSportsMicrospace.Views
private readonly ConcurrentQueue<VideoFrame> _frameQueue = new(); private readonly ConcurrentQueue<VideoFrame> _frameQueue = new();
private readonly CancellationTokenSource _cts = new(); private readonly CancellationTokenSource _cts = new();
private readonly object _reconnectLock = new(); private readonly object _reconnectLock = new();
private int _lastEnqueuedFrame = -1;
private const int MaxFrameQueueSize = 8; // 队列上限,防止内存堆积
public event EventHandler<List<Human>> HumanFrameUpdated; public event EventHandler<List<Human>> HumanFrameUpdated;
@ -75,6 +77,14 @@ namespace Wpf_AiSportsMicrospace.Views
WebcamClient.OnExtractFrame += frame => WebcamClient.OnExtractFrame += frame =>
{ {
// 跳帧:降低识别负载,保持约 15fps,与其他预览逻辑一致
if (_lastEnqueuedFrame >= 0 && frame.Number - _lastEnqueuedFrame < 2)
return;
_lastEnqueuedFrame = (int)frame.Number;
// 队列满时丢弃新帧,避免内存堆积和延迟
if (_frameQueue.Count >= MaxFrameQueueSize)
return;
_frameQueue.Enqueue(frame); _frameQueue.Enqueue(frame);
}; };
@ -90,8 +100,8 @@ namespace Wpf_AiSportsMicrospace.Views
} }
else else
{ {
// 避免 CPU 占满 // 队列空时短暂休眠,1ms 比 5ms 响应更快
Thread.Sleep(5); Thread.Sleep(1);
} }
} }
}, _cts.Token); }, _cts.Token);