Interactive 3D visualizer using Viser.
| 280 | |
| 281 | |
| 282 | class TrackingVisualizer: |
| 283 | """Interactive 3D visualizer using Viser.""" |
| 284 | |
| 285 | def __init__(self, frames: list[dict], metadata: dict | None = None, port: int = 8080, |
| 286 | video_path: Path | None = None): |
| 287 | self.frames = frames |
| 288 | self.metadata = metadata |
| 289 | self.port = port |
| 290 | self.video_path = video_path |
| 291 | |
| 292 | # Video capture |
| 293 | self.video_cap = None |
| 294 | self.video_frame_count = 0 |
| 295 | self.video_fps = 30.0 |
| 296 | if video_path and CV2_AVAILABLE and video_path.exists(): |
| 297 | self.video_cap = cv2.VideoCapture(str(video_path)) |
| 298 | self.video_frame_count = int(self.video_cap.get(cv2.CAP_PROP_FRAME_COUNT)) |
| 299 | self.video_fps = self.video_cap.get(cv2.CAP_PROP_FPS) or 30.0 |
| 300 | print(f"Loaded video: {video_path.name} ({self.video_frame_count} frames, {self.video_fps:.1f} fps)") |
| 301 | |
| 302 | # Parse extrinsic calibration |
| 303 | self.extrinsic = None |
| 304 | if metadata: |
| 305 | ext_data = metadata.get("extrinsicCalibration") |
| 306 | if ext_data: |
| 307 | if isinstance(ext_data, str): |
| 308 | self.extrinsic = json.loads(ext_data) |
| 309 | else: |
| 310 | self.extrinsic = ext_data |
| 311 | |
| 312 | # Playback state |
| 313 | self.state = PlaybackState() |
| 314 | self.state.current_frame = 0 |
| 315 | |
| 316 | # Pre-compute some analytics |
| 317 | self._precompute_analytics() |
| 318 | |
| 319 | # Initialize server |
| 320 | self.server: Optional[viser.ViserServer] = None |
| 321 | self._scene_handles = {} # Store scene node handles for updates |
| 322 | self._gui_handles = {} |
| 323 | |
| 324 | def _precompute_analytics(self): |
| 325 | """Pre-compute analytics like velocities, distances.""" |
| 326 | n = len(self.frames) |
| 327 | |
| 328 | self.timestamps = np.zeros(n) |
| 329 | self.left_wrist_pos = np.zeros((n, 3)) |
| 330 | self.right_wrist_pos = np.zeros((n, 3)) |
| 331 | self.head_pos = np.zeros((n, 3)) |
| 332 | self.left_pinch = np.zeros(n) |
| 333 | self.right_pinch = np.zeros(n) |
| 334 | self.has_left = np.zeros(n, dtype=bool) |
| 335 | self.has_right = np.zeros(n, dtype=bool) |
| 336 | self.has_head = np.zeros(n, dtype=bool) |
| 337 | |
| 338 | for i, frame in enumerate(self.frames): |
| 339 | self.timestamps[i] = frame.get("timestamp", i / 60.0) |