Load tracking data and metadata from a file or directory.
(path: Path)
| 124 | # ============================================================================ |
| 125 | |
| 126 | def load_tracking_data(path: Path) -> tuple[list[dict], dict | None]: |
| 127 | """Load tracking data and metadata from a file or directory.""" |
| 128 | |
| 129 | # If path is a directory, look for tracking.jsonl |
| 130 | if path.is_dir(): |
| 131 | tracking_file = path / "tracking.jsonl" |
| 132 | metadata_file = path / "metadata.json" |
| 133 | else: |
| 134 | tracking_file = path |
| 135 | metadata_file = path.parent / "metadata.json" |
| 136 | |
| 137 | if not tracking_file.exists(): |
| 138 | raise FileNotFoundError(f"Tracking file not found: {tracking_file}") |
| 139 | |
| 140 | # Load tracking frames |
| 141 | frames = [] |
| 142 | print(f"Loading tracking data from: {tracking_file}") |
| 143 | with open(tracking_file, "r") as f: |
| 144 | for i, line in enumerate(f): |
| 145 | line = line.strip() |
| 146 | if line: |
| 147 | try: |
| 148 | frames.append(json.loads(line)) |
| 149 | except json.JSONDecodeError as e: |
| 150 | print(f"Warning: Failed to parse line {i+1}: {e}") |
| 151 | if i % 1000 == 0 and i > 0: |
| 152 | print(f" Loaded {i} frames...") |
| 153 | |
| 154 | print(f"Loaded {len(frames)} frames total") |
| 155 | |
| 156 | # Load metadata if available |
| 157 | metadata = None |
| 158 | if metadata_file.exists(): |
| 159 | with open(metadata_file, "r") as f: |
| 160 | metadata = json.load(f) |
| 161 | print(f"Loaded metadata: duration={metadata.get('duration', 'N/A'):.1f}s") |
| 162 | |
| 163 | return frames, metadata |
| 164 | |
| 165 | |
| 166 | # ============================================================================ |