feat: 添加PoseTransformHandler接口 提供结欢过滤方法的功能
添加直接返回和平均过滤两种过滤方法
This commit is contained in:
		| @@ -1,6 +1,7 @@ | ||||
| using System.Collections.Generic; | ||||
| using Models; | ||||
| using UnityEngine; | ||||
| using Utils.PoseTransformHandlers; | ||||
| 
 | ||||
| public class MainBehaviour : MonoBehaviour | ||||
| { | ||||
| @@ -27,7 +28,7 @@ public class MainBehaviour : MonoBehaviour | ||||
|     { | ||||
|         for (var i = 0; i < PoseLandmarkType.MaxValue; i++) | ||||
|         { | ||||
|             _nodes[i].transform.position = _poseTransforms[i].AveragePos * -5; | ||||
|             _nodes[i].transform.position = _poseTransforms[i].ResultPosition * -5; | ||||
|         } | ||||
| 
 | ||||
|         foreach (var bond in _bonds) | ||||
| @@ -51,7 +52,7 @@ public class MainBehaviour : MonoBehaviour | ||||
| 
 | ||||
|     private void CreateNodes() | ||||
|     { | ||||
|         for (var i = 0; i < (int)PoseLandmarkType.MaxValue; i++) | ||||
|         for (var i = 0; i < PoseLandmarkType.MaxValue; i++) | ||||
|         { | ||||
|             var ball = GameObject.CreatePrimitive(PrimitiveType.Sphere); | ||||
| 
 | ||||
| @@ -71,7 +72,7 @@ public class MainBehaviour : MonoBehaviour | ||||
|             } | ||||
| 
 | ||||
|             _nodes[i] = ball; | ||||
|             _poseTransforms[i] = new PoseTransform(i); | ||||
|             _poseTransforms[i] = new PoseTransform(i, new AverageHandler()); | ||||
|         } | ||||
|     } | ||||
| 
 | ||||
|   | ||||
| @@ -1,6 +1,7 @@ | ||||
| using System.Collections.Generic; | ||||
| using Models; | ||||
| using UnityEngine; | ||||
| using Utils.PoseTransformHandlers; | ||||
| 
 | ||||
| public class ModelBehaviour : MonoBehaviour | ||||
| { | ||||
| @@ -34,12 +35,12 @@ public class ModelBehaviour : MonoBehaviour | ||||
| 
 | ||||
|                 foreach (var parent in landmark.Parent) | ||||
|                 { | ||||
|                     parentsum += PoseTransforms[parent.Value].AveragePos; | ||||
|                     parentsum += PoseTransforms[parent.Value].ResultPosition; | ||||
|                 } | ||||
| 
 | ||||
|                 parentsum /= landmark.Parent.Count; | ||||
|                  | ||||
|                 transfrom.rotation = Quaternion.FromToRotation(transfrom.rotation.eulerAngles,landmark.AveragePos-parentsum); | ||||
|                 transfrom.rotation = Quaternion.FromToRotation(transfrom.rotation.eulerAngles,landmark.ResultPosition-parentsum); | ||||
| 
 | ||||
|             } | ||||
|         } | ||||
| @@ -71,7 +72,7 @@ public class ModelBehaviour : MonoBehaviour | ||||
|              | ||||
|             PoseTransform.UpdatePosition(ref PoseTransforms[landmark.Type.Value], landmark); | ||||
|              | ||||
|             Debug.Log(PoseTransforms[landmark.Type.Value].UnityName+":"+PoseTransforms[landmark.Type.Value].AveragePos); | ||||
|             Debug.Log(PoseTransforms[landmark.Type.Value].UnityName+":"+PoseTransforms[landmark.Type.Value].ResultPosition); | ||||
|         } | ||||
| 
 | ||||
