Spaces:
Running on T4
Running on T4
Download baseball_batting/motion.py from modventures/baseball-engine: direct link, hf CLI and curl.
- Browser
- Download file 2.69 kB
-
https://huggingface.co/spaces/modventures/baseball-engine/resolve/main/baseball_batting/motion.py
- Command line
-
hf download hf://spaces/modventures/baseball-engine/baseball_batting/motion.py
-
curl -L -o motion.py https://huggingface.co/spaces/modventures/baseball-engine/resolve/main/baseball_batting/motion.py
2.69 kB
| """Time-aware penalties and segment-frame measurements with explicit geometry.""" | |
| import numpy as np | |
| from scipy.spatial.transform import Rotation | |
| def contiguous_segments(valid, times, max_gap=None): | |
| valid, times = np.asarray(valid, bool), np.asarray(times, float) | |
| result, start = [], None | |
| for i in range(len(times)): | |
| supported = valid[i] and np.isfinite(times[i]) | |
| connected = i == 0 or (times[i] > times[i-1] and (max_gap is None or times[i]-times[i-1] <= max_gap)) | |
| if start is not None and (not supported or not connected): | |
| result.append((start, i)); start = None | |
| if supported and start is None: | |
| start = i | |
| if start is not None: | |
| result.append((start, len(times))) | |
| return result | |
| def angular_velocity(rotations, times, valid=None): | |
| """World angular-velocity vectors from rotation logarithms, never Euler derivatives.""" | |
| rotations, times = np.asarray(rotations), np.asarray(times, float) | |
| valid = np.isfinite(rotations).all(axis=(1, 2)) if valid is None else np.asarray(valid, bool) | |
| velocity = np.full((len(times), 3), np.nan) | |
| for a, b in contiguous_segments(valid, times): | |
| if b-a < 3: | |
| continue | |
| intervals = Rotation.from_matrix(rotations[a+1:b] @ rotations[a:b-1].transpose(0, 2, 1)).as_rotvec() | |
| rates = intervals / np.diff(times[a:b])[:, None] | |
| velocity[a], velocity[b-1] = rates[0], rates[-1] | |
| left, right = np.diff(times[a:b])[:-1], np.diff(times[a:b])[1:] | |
| velocity[a+1:b-1] = (rates[:-1]*right[:, None] + rates[1:]*left[:, None])/(left+right)[:, None] | |
| return velocity | |
| def heading(rotations, forward_axis=2): | |
| vector = np.asarray(rotations)[..., :, forward_axis] | |
| result = np.arctan2(vector[..., 2], vector[..., 0]) | |
| return np.where(np.linalg.norm(vector[..., [0, 2]], axis=-1) > 1e-8, result, np.nan) | |
| def wrapped_difference(a, b): | |
| return np.arctan2(np.sin(np.asarray(a)-b), np.cos(np.asarray(a)-b)) | |
| def sparse_temporal_pattern(frame_count, coordinate_count, marker_count): | |
| from scipy.sparse import lil_matrix | |
| rows = frame_count*marker_count*3 + max(frame_count-2, 0)*coordinate_count | |
| pattern = lil_matrix((rows, frame_count*coordinate_count), dtype=int) | |
| for frame in range(frame_count): | |
| pattern[frame*marker_count*3:(frame+1)*marker_count*3, | |
| frame*coordinate_count:(frame+1)*coordinate_count] = 1 | |
| offset = frame_count*marker_count*3 | |
| for frame in range(max(frame_count-2, 0)): | |
| pattern[offset+frame*coordinate_count:offset+(frame+1)*coordinate_count, | |
| frame*coordinate_count:(frame+3)*coordinate_count] = 1 | |
| return pattern.tocsr() | |