FollowBoxManager.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. /// <summary>
  2. /// ZWN
  3. /// 20220104
  4. /// </summary>
  5. using System.Collections;
  6. using System.Collections.Generic;
  7. using UnityEngine;
  8. using UnityEngine.UI;
  9. //using zwn;
  10. /// <summary>
  11. /// 显示对齐框的框工具类接口
  12. /// </summary>
  13. public class FollowBoxManager : MonoBehaviour
  14. {
  15. public static FollowBoxManager sigle;
  16. float[] pose_kpts = new float[4 * 2];//4个点的坐标
  17. int pose_kpts_count = 4;//数量
  18. public GameObject show_rect_perfab;//克隆物体
  19. public Transform show_rect_tra;//克隆物体父级
  20. [HideInInspector]
  21. public List<GameObject> show_rect_list;//克隆出来的物体
  22. //固定框
  23. public int x; ///< 左上角顶点 X 轴坐标
  24. public int y; ///< 左上角顶点 Y 轴坐标
  25. public int width; ///< 矩形的宽度
  26. public int height; ///< 矩形的高度
  27. public bool isShowFollowBox = false;
  28. public static float followRatio = 0f;
  29. // dataType.zwn_rect_t_fixedframe aligned_Rect = new dataType.zwn_rect_t_fixedframe();
  30. private bool isAlignmentSuccee;
  31. public bool GetIsSuccee
  32. {
  33. get { return isAlignmentSuccee; }
  34. set { isAlignmentSuccee = value; }
  35. }
  36. void Awake()
  37. {
  38. sigle = this;
  39. }
  40. // Use this for initialization
  41. void Start()
  42. {
  43. //aligned_Rect.x = x;
  44. //aligned_Rect.y = y;
  45. //aligned_Rect.width = width;
  46. //aligned_Rect.height = height;
  47. GetCurrFollow();
  48. }
  49. // Update is called once per frame
  50. void Update()
  51. {
  52. ShowFollowBox();
  53. GetRectRatio();
  54. GetIsinclude();
  55. }
  56. /// <summary>
  57. ///计算关键点的矩形框
  58. /// </summary>
  59. void ShowFollowBox()
  60. {
  61. if (!ZwnAmlogicHelperData.isSomeone)
  62. {
  63. return;
  64. }
  65. if (ZwnAmlogicHelperData.ZWN_POSE_KEY_POINTS_DIM_LEN == 29)
  66. {
  67. 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)
  68. {
  69. pose_kpts[0] = zwn_common_data.single.zwn_original_pose_twentynine[5].x;
  70. pose_kpts[1] = zwn_common_data.single.zwn_original_pose_twentynine[5].y;
  71. pose_kpts[2] = zwn_common_data.single.zwn_original_pose_twentynine[6].x;
  72. pose_kpts[3] = zwn_common_data.single.zwn_original_pose_twentynine[6].y;
  73. pose_kpts[4] = zwn_common_data.single.zwn_original_pose_twentynine[17].x;
  74. pose_kpts[5] = zwn_common_data.single.zwn_original_pose_twentynine[17].y;
  75. pose_kpts[6] = zwn_common_data.single.zwn_original_pose_twentynine[18].x;
  76. pose_kpts[7] = zwn_common_data.single.zwn_original_pose_twentynine[18].y;
  77. }
  78. //utilCAPI.zwn_get_bounding_rect(pose_kpts, pose_kpts_count, ref UtilHelper.zwn_rect_t_alignments);
  79. float x = (zwn_common_data.single.zwn_transform_pose_twentynine[5].x + zwn_common_data.single.zwn_transform_pose_twentynine[6].x) / 2;
  80. float y = (zwn_common_data.single.zwn_transform_pose_twentynine[5].y + zwn_common_data.single.zwn_transform_pose_twentynine[17].y) / 2;
  81. float height = Vector3.Distance(zwn_common_data.single.zwn_transform_pose_twentynine[5], zwn_common_data.single.zwn_transform_pose_twentynine[17]);
  82. float width = Vector3.Distance(zwn_common_data.single.zwn_transform_pose_twentynine[5], zwn_common_data.single.zwn_transform_pose_twentynine[6]);
  83. Vector2 rect_weightheight = new Vector2(width, height);
  84. Vector3 rect_xy = new Vector3(x, y, 0);
  85. if (isShowFollowBox)
  86. {
  87. for (int i = 0; i < 1; i++)
  88. {
  89. //关于人形框
  90. if (i >= show_rect_list.Count)
  91. {
  92. GameObject go = Instantiate(show_rect_perfab);
  93. show_rect_list.Add(go);
  94. go.transform.SetParent(show_rect_tra);
  95. go.transform.localScale = new Vector3(1, 1, 1);
  96. }
  97. show_rect_list[i].transform.localPosition = rect_xy;
  98. show_rect_list[i].GetComponent<RectTransform>().sizeDelta = rect_weightheight;
  99. }
  100. }
  101. }
  102. else if (ZwnAmlogicHelperData.ZWN_POSE_KEY_POINTS_DIM_LEN == 17)
  103. {
  104. 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)
  105. {
  106. pose_kpts[0] = zwn_common_data.single.zwn_original_pose[5].x;
  107. pose_kpts[1] = zwn_common_data.single.zwn_original_pose[5].y;
  108. pose_kpts[2] = zwn_common_data.single.zwn_original_pose[6].x;
  109. pose_kpts[3] = zwn_common_data.single.zwn_original_pose[6].y;
  110. pose_kpts[4] = zwn_common_data.single.zwn_original_pose[11].x;
  111. pose_kpts[5] = zwn_common_data.single.zwn_original_pose[11].y;
  112. pose_kpts[6] = zwn_common_data.single.zwn_original_pose[12].x;
  113. pose_kpts[7] = zwn_common_data.single.zwn_original_pose[12].y;
  114. }
  115. //utilCAPI.zwn_get_bounding_rect(pose_kpts, pose_kpts_count, ref UtilHelper.zwn_rect_t_alignments);
  116. float x = (zwn_common_data.single.zwn_transform_pose[5].x + zwn_common_data.single.zwn_transform_pose[6].x) / 2;
  117. float y = (zwn_common_data.single.zwn_transform_pose[5].y + zwn_common_data.single.zwn_transform_pose[11].y) / 2;
  118. float height = Vector3.Distance(zwn_common_data.single.zwn_transform_pose[5], zwn_common_data.single.zwn_transform_pose[11]);
  119. float width = Vector3.Distance(zwn_common_data.single.zwn_transform_pose[5], zwn_common_data.single.zwn_transform_pose[6]);
  120. Vector2 rect_weightheight = new Vector2(width, height);
  121. Vector3 rect_xy = new Vector3(x, y, 0);
  122. if (isShowFollowBox)
  123. {
  124. for (int i = 0; i < 1; i++)
  125. {
  126. //关于人形框
  127. if (i >= show_rect_list.Count)
  128. {
  129. GameObject go = Instantiate(show_rect_perfab);
  130. show_rect_list.Add(go);
  131. go.transform.SetParent(show_rect_tra);
  132. go.transform.localScale = new Vector3(1, 1, 1);
  133. }
  134. show_rect_list[i].transform.localPosition = rect_xy;
  135. show_rect_list[i].GetComponent<RectTransform>().sizeDelta = rect_weightheight;
  136. }
  137. }
  138. }
  139. }
  140. /// <summary>
  141. /// 当计算两个框的IOU分数小于thres阈值时,ratio 返回的是IOU的分数。(0.5-2)
  142. /// </summary>
  143. void GetRectRatio()
  144. {
  145. //utilCAPI.zwn_get_rect_ratio(UtilHelper.zwn_rect_t_alignments, aligned_Rect, 0.1f, ref followRatio);
  146. //print("followRatio " + followRatio);
  147. }
  148. /// <summary>
  149. /// 坐标转换
  150. /// </summary>
  151. void GetCurrFollow()
  152. {
  153. int width = Screen.width;
  154. int height = Screen.height;
  155. //aligned_Rect.x = width / 2 + aligned_Rect.x;
  156. //aligned_Rect.y = height / 2 - aligned_Rect.y;
  157. }
  158. /// <summary>
  159. /// 判断身体框是否在对齐框内
  160. /// </summary>
  161. public void GetIsinclude()
  162. {
  163. float x1 = -(width / 2);
  164. float x2 = width / 2;
  165. float y1 = -(height / 2);
  166. float y2 = height / 2;
  167. bool isinclude = false;
  168. bool isinclude1 = false;
  169. bool isinclude2 = false;
  170. bool isinclude3 = false;
  171. bool isinclude4 = false;
  172. bool isinclude5 = false;
  173. bool isinclude6 = false;
  174. bool isinclude7 = false;
  175. if (!ZwnAmlogicHelperData.isSomeone)
  176. {
  177. return;
  178. }
  179. if (ZwnAmlogicHelperData.ZWN_POSE_KEY_POINTS_DIM_LEN == 29)
  180. {
  181. 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)
  182. {
  183. isinclude = zwn_common_data.single.zwn_transform_pose_twentynine[6].x > x1;
  184. isinclude1 = zwn_common_data.single.zwn_transform_pose_twentynine[6].y < y2;
  185. isinclude2 = zwn_common_data.single.zwn_transform_pose_twentynine[17].x < x2;
  186. isinclude3 = zwn_common_data.single.zwn_transform_pose_twentynine[17].y > y1;
  187. isinclude4 = zwn_common_data.single.zwn_transform_pose_twentynine[5].x < x2;
  188. isinclude5 = zwn_common_data.single.zwn_transform_pose_twentynine[5].y < y2;
  189. isinclude6 = zwn_common_data.single.zwn_transform_pose_twentynine[18].x > x1;
  190. isinclude7 = zwn_common_data.single.zwn_transform_pose_twentynine[18].y > y1;
  191. }
  192. if (isinclude && isinclude1 && isinclude2 && isinclude3 && isinclude4 && isinclude5 && isinclude6 && isinclude7)
  193. {
  194. isAlignmentSuccee = true;
  195. }
  196. else
  197. {
  198. isAlignmentSuccee = false;
  199. }
  200. }
  201. else if (ZwnAmlogicHelperData.ZWN_POSE_KEY_POINTS_DIM_LEN == 17)
  202. {
  203. 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)
  204. {
  205. isinclude = zwn_common_data.single.zwn_transform_pose[6].x > x1;
  206. isinclude1 = zwn_common_data.single.zwn_transform_pose[6].y < y2;
  207. isinclude2 = zwn_common_data.single.zwn_transform_pose[11].x < x2;
  208. isinclude3 = zwn_common_data.single.zwn_transform_pose[11].y > y1;
  209. isinclude4 = zwn_common_data.single.zwn_transform_pose[5].x < x2;
  210. isinclude5 = zwn_common_data.single.zwn_transform_pose[5].y < y2;
  211. isinclude6 = zwn_common_data.single.zwn_transform_pose[12].x > x1;
  212. isinclude7 = zwn_common_data.single.zwn_transform_pose[12].y > y1;
  213. }
  214. if (isinclude && isinclude1 && isinclude2 && isinclude3 && isinclude4 && isinclude5 && isinclude6 && isinclude7)
  215. {
  216. isAlignmentSuccee = true;
  217. }
  218. else
  219. {
  220. isAlignmentSuccee = false;
  221. }
  222. }
  223. }
  224. }