|     } | ||||
| @@ -83,7 +84,7 @@ public class ModelBehaviour : MonoBehaviour | ||||
|     { | ||||
|         for (var type = 0; type <= PoseLandmarkType.RightFootIndex; type++) | ||||
|         { | ||||
|             var item = new PoseTransform(type); | ||||
|             var item = new PoseTransform(type, new NormalHandler()); | ||||
| 
 | ||||
|             if (item.UnityName != HumanBodyBones.LastBone) | ||||
|             { | ||||
|   | ||||
| @@ -3,7 +3,7 @@ using System; | ||||
| namespace Models | ||||
| { | ||||
|    //名字  | ||||
|    public struct PoseLandmarkType | ||||
|    public readonly struct PoseLandmarkType | ||||
|    { | ||||
|       public const int Nose = 0; | ||||
|       public const int LeftEyeInner = 1; | ||||
|   | ||||
| @@ -1,7 +1,6 @@ | ||||
| using System.Collections.Generic; | ||||
| using UnityEngine; | ||||
| using System; | ||||
| using System.Collections; | ||||
| using Utils; | ||||
| 
 | ||||
| namespace Models | ||||
| { | ||||
| @@ -23,12 +22,12 @@ namespace Models | ||||
|         /// <summary> | ||||
|         /// 取平均的长度 | ||||
|         /// </summary> | ||||
|         public static int AverageLength; | ||||
|         public static int AverageLength = 3; | ||||
| 
 | ||||
|         /// <summary> | ||||
|         /// 取平均之后的结果 | ||||
|         /// </summary> | ||||
|         public Vector3 AveragePos; | ||||
|         public Vector3 ResultPosition => _transformHandler.GetResultPosition(); | ||||
| 
 | ||||
|         /// <summary> | ||||
|         /// 节点的父节点列表 | ||||
| @@ -45,55 +44,25 @@ namespace Models | ||||
|         /// </summary> | ||||
|         public Quaternion CurrentQuaternion; | ||||
|          | ||||
|         private int _count; | ||||
|         private readonly Queue<PoseLandmark> _landmarkQueue; | ||||
|         private readonly IPoseTransformHandler _transformHandler; | ||||
| 
 | ||||
|         public PoseTransform( | ||||
|             int type, | ||||
|             int averageLength = 5 | ||||
|             IPoseTransformHandler handler | ||||
|         ) | ||||
|         { | ||||
|             MediaPipeName = new PoseLandmarkType(type); | ||||
|             UnityName = GetRelatedBone(MediaPipeName); | ||||
|             AveragePos = new Vector3(); | ||||
|             Parent = new List<PoseLandmarkType>(); | ||||
|             PreviousQuaternion = new Quaternion(); | ||||
|             CurrentQuaternion = new Quaternion(); | ||||
|             AverageLength = averageLength; | ||||
|              | ||||
|             _count = 0; | ||||
|             _landmarkQueue = new Queue<PoseLandmark>(); | ||||
|             _transformHandler = handler; | ||||
|         } | ||||
| 
 | ||||
|         public static void UpdatePosition(ref PoseTransform pose, PoseLandmark landmark) | ||||
|         { | ||||
|             if (pose._count < AverageLength) | ||||
|             { | ||||
|                 pose._landmarkQueue.Enqueue(landmark); | ||||
|                 pose._count++; | ||||
|             } | ||||
|             else | ||||
|             { | ||||
|                 pose._landmarkQueue.Dequeue(); | ||||
|                 pose._landmarkQueue.Enqueue(landmark); | ||||
| 
 | ||||
|                 var sum = new Vector3(); | ||||
| 
 | ||||
|                 var parentsum = new Vector3(0f,0f,0f); | ||||
| 
 | ||||
|                 foreach (var poseLandmark in pose._landmarkQueue) | ||||
|                 { | ||||
|                     sum += new Vector3(poseLandmark.X, poseLandmark.Y, poseLandmark.Z); | ||||
|                 } | ||||
| 
 | ||||
|                 pose.AveragePos = sum / AverageLength; | ||||
|                  | ||||
|                  | ||||
| 
 | ||||
|                  | ||||
|                 //pose.CurrentQuaternion = Quaternion.LookRotation(pose.AveragePos - parentsum).normalized; | ||||
|                 | ||||
|             } | ||||
|             pose._transformHandler.ReceivePoseLandmark(landmark); | ||||
|         } | ||||
| 
 | ||||
|         public static void CalculateRotation(ref PoseTransform poseTransform, PoseLandmark landmark) | ||||
|   | ||||
							
								
								
									
										23
									
								
								Assets/Utils/IPoseTransformHandler.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										23
									
								
								Assets/Utils/IPoseTransformHandler.cs
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,23 @@ | ||||
| using Models; | ||||
| using UnityEngine; | ||||
| 
 | ||||
| namespace Utils | ||||
| { | ||||
|     /// <summary> | ||||
|     /// 坐标处理接口 | ||||
|     /// </summary> | ||||
|     public interface IPoseTransformHandler | ||||
|     { | ||||
|         /// <summary> | ||||
|         /// 传入新收到的坐标点 | ||||
|         /// </summary> | ||||
|         /// <param name="landmark"></param> | ||||
|         public void ReceivePoseLandmark(PoseLandmark landmark); | ||||
| 
 | ||||
|         /// <summary> | ||||
|         /// 获得处理之后的坐标点 | ||||
|         /// </summary> | ||||
|         /// <returns></returns> | ||||
|         public Vector3 GetResultPosition(); | ||||
|     } | ||||
| } | ||||
							
								
								
									
										3
									
								
								Assets/Utils/IPoseTransformHandler.cs.meta
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										3
									
								
								Assets/Utils/IPoseTransformHandler.cs.meta
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,3 @@ | ||||
| fileFormatVersion: 2 | ||||
| guid: 09e98a9502f0470f827a472058068bc4 | ||||
| timeCreated: 1677676325 | ||||
							
								
								
									
										3
									
								
								Assets/Utils/PoseTransformHandlers.meta
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										3
									
								
								Assets/Utils/PoseTransformHandlers.meta
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,3 @@ | ||||
| fileFormatVersion: 2 | ||||
| guid: 790fe29ab090465183945d02718de6a9 | ||||
| timeCreated: 1677676888 | ||||
							
								
								
									
										41
									
								
								Assets/Utils/PoseTransformHandlers/AverageHandler.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										41
									
								
								Assets/Utils/PoseTransformHandlers/AverageHandler.cs
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,41 @@ | ||||
| using Models; | ||||
| using UnityEngine; | ||||
| using System.Collections.Concurrent; | ||||
| 
 | ||||
| namespace Utils.PoseTransformHandlers | ||||
| { | ||||
|     public class AverageHandler : IPoseTransformHandler | ||||
|     { | ||||
|         public static int AverageLength = 3; | ||||
|         // 线程安全的队列集合 | ||||
|         // 据文档描述甚至没有使用锁 | ||||
|         private readonly ConcurrentQueue<PoseLandmark> _queue = new ConcurrentQueue<PoseLandmark>(); | ||||
|          | ||||
|         public void ReceivePoseLandmark(PoseLandmark landmark) | ||||
|         { | ||||
|             if (_queue.Count < AverageLength) | ||||
|             { | ||||
|                 _queue.Enqueue(landmark); | ||||
|             } | ||||
|             else | ||||
|             { | ||||
|                 if (_queue.TryDequeue(out _)) | ||||
|                 { | ||||
|                     _queue.Enqueue(landmark); | ||||
|                 } | ||||
|             } | ||||
|         } | ||||
| 
 | ||||
|         public Vector3 GetResultPosition() | ||||
|         { | ||||
|             var sum = new Vector3(); | ||||
| 
 | ||||
|             foreach (var item in _queue) | ||||
|             { | ||||
|                 sum += new Vector3(item.X, item.Y, item.Z); | ||||
|             } | ||||
| 
 | ||||
|             return sum / AverageLength; | ||||
|         } | ||||
|     } | ||||
| } | ||||
| @@ -0,0 +1,3 @@ | ||||
| fileFormatVersion: 2 | ||||
| guid: c02b0d03301d408faf81931a69679f5c | ||||
| timeCreated: 1677676898 | ||||
							
								
								
									
										20
									
								
								Assets/Utils/PoseTransformHandlers/NormalHandler.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										20
									
								
								Assets/Utils/PoseTransformHandlers/NormalHandler.cs
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,20 @@ | ||||
| using Models; | ||||
| using UnityEngine; | ||||
| 
 | ||||
| namespace Utils.PoseTransformHandlers | ||||
| { | ||||
|     public class NormalHandler : IPoseTransformHandler | ||||
|     { | ||||
|         private Vector3 _result; | ||||
|          | ||||
|         public void ReceivePoseLandmark(PoseLandmark landmark) | ||||
|         { | ||||
|             _result = new Vector3(landmark.X, landmark.Y, landmark.Z); | ||||
|         } | ||||
| 
 | ||||
|         public Vector3 GetResultPosition() | ||||
|         { | ||||
|             return _result; | ||||
|         } | ||||
|     } | ||||
| } | ||||
							
								
								
									
										3
									
								
								Assets/Utils/PoseTransformHandlers/NormalHandler.cs.meta
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										3
									
								
								Assets/Utils/PoseTransformHandlers/NormalHandler.cs.meta
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,3 @@ | ||||
| fileFormatVersion: 2 | ||||
| guid: 93a33f47f3654fd3b27dd18db88fcc55 | ||||
| timeCreated: 1677678654 | ||||
		Reference in New Issue
	
	Block a user