Upload src/data/skeleton_graph.py with huggingface_hub
Browse files- src/data/skeleton_graph.py +88 -25
src/data/skeleton_graph.py
CHANGED
|
@@ -101,46 +101,109 @@ class SkeletonGraph:
|
|
| 101 |
return dist.astype(np.float32)
|
| 102 |
|
| 103 |
def _infer_side_tags(self) -> list[str]:
|
| 104 |
-
"""Infer left/right/center from joint names.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 105 |
tags = []
|
| 106 |
-
left_keywords = ['left', 'l_', '_l', 'lft']
|
| 107 |
-
right_keywords = ['right', 'r_', '_r', 'rgt']
|
| 108 |
-
|
| 109 |
for name in self.joint_names:
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 117 |
|
| 118 |
return tags
|
| 119 |
|
| 120 |
def _find_symmetry_pairs(self) -> list[tuple[int, int]]:
|
| 121 |
-
"""Find symmetric joint pairs based on naming conventions.
|
|
|
|
|
|
|
|
|
|
|
|
|
| 122 |
pairs = []
|
| 123 |
used = set()
|
| 124 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 125 |
for i, name_i in enumerate(self.joint_names):
|
| 126 |
if i in used:
|
| 127 |
continue
|
| 128 |
-
name_lower = name_i.lower()
|
| 129 |
|
| 130 |
-
# Try to find mirror
|
| 131 |
mirror_name = None
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
mirror_name = name_lower.replace('_r_', '_l_')
|
| 140 |
-
|
| 141 |
-
if mirror_name:
|
| 142 |
for j, name_j in enumerate(self.joint_names):
|
| 143 |
-
if j != i and j not in used and name_j.lower() ==
|
| 144 |
pairs.append((i, j))
|
| 145 |
used.add(i)
|
| 146 |
used.add(j)
|
|
|
|
| 101 |
return dist.astype(np.float32)
|
| 102 |
|
| 103 |
def _infer_side_tags(self) -> list[str]:
|
| 104 |
+
"""Infer left/right/center from joint names.
|
| 105 |
+
|
| 106 |
+
Handles diverse naming conventions:
|
| 107 |
+
- 'left'/'right' anywhere: LeftArm, left_hip, RightFoot
|
| 108 |
+
- '_L'/'_R' suffix: Shoulder_L, UpperArm_R
|
| 109 |
+
- '_l_'/'_r_' infix: Bip01_L_Thigh, BN_Tai_R_01
|
| 110 |
+
- 'L'/'R' prefix before uppercase: LHipJoint, RThumb
|
| 111 |
+
- '_L'/'_R' suffix before numbers: Leg_L_30_
|
| 112 |
+
"""
|
| 113 |
+
import re
|
| 114 |
tags = []
|
|
|
|
|
|
|
|
|
|
| 115 |
for name in self.joint_names:
|
| 116 |
+
n = name # preserve case for regex
|
| 117 |
+
nl = name.lower()
|
| 118 |
+
|
| 119 |
+
side = 'center'
|
| 120 |
+
# Priority 1: explicit 'left'/'right'
|
| 121 |
+
if 'left' in nl or 'lft' in nl:
|
| 122 |
+
side = 'left'
|
| 123 |
+
elif 'right' in nl or 'rgt' in nl:
|
| 124 |
+
side = 'right'
|
| 125 |
+
# Priority 2: _L / _R suffix (with optional trailing digits/underscores)
|
| 126 |
+
elif re.search(r'[_]L(?:[_\d]|$)', n):
|
| 127 |
+
side = 'left'
|
| 128 |
+
elif re.search(r'[_]R(?:[_\d]|$)', n):
|
| 129 |
+
side = 'right'
|
| 130 |
+
# Priority 3: _l_ / _r_ infix
|
| 131 |
+
elif '_l_' in nl or nl.startswith('l_'):
|
| 132 |
+
side = 'left'
|
| 133 |
+
elif '_r_' in nl or nl.startswith('r_'):
|
| 134 |
+
side = 'right'
|
| 135 |
+
# Priority 4: L/R prefix before uppercase (LHipJoint, RThumb)
|
| 136 |
+
elif re.match(r'^L[A-Z]', n):
|
| 137 |
+
side = 'left'
|
| 138 |
+
elif re.match(r'^R[A-Z]', n):
|
| 139 |
+
side = 'right'
|
| 140 |
+
# Priority 5: prefix_L / prefix_R patterns
|
| 141 |
+
# NPC_LLeg1, NPC_RArm1, BN_LWing01, BN_RWing01, ArmL, ArmR, LegR, etc.
|
| 142 |
+
elif re.search(r'(?:NPC|BN|Bn)_L[A-Z]', n):
|
| 143 |
+
side = 'left'
|
| 144 |
+
elif re.search(r'(?:NPC|BN|Bn)_R[A-Z]', n):
|
| 145 |
+
side = 'right'
|
| 146 |
+
# Priority 6: {Word}L{Upper} or {Word}R{Upper} pattern
|
| 147 |
+
# Catches: ArmLClaw, ElkLFemur, ElkRScapula, LegR_01_, etc.
|
| 148 |
+
elif re.search(r'[a-z0-9]L[A-Z_]', n) or re.search(r'[a-z0-9]L$', n):
|
| 149 |
+
side = 'left'
|
| 150 |
+
elif re.search(r'[a-z0-9]R[A-Z_]', n) or re.search(r'[a-z0-9]R$', n):
|
| 151 |
+
side = 'right'
|
| 152 |
+
|
| 153 |
+
tags.append(side)
|
| 154 |
|
| 155 |
return tags
|
| 156 |
|
| 157 |
def _find_symmetry_pairs(self) -> list[tuple[int, int]]:
|
| 158 |
+
"""Find symmetric joint pairs based on naming conventions.
|
| 159 |
+
|
| 160 |
+
Handles: Left↔Right, _L↔_R, _l_↔_r_, L-prefix↔R-prefix.
|
| 161 |
+
"""
|
| 162 |
+
import re
|
| 163 |
pairs = []
|
| 164 |
used = set()
|
| 165 |
|
| 166 |
+
# Build replacement rules: (pattern, left_repl, right_repl)
|
| 167 |
+
swap_rules = [
|
| 168 |
+
# Full words
|
| 169 |
+
(r'(?i)left', 'left', 'right'),
|
| 170 |
+
(r'(?i)right', 'right', 'left'),
|
| 171 |
+
# _L / _R suffix (preserving case/context)
|
| 172 |
+
(r'_L(?=[_\d]|$)', '_L', '_R'),
|
| 173 |
+
(r'_R(?=[_\d]|$)', '_R', '_L'),
|
| 174 |
+
# _l_ / _r_ infix
|
| 175 |
+
(r'_l_', '_l_', '_r_'),
|
| 176 |
+
(r'_r_', '_r_', '_l_'),
|
| 177 |
+
# L/R prefix before uppercase
|
| 178 |
+
(r'^L(?=[A-Z])', 'L', 'R'),
|
| 179 |
+
(r'^R(?=[A-Z])', 'R', 'L'),
|
| 180 |
+
# BN_L/BN_R prefix (BN_LWing01 ↔ BN_RWing01)
|
| 181 |
+
(r'BN_L(?=[A-Z])', 'BN_L', 'BN_R'),
|
| 182 |
+
(r'BN_R(?=[A-Z])', 'BN_R', 'BN_L'),
|
| 183 |
+
(r'Bn_L(?=[A-Z])', 'Bn_L', 'Bn_R'),
|
| 184 |
+
(r'Bn_R(?=[A-Z])', 'Bn_R', 'Bn_L'),
|
| 185 |
+
# NPC_L/NPC_R prefix (NPC_LLeg1 ↔ NPC_RLeg1)
|
| 186 |
+
(r'NPC_L(?=[A-Z_])', 'NPC_L', 'NPC_R'),
|
| 187 |
+
(r'NPC_R(?=[A-Z_])', 'NPC_R', 'NPC_L'),
|
| 188 |
+
# {word}L{Upper} ↔ {word}R{Upper} (ElkLFemur↔ElkRFemur, ArmL↔ArmR)
|
| 189 |
+
(r'(?<=[a-z0-9])L(?=[A-Z_]|$)', 'L', 'R'),
|
| 190 |
+
(r'(?<=[a-z0-9])R(?=[A-Z_]|$)', 'R', 'L'),
|
| 191 |
+
]
|
| 192 |
+
|
| 193 |
for i, name_i in enumerate(self.joint_names):
|
| 194 |
if i in used:
|
| 195 |
continue
|
|
|
|
| 196 |
|
|
|
|
| 197 |
mirror_name = None
|
| 198 |
+
for pattern, from_str, to_str in swap_rules:
|
| 199 |
+
if re.search(pattern, name_i):
|
| 200 |
+
mirror_name = re.sub(pattern, to_str, name_i, count=1)
|
| 201 |
+
break
|
| 202 |
+
|
| 203 |
+
if mirror_name and mirror_name != name_i:
|
| 204 |
+
mirror_lower = mirror_name.lower()
|
|
|
|
|
|
|
|
|
|
| 205 |
for j, name_j in enumerate(self.joint_names):
|
| 206 |
+
if j != i and j not in used and name_j.lower() == mirror_lower:
|
| 207 |
pairs.append((i, j))
|
| 208 |
used.add(i)
|
| 209 |
used.add(j)
|