squat_down.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /// <summary>
  2. /// ZWN-XHJ
  3. /// 20210928
  4. /// </summary>
  5. using System.Collections.Generic;
  6. using UnityEngine;
  7. using UnityEngine.UI;
  8. public class squat_down : MonoBehaviour
  9. {
  10. float squat_scale = 0.85f;
  11. bool squat_stay;
  12. private string str_test;//提示相关
  13. private float BASE_HEIGHT = 0;//高度计算
  14. bool even_pose = false;
  15. private float this_height;
  16. float current_height;
  17. Vector3 head, right_ankle, left_ankle, right_ankle_last, left_ankle_last;
  18. public static squat_down single;
  19. void Awake()
  20. {
  21. if (stay_hight == null) stay_hight = new List<float>();
  22. stay_hight.Add(0);
  23. single = this;
  24. squat_scale = 0.85f;
  25. }
  26. /* public Text cur_text;*/
  27. /* public human_area cur_human;*/
  28. /* void Update()
  29. {
  30. //初始化
  31. if (cur_human.human_area_true() == 0 && !squat_stay && !even_pose)
  32. {
  33. squat_stay = true;
  34. }
  35. log_height();
  36. int num=keep_down();//检测状态
  37. }*/
  38. bool stand = true;
  39. public List<float> stay_hight;
  40. float ave_hight;
  41. /// <summary>
  42. /// 检测状态
  43. /// -1:站起
  44. /// 1:蹲下
  45. /// </summary>
  46. public int keep_down()
  47. {
  48. if (!even_pose)
  49. {
  50. even_pose = true;
  51. }
  52. if (even_pose)
  53. {
  54. if (squat_stay)
  55. {
  56. squat_stay = false;
  57. }
  58. head = common_data.single.zwn_single_vec[1];
  59. right_ankle = common_data.single.zwn_single_vec[15];
  60. left_ankle = common_data.single.zwn_single_vec[16];
  61. if (right_ankle.y != -1 && left_ankle.y != -1)
  62. {
  63. this_height = Mathf.Max(Mathf.Abs(Mathf.Abs(head.y) - Mathf.Abs(right_ankle.y)), Mathf.Abs(Mathf.Abs(head.y) - Mathf.Abs(left_ankle.y)));
  64. current_height = this_height;
  65. right_ankle_last = right_ankle;
  66. left_ankle_last = left_ankle;
  67. }
  68. else
  69. {
  70. this_height = Mathf.Max(Mathf.Abs(Mathf.Abs(head.y) - Mathf.Abs(right_ankle_last.y)), Mathf.Abs(Mathf.Abs(head.y) - Mathf.Abs(left_ankle_last.y)));
  71. current_height = this_height;
  72. }
  73. if (even_pose)
  74. {
  75. //检测蹲下中
  76. if (stay_hight.Count > 0)
  77. {
  78. BASE_HEIGHT = 0;
  79. stay_hight.Clear();
  80. }
  81. if (this_height < ave_hight * squat_scale)//蹲下
  82. {
  83. if (stand)
  84. {
  85. /* str_test = "蹲下";*/
  86. stand = false;
  87. }
  88. }
  89. else
  90. {
  91. if (!stand)
  92. {
  93. /* str_test = "站起";*/
  94. stand = true;
  95. }
  96. }
  97. /*
  98. print("蹲下检测:" + str_test);
  99. cur_text.text = "蹲下检测:" + str_test + "\n" + this_height + "\n" + ave_hight * squat_scale;*/
  100. }
  101. }
  102. else
  103. {
  104. if (!stand)
  105. {
  106. /* str_test = "站起";*/
  107. stand = true;
  108. }
  109. }
  110. if (stand)
  111. {
  112. return -1;
  113. }
  114. else
  115. {
  116. return 1;
  117. }
  118. }
  119. /* public float time;*/
  120. /// <summary>
  121. /// 检测到人在框内后(321)需调用
  122. /// </summary>
  123. public void log_height()
  124. {
  125. if (!squat_stay)
  126. {
  127. squat_stay = true;
  128. }
  129. if (squat_stay)
  130. {
  131. if (!stand)
  132. {
  133. stand = true;
  134. }
  135. head = common_data.single.zwn_single_vec[1];
  136. right_ankle = common_data.single.zwn_single_vec[15];
  137. left_ankle = common_data.single.zwn_single_vec[16];
  138. if (right_ankle.y != -1 && left_ankle.y != -1)
  139. {
  140. this_height = Mathf.Max(Mathf.Abs(Mathf.Abs(head.y) - Mathf.Abs(right_ankle.y)), Mathf.Abs(Mathf.Abs(head.y) - Mathf.Abs(left_ankle.y)));
  141. current_height = this_height;
  142. right_ankle_last = right_ankle;
  143. left_ankle_last = left_ankle;
  144. }
  145. else
  146. {
  147. this_height = Mathf.Max(Mathf.Abs(Mathf.Abs(head.y) - Mathf.Abs(right_ankle_last.y)), Mathf.Abs(Mathf.Abs(head.y) - Mathf.Abs(left_ankle_last.y)));
  148. current_height = this_height;
  149. this_height = current_height;
  150. }
  151. if (this_height > 0 && BASE_HEIGHT == 0)
  152. {
  153. BASE_HEIGHT = this_height;
  154. }
  155. else
  156. {
  157. stay_hight.Add(current_height);
  158. if (stay_hight.Count > 10)
  159. {
  160. ave_hight = 0;
  161. for (int i = 0; i < stay_hight.Count; i++)
  162. {
  163. ave_hight += stay_hight[i];
  164. }
  165. ave_hight = ave_hight / stay_hight.Count;
  166. stay_hight.Remove(stay_hight[0]);
  167. }
  168. }
  169. /* print("等待身高:" + ave_hight);
  170. cur_text.text = "等待身高:" + ave_hight;
  171. time += Time.deltaTime;
  172. if (time >= 3)
  173. {
  174. if (!even_pose)
  175. {
  176. even_pose = true;
  177. }
  178. }*/
  179. }
  180. }
  181. }