File size: 5,358 Bytes
386005a |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 |
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
|