| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| from typing import Dict, List, Optional |
|
|
| import numpy as np |
| from data_loaders.frameset import compute_frameset_for_timestamp |
| from data_loaders.io_utils import load_json |
| from PIL import Image |
| from projectaria_tools.core.calibration import ( |
| CameraCalibration, |
| DeviceCadExtrinsics, |
| DeviceCalibration, |
| distort_by_calibration, |
| FISHEYE624, |
| get_linear_camera_calibration, |
| LINEAR, |
| ) |
| from projectaria_tools.core.sensor_data import TimeDomain |
| from projectaria_tools.core.sophus import SE3 |
| from projectaria_tools.core.stream_id import StreamId |
|
|
| try: |
| from pyvrs import ImageConversion, SyncVRSReader |
| except ImportError: |
| from pyvrs2 import SyncVRSReader |
| from vrsbindings import ImageConversion |
|
|
|
|
| class QuestDataProvider: |
| |
| |
| def __init__(self, vrs_filepath: str, device_calibration_filepath: str) -> None: |
| |
| self._vrs_reader = SyncVRSReader(vrs_filepath) |
| |
| |
| self._vrs_reader.set_image_conversion(ImageConversion.NORMALIZE) |
| |
| self._vrs_reader.set_stream_type_image_conversion( |
| 8010, ImageConversion.NORMALIZE_GREY8 |
| ) |
|
|
| |
| |
| image_stream_ids = [] |
| for stream_id in self._vrs_reader.stream_ids: |
| if self._vrs_reader.might_contain_images(stream_id): |
| image_stream_ids.append(stream_id) |
| image_stream_ids = sorted(image_stream_ids) |
| image_stream_ids = [] |
| for stream_id in self._vrs_reader.stream_ids: |
| if self._vrs_reader.might_contain_images(stream_id): |
| image_stream_ids.append(stream_id) |
| image_stream_ids = sorted(image_stream_ids) |
| |
| filtered_reader = self._vrs_reader.filtered_by_fields( |
| stream_ids=image_stream_ids |
| ) |
| self._vrs_reader = filtered_reader |
|
|
| |
| device_calibration_json = load_json(device_calibration_filepath) |
| camera_calibration = {} |
| for it in device_calibration_json: |
| quaternion = it["T_Device_Camera"]["quaternion_wxyz"] |
| translation = it["T_Device_Camera"]["translation_xyz"] |
| image_height = it["imageHeight"] |
| image_width = it["imageWidth"] |
| label = it["label"] |
| max_solid_angle = 1 |
| |
| projection_params = it["projectionParams"] |
| serial_number = it["serialNumber"] |
| |
| |
| T_world_device = SE3.from_quat_and_translation( |
| quaternion[0], |
| quaternion[1:4], |
| translation, |
| ) |
| |
| projection_params = projection_params[:1] + projection_params[2:] |
|
|
| |
| camera_calibration[label] = CameraCalibration( |
| label, |
| FISHEYE624, |
| projection_params, |
| T_world_device, |
| image_width, |
| image_height, |
| None, |
| max_solid_angle, |
| serial_number, |
| ) |
|
|
| self._device_calibration = DeviceCalibration( |
| camera_calibration, |
| {}, |
| {}, |
| {}, |
| {}, |
| DeviceCadExtrinsics(), |
| "", |
| "", |
| ) |
|
|
| |
| self._stream_timestamps_sorted: Dict[str, List[int]] = {} |
| for stream_id in self.get_image_stream_ids(): |
| self._stream_timestamps_sorted[str(stream_id)] = sorted( |
| self.get_sequence_timestamps() |
| ) |
|
|
| def get_device_calibration(self) -> DeviceCalibration: |
| """ |
| Return the device calibration (factory calibration of all sensors) |
| """ |
| return self._device_calibration |
| |
| def get_camera_calibration( |
| self, |
| stream_id: StreamId, |
| camera_model=FISHEYE624, |
| ) -> tuple[SE3, CameraCalibration]: |
| """ |
| Return the camera calibration of the device of the sequence as [Extrinsics, Intrinsics] |
| Note: |
| - A corresponding pinhole camera can be requested by using camera_model = LINEAR. |
| - This is the camera model used to generate the 'get_undistorted_image'. |
| """ |
| if not (camera_model is FISHEYE624 or camera_model is LINEAR): |
| raise ValueError( |
| "Invalid camera_model type, only FISHEYE624 and LINEAR are supported" |
| ) |
|
|
| device_calibration = self.get_device_calibration() |
| |
| stream_label = self.get_image_stream_label(stream_id) |
| stream_labels_str = [ |
| self.get_image_stream_label(x) for x in self.get_image_stream_ids() |
| ] |
| idx_stream = stream_labels_str.index(stream_label) |
| corresponding_calibration_label = device_calibration.get_camera_labels()[ |
| idx_stream |
| ] |
| camera_calibration = device_calibration.get_camera_calib( |
| corresponding_calibration_label |
| ) |
|
|
| |
| T_device_camera = camera_calibration.get_transform_device_camera() |
|
|
| |
| if camera_model == LINEAR: |
| focal_lengths = camera_calibration.get_focal_lengths() |
| image_size = camera_calibration.get_image_size() |
| camera_calibration = get_linear_camera_calibration( |
| image_size[0], image_size[1], focal_lengths[0] |
| ) |
| |
|
|
| return [T_device_camera, camera_calibration] |
|
|
| def get_image_stream_ids(self) -> List[StreamId]: |
| |
| image_stream_ids = [] |
| for stream_id in self._vrs_reader.stream_ids: |
| if self._vrs_reader.might_contain_images(stream_id): |
| image_stream_ids.append(stream_id) |
| image_stream_ids = sorted(image_stream_ids) |
| return [StreamId(x) for x in image_stream_ids] |
|
|
| def get_sequence_timestamps(self) -> List[int]: |
| """ |
| Returns the list of "time code" timestamp for the sequence |
| """ |
| timestamps = self._vrs_reader.get_timestamp_list() |
| |
| return sorted({int(x * 1e9) for x in timestamps}) |
|
|
| def get_frameset_from_timestamp( |
| self, |
| timestamp_ns: int, |
| frameset_acceptable_time_diff_ns: int, |
| time_domain: TimeDomain = TimeDomain.TIME_CODE, |
| ) -> Dict[str, Optional[int]]: |
| """ |
| Computes a frameset from a given timestamp within an acceptable time difference. |
| The frameset consists of the closest timestamps for each stream that are within the acceptable time difference. |
| For Quest3, the recommended acceptable time difference is 1e6 ns (or 1ms). |
| Returns a dictionary mapping each str(StreamId) to its closest timestamp. |
| """ |
| if time_domain is not TimeDomain.TIME_CODE: |
| raise ValueError( |
| f"{time_domain} is not supported. Only TIME_CODE is supported" |
| ) |
| out_frameset = compute_frameset_for_timestamp( |
| stream_timestamps_sorted=self._stream_timestamps_sorted, |
| target_timestamp=timestamp_ns, |
| frameset_acceptable_time_diff=frameset_acceptable_time_diff_ns, |
| ) |
| return out_frameset |
|
|
| def get_image_stream_label(self, stream_id: StreamId) -> str: |
| return str(stream_id) |
|
|
| def get_image(self, timestamp_ns: int, stream_id: StreamId) -> Optional[np.ndarray]: |
| try: |
| record = self._vrs_reader.read_record_by_time( |
| stream_id=self.get_image_stream_label(stream_id), |
| timestamp=timestamp_ns / 1e9, |
| ) |
| except ValueError as e: |
| print( |
| f"No record found for timestamp {timestamp_ns} and stream {stream_id}. Caught exception: {e}" |
| ) |
| record = None |
|
|
| if record is not None and record.record_type == "data": |
| grey8 = Image.fromarray(record.image_blocks[0]) |
| return np.array(grey8) |
| else: |
| print(f"No image found for timestamp {timestamp_ns} and stream {stream_id}") |
| return None |
|
|
| def get_undistorted_image( |
| self, timestamp_ns: int, stream_id: StreamId |
| ) -> Optional[np.ndarray]: |
| image = self.get_image(timestamp_ns, stream_id) |
| if image is None: |
| return None |
|
|
| [T_device_camera, native_camera_calibration] = self.get_camera_calibration( |
| stream_id, camera_model=FISHEYE624 |
| ) |
| [T_device_camera, pinhole_camera_calibration] = self.get_camera_calibration( |
| stream_id, camera_model=LINEAR |
| ) |
|
|
| |
| undistorted_image = distort_by_calibration( |
| image, pinhole_camera_calibration, native_camera_calibration |
| ) |
| return undistorted_image |
|
|