Health/Assets/Scripts/PoseCheck/HoldPosition.cs

86 lines
2.8 KiB
C#
Raw Normal View History

2023-11-27 05:49:53 +00:00
using OpenCVForUnity.CoreModule;
using System;
using System.Collections.Generic;
2023-11-27 18:00:13 +00:00
using System.Linq;
2023-11-27 05:49:53 +00:00
using UnityEngine;
public class HoldPosition : PoseBase
{
2023-11-27 18:00:13 +00:00
public override bool? AnalyzingAction(List<(DateTime, List<Point>)> points, bool isDataReGather = false)
{
if (points.Count < 2) // 0级实现逻辑 不做操作
return null;
var distance = Vector2.zero;
TimeSpan totalTimeSpan = TimeSpan.MinValue;
var startPoint = points.FirstOrDefault().Item2;
var endPoint = points.LastOrDefault().Item2;
var startDir = "Nose".vector(startPoint) - "Neck".vector(startPoint);
var endDir = "Nose".vector(endPoint) - "Neck".vector(endPoint);
var angle = Vector2.SignedAngle(startDir, endDir);
for (int i = 0; i < points.Count - 1; i++)
{
var p1 = points[i].Item2;
var p2 = points[i + 1].Item2;
var vector = GetBasicVectorDirection(p1, p2);
distance += vector;
TimeSpan timeSpan = points[i + 1].Item1 - points[i].Item1;
totalTimeSpan = timeSpan;
}
LogPrint.Warning($"angle: {angle}", PrintLevel.Normal);
LogPrint.Log($"distance: {distance}, totalTimeSpan: {totalTimeSpan}", PrintLevel.Normal);
return (angle < 10 || MovementValidation(distance, totalTimeSpan, 1).GetValueOrDefault());
}
2023-11-27 05:49:53 +00:00
public override Vector2 GetBasicVectorDirection(List<Point> startPoint, List<Point> endPoint)
{
List<Vector2> vectors = new List<Vector2>();
//必要点位 Nose Neck
//计算鼻子到头颈矢量
//return GetAverageVector(startPoint, endPoint, "Nose");
return GetTwoPointAverageVector(startPoint, endPoint, "Nose", "Neck");
}
protected override bool IsMovePoint(string tagName)
{
return "Nose" == tagName || "REye" == tagName || "LEye" == tagName || "REar" == tagName || "LEar" == tagName;
}
public override bool? MovementValidation(Vector2 distance, TimeSpan totalTimeSpan, int level)
{
if (totalTimeSpan.TotalMilliseconds / 1000 < 0.05f)
{
LogPrint.Error("TimeSpan too short");
return null;
}
//保持姿势不发生移动
2023-11-27 18:00:13 +00:00
if (distance.magnitude < 15f)//允许一定误差
2023-11-27 05:49:53 +00:00
{
//EventManager.Instance.Dispatch(YogaEventType.Action_Success);
return true;
}
else
{
LogPrint.Warning($"Hold Position, magnitude:{distance.magnitude}", PrintLevel.Important);
//EventManager.Instance.Dispatch(YogaEventType.Action_Fail);
return false;
}
}
public override void ExcellenceEstimate(List<(TimeSpan, Vector2)> frameData, Vector2 distance, TimeSpan totalTimeSpan)
{
throw new NotImplementedException();
}
}