Spaces:
Running on T4
Running on T4
Download baseball_batting/coach_geometry.py from modventures/baseball-engine: direct link, hf CLI and curl.
- Browser
- Download file 2.21 kB
-
https://huggingface.co/spaces/modventures/baseball-engine/resolve/main/baseball_batting/coach_geometry.py
- Command line
-
hf download hf://spaces/modventures/baseball-engine/baseball_batting/coach_geometry.py
-
curl -L -o coach_geometry.py https://huggingface.co/spaces/modventures/baseball-engine/resolve/main/baseball_batting/coach_geometry.py
2.21 kB
| """Contract 1.1 geometry shared by video export and reference-data calibration. | |
| Inputs are anatomical joint centres in any fixed orthonormal frame, in metres. | |
| These are kinematic descriptions, not measurements of force or energy transfer. | |
| """ | |
| import numpy as np | |
| EXTENDED_METRICS = ( | |
| 'torso_inclination_at_stride_start', | |
| 'hip_shoulder_separation_at_stride_start', | |
| 'pelvis_rotation_to_contact', | |
| 'lead_knee_extension_to_contact', | |
| 'torso_inclination_at_contact', | |
| 'shoulder_rotation_after_contact', | |
| ) | |
| FOLLOW_THROUGH_SECONDS = .1 | |
| def inclination(hips, shoulders, up): | |
| torso = np.asarray(shoulders) - np.asarray(hips) | |
| vertical = torso @ np.asarray(up) | |
| horizontal = torso - vertical[..., None] * np.asarray(up) | |
| angle = np.degrees(np.arctan2(np.linalg.norm(horizontal, axis=-1), vertical)) | |
| return np.where(vertical > .05, angle, np.nan) | |
| def knee_angle(hip, knee, ankle): | |
| a, b = np.asarray(hip) - knee, np.asarray(ankle) - knee | |
| denominator = np.linalg.norm(a, axis=-1) * np.linalg.norm(b, axis=-1) | |
| cosine = np.divide(np.sum(a * b, axis=-1), denominator, | |
| out=np.full_like(denominator, np.nan), where=denominator > 1e-8) | |
| return np.degrees(np.arccos(np.clip(cosine, -1, 1))) | |
| def rotation_amount(headings, start, end): | |
| """Magnitude of net unwrapped horizontal rotation; never a peak-speed proxy.""" | |
| if start is None or end is None or start >= end: | |
| return np.nan | |
| values = np.asarray(headings)[start:end + 1] | |
| if not np.isfinite(values).all(): | |
| return np.nan | |
| return float(abs(np.degrees(np.unwrap(np.radians(values))[-1] - np.radians(values[0])))) | |
| def follow_through_frame(times, contact, end): | |
| """Nearest original sample to contact + 100 ms; a complete interval is required.""" | |
| if contact is None: | |
| return None | |
| times = np.asarray(times, float) | |
| if not np.isfinite(times[contact:end + 1]).all() or np.any(np.diff(times[contact:end + 1]) <= 0): | |
| return None | |
| target = times[contact] + FOLLOW_THROUGH_SECONDS | |
| if times[end] < target: | |
| return None | |
| i = int(np.argmin(np.abs(times[contact:end + 1] - target))) + contact | |
| return i if i > contact else None | |