///
/// ZWN-XHJ
/// 20210928
///
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();
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 stay_hight;
float ave_hight;
///
/// 检测状态
/// -1:站起
/// 1:蹲下
///
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;*/
///
/// 检测到人在框内后(321)需调用
///
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;
}
}*/
}
}
}