from pathlib import Path from typing import Any, List, Tuple import struct import numpy as np import cv2 from .types import Vector3, Quaternion, Keypoint def load_frames(frames_path: Path) -> List[Tuple[int, np.ndarray]]: """Load encoded frames from a SEA stereo .dat file.""" frames: List[Tuple[int, np.ndarray]] = [] header_size = 8 + 8 + 4 with open(frames_path, "rb") as f: while True: header = f.read(header_size) if len(header) < header_size: break frame_timestamp, frame_index, frame_size = struct.unpack(">qqi", header) frame_bytes = f.read(frame_size) if len(frame_bytes) != frame_size: raise EOFError("Unexpected EOF while reading frame data") frame_array = np.frombuffer(frame_bytes, dtype=np.uint8) frame = cv2.imdecode(frame_array, cv2.IMREAD_COLOR) if frame is not None: frames.append((frame_timestamp, frame)) return frames def load_depth(depth_dir: Path) -> list[tuple[int, np.ndarray]]: """Load depth from .npy files: _depth_meter.npy.""" depth_frames: list[tuple[int, np.ndarray]] = [] for path in sorted(depth_dir.glob("*_depth_meter.npy")): timestamp = int(path.stem.split("_")[0]) depth = np.load(path).astype(np.float32) depth_frames.append((timestamp, depth)) return depth_frames def load_trajectory(trajectory_path: Path) -> List[Tuple[int, np.ndarray, np.ndarray]]: """Load camera trajectory from SEA binary trajectory file.""" trajectory: List[Tuple[int, np.ndarray, np.ndarray]] = [] trajectory_buffer_size = 8 + 7 * 4 with open(trajectory_path, "rb") as f: while True: trajectory_buffer = f.read(trajectory_buffer_size) if len(trajectory_buffer) < trajectory_buffer_size: break timestamp = struct.unpack(" List[Any]: """Load body tracking data from SEA binary body_data file.""" body_frames: List[Any] = [] header_size = 8 + 4 with open(body_data_path, "rb") as f: while True: header = f.read(header_size) if len(header) < header_size: break timestamp, keypoint_count = struct.unpack(" List[Any]: """Load hand tracking data from SEA binary hand_data file.""" hand_frames: List[Any] = [] timestamp_buffer_size = 8 left_count_buffer_size = 4 right_count_buffer_size = 4 with open(hand_data_path, "rb") as f: while True: timestamp_buffer = f.read(timestamp_buffer_size) if len(timestamp_buffer) < timestamp_buffer_size: break timestamp = struct.unpack(" List[Keypoint]: if keypoint_count <= 0: return [] floats_per_keypoint = 7 total_float_count = keypoint_count * floats_per_keypoint bytes_per_float = 4 expected_byte_count = total_float_count * bytes_per_float raw_bytes = f.read(expected_byte_count) if len(raw_bytes) != expected_byte_count: raise EOFError("Unexpected EOF while reading keypoint data") float_values = struct.unpack("<" + "f" * total_float_count, raw_bytes) keypoints: List[Keypoint] = [] for keypoint_index in range(keypoint_count): base_index = keypoint_index * floats_per_keypoint pos_x, pos_y, pos_z, quat_x, quat_y, quat_z, quat_w = float_values[ base_index : base_index + floats_per_keypoint ] keypoints.append( Keypoint( position=Vector3(pos_x, pos_y, pos_z), rotation=Quaternion(quat_x, quat_y, quat_z, quat_w), ) ) return keypoints