Compare commits

...

3 Commits
dev ... 1014_2

Author SHA1 Message Date
20c62d9927 ss 2025-10-14 19:47:25 +08:00
68b5ad8c1e kadunban 2025-10-14 18:44:01 +08:00
8ab511e961 ss 2025-10-14 16:11:12 +08:00
2 changed files with 89 additions and 98 deletions

View File

@ -56,7 +56,7 @@ namespace Wpf_AiSportsMicrospace.Common
{ {
try try
{ {
_webcamClient = WebcamClient.CreateRTSP("192.168.3.64", "admin", "yd708090", 554u); _webcamClient = WebcamClient.CreateRTSP("172.17.30.64", "admin", "yd708090", 554u);
} }
catch (Exception) catch (Exception)
{ {

View File

@ -44,14 +44,16 @@ namespace Wpf_AiSportsMicrospace.Views
private List<SportBase> sports = new(); private List<SportBase> sports = new();
private List<TextBlock> circleTexts = new(); private List<TextBlock> circleTexts = new();
private List<SportUserItem> userList = new(); private List<SportUserItem> userList = new();
private List<string> userNumberList = ["0", "0", "0", "0", "0", "0", "0"]; private List<string> userNumberList = ["0", "0", "0", "0", "0", "0"];
private double[] circlePositionsX = { 0.07, 0.21, 0.36, 0.50, 0.64, 0.78, 0.92 };
private Main _mainWin => Application.Current.MainWindow as Main; private Main _mainWin => Application.Current.MainWindow as Main;
private List<(double XNorm, double YNorm)> circlePositions = new(); private List<(double XNorm, double YNorm)> circlePositions = new();
private MediaPlayer _mediaPlayer = new MediaPlayer(); private MediaPlayer _mediaPlayer = new MediaPlayer();
private bool IsGameStarted = false; private bool IsGameStarted = false;
List<string> _useName = ["四号位", "一号位", "五号位", "二号位", "六号位", "三号位", "七号位",]; List<string> _useName = ["一号位", "五号位", "二号位", "六号位", "三号位", "七号位",];
private readonly object _updateLock = new object();
private readonly Dictionary<int, (string lastNumber, DateTime lastChangeTime, string currentState)> _jumpStatus = new Dictionary<int, (string, DateTime, string)>();
public GroupJumpRope() public GroupJumpRope()
{ {
InitializeComponent(); InitializeComponent();
@ -345,23 +347,21 @@ namespace Wpf_AiSportsMicrospace.Views
_firstHandTriggered = false; _firstHandTriggered = false;
_lastCountdownSecond = 3; _lastCountdownSecond = 3;
} }
private Dictionary<int, DateTime> _lastJumpUpdateTime = new(); // 用于存储最后更新时间
private Dictionary<int, DateTime> _lastUIUpdateTime = new(); // 用于防抖处理 private void UpdateCircleCounts(List<Human> humans)
public Human LocateHuman(List<Human> humans, double frameWidth, double frameHeight, int circleIndex)
{ {
if (humans == null || humans.Count == 0)
double radiusNormX = 0.07;
double radiusNormY = 0.14;
for (int i = 0; i < circlePositions.Count; i++)
{ {
return null; var circleX = circlePositions[i].XNorm;
} var circleY = circlePositions[i].YNorm;
double circleX = circlePositions[circleIndex].XNorm; Human humanInCircle = null;
double circleY = circlePositions[circleIndex].YNorm;
double radiusNormX = 0.073;
double radiusNormY = 0.135;
Human inCircleHuman = null;
bool isOut = false;
// 找圈内的人
foreach (var hu in humans) foreach (var hu in humans)
{ {
var rightFoot = hu.Keypoints.FirstOrDefault(k => k.Name == "right_ankle"); var rightFoot = hu.Keypoints.FirstOrDefault(k => k.Name == "right_ankle");
@ -369,10 +369,10 @@ namespace Wpf_AiSportsMicrospace.Views
if (rightFoot == null || leftFoot == null) if (rightFoot == null || leftFoot == null)
continue; continue;
double xRightNorm = rightFoot.X / frameWidth; double xRightNorm = rightFoot.X / userBox.ActualWidth;
double xLeftNorm = leftFoot.X / frameWidth; double xLeftNorm = leftFoot.X / userBox.ActualWidth;
double yRightNorm = rightFoot.Y / frameHeight; double yRightNorm = rightFoot.Y / userBox.ActualHeight;
double yLeftNorm = leftFoot.Y / frameHeight; double yLeftNorm = leftFoot.Y / userBox.ActualHeight;
bool outOfCircle = bool outOfCircle =
xRightNorm < (circleX - radiusNormX) || xRightNorm > (circleX + radiusNormX) || xRightNorm < (circleX - radiusNormX) || xRightNorm > (circleX + radiusNormX) ||
@ -380,52 +380,63 @@ namespace Wpf_AiSportsMicrospace.Views
yRightNorm < (circleY - radiusNormY) || yRightNorm > (circleY + radiusNormY) || yRightNorm < (circleY - radiusNormY) || yRightNorm > (circleY + radiusNormY) ||
yLeftNorm < (circleY - radiusNormY) || yLeftNorm > (circleY + radiusNormY); yLeftNorm < (circleY - radiusNormY) || yLeftNorm > (circleY + radiusNormY);
if (outOfCircle) if (!outOfCircle)
{ {
isOut = true; humanInCircle = hu;
break; // 每圈只处理一个人
} }
else if (inCircleHuman == null)
{
inCircleHuman = hu;
} }
// 根据是否有人和跳绳数字判断状态
bool hasHuman = humanInCircle != null;
lock (_updateLock)
{
userList[i].ImageState = GetJumpState(i, hasHuman);
} }
if (isOut) // 推送计数
userList[circleIndex].ImageState = "3"; if (hasHuman)
else sports[i].Pushing(humanInCircle);
}
}
private string GetJumpState(int circleIndex, bool humanInCircle)
{ {
//if (userList[circleIndex].ImageState == "3") if (!humanInCircle)
{
// 无人 → 出圈
return "3";
}
string currentNumber = userNumberList[circleIndex];
if (!_jumpStatus.ContainsKey(circleIndex))
{
_jumpStatus[circleIndex] = (currentNumber, DateTime.Now, "1"); // 初始状态先为停止
return "1";
}
var (lastNumber, lastChangeTime, lastState) = _jumpStatus[circleIndex];
//if (currentNumber != lastNumber)
//{ //{
// userList[circleIndex].ImageState = "1"; // // 数字变化 → 跳绳中
// _jumpStatus[circleIndex] = (currentNumber, DateTime.Now, "2");
// return "2";
//} //}
// 检查当前编号是否未变化超过 2 秒 // 数字未变化,判断是否超过 2 秒
if (userNumberList[circleIndex] == userList[circleIndex].NumberText) double elapsed = (DateTime.Now - lastChangeTime).TotalSeconds;
if (elapsed >= 2)
{ {
// 获取上次变化时间 // 超过 2 秒未变化 → 停止
if (!_lastJumpUpdateTime.TryGetValue(circleIndex, out var lastTime)) _jumpStatus[circleIndex] = (currentNumber, lastChangeTime, "1");
lastTime = DateTime.Now; // 默认当前时间 return "1";
var elapsed = (DateTime.Now - lastTime).TotalSeconds;
if (elapsed > 1)
{
userList[circleIndex].ImageState = "1";
_lastJumpUpdateTime[circleIndex] = DateTime.Now;
}
// 否则2 秒内仍在变化,不更新状态
}
else
{
// 编号发生变化,更新记录,并保持状态不变
userNumberList[circleIndex] = userList[circleIndex].NumberText;
_lastJumpUpdateTime[circleIndex] = DateTime.Now;
}
}
return inCircleHuman;
} }
// 维持上次状态
return lastState;
}
private void DrawCirclesWithText() private void DrawCirclesWithText()
{ {
@ -441,13 +452,13 @@ namespace Wpf_AiSportsMicrospace.Views
// 每个圆的位置X 和 Y 都归一化 0~1 // 每个圆的位置X 和 Y 都归一化 0~1
circlePositions = new List<(double XNorm, double YNorm)> circlePositions = new List<(double XNorm, double YNorm)>
{ {
(0.07, 0.58), //(0.07, 0.58),
(0.21, 0.88 ), (0.21, 0.88 ),
(0.36, 0.58 ), (0.36, 0.68 ),
(0.50, 0.88), (0.50, 0.88),
(0.64, 0.58 ), (0.64, 0.68 ),
(0.78, 0.88), (0.78, 0.88),
(0.92, 0.58 ) (0.92, 0.68 )
}; };
for (int i = 0; i < circlePositions.Count; i++) for (int i = 0; i < circlePositions.Count; i++)
@ -474,30 +485,10 @@ namespace Wpf_AiSportsMicrospace.Views
currentItem.ImageState = "2"; currentItem.ImageState = "2";
} }
}; };
sport.PointThreshold = 0.03f;
sport.Start(); sport.Start();
sports.Add(sport); sports.Add(sport);
} }
} }
private void UpdateCircleCounts(List<Human> humans)
{
// 在后台线程运行检测逻辑
//Parallel.For(0, circlePositions.Count, i =>
//{
// var human = LocateHuman(humans, userBox.ActualWidth, userBox.ActualHeight, i);
// sports[i].Pushing(human);
//});
for (int i = 0; i < circlePositions.Count; i++)
{
var human = LocateHuman(humans, userBox.ActualWidth, userBox.ActualHeight, i);
if (human != null)
{
sports[i].Pushing(human);
}
}
}
/// <summary> /// <summary>
/// 添加带渐变光的圆圈(中心红色,边缘蓝色) /// 添加带渐变光的圆圈(中心红色,边缘蓝色)
@ -511,7 +502,7 @@ namespace Wpf_AiSportsMicrospace.Views
userItem.VerticalAlignment = VerticalAlignment.Top; userItem.VerticalAlignment = VerticalAlignment.Top;
userItem.HorizontalAlignment = HorizontalAlignment.Left; userItem.HorizontalAlignment = HorizontalAlignment.Left;
userItem.ImageState = "1"; userItem.ImageState = "1";
userItem.Margin = new Thickness(centerX - (index == 0 ? 80 : index == 6 ? 190 : 135), centerY - 540, 0, 0); userItem.Margin = new Thickness(centerX - 265, centerY - 500, 0, 0);
userBox.Children.Add(userItem); userBox.Children.Add(userItem);
userList.Add(userItem); userList.Add(userItem);
return userItem; // return userItem; //