refact: 使用int重构了PoseLandmarkType
This commit is contained in:
parent
87831fd869
commit
aaba183c48
|
@ -4,9 +4,9 @@
|
||||||
|
|
||||||
public class MainBehaviour : MonoBehaviour
|
public class MainBehaviour : MonoBehaviour
|
||||||
{
|
{
|
||||||
private readonly GameObject[] _nodes = new GameObject[(int)PoseLandmarkType.MaxValue];
|
private readonly GameObject[] _nodes = new GameObject[PoseLandmarkType.MaxValue];
|
||||||
private readonly List<Bond> _bonds = new List<Bond>();
|
private readonly List<Bond> _bonds = new List<Bond>();
|
||||||
private readonly PoseTransform[] _poseTransforms = new PoseTransform[(int)PoseLandmarkType.MaxValue];
|
private readonly PoseTransform[] _poseTransforms = new PoseTransform[PoseLandmarkType.MaxValue];
|
||||||
private readonly UdpListener _listener = new UdpListener();
|
private readonly UdpListener _listener = new UdpListener();
|
||||||
private const float Scale = 0.2f;
|
private const float Scale = 0.2f;
|
||||||
|
|
||||||
|
@ -25,7 +25,7 @@ private void Start()
|
||||||
// Update is called once per frame
|
// Update is called once per frame
|
||||||
private void Update()
|
private void Update()
|
||||||
{
|
{
|
||||||
for (var i = 0; i < (int)PoseLandmarkType.MaxValue; i++)
|
for (var i = 0; i < PoseLandmarkType.MaxValue; i++)
|
||||||
{
|
{
|
||||||
_nodes[i].transform.position = _poseTransforms[i].AveragePos * -5;
|
_nodes[i].transform.position = _poseTransforms[i].AveragePos * -5;
|
||||||
}
|
}
|
||||||
|
@ -45,7 +45,7 @@ private void OnReceive(List<PoseLandmark> landmarks)
|
||||||
{
|
{
|
||||||
foreach (var landmark in landmarks)
|
foreach (var landmark in landmarks)
|
||||||
{
|
{
|
||||||
PoseTransform.UpdatePosition(ref _poseTransforms[(int)landmark.Type], landmark);
|
PoseTransform.UpdatePosition(ref _poseTransforms[landmark.Type.Value], landmark);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -71,7 +71,7 @@ private void CreateNodes()
|
||||||
}
|
}
|
||||||
|
|
||||||
_nodes[i] = ball;
|
_nodes[i] = ball;
|
||||||
_poseTransforms[i] = new PoseTransform((PoseLandmarkType)i);
|
_poseTransforms[i] = new PoseTransform(i);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -148,12 +148,12 @@ private void CreateBonds()
|
||||||
|
|
||||||
// 最后手动添加身体上的两条横线
|
// 最后手动添加身体上的两条横线
|
||||||
_bonds.Add(new Bond(
|
_bonds.Add(new Bond(
|
||||||
_nodes[(int)PoseLandmarkType.LeftShoulder],
|
_nodes[PoseLandmarkType.LeftShoulder],
|
||||||
_nodes[(int)PoseLandmarkType.RightShoulder],
|
_nodes[PoseLandmarkType.RightShoulder],
|
||||||
Scale));
|
Scale));
|
||||||
_bonds.Add(new Bond(
|
_bonds.Add(new Bond(
|
||||||
_nodes[(int)PoseLandmarkType.LeftHip],
|
_nodes[PoseLandmarkType.LeftHip],
|
||||||
_nodes[(int)PoseLandmarkType.RightHip],
|
_nodes[PoseLandmarkType.RightHip],
|
||||||
Scale));
|
Scale));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -162,15 +162,15 @@ private void CreateBonds()
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="nodes">需要连接起来的关键点 需要按顺序设置</param>
|
/// <param name="nodes">需要连接起来的关键点 需要按顺序设置</param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
private List<Bond> GenerateBondsList(PoseLandmarkType[] nodes)
|
private List<Bond> GenerateBondsList(int[] nodes)
|
||||||
{
|
{
|
||||||
var bonds = new List<Bond>();
|
var bonds = new List<Bond>();
|
||||||
|
|
||||||
for (var i = 0; i < nodes.Length - 1; i++)
|
for (var i = 0; i < nodes.Length - 1; i++)
|
||||||
{
|
{
|
||||||
bonds.Add(new Bond(
|
bonds.Add(new Bond(
|
||||||
_nodes[(int)nodes[i]],
|
_nodes[nodes[i]],
|
||||||
_nodes[(int)nodes[i + 1]],
|
_nodes[nodes[i + 1]],
|
||||||
Scale));
|
Scale));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -7,7 +7,7 @@ public class ModelBehaviour : MonoBehaviour
|
||||||
private Animator _animator;
|
private Animator _animator;
|
||||||
|
|
||||||
private readonly UdpListener _listener = new UdpListener();
|
private readonly UdpListener _listener = new UdpListener();
|
||||||
private static readonly PoseTransform[] PoseTransforms = new PoseTransform[(int)PoseLandmarkType.MaxValue];
|
private static readonly PoseTransform[] PoseTransforms = new PoseTransform[PoseLandmarkType.MaxValue];
|
||||||
|
|
||||||
private void Start()
|
private void Start()
|
||||||
{
|
{
|
||||||
|
@ -34,7 +34,7 @@ private void Update()
|
||||||
|
|
||||||
foreach (var parent in landmark.Parent)
|
foreach (var parent in landmark.Parent)
|
||||||
{
|
{
|
||||||
parentsum += PoseTransforms[(int)parent].AveragePos;
|
parentsum += PoseTransforms[parent.Value].AveragePos;
|
||||||
}
|
}
|
||||||
|
|
||||||
parentsum /= landmark.Parent.Count;
|
parentsum /= landmark.Parent.Count;
|
||||||
|
@ -69,9 +69,9 @@ private static void RigPoint(List<PoseLandmark> landmarks)
|
||||||
foreach (var landmark in landmarks)
|
foreach (var landmark in landmarks)
|
||||||
{
|
{
|
||||||
|
|
||||||
PoseTransform.UpdatePosition(ref PoseTransforms[(int)landmark.Type], landmark);
|
PoseTransform.UpdatePosition(ref PoseTransforms[landmark.Type.Value], landmark);
|
||||||
|
|
||||||
Debug.Log(PoseTransforms[(int)landmark.Type].UnityName+":"+PoseTransforms[(int)landmark.Type].AveragePos);
|
Debug.Log(PoseTransforms[landmark.Type.Value].UnityName+":"+PoseTransforms[landmark.Type.Value].AveragePos);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -81,9 +81,9 @@ private static void RigPoint(List<PoseLandmark> landmarks)
|
||||||
/// </summary>
|
/// </summary>
|
||||||
private void InitPoseTransformList()
|
private void InitPoseTransformList()
|
||||||
{
|
{
|
||||||
for (var type = 0; type <= (int)PoseLandmarkType.RightFootIndex; type++)
|
for (var type = 0; type <= PoseLandmarkType.RightFootIndex; type++)
|
||||||
{
|
{
|
||||||
var item = new PoseTransform((PoseLandmarkType)type);
|
var item = new PoseTransform(type);
|
||||||
|
|
||||||
if (item.UnityName != HumanBodyBones.LastBone)
|
if (item.UnityName != HumanBodyBones.LastBone)
|
||||||
{
|
{
|
||||||
|
@ -125,10 +125,10 @@ private void InitPoseTransformList()
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="target">需要添加的节点</param>
|
/// <param name="target">需要添加的节点</param>
|
||||||
/// <param name="parent">添加的父节点</param>
|
/// <param name="parent">添加的父节点</param>
|
||||||
private void AddParentTransform(PoseLandmarkType target, PoseLandmarkType parent)
|
private void AddParentTransform(int target, int parent)
|
||||||
{
|
{
|
||||||
var parents = PoseTransforms[(int)target].Parent;
|
var parents = PoseTransforms[target].Parent;
|
||||||
parents.Add(PoseTransforms[(int)parent].MediaPipeName);
|
parents.Add(PoseTransforms[parent].MediaPipeName);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -47,9 +47,9 @@ public class PoseLandmark
|
||||||
|
|
||||||
public long TimeStamp { get; }
|
public long TimeStamp { get; }
|
||||||
|
|
||||||
public PoseLandmark(PoseLandmarkType type, float x, float y, float z, float visibility, long timeStamp)
|
public PoseLandmark(int type, float x, float y, float z, float visibility, long timeStamp)
|
||||||
{
|
{
|
||||||
Type = type;
|
Type = new PoseLandmarkType(type);
|
||||||
X = x;
|
X = x;
|
||||||
Y = y;
|
Y = y;
|
||||||
Z = z;
|
Z = z;
|
||||||
|
@ -66,7 +66,7 @@ public byte[] ToByteArray()
|
||||||
{
|
{
|
||||||
var result = new byte[PacketLength];
|
var result = new byte[PacketLength];
|
||||||
|
|
||||||
BitConverter.GetBytes((int)Type).CopyTo(result, 0);
|
BitConverter.GetBytes(Type.Value).CopyTo(result, 0);
|
||||||
BitConverter.GetBytes(X).CopyTo(result, 4);
|
BitConverter.GetBytes(X).CopyTo(result, 4);
|
||||||
BitConverter.GetBytes(Y).CopyTo(result, 8);
|
BitConverter.GetBytes(Y).CopyTo(result, 8);
|
||||||
BitConverter.GetBytes(Z).CopyTo(result, 12);
|
BitConverter.GetBytes(Z).CopyTo(result, 12);
|
||||||
|
@ -84,7 +84,7 @@ public byte[] ToByteArray()
|
||||||
public static PoseLandmark ValueOf(byte[] bytes)
|
public static PoseLandmark ValueOf(byte[] bytes)
|
||||||
{
|
{
|
||||||
var result = new PoseLandmark(
|
var result = new PoseLandmark(
|
||||||
(PoseLandmarkType)BitConverter.ToInt32(bytes, 0),
|
BitConverter.ToInt32(bytes, 0),
|
||||||
BitConverter.ToSingle(bytes, 4),
|
BitConverter.ToSingle(bytes, 4),
|
||||||
BitConverter.ToSingle(bytes, 8),
|
BitConverter.ToSingle(bytes, 8),
|
||||||
BitConverter.ToSingle(bytes, 12),
|
BitConverter.ToSingle(bytes, 12),
|
||||||
|
@ -106,7 +106,7 @@ public static List<PoseLandmark> ArrayOf(byte[] bytes)
|
||||||
|
|
||||||
for (var i = 0; i < bytes.Length; i = i + PacketLength)
|
for (var i = 0; i < bytes.Length; i = i + PacketLength)
|
||||||
{
|
{
|
||||||
var landmark = new PoseLandmark((PoseLandmarkType)BitConverter.ToInt32(bytes, i),
|
var landmark = new PoseLandmark(BitConverter.ToInt32(bytes, i),
|
||||||
BitConverter.ToSingle(bytes, i + 4),
|
BitConverter.ToSingle(bytes, i + 4),
|
||||||
BitConverter.ToSingle(bytes, i + 8),
|
BitConverter.ToSingle(bytes, i + 8),
|
||||||
BitConverter.ToSingle(bytes, i + 12),
|
BitConverter.ToSingle(bytes, i + 12),
|
||||||
|
@ -127,7 +127,7 @@ public override bool Equals(object obj)
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
return Type == landmark.Type
|
return Type.Value == landmark.Type.Value
|
||||||
&& Math.Abs(X - landmark.X) < Tolerance
|
&& Math.Abs(X - landmark.X) < Tolerance
|
||||||
&& Math.Abs(Y - landmark.Y) < Tolerance
|
&& Math.Abs(Y - landmark.Y) < Tolerance
|
||||||
&& Math.Abs(Z - landmark.Z) < Tolerance
|
&& Math.Abs(Z - landmark.Z) < Tolerance
|
||||||
|
|
|
@ -1,41 +1,70 @@
|
||||||
|
using System;
|
||||||
|
|
||||||
namespace Models
|
namespace Models
|
||||||
{
|
{
|
||||||
//名字
|
//名字
|
||||||
public enum PoseLandmarkType
|
public struct PoseLandmarkType
|
||||||
{
|
{
|
||||||
Nose,
|
public const int Nose = 0;
|
||||||
LeftEyeInner,
|
public const int LeftEyeInner = 1;
|
||||||
LeftEye,
|
public const int LeftEye = 2;
|
||||||
LeftEyeOuter,
|
public const int LeftEyeOuter = 3;
|
||||||
RightEyeInner,
|
public const int RightEyeInner = 4;
|
||||||
RightEye,
|
public const int RightEye = 5;
|
||||||
RightEyeOuter,
|
public const int RightEyeOuter = 6;
|
||||||
LeftEar,
|
public const int LeftEar = 7;
|
||||||
RightEar,
|
public const int RightEar = 8;
|
||||||
MouthLeft,
|
public const int MouthLeft = 9;
|
||||||
MouthRight,
|
public const int MouthRight = 10;
|
||||||
LeftShoulder,
|
public const int LeftShoulder = 11;
|
||||||
RightShoulder,
|
public const int RightShoulder = 12;
|
||||||
LeftElbow,
|
public const int LeftElbow = 13;
|
||||||
RightElbow,
|
public const int RightElbow = 14;
|
||||||
LeftWrist,
|
public const int LeftWrist = 15;
|
||||||
RightWrist,
|
public const int RightWrist = 16;
|
||||||
LeftPinky,
|
public const int LeftPinky = 17;
|
||||||
RightPinky,
|
public const int RightPinky = 18;
|
||||||
LeftIndex,
|
public const int LeftIndex = 19;
|
||||||
RightIndex,
|
public const int RightIndex = 20;
|
||||||
LeftThumb,
|
public const int LeftThumb = 21;
|
||||||
RightThumb,
|
public const int RightThumb = 22;
|
||||||
LeftHip,
|
public const int LeftHip = 23;
|
||||||
RightHip,
|
public const int RightHip = 24;
|
||||||
LeftKnee,
|
public const int LeftKnee = 25;
|
||||||
RightKnee,
|
public const int RightKnee = 26;
|
||||||
LeftAnkle,
|
public const int LeftAnkle = 27;
|
||||||
RightAnkle,
|
public const int RightAnkle = 28;
|
||||||
LeftHeel,
|
public const int LeftHeel = 29;
|
||||||
RightHeel,
|
public const int RightHeel = 30;
|
||||||
LeftFootIndex,
|
public const int LeftFootIndex = 31;
|
||||||
RightFootIndex,
|
public const int RightFootIndex = 32;
|
||||||
MaxValue
|
public const int MaxValue = 33;
|
||||||
|
|
||||||
|
public int Value { get; }
|
||||||
|
|
||||||
|
public PoseLandmarkType(int value)
|
||||||
|
{
|
||||||
|
if (value >= MaxValue)
|
||||||
|
{
|
||||||
|
throw new ArgumentOutOfRangeException(nameof(value));
|
||||||
|
}
|
||||||
|
|
||||||
|
Value = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
public override bool Equals(object obj)
|
||||||
|
{
|
||||||
|
return Value.Equals(obj);
|
||||||
|
}
|
||||||
|
|
||||||
|
public override int GetHashCode()
|
||||||
|
{
|
||||||
|
return Value.GetHashCode();
|
||||||
|
}
|
||||||
|
|
||||||
|
public override string ToString()
|
||||||
|
{
|
||||||
|
return Value.ToString();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -49,12 +49,12 @@ public struct PoseTransform
|
||||||
private readonly Queue<PoseLandmark> _landmarkQueue;
|
private readonly Queue<PoseLandmark> _landmarkQueue;
|
||||||
|
|
||||||
public PoseTransform(
|
public PoseTransform(
|
||||||
PoseLandmarkType type,
|
int type,
|
||||||
int averageLength = 5
|
int averageLength = 5
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
MediaPipeName = type;
|
MediaPipeName = new PoseLandmarkType(type);
|
||||||
UnityName = GetRelatedBone(type);
|
UnityName = GetRelatedBone(MediaPipeName);
|
||||||
AveragePos = new Vector3();
|
AveragePos = new Vector3();
|
||||||
Parent = new List<PoseLandmarkType>();
|
Parent = new List<PoseLandmarkType>();
|
||||||
PreviousQuaternion = new Quaternion();
|
PreviousQuaternion = new Quaternion();
|
||||||
|
@ -108,7 +108,7 @@ public static void CalculateRotation(ref PoseTransform poseTransform, PoseLandma
|
||||||
/// <returns>关联骨骼的种类</returns>
|
/// <returns>关联骨骼的种类</returns>
|
||||||
private static HumanBodyBones GetRelatedBone(PoseLandmarkType type)
|
private static HumanBodyBones GetRelatedBone(PoseLandmarkType type)
|
||||||
{
|
{
|
||||||
switch (type)
|
switch (type.Value)
|
||||||
{
|
{
|
||||||
case PoseLandmarkType.LeftHip:
|
case PoseLandmarkType.LeftHip:
|
||||||
return HumanBodyBones.LeftUpperLeg;
|
return HumanBodyBones.LeftUpperLeg;
|
||||||
|
|
Loading…
Reference in New Issue
Block a user