skeleton.py 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. # Copyright (c) 2018-present, Facebook, Inc.
  2. # All rights reserved.
  3. #
  4. # This source code is licensed under the license found in the
  5. # LICENSE file in the root directory of this source tree.
  6. #
  7. import numpy as np
  8. class Skeleton:
  9. def __init__(self, parents, joints_left, joints_right):
  10. assert len(joints_left) == len(joints_right)
  11. self._parents = np.array(parents)
  12. self._joints_left = joints_left
  13. self._joints_right = joints_right
  14. self._compute_metadata()
  15. def num_joints(self):
  16. return len(self._parents)
  17. def parents(self):
  18. return self._parents
  19. def has_children(self):
  20. return self._has_children
  21. def children(self):
  22. return self._children
  23. def remove_joints(self, joints_to_remove):
  24. """
  25. Remove the joints specified in 'joints_to_remove'.
  26. """
  27. valid_joints = []
  28. for joint in range(len(self._parents)):
  29. if joint not in joints_to_remove:
  30. valid_joints.append(joint)
  31. for i in range(len(self._parents)):
  32. while self._parents[i] in joints_to_remove:
  33. self._parents[i] = self._parents[self._parents[i]]
  34. index_offsets = np.zeros(len(self._parents), dtype=int)
  35. new_parents = []
  36. for i, parent in enumerate(self._parents):
  37. if i not in joints_to_remove:
  38. new_parents.append(parent - index_offsets[parent])
  39. else:
  40. index_offsets[i:] += 1
  41. self._parents = np.array(new_parents)
  42. if self._joints_left is not None:
  43. new_joints_left = []
  44. for joint in self._joints_left:
  45. if joint in valid_joints:
  46. new_joints_left.append(joint - index_offsets[joint])
  47. self._joints_left = new_joints_left
  48. if self._joints_right is not None:
  49. new_joints_right = []
  50. for joint in self._joints_right:
  51. if joint in valid_joints:
  52. new_joints_right.append(joint - index_offsets[joint])
  53. self._joints_right = new_joints_right
  54. self._compute_metadata()
  55. return valid_joints
  56. def joints_left(self):
  57. return self._joints_left
  58. def joints_right(self):
  59. return self._joints_right
  60. def _compute_metadata(self):
  61. self._has_children = np.zeros(len(self._parents)).astype(bool)
  62. for i, parent in enumerate(self._parents):
  63. if parent != -1:
  64. self._has_children[parent] = True
  65. self._children = []
  66. for i, parent in enumerate(self._parents):
  67. self._children.append([])
  68. for i, parent in enumerate(self._parents):
  69. if parent != -1:
  70. self._children[parent].append(i)