| 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: <timestamp>_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("<q", trajectory_buffer[:8])[0] | |
| pos_x, pos_y, pos_z, quat_x, quat_y, quat_z, quat_w = struct.unpack("<7f", trajectory_buffer[8:]) | |
| pos = np.array([pos_x, pos_y, pos_z], dtype=np.float32) | |
| quat = np.array([quat_x, quat_y, quat_z, quat_w], dtype=np.float32) | |
| trajectory.append((timestamp, pos, quat)) | |
| return trajectory | |
| def load_body_data(body_data_path: Path) -> 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("<qi", header) | |
| keypoints = _read_keypoints(f, keypoint_count)[:70] | |
| body_frames.append( | |
| ( | |
| timestamp, | |
| keypoints | |
| ) | |
| ) | |
| return body_frames | |
| def load_hand_data(hand_data_path: Path) -> 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("<q", timestamp_buffer)[0] | |
| left_count_buffer = f.read(left_count_buffer_size) | |
| if len(left_count_buffer) < left_count_buffer_size: | |
| break | |
| left_count = struct.unpack("<i", left_count_buffer)[0] | |
| left_keypoints = _read_keypoints(f, left_count) | |
| right_count_buffer = f.read(right_count_buffer_size) | |
| if len(right_count_buffer) < right_count_buffer_size: | |
| break | |
| right_count = struct.unpack("<i", right_count_buffer)[0] | |
| right_keypoints = _read_keypoints(f, right_count) | |
| hand_frames.append( | |
| ( | |
| timestamp, | |
| left_keypoints if left_keypoints else None, | |
| right_keypoints if right_keypoints else None, | |
| ) | |
| ) | |
| return hand_frames | |
| def _read_keypoints(f: Any, keypoint_count: int) -> 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 | |