///
/// ZWN
/// 20220104
///
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
//using zwn;
///
/// 显示对齐框的框工具类接口
///
public class FollowBoxManager : MonoBehaviour
{
public static FollowBoxManager sigle;
float[] pose_kpts = new float[4 * 2];//4个点的坐标
int pose_kpts_count = 4;//数量
public GameObject show_rect_perfab;//克隆物体
public Transform show_rect_tra;//克隆物体父级
[HideInInspector]
public List show_rect_list;//克隆出来的物体
//固定框
public int x; ///< 左上角顶点 X 轴坐标
public int y; ///< 左上角顶点 Y 轴坐标
public int width; ///< 矩形的宽度
public int height; ///< 矩形的高度
public bool isShowFollowBox = false;
public static float followRatio = 0f;
// dataType.zwn_rect_t_fixedframe aligned_Rect = new dataType.zwn_rect_t_fixedframe();
private bool isAlignmentSuccee;
public bool GetIsSuccee
{
get { return isAlignmentSuccee; }
set { isAlignmentSuccee = value; }
}
void Awake()
{
sigle = this;
}
// Use this for initialization
void Start()
{
//aligned_Rect.x = x;
//aligned_Rect.y = y;
//aligned_Rect.width = width;
//aligned_Rect.height = height;
GetCurrFollow();
}
// Update is called once per frame
void Update()
{
ShowFollowBox();
GetRectRatio();
GetIsinclude();
}
///
///计算关键点的矩形框
///
void ShowFollowBox()
{
if (!ZwnAmlogicHelperData.isSomeone)
{
return;
}
if (ZwnAmlogicHelperData.ZWN_POSE_KEY_POINTS_DIM_LEN == 29)
{
if (Mathf.Abs(zwn_common_data.single.zwn_transform_pose_twentynine[5].x) != 1920 && Mathf.Abs(zwn_common_data.single.zwn_transform_pose_twentynine[6].x) != 1920 && Mathf.Abs(zwn_common_data.single.zwn_transform_pose_twentynine[17].x) != 1920 && Mathf.Abs(zwn_common_data.single.zwn_transform_pose_twentynine[18].x) != 1920)
{
pose_kpts[0] = zwn_common_data.single.zwn_original_pose_twentynine[5].x;
pose_kpts[1] = zwn_common_data.single.zwn_original_pose_twentynine[5].y;
pose_kpts[2] = zwn_common_data.single.zwn_original_pose_twentynine[6].x;
pose_kpts[3] = zwn_common_data.single.zwn_original_pose_twentynine[6].y;
pose_kpts[4] = zwn_common_data.single.zwn_original_pose_twentynine[17].x;
pose_kpts[5] = zwn_common_data.single.zwn_original_pose_twentynine[17].y;
pose_kpts[6] = zwn_common_data.single.zwn_original_pose_twentynine[18].x;
pose_kpts[7] = zwn_common_data.single.zwn_original_pose_twentynine[18].y;
}
//utilCAPI.zwn_get_bounding_rect(pose_kpts, pose_kpts_count, ref UtilHelper.zwn_rect_t_alignments);
float x = (zwn_common_data.single.zwn_transform_pose_twentynine[5].x + zwn_common_data.single.zwn_transform_pose_twentynine[6].x) / 2;
float y = (zwn_common_data.single.zwn_transform_pose_twentynine[5].y + zwn_common_data.single.zwn_transform_pose_twentynine[17].y) / 2;
float height = Vector3.Distance(zwn_common_data.single.zwn_transform_pose_twentynine[5], zwn_common_data.single.zwn_transform_pose_twentynine[17]);
float width = Vector3.Distance(zwn_common_data.single.zwn_transform_pose_twentynine[5], zwn_common_data.single.zwn_transform_pose_twentynine[6]);
Vector2 rect_weightheight = new Vector2(width, height);
Vector3 rect_xy = new Vector3(x, y, 0);
if (isShowFollowBox)
{
for (int i = 0; i < 1; i++)
{
//关于人形框
if (i >= show_rect_list.Count)
{
GameObject go = Instantiate(show_rect_perfab);
show_rect_list.Add(go);
go.transform.SetParent(show_rect_tra);
go.transform.localScale = new Vector3(1, 1, 1);
}
show_rect_list[i].transform.localPosition = rect_xy;
show_rect_list[i].GetComponent().sizeDelta = rect_weightheight;
}
}
}
else if (ZwnAmlogicHelperData.ZWN_POSE_KEY_POINTS_DIM_LEN == 17)
{
if (Mathf.Abs(zwn_common_data.single.zwn_transform_pose[5].x) != 1920 && Mathf.Abs(zwn_common_data.single.zwn_transform_pose[6].x) != 1920 && Mathf.Abs(zwn_common_data.single.zwn_transform_pose[11].x) != 1920 && Mathf.Abs(zwn_common_data.single.zwn_transform_pose[12].x) != 1920)
{
pose_kpts[0] = zwn_common_data.single.zwn_original_pose[5].x;
pose_kpts[1] = zwn_common_data.single.zwn_original_pose[5].y;
pose_kpts[2] = zwn_common_data.single.zwn_original_pose[6].x;
pose_kpts[3] = zwn_common_data.single.zwn_original_pose[6].y;
pose_kpts[4] = zwn_common_data.single.zwn_original_pose[11].x;
pose_kpts[5] = zwn_common_data.single.zwn_original_pose[11].y;
pose_kpts[6] = zwn_common_data.single.zwn_original_pose[12].x;
pose_kpts[7] = zwn_common_data.single.zwn_original_pose[12].y;
}
//utilCAPI.zwn_get_bounding_rect(pose_kpts, pose_kpts_count, ref UtilHelper.zwn_rect_t_alignments);
float x = (zwn_common_data.single.zwn_transform_pose[5].x + zwn_common_data.single.zwn_transform_pose[6].x) / 2;
float y = (zwn_common_data.single.zwn_transform_pose[5].y + zwn_common_data.single.zwn_transform_pose[11].y) / 2;
float height = Vector3.Distance(zwn_common_data.single.zwn_transform_pose[5], zwn_common_data.single.zwn_transform_pose[11]);
float width = Vector3.Distance(zwn_common_data.single.zwn_transform_pose[5], zwn_common_data.single.zwn_transform_pose[6]);
Vector2 rect_weightheight = new Vector2(width, height);
Vector3 rect_xy = new Vector3(x, y, 0);
if (isShowFollowBox)
{
for (int i = 0; i < 1; i++)
{
//关于人形框
if (i >= show_rect_list.Count)
{
GameObject go = Instantiate(show_rect_perfab);
show_rect_list.Add(go);
go.transform.SetParent(show_rect_tra);
go.transform.localScale = new Vector3(1, 1, 1);
}
show_rect_list[i].transform.localPosition = rect_xy;
show_rect_list[i].GetComponent().sizeDelta = rect_weightheight;
}
}
}
}
///
/// 当计算两个框的IOU分数小于thres阈值时,ratio 返回的是IOU的分数。(0.5-2)
///
void GetRectRatio()
{
//utilCAPI.zwn_get_rect_ratio(UtilHelper.zwn_rect_t_alignments, aligned_Rect, 0.1f, ref followRatio);
//print("followRatio " + followRatio);
}
///
/// 坐标转换
///
void GetCurrFollow()
{
int width = Screen.width;
int height = Screen.height;
//aligned_Rect.x = width / 2 + aligned_Rect.x;
//aligned_Rect.y = height / 2 - aligned_Rect.y;
}
///
/// 判断身体框是否在对齐框内
///
public void GetIsinclude()
{
float x1 = -(width / 2);
float x2 = width / 2;
float y1 = -(height / 2);
float y2 = height / 2;
bool isinclude = false;
bool isinclude1 = false;
bool isinclude2 = false;
bool isinclude3 = false;
bool isinclude4 = false;
bool isinclude5 = false;
bool isinclude6 = false;
bool isinclude7 = false;
if (!ZwnAmlogicHelperData.isSomeone)
{
return;
}
if (ZwnAmlogicHelperData.ZWN_POSE_KEY_POINTS_DIM_LEN == 29)
{
if (Mathf.Abs(zwn_common_data.single.zwn_transform_pose_twentynine[5].x) != 1920 && Mathf.Abs(zwn_common_data.single.zwn_transform_pose_twentynine[6].x) != 1920 && Mathf.Abs(zwn_common_data.single.zwn_transform_pose_twentynine[17].x) != 1920 && Mathf.Abs(zwn_common_data.single.zwn_transform_pose_twentynine[18].x) != 1920)
{
isinclude = zwn_common_data.single.zwn_transform_pose_twentynine[6].x > x1;
isinclude1 = zwn_common_data.single.zwn_transform_pose_twentynine[6].y < y2;
isinclude2 = zwn_common_data.single.zwn_transform_pose_twentynine[17].x < x2;
isinclude3 = zwn_common_data.single.zwn_transform_pose_twentynine[17].y > y1;
isinclude4 = zwn_common_data.single.zwn_transform_pose_twentynine[5].x < x2;
isinclude5 = zwn_common_data.single.zwn_transform_pose_twentynine[5].y < y2;
isinclude6 = zwn_common_data.single.zwn_transform_pose_twentynine[18].x > x1;
isinclude7 = zwn_common_data.single.zwn_transform_pose_twentynine[18].y > y1;
}
if (isinclude && isinclude1 && isinclude2 && isinclude3 && isinclude4 && isinclude5 && isinclude6 && isinclude7)
{
isAlignmentSuccee = true;
}
else
{
isAlignmentSuccee = false;
}
}
else if (ZwnAmlogicHelperData.ZWN_POSE_KEY_POINTS_DIM_LEN == 17)
{
if (Mathf.Abs(zwn_common_data.single.zwn_transform_pose[5].x) != 1920 && Mathf.Abs(zwn_common_data.single.zwn_transform_pose[6].x) != 1920 && Mathf.Abs(zwn_common_data.single.zwn_transform_pose[11].x) != 1920 && Mathf.Abs(zwn_common_data.single.zwn_transform_pose[12].x) != 1920)
{
isinclude = zwn_common_data.single.zwn_transform_pose[6].x > x1;
isinclude1 = zwn_common_data.single.zwn_transform_pose[6].y < y2;
isinclude2 = zwn_common_data.single.zwn_transform_pose[11].x < x2;
isinclude3 = zwn_common_data.single.zwn_transform_pose[11].y > y1;
isinclude4 = zwn_common_data.single.zwn_transform_pose[5].x < x2;
isinclude5 = zwn_common_data.single.zwn_transform_pose[5].y < y2;
isinclude6 = zwn_common_data.single.zwn_transform_pose[12].x > x1;
isinclude7 = zwn_common_data.single.zwn_transform_pose[12].y > y1;
}
if (isinclude && isinclude1 && isinclude2 && isinclude3 && isinclude4 && isinclude5 && isinclude6 && isinclude7)
{
isAlignmentSuccee = true;
}
else
{
isAlignmentSuccee = false;
}
}
}
}