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 System.Runtime.CompilerServices; using UnityEngine; using UnityEngine.UI; using Yoga; public class YogaManager : MonoSingleton { private List _points = new List(); private Mat _rgbaMat; public Mat RgbaMat { get { return _rgbaMat; } set => _rgbaMat = value; } private List _currPersonPoints = new List(); public List CurrPersonPoints { get => _currPersonPoints; set => _currPersonPoints = value; } private List _voloResult = new List(); public List VoloResult { get => _voloResult; set => _voloResult = value; } public List Points { get => _points; set => _points = value; } private int _currentActionCount; //当前动作计数 public int CurrentActionCount { get => _currentActionCount; } private int _currentSuccessActionCount; //当前成功动作计数 public int CurrentSuccessActionCount { get => _currentSuccessActionCount; } public int MaxActionCount => Action.MaxActionCount; private int _actionIndex = -1; //用户选择界面选择的动作索引 public int ActionIndex { get => _actionIndex; internal set => _actionIndex = value; } private Dictionary _actions = new Dictionary(); public YogaData Action { get => _action; set => _action = value; } public Estimator CurrentEstimator { get => currentEstimator; internal set => currentEstimator = value; } private YogaData _action = null; private Estimator currentEstimator; public void InitData() { _actions[AvatarAction.HeadTurnLeft] = new HeadTurnLeft(); _actions[AvatarAction.HeadTurnRight] = new HeadTurnRight(); _actions[AvatarAction.HeadTurnUp] = new HeadTurnUp(); _actions[AvatarAction.HeadTurnDown] = new HeadTurnDown(); _actions[AvatarAction.HandsUp] = new HandsUp(); _actions[AvatarAction.HandsHold] = new HandsHold(); _currentActionCount = 0; _currentSuccessActionCount = 0; //根据用户选择的动作索引,获取相应的资源 Action = YogaDataLoader.LoadData(ActionIndex); EventManager.Instance.Dispatch(YogaEventType.UpdateProgress, CurrentSuccessActionCount, CurrentActionCount); } private void OnEnable() { EventManager.Instance.AddEventListener(YogaEventType.ActionSuccess, OnActionSuccess); EventManager.Instance.AddEventListener(YogaEventType.ActionFailed, OnActionFailed); } private void OnDisable() { EventManager.Instance.RemoveEventListener(YogaEventType.ActionSuccess, OnActionSuccess); EventManager.Instance.RemoveEventListener(YogaEventType.ActionFailed, OnActionFailed); } private void OnActionSuccess() { _currentSuccessActionCount++; _currentActionCount++; UpdateData(); } private void OnActionFailed() { _currentActionCount++; UpdateData(); } private void UpdateData() { //如果达到最大数,则停止检测,跳转到下一个动作/下一个动作引导/奖励界面 if (CurrentActionCount >= MaxActionCount) { //跳转到下一个动作/下一个动作引导/奖励界面 UIManager.Instance.CloseCurrent(); //关闭当前界面并停止检测 //args[0] = successCount args[1] = totalCount UIManager.Instance.ShowPanel(false, CurrentSuccessActionCount, MaxActionCount); } //args[0] = successCount args[1] = excutedCount args[2] = totalCount EventManager.Instance.Dispatch(YogaEventType.UpdateProgress, CurrentSuccessActionCount, CurrentActionCount, MaxActionCount); } public bool IsCorrectAction(List personPoints, AvatarAction actionType) { if (!_actions.ContainsKey(actionType)) { Debug.LogError("ActionType is not exist"); return false; } var result = _actions[actionType].CheckPose(personPoints); return result; } public bool ActionCheckPoints(List points) { if (points == null || points.Count == 0) { //Debug.Log("ActionCheckPoints points is null"); return false; } //for (int i = 0; i < points.Count; i++) //{ // Point p = points[i]; // if (p != new Point(-1, -1)) // Debug.LogWarning($"ActionPoints p({i}): {i.tagName()}, value: {p.x},{p.y}"); //} foreach (var p in Action.MustPoints) { if (!p.IsValid(points)) //有一个点不符合 返回false { //Debug.LogError($"ActionCheckPoints failed, {p} is not valid"); return false; } } //不设的情况下,不检测 if (Action.AnyPoints != null && Action.AnyPoints.Count > 0) { foreach (var p in Action.AnyPoints) { if (p.IsValid(points)) //有一个点符合 返回true { return true; } } return false; //没有一个点符合 返回false } return true; } }