Health/Assets/Scripts/YogaManager.cs

112 lines
3.1 KiB
C#

using NUnit.Framework.Internal;
using OpenCVForUnity.CoreModule;
using OpenCVForUnity.DnnModule;
using OpenCVForUnity.UnityUtils;
using OpenCVForUnity.UnityUtils.Helper;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using UnityEngine;
public class YogaManager : MonoSingleton<YogaManager>
{
private bool _isInited = false;
private LinkedList<PoseBase> _poseChecks = new LinkedList<PoseBase>();
private PoseBase _currPose = null;
private double threshold = 0.1;
private List<Point> _points = new List<Point>();
public List<Point> Points { get => _points; private set => _points = value; }
public Action<string> UpdateStatus { get; internal set; }
public Action<string> ShowHint { get; internal set; }
public int ActionIndex { get; internal set; }
public int CurrentActionCount { get => _currentActionCount; internal set => _currentActionCount = value; }
public int CurrentSuccessActionCount { get => _currentSuccessActionCount; internal set => _currentSuccessActionCount = value; }
//创建帧数据缓存
private Dictionary<int, List<List<Point>>> _framePoints = new (); // actionID, frameID, points
private int _currActionID;
private int _currentActionCount;
private int _currentSuccessActionCount;
public void InitData()
{
_poseChecks.AddFirst(new IsReady(ModelType.OpenPose));
_poseChecks.AddLast(new HeadTurnLeft(ModelType.OpenPose));
_poseChecks.AddLast(new HeadTurnRight(ModelType.OpenPose));
ShowHint?.Invoke(string.Empty);
UpdateStatus?.Invoke(string.Empty);
_currPose = _poseChecks.First.Value;
_currPose.ShowHint();
_currActionID = 0;
_isInited = true;
_currentActionCount = 0;
_currentSuccessActionCount = 0;
}
public void Dispose()
{
_currPose = null;
UpdateStatus = null;
_isInited = false;
_poseChecks.Clear();
}
public List<Point> Process(Mat img, Dictionary<ModelType, KeypointsModel> models)
{
List<Point> points = models[_currPose.ModelName].estimate(img, (float)threshold).toList();
//_framePoints[_currActionID].Add(points);
ProcessPoints(points);
return points;
}
public void ProcessPoints(List<Point> points)
{
if (!_isInited)
{
InitData();
}
Points = points;
if (_currPose == null)
{
_currPose = _poseChecks.First.Value;
_currPose.ShowHint();
return;
}
if (_currPose.CheckPose(points))
{
_currPose.ProcessSuccessCallBack(() =>
{
SwitchNextPost();
});
}
else
{
_currPose.ProcessFailCallBack();
}
}
private void SwitchNextPost()
{
if (_currPose == _poseChecks.Last.Value)
{
Dispose();
return;
}
_currPose = _poseChecks.Find(_currPose).Next.Value;
_currPose.ShowHint();
}
}