Spaces:
Running on T4
Running on T4
Download baseball_batting/annotations.py from modventures/baseball-engine: direct link, hf CLI and curl.
- Browser
- Download file 3.21 kB
-
https://huggingface.co/spaces/modventures/baseball-engine/resolve/main/baseball_batting/annotations.py
- Command line
-
hf download hf://spaces/modventures/baseball-engine/baseball_batting/annotations.py
-
curl -L -o annotations.py https://huggingface.co/spaces/modventures/baseball-engine/resolve/main/baseball_batting/annotations.py
3.21 kB
| """Reviewed labels are immutable input records, separate from automatic observations.""" | |
| from pathlib import Path | |
| import json | |
| import math | |
| from .contracts import RTMW_CORE | |
| def load_annotations(path, input_hash, frame_count, width, height): | |
| if not path: | |
| return {"schema_version": "1.0", "frames": {}, "events": [], "annotator": None} | |
| data = json.loads(Path(path).read_text()) | |
| if data.get("schema_version") != "1.0" or data.get("input_sha256") != input_hash: | |
| raise ValueError("Annotations must identify this exact video and schema") | |
| if not data.get("annotator"): | |
| raise ValueError("Reviewed labels require an annotator ID") | |
| for raw_id, frame in data.get("frames", {}).items(): | |
| index = int(raw_id) | |
| if str(index) != raw_id or not 0 <= index < frame_count: | |
| raise ValueError("Invalid annotation frame ID") | |
| for name, p in frame.get("landmarks", {}).items(): | |
| if name not in RTMW_CORE: | |
| raise ValueError(f"Unknown RTMW landmark {name}") | |
| _point(p, width, height) | |
| if any(name not in RTMW_CORE for name in frame.get("occluded_landmarks", [])): | |
| raise ValueError("Unknown occluded RTMW landmark") | |
| for name in ("knob", "barrel", "ball"): | |
| if frame.get(name) is not None: | |
| _point(frame[name], width, height) | |
| if "crop" in frame: | |
| a, b, c, d = frame["crop"] | |
| if not 0 <= a < c <= width or not 0 <= b < d <= height: | |
| raise ValueError("Crop must lie in oriented image bounds") | |
| known_events = {"contact", "forward_swing_onset", "lead_foot_lift", "lead_foot_contact", "load", "follow_through", "miss", "dry_swing", "contact_unknown"} | |
| for event in data.get("events", []): | |
| if event.get("type") not in known_events or event.get("state") not in {"confirmed", "unknown", "absent"}: | |
| raise ValueError("Invalid reviewed event type/state") | |
| interval = event.get("frame_interval") | |
| if interval is not None and (len(interval) != 2 or any(not isinstance(f, int) for f in interval) | |
| or not 0 <= interval[0] <= interval[1] < frame_count): | |
| raise ValueError("Reviewed event interval is outside the recording") | |
| if event["state"] == "confirmed": | |
| if not interval or len(interval) != 2 or not 0 <= interval[0] <= interval[1] < frame_count: | |
| raise ValueError("Confirmed event needs an in-range frame interval") | |
| if not event.get("evidence_frames") or not event.get("evidence_description"): | |
| raise ValueError("Confirmed events need observable evidence and a description") | |
| for f in event.get("evidence_frames", []): | |
| if not isinstance(f, int) or not 0 <= f < frame_count: | |
| raise ValueError("Invalid event evidence frame") | |
| return data | |
| def _point(p, width, height): | |
| if len(p) != 2 or not all(isinstance(v, (int, float)) and math.isfinite(v) for v in p): | |
| raise ValueError("Annotation points must be finite x,y pairs") | |
| if not 0 <= p[0] < width or not 0 <= p[1] < height: | |
| raise ValueError("Annotation lies outside oriented frame") | |