Spaces:
Running on T4
Running on T4
Download baseball_batting/motion_adapters.py from modventures/baseball-engine: direct link, hf CLI and curl.
- Browser
- Download file 4.07 kB
-
https://huggingface.co/spaces/modventures/baseball-engine/resolve/main/baseball_batting/motion_adapters.py
- Command line
-
hf download hf://spaces/modventures/baseball-engine/baseball_batting/motion_adapters.py
-
curl -L -o motion_adapters.py https://huggingface.co/spaces/modventures/baseball-engine/resolve/main/baseball_batting/motion_adapters.py
4.07 kB
| """Model-specific anatomy at the boundary of the shared motion exporter. | |
| The legacy calculator has a sparse 70-slot layout. It is an internal calculation | |
| layout, NOT a claim that SMPL-X is MHR70. Public joints always use canonical names | |
| and declare their actual source and measurement-definition revision. | |
| """ | |
| from pathlib import Path | |
| import numpy as np | |
| from .sam_motion import MHR, BONES | |
| from .storage import sha256_file | |
| GVHMR_REVISION = 'ee960bb6e2ea2d381aa97f08e9b71ef320b624b1' | |
| SMPLX_MAP = {'left_ear':59, 'right_ear':58, 'left_shoulder':16, 'right_shoulder':17, | |
| 'left_elbow':18, 'right_elbow':19, 'left_hip':1, 'right_hip':2, | |
| 'left_knee':4, 'right_knee':5, 'left_ankle':7, 'right_ankle':8, | |
| 'left_big_toe':60, 'left_small_toe':61, 'left_heel':62, | |
| 'right_big_toe':63, 'right_small_toe':64, 'right_heel':65, | |
| 'right_wrist':21, 'left_wrist':20} | |
| SURFACE_LANDMARKS = {name for name in MHR if any(x in name for x in ('ear','toe','heel'))} | |
| def descriptor(model='sam3d'): | |
| if model not in ('sam3d','gvhmr'): raise ValueError('Unknown reconstruction model') | |
| sam = model == 'sam3d' | |
| return {'id':model, 'name':'SAM 3D Body' if sam else 'GVHMR', | |
| 'adapter_version':'sam-mhr70-motion-v1' if sam else 'gvhmr-smplx-motion-v1', | |
| 'definition_version':'batting-corrected-points-v1' if sam else 'batting-corrected-points-smplx-v1', | |
| 'worker_file':'sam_pose_worker.py' if sam else 'gvhmr_pose_worker.py', | |
| 'raw_files':('sam-raw-private.npz','sam-raw-private.json','person-track-private.json') if sam else | |
| ('gvhmr-raw-private.npz','gvhmr-raw-private.json','person-track-private.json'), | |
| 'source_schema':'MHR70' if sam else 'SMPL-X', | |
| 'mapping':MHR if sam else SMPLX_MAP} | |
| def adapter_hash(model): | |
| item=descriptor(model) | |
| # Pin both the native extractor and anatomy adapter. SAM's existing hash | |
| # remains compatible with results written before the model selector existed. | |
| if model=='sam3d':return sha256_file(Path(__file__).with_name(item['worker_file'])) | |
| from .storage import digest | |
| return digest({name:sha256_file(Path(__file__).with_name(name)) for name in | |
| ('motion_adapters.py','gvhmr_pose_worker.py','gvhmr_adapter.py','gvhmr_worker.py')}) | |
| def skeleton(model): | |
| item=descriptor(model); sam=model=='sam3d'; names={v:k for k,v in MHR.items()} | |
| joints=[] | |
| for name in MHR: | |
| surface=not sam and name in SURFACE_LANDMARKS | |
| definition=('Native '+name.replace('_',' ')+' MHR70 landmark; no mesh-vertex or acromion substitution' if sam else | |
| 'SMPL-X named surface vertex selected by the official vertex-joint selector' if surface else | |
| 'SMPL-X regressed anatomical joint center; model-specific definition, not MHR70') | |
| joints.append({'name':name,'source_schema':item['source_schema'],'source_index':item['mapping'][name], | |
| 'definition':definition,'derived':surface, | |
| 'derivation':'official_smplx_vertex_joint_selector' if surface else None}) | |
| return {'version':f"{'mhr70' if sam else 'smplx'}-batting-{len(MHR)}-v1",'joint_order':list(MHR), | |
| 'joints':joints,'bones':[[names[a],names[b]] for a,b in BONES], | |
| 'head_center_proxy':'midpoint_of_bilateral_ear_landmarks', | |
| 'hand_motion_proxy':'midpoint_of_bilateral_wrist_landmarks', | |
| 'cross_model_comparability':'different native anatomical definitions; do not pool as identical measurements'} | |
| def smplx_to_calculation(joints, joint_names): | |
| """Validate the upstream model mapping before adapting any native prediction.""" | |
| values=np.asarray(joints,float) | |
| if values.ndim!=3 or values.shape[-1]!=3:raise ValueError('SMPL-X joints must be N x J x 3') | |
| result=np.full((len(values),70,3),np.nan) | |
| for name,index in SMPLX_MAP.items(): | |
| if index>=values.shape[1] or index>=len(joint_names) or joint_names[index]!=name: | |
| raise ValueError('Deployed SMPL-X anatomical mapping differs from the adapter') | |
| result[:,MHR[name]]=values[:,index] | |
| return result | |