| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190 |
- /// <summary>
- /// ZWN-XHJ
- /// 20210928
- /// </summary>
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.UI;
- public class squat_down : MonoBehaviour
- {
- float squat_scale = 0.85f;
- bool squat_stay;
- private string str_test;//提示相关
- private float BASE_HEIGHT = 0;//高度计算
- bool even_pose = false;
- private float this_height;
- float current_height;
- Vector3 head, right_ankle, left_ankle, right_ankle_last, left_ankle_last;
- public static squat_down single;
- void Awake()
- {
- if (stay_hight == null) stay_hight = new List<float>();
- stay_hight.Add(0);
- single = this;
- squat_scale = 0.85f;
- }
- /* public Text cur_text;*/
- /* public human_area cur_human;*/
- /* void Update()
- {
- //初始化
- if (cur_human.human_area_true() == 0 && !squat_stay && !even_pose)
- {
- squat_stay = true;
- }
- log_height();
- int num=keep_down();//检测状态
- }*/
- bool stand = true;
- public List<float> stay_hight;
- float ave_hight;
- /// <summary>
- /// 检测状态
- /// -1:站起
- /// 1:蹲下
- /// </summary>
- public int keep_down()
- {
- if (!even_pose)
- {
- even_pose = true;
- }
- if (even_pose)
- {
- if (squat_stay)
- {
- squat_stay = false;
- }
- head = common_data.single.zwn_single_vec[1];
- right_ankle = common_data.single.zwn_single_vec[15];
- left_ankle = common_data.single.zwn_single_vec[16];
- if (right_ankle.y != -1 && left_ankle.y != -1)
- {
- 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)));
- current_height = this_height;
- right_ankle_last = right_ankle;
- left_ankle_last = left_ankle;
- }
- else
- {
- 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)));
- current_height = this_height;
- }
- if (even_pose)
- {
- //检测蹲下中
- if (stay_hight.Count > 0)
- {
- BASE_HEIGHT = 0;
- stay_hight.Clear();
- }
- if (this_height < ave_hight * squat_scale)//蹲下
- {
- if (stand)
- {
- /* str_test = "蹲下";*/
- stand = false;
- }
- }
- else
- {
- if (!stand)
- {
- /* str_test = "站起";*/
- stand = true;
- }
- }
- /*
- print("蹲下检测:" + str_test);
- cur_text.text = "蹲下检测:" + str_test + "\n" + this_height + "\n" + ave_hight * squat_scale;*/
- }
- }
- else
- {
- if (!stand)
- {
- /* str_test = "站起";*/
- stand = true;
- }
- }
- if (stand)
- {
- return -1;
- }
- else
- {
- return 1;
- }
- }
- /* public float time;*/
- /// <summary>
- /// 检测到人在框内后(321)需调用
- /// </summary>
- public void log_height()
- {
- if (!squat_stay)
- {
- squat_stay = true;
- }
- if (squat_stay)
- {
- if (!stand)
- {
- stand = true;
- }
- head = common_data.single.zwn_single_vec[1];
- right_ankle = common_data.single.zwn_single_vec[15];
- left_ankle = common_data.single.zwn_single_vec[16];
- if (right_ankle.y != -1 && left_ankle.y != -1)
- {
- 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)));
- current_height = this_height;
- right_ankle_last = right_ankle;
- left_ankle_last = left_ankle;
- }
- else
- {
- 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)));
- current_height = this_height;
- this_height = current_height;
- }
- if (this_height > 0 && BASE_HEIGHT == 0)
- {
- BASE_HEIGHT = this_height;
- }
- else
- {
- stay_hight.Add(current_height);
- if (stay_hight.Count > 10)
- {
- ave_hight = 0;
- for (int i = 0; i < stay_hight.Count; i++)
- {
- ave_hight += stay_hight[i];
- }
- ave_hight = ave_hight / stay_hight.Count;
- stay_hight.Remove(stay_hight[0]);
- }
- }
- /* print("等待身高:" + ave_hight);
- cur_text.text = "等待身高:" + ave_hight;
- time += Time.deltaTime;
- if (time >= 3)
- {
- if (!even_pose)
- {
- even_pose = true;
- }
- }*/
- }
- }
- }
|