From 79b9e911495828d2adfe880744885abc3f84047d Mon Sep 17 00:00:00 2001 From: tanglong <842690096@qq.com> Date: Tue, 20 Jan 2026 16:28:55 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8A=A8=E6=80=81=E9=85=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../DBContext/AppDbContext.cs | 2 +- .../{SixGroupPoints.cs => GroupPoints.cs} | 9 ++-- Wpf_AiSportsMicrospace/Common/AppSettings.cs | 36 +++++++++++++--- Wpf_AiSportsMicrospace/Common/SportOperate.cs | 14 +++++-- Wpf_AiSportsMicrospace/Views/Home.xaml.cs | 22 ---------- .../Views/JumpRope/GroupJumpRope.xaml.cs | 26 ++++++++---- .../Views/JumpRope/MusicJumpRope.xaml.cs | 42 ++++--------------- .../Views/JumpingJack/BambooDragonfly.xaml.cs | 2 +- Wpf_AiSportsMicrospace/Views/Main.xaml.cs | 10 ----- .../Views/SquatFu/WhackAMole.xaml.cs | 2 +- 10 files changed, 74 insertions(+), 91 deletions(-) rename AiSportsMicrospaceDB/Entities/{SixGroupPoints.cs => GroupPoints.cs} (86%) diff --git a/AiSportsMicrospaceDB/DBContext/AppDbContext.cs b/AiSportsMicrospaceDB/DBContext/AppDbContext.cs index 97b1724..3aa0d15 100644 --- a/AiSportsMicrospaceDB/DBContext/AppDbContext.cs +++ b/AiSportsMicrospaceDB/DBContext/AppDbContext.cs @@ -18,7 +18,7 @@ namespace AiSportsMicrospaceDB.DBContext public DbSet BasicConfig { get; set; } - public DbSet SixGroupPoints { get; set; } + public DbSet GroupPoints { get; set; } public DbSet JumpLongPoints { get; set; } public DbSet AppVersion { get; set; } public DbSet DeviceAssocSchool { get; set; } diff --git a/AiSportsMicrospaceDB/Entities/SixGroupPoints.cs b/AiSportsMicrospaceDB/Entities/GroupPoints.cs similarity index 86% rename from AiSportsMicrospaceDB/Entities/SixGroupPoints.cs rename to AiSportsMicrospaceDB/Entities/GroupPoints.cs index 710df43..4d888d3 100644 --- a/AiSportsMicrospaceDB/Entities/SixGroupPoints.cs +++ b/AiSportsMicrospaceDB/Entities/GroupPoints.cs @@ -4,15 +4,16 @@ using System.Collections; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema; +using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; namespace AiSportsMicrospaceDB.Entities { - - [Table("SixGroupPoints")] - public class SixGroupPoints { + [Table("GroupPoints")] + public class GroupPoints + { [Key] [DatabaseGenerated(DatabaseGeneratedOption.Identity)] [Display(Description = "主键Id")] @@ -37,5 +38,7 @@ namespace AiSportsMicrospaceDB.Entities public Point? Point8 { get; set; } public Point? Point9 { get; set; } public Point? Point10 { get; set; } + public Point? ControlPoint { get; set; } + public Point? AreaSize { get; set; } } } diff --git a/Wpf_AiSportsMicrospace/Common/AppSettings.cs b/Wpf_AiSportsMicrospace/Common/AppSettings.cs index a24ef05..e12d4f6 100644 --- a/Wpf_AiSportsMicrospace/Common/AppSettings.cs +++ b/Wpf_AiSportsMicrospace/Common/AppSettings.cs @@ -1,12 +1,11 @@ -using Microsoft.Extensions.Configuration; -using System; -using System.Collections.Generic; +using AiSportsMicrospaceDB.DBContext; +using AiSportsMicrospaceDB.Entities; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; using System.IO; -using System.Net.Sockets; -using System.Text; using System.Text.Json; +using Wpf_AiSportsMicrospace; using Wpf_AiSportsMicrospace.Common; - namespace Common { public static class AppSettings @@ -16,15 +15,40 @@ namespace Common public static string BackendUrl; public static string Version; + public static int Resolution_X = 1920; + public static int Resolution_Y = 1080; + + public static Point ControlPoint { get; set; } + public static Point AreaSize { get; set; } public static void Init(IConfiguration configuration) { Code = Utils.GetMacAddress() ?? configuration["SchoolCode"] ?? ""; + + if (string.IsNullOrWhiteSpace(Code)) + { + HandyControl.Controls.MessageBox.Show("获取设备编码失败!"); + return; + } + + using var scope = App.AppHost.Services.CreateScope(); + var db = scope.ServiceProvider.GetRequiredService(); + var groupData = db.GroupPoints.Where(x => x.Code == Code).Select(x => new { x.ControlPoint, x.AreaSize }).FirstOrDefault(); + + if (groupData != null && groupData.ControlPoint != null && groupData.AreaSize != null) + { + ControlPoint = groupData.ControlPoint; + AreaSize = groupData.AreaSize; + } + else + HandyControl.Controls.MessageBox.Show("请先设置点位并重启应用!"); + SchoolCode = configuration["SchoolCode"] ?? ""; BackendUrl = configuration["BackendUrl"] ?? ""; Version = configuration["Version"] ?? ""; } + public static void UpdateCodeAndSchool(string filePath, string code, string schoolCode) { if (!File.Exists(filePath)) diff --git a/Wpf_AiSportsMicrospace/Common/SportOperate.cs b/Wpf_AiSportsMicrospace/Common/SportOperate.cs index c5ae17d..01354f5 100644 --- a/Wpf_AiSportsMicrospace/Common/SportOperate.cs +++ b/Wpf_AiSportsMicrospace/Common/SportOperate.cs @@ -82,10 +82,18 @@ namespace Wpf_AiSportsMicrospace.Common if (rightAnkle == null) return false; - double xNorm = rightAnkle.X / 1920.0; - double yNorm = rightAnkle.Y / 1080.0; + double xNorm = rightAnkle.X / AppSettings.Resolution_X; + double yNorm = rightAnkle.Y / AppSettings.Resolution_Y; - return xNorm >= 0.44 && xNorm <= 0.57 && yNorm >= 0.81; + // 控制点 + double cx = AppSettings.ControlPoint.X; // 已经是归一化 0~1 + double cy = AppSettings.ControlPoint.Y; + + double halfWidth = AppSettings.AreaSize.X; + double halfHeight = AppSettings.AreaSize.Y; + + return xNorm >= (cx - halfWidth) && xNorm <= (cx + halfWidth) && + yNorm >= (cy - halfHeight) && yNorm <= (cy + halfHeight); }); if (human != null) diff --git a/Wpf_AiSportsMicrospace/Views/Home.xaml.cs b/Wpf_AiSportsMicrospace/Views/Home.xaml.cs index 1e0b2b2..fde0378 100644 --- a/Wpf_AiSportsMicrospace/Views/Home.xaml.cs +++ b/Wpf_AiSportsMicrospace/Views/Home.xaml.cs @@ -3,42 +3,20 @@ using AiSportsMicrospaceDB.Entities; using Common; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.DependencyInjection; -using Microsoft.ML.Runtime; -using System; -using System.Collections.Concurrent; -using System.Collections.Generic; -using System.Collections.ObjectModel; -using System.ComponentModel; using System.Diagnostics; using System.IO; -using System.Linq; using System.Net.NetworkInformation; -using System.Text; -using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Input; -using System.Windows.Media; -using System.Windows.Media.Animation; using System.Windows.Threading; -using Views.JumpingJack; -using Views.JumpLong; using Views.JumpRope; -using Views.SquatFu; -using Wpf_AiSportsMicrospace; using Wpf_AiSportsMicrospace.Common; using Wpf_AiSportsMicrospace.Enum; using Wpf_AiSportsMicrospace.MyUserControl; using Wpf_AiSportsMicrospace.Views; using Wpf_AiSportsMicrospace.Views.JumpRope; -using Yztob.AiSports.Inferences.Abstractions; using Yztob.AiSports.Inferences.Things; -using Yztob.AiSports.Postures; -using Yztob.AiSports.Postures.Abstractions; -using Yztob.AiSports.Postures.Sports; -using Yztob.AiSports.Postures.Things; -using Yztob.AiSports.Sensors.Abstractions; -using Yztob.AiSports.Sensors.Things; namespace Wpf_AiSportsMicrospace { /// diff --git a/Wpf_AiSportsMicrospace/Views/JumpRope/GroupJumpRope.xaml.cs b/Wpf_AiSportsMicrospace/Views/JumpRope/GroupJumpRope.xaml.cs index edacff9..aef1b45 100644 --- a/Wpf_AiSportsMicrospace/Views/JumpRope/GroupJumpRope.xaml.cs +++ b/Wpf_AiSportsMicrospace/Views/JumpRope/GroupJumpRope.xaml.cs @@ -82,7 +82,7 @@ namespace Wpf_AiSportsMicrospace.Views var app = (App)Application.Current; using var scope = App.AppHost.Services.CreateScope(); var db = scope.ServiceProvider.GetRequiredService(); - var cfg = await db.SixGroupPoints.FirstOrDefaultAsync(x => x.Code == AppSettings.Code); + var cfg = await db.GroupPoints.FirstOrDefaultAsync(x => x.Code == AppSettings.Code); //var pointlist = JsonSerializer.Deserialize>(cfg!.PointsJson); List<(double XNorm, double YNorm)> pointlist = @@ -136,8 +136,8 @@ namespace Wpf_AiSportsMicrospace.Views }; // 增加图片的大小,调整比例 - tipImage.Width = 1920 * 0.9; // 宽度为 Canvas 宽度的 90% - tipImage.Height = 1080 * 0.6; // 高度为 Canvas 高度的 60% + tipImage.Width = AppSettings.Resolution_X * 0.9; + tipImage.Height = AppSettings.Resolution_Y * 0.6; // 将图片添加到 Overlay Canvas userBox.Children.Add(tipImage); @@ -196,10 +196,18 @@ namespace Wpf_AiSportsMicrospace.Views if (rightAnkle == null) return false; - double xNorm = rightAnkle.X / 1920.0; - double yNorm = rightAnkle.Y / 1080.0; + double xNorm = rightAnkle.X / AppSettings.Resolution_X; + double yNorm = rightAnkle.Y / AppSettings.Resolution_Y; - return xNorm >= 0.44 && xNorm <= 0.57 && yNorm >= 0.81; + // 控制点 + double cx = AppSettings.ControlPoint.X; // 已经是归一化 0~1 + double cy = AppSettings.ControlPoint.Y; + + double halfWidth = AppSettings.AreaSize.X; + double halfHeight = AppSettings.AreaSize.Y; + + return xNorm >= (cx - halfWidth) && xNorm <= (cx + halfWidth) && + yNorm >= (cy - halfHeight) && yNorm <= (cy + halfHeight); }); if (human != null) @@ -420,7 +428,7 @@ namespace Wpf_AiSportsMicrospace.Views return (int)WavingAction.Raising; } - + public int DetectLeftHandRaise(Human human) { var calculator = PostureCalculate.CreateCalculator(); @@ -486,8 +494,8 @@ namespace Wpf_AiSportsMicrospace.Views } private void UpdateCircleCounts(List humans) { - double radiusNormX = 0.07; - double radiusNormY = 0.14; + double radiusNormX = AppSettings.AreaSize.X; + double radiusNormY = AppSettings.AreaSize.Y; for (int i = 0; i < _groupJumpRopeContext.CirclePositions.Count; i++) { diff --git a/Wpf_AiSportsMicrospace/Views/JumpRope/MusicJumpRope.xaml.cs b/Wpf_AiSportsMicrospace/Views/JumpRope/MusicJumpRope.xaml.cs index abf25e2..8e97426 100644 --- a/Wpf_AiSportsMicrospace/Views/JumpRope/MusicJumpRope.xaml.cs +++ b/Wpf_AiSportsMicrospace/Views/JumpRope/MusicJumpRope.xaml.cs @@ -89,7 +89,7 @@ namespace Wpf_AiSportsMicrospace.Views.JumpRope var app = (App)Application.Current; using var scope = App.AppHost.Services.CreateScope(); var db = scope.ServiceProvider.GetRequiredService(); - var cfg = await db.SixGroupPoints.FirstOrDefaultAsync(x => x.Code == AppSettings.Code); + var cfg = await db.GroupPoints.FirstOrDefaultAsync(x => x.Code == AppSettings.Code); //var pointlist = JsonSerializer.Deserialize>(cfg!.PointsJson); List<(double XNorm, double YNorm)> pointlist = [(cfg.Point3.X, cfg.Point3.Y), (cfg.Point5.X, cfg.Point5.Y)]; @@ -134,8 +134,8 @@ namespace Wpf_AiSportsMicrospace.Views.JumpRope }; // 增加图片的大小,调整比例 - tipImage.Width = 1920 * 0.9; // 宽度为 Canvas 宽度的 90% - tipImage.Height = 1080 * 0.6; // 高度为 Canvas 高度的 60% + tipImage.Width = AppSettings.Resolution_X * 0.9; // 宽度为 Canvas 宽度的 90% + tipImage.Height = AppSettings.Resolution_Y * 0.6; // 高度为 Canvas 高度的 60% // 将图片添加到 Overlay Canvas userBox.Children.Add(tipImage); @@ -192,8 +192,8 @@ namespace Wpf_AiSportsMicrospace.Views.JumpRope if (rightAnkle == null) return false; - double xNorm = rightAnkle.X / 1920.0; - double yNorm = rightAnkle.Y / 1080.0; + double xNorm = rightAnkle.X / AppSettings.Resolution_X; + double yNorm = rightAnkle.Y / AppSettings.Resolution_Y; return xNorm >= 0.44 && xNorm <= 0.57 && yNorm >= 0.81; }); @@ -599,34 +599,6 @@ namespace Wpf_AiSportsMicrospace.Views.JumpRope } } - private void UpdateScore(double to, TextBlock score) - { - DoubleAnimation animation = new DoubleAnimation - { - From = double.Parse(score1.Text), - To = to, - Duration = TimeSpan.FromSeconds(0.2), - EasingFunction = new QuadraticEase { EasingMode = EasingMode.EaseOut }, - FillBehavior = FillBehavior.HoldEnd - }; - score.BeginAnimation(TextBlock.TextProperty, animation); - } - - //private void ChangeImgState(int index , string state) - //{ - // if(index== 0) - // { - // left1.Visibility = state == "1" ? Visibility.Visible : Visibility.Hidden; - // left2.Visibility = state == "2" ? Visibility.Visible : Visibility.Hidden; - // left3.Visibility = state == "3" ? Visibility.Visible : Visibility.Hidden; - // }else if (index == 1) - // { - // right1.Visibility = state == "1" ? Visibility.Visible : Visibility.Hidden; - // right2.Visibility = state == "1" ? Visibility.Visible : Visibility.Hidden; - // right3.Visibility = state == "1" ? Visibility.Visible : Visibility.Hidden; - // } - //} - public void ShowMusicAnimation() { ImageBehavior.SetRepeatBehavior(leftm, RepeatBehavior.Forever); @@ -637,8 +609,8 @@ namespace Wpf_AiSportsMicrospace.Views.JumpRope private void UpdateCircleCounts(List humans) { - double radiusNormX = 0.07; - double radiusNormY = 0.14; + double radiusNormX = AppSettings.Resolution_X; + double radiusNormY = AppSettings.Resolution_Y; for (int i = 0; i < _musicJumpRopeContext.CirclePositions.Count; i++) { diff --git a/Wpf_AiSportsMicrospace/Views/JumpingJack/BambooDragonfly.xaml.cs b/Wpf_AiSportsMicrospace/Views/JumpingJack/BambooDragonfly.xaml.cs index 916a2c9..3a518f0 100644 --- a/Wpf_AiSportsMicrospace/Views/JumpingJack/BambooDragonfly.xaml.cs +++ b/Wpf_AiSportsMicrospace/Views/JumpingJack/BambooDragonfly.xaml.cs @@ -67,7 +67,7 @@ namespace Views.JumpingJack var app = (App)Application.Current; using var scope = App.AppHost.Services.CreateScope(); var db = scope.ServiceProvider.GetRequiredService(); - var cfg = await db.SixGroupPoints.FirstOrDefaultAsync(x => x.Code == AppSettings.Code); + var cfg = await db.GroupPoints.FirstOrDefaultAsync(x => x.Code == AppSettings.Code); //var pointlist = JsonSerializer.Deserialize>(cfg!.PointsJson); List<(double XNorm, double YNorm)> pointlist = diff --git a/Wpf_AiSportsMicrospace/Views/Main.xaml.cs b/Wpf_AiSportsMicrospace/Views/Main.xaml.cs index fd0d58e..74f57ab 100644 --- a/Wpf_AiSportsMicrospace/Views/Main.xaml.cs +++ b/Wpf_AiSportsMicrospace/Views/Main.xaml.cs @@ -101,16 +101,6 @@ namespace Wpf_AiSportsMicrospace.Views { try { - //if (frame.Number % 2 != 0) - // return; - - // 触发全局事件 - //Application.Current.Dispatcher.BeginInvoke(() => - //{ - // FacialRecognitionEvent?.Invoke(frame); - //}); - - var buffer = frame.GetImageBuffer(ImageFormat.Jpeg).ToArray(); var humanResult = _humanPredictor.Predicting(buffer, frame.Number); diff --git a/Wpf_AiSportsMicrospace/Views/SquatFu/WhackAMole.xaml.cs b/Wpf_AiSportsMicrospace/Views/SquatFu/WhackAMole.xaml.cs index 9e589c1..04dec12 100644 --- a/Wpf_AiSportsMicrospace/Views/SquatFu/WhackAMole.xaml.cs +++ b/Wpf_AiSportsMicrospace/Views/SquatFu/WhackAMole.xaml.cs @@ -60,7 +60,7 @@ namespace Views.SquatFu var app = (App)Application.Current; using var scope = App.AppHost.Services.CreateScope(); var db = scope.ServiceProvider.GetRequiredService(); - var cfg = await db.SixGroupPoints.FirstOrDefaultAsync(x => x.Code == AppSettings.Code); + var cfg = await db.GroupPoints.FirstOrDefaultAsync(x => x.Code == AppSettings.Code); //var pointlist = JsonSerializer.Deserialize>(cfg!.PointsJson); List<(double XNorm, double YNorm)> pointlist =