| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| from typing import Dict, List, Optional |
|
|
| import matplotlib.pyplot as plt |
| import numpy as np |
| import rerun as rr |
| from data_loaders.hand_common import LANDMARK_CONNECTIVITY |
| from data_loaders.headsets import Headset |
| from data_loaders.loader_hand_poses import HandType |
| from data_loaders.loader_object_library import ObjectLibrary |
| from projectaria_tools.core.stream_id import StreamId |
|
|
| try: |
| from dataset_api import Hot3dDataProvider |
| except ImportError: |
| from hot3d.dataset_api import Hot3dDataProvider |
|
|
| from data_loaders.HandDataProviderBase import ( |
| HandDataProviderBase, |
| HandPose3dCollectionWithDt, |
| ) |
| from data_loaders.ObjectBox2dDataProvider import ( |
| ObjectBox2dCollectionWithDt, |
| ObjectBox2dProvider, |
| ) |
| from data_loaders.ObjectPose3dProvider import ( |
| ObjectPose3dCollectionWithDt, |
| ObjectPose3dProvider, |
| ) |
| from projectaria_tools.core.calibration import ( |
| CameraCalibration, |
| DeviceCalibration, |
| FISHEYE624, |
| LINEAR, |
| ) |
| from projectaria_tools.core.mps import get_eyegaze_point_at_depth |
| from projectaria_tools.core.mps.utils import ( |
| filter_points_from_confidence, |
| filter_points_from_count, |
| ) |
| from projectaria_tools.core.sensor_data import TimeDomain, TimeQueryOptions |
| from projectaria_tools.core.sophus import SE3 |
| from projectaria_tools.utils.rerun_helpers import ( |
| AriaGlassesOutline, |
| ToTransform3D, |
| ) |
|
|
|
|
| class Hot3DVisualizer: |
| def __init__( |
| self, |
| hot3d_data_provider: Hot3dDataProvider, |
| hand_type: HandType = HandType.Umetrack, |
| ) -> None: |
| self._hot3d_data_provider = hot3d_data_provider |
| |
| self._device_data_provider = hot3d_data_provider.device_data_provider |
| |
| self._device_pose_provider = hot3d_data_provider.device_pose_data_provider |
| self._hand_data_provider = ( |
| hot3d_data_provider.umetrack_hand_data_provider |
| if hand_type == HandType.Umetrack |
| else hot3d_data_provider.mano_hand_data_provider |
| ) |
| if hand_type is HandType.Umetrack: |
| print("Hot3DVisualizer is using UMETRACK hand model") |
| elif hand_type is HandType.Mano: |
| print("Hot3DVisualizer is using MANO hand model") |
| self._object_pose_data_provider = hot3d_data_provider.object_pose_data_provider |
| self._object_box2d_data_provider = ( |
| hot3d_data_provider.object_box2d_data_provider |
| ) |
| |
| self._object_library = hot3d_data_provider.object_library |
|
|
| |
| |
| |
| self._object_box2d_colors = None |
| if self._object_box2d_data_provider is not None: |
| color_map = plt.get_cmap("viridis") |
| self._object_box2d_colors = color_map( |
| np.linspace(0, 1, len(self._object_box2d_data_provider.object_uids)) |
| ) |
|
|
| |
| self._object_cache_status = {} |
|
|
| |
| self._jpeg_quality = 75 |
|
|
| def log_static_assets( |
| self, |
| image_stream_ids: List[StreamId], |
| ) -> None: |
| """ |
| Log all static assets (aka Timeless assets) |
| - assets that are immutable (but can still move if attached to a 3D Pose) |
| """ |
|
|
| |
| if self._hot3d_data_provider.get_device_type() is Headset.Aria: |
| rr.log("world", rr.ViewCoordinates.RIGHT_HAND_Z_UP, static=True) |
| else: |
| rr.log("world", rr.ViewCoordinates.RIGHT_HAND_Y_UP, static=True) |
|
|
| if self._hot3d_data_provider.get_device_type() is Headset.Aria: |
| |
| pass |
| elif self._hot3d_data_provider.get_device_type() is Headset.Quest3: |
| |
| for stream_id in image_stream_ids: |
| |
| |
| [extrinsics, intrinsics] = ( |
| self._device_data_provider.get_camera_calibration(stream_id) |
| ) |
| Hot3DVisualizer.log_pose( |
| f"world/device/{stream_id}", extrinsics, static=True |
| ) |
| Hot3DVisualizer.log_calibration(f"world/device/{stream_id}", intrinsics) |
|
|
| |
| |
| |
| if self._hot3d_data_provider.get_device_type() is Headset.Aria: |
| Hot3DVisualizer.log_aria_glasses( |
| "world/device/glasses_outline", |
| self._device_data_provider.get_device_calibration(), |
| ) |
|
|
| |
| point_cloud = self._device_data_provider.get_point_cloud() |
| if point_cloud: |
| |
| threshold_invdep = 5e-4 |
| threshold_dep = 5e-4 |
| point_cloud = filter_points_from_confidence( |
| point_cloud, threshold_invdep, threshold_dep |
| ) |
| |
| points_data_down_sampled = filter_points_from_count( |
| point_cloud, 500_000 |
| ) |
| |
| point_positions = [it.position_world for it in points_data_down_sampled] |
| POINT_COLOR = [200, 200, 200] |
| rr.log( |
| "world/points", |
| rr.Points3D(point_positions, colors=POINT_COLOR, radii=0.002), |
| static=True, |
| ) |
|
|
| def log_dynamic_assets( |
| self, |
| stream_ids: List[StreamId], |
| timestamp_ns: int, |
| ) -> None: |
| """ |
| Log dynamic assets: |
| I.e assets that are moving, such as: |
| - 3D assets |
| - Device pose |
| - Hands |
| - Object poses |
| - Image related specifics assets |
| - images (stream_ids) |
| - Object Bounding boxes |
| - Aria Eye Gaze |
| """ |
|
|
| |
| |
| |
| acceptable_time_delta = 0 |
|
|
| if self._hot3d_data_provider.get_device_type() is Headset.Aria: |
| |
| for stream_id in stream_ids: |
| |
| |
| [extrinsics, intrinsics] = ( |
| self._device_data_provider.get_online_camera_calibration( |
| stream_id=stream_id, timestamp_ns=timestamp_ns |
| ) |
| ) |
| Hot3DVisualizer.log_pose(f"world/device/{stream_id}", extrinsics) |
| Hot3DVisualizer.log_calibration(f"world/device/{stream_id}", intrinsics) |
|
|
| elif self._hot3d_data_provider.get_device_type() is Headset.Quest3: |
| |
| pass |
|
|
| headset_pose3d_with_dt = None |
| if self._device_data_provider is not None: |
| headset_pose3d_with_dt = self._device_pose_provider.get_pose_at_timestamp( |
| timestamp_ns=timestamp_ns, |
| time_query_options=TimeQueryOptions.CLOSEST, |
| time_domain=TimeDomain.TIME_CODE, |
| acceptable_time_delta=acceptable_time_delta, |
| ) |
|
|
| hand_poses_with_dt = None |
| if self._hand_data_provider is not None: |
| hand_poses_with_dt = self._hand_data_provider.get_pose_at_timestamp( |
| timestamp_ns=timestamp_ns, |
| time_query_options=TimeQueryOptions.CLOSEST, |
| time_domain=TimeDomain.TIME_CODE, |
| acceptable_time_delta=acceptable_time_delta, |
| ) |
|
|
| object_poses_with_dt = None |
| if self._object_pose_data_provider is not None: |
| object_poses_with_dt = ( |
| self._object_pose_data_provider.get_pose_at_timestamp( |
| timestamp_ns=timestamp_ns, |
| time_query_options=TimeQueryOptions.CLOSEST, |
| time_domain=TimeDomain.TIME_CODE, |
| acceptable_time_delta=acceptable_time_delta, |
| ) |
| ) |
|
|
| aria_eye_gaze_data = ( |
| self._device_data_provider.get_eye_gaze(timestamp_ns) |
| if self._hot3d_data_provider.get_device_type() is Headset.Aria |
| else None |
| ) |
|
|
| |
| |
| |
| if headset_pose3d_with_dt is not None: |
| headset_pose3d = headset_pose3d_with_dt.pose3d |
| Hot3DVisualizer.log_pose( |
| "world/device", headset_pose3d.T_world_device, static=False |
| ) |
|
|
| |
| |
| |
| Hot3DVisualizer.log_hands( |
| "world/hands", |
| self._hand_data_provider, |
| hand_poses_with_dt, |
| show_hand_mesh=True, |
| show_hand_vertices=False, |
| show_hand_landmarks=False, |
| ) |
|
|
| |
| |
| |
| Hot3DVisualizer.log_object_poses( |
| "world/objects", |
| object_poses_with_dt, |
| self._object_pose_data_provider, |
| self._object_library, |
| self._object_cache_status, |
| ) |
|
|
| |
| |
| |
| for stream_id in stream_ids: |
| |
| |
| |
|
|
| |
| image_data = self._device_data_provider.get_undistorted_image( |
| timestamp_ns, stream_id |
| ) |
| if image_data is not None: |
| rr.log( |
| f"world/device/{stream_id}", |
| rr.Image(image_data).compress(jpeg_quality=self._jpeg_quality), |
| ) |
|
|
| |
| image_data = self._device_data_provider.get_image(timestamp_ns, stream_id) |
| if image_data is not None: |
| rr.log( |
| f"world/device/{stream_id}_raw", |
| rr.Image(image_data).compress(jpeg_quality=self._jpeg_quality), |
| ) |
|
|
| if ( |
| self._object_box2d_data_provider is not None |
| and stream_id in self._object_box2d_data_provider.stream_ids |
| ): |
| box2d_collection_with_dt = ( |
| self._object_box2d_data_provider.get_bbox_at_timestamp( |
| stream_id=stream_id, |
| timestamp_ns=timestamp_ns, |
| time_query_options=TimeQueryOptions.CLOSEST, |
| time_domain=TimeDomain.TIME_CODE, |
| ) |
| ) |
| Hot3DVisualizer.log_object_bounding_boxes( |
| stream_id, |
| box2d_collection_with_dt, |
| self._object_box2d_data_provider, |
| self._object_library, |
| self._object_box2d_colors, |
| ) |
|
|
| |
| |
| |
| if self._hot3d_data_provider.get_device_type() is Headset.Aria: |
| |
| if stream_id != StreamId("214-1"): |
| continue |
|
|
| |
| camera_configurations = [FISHEYE624, LINEAR] |
| for camera_model in camera_configurations: |
| eye_gaze_reprojection_data = ( |
| self._device_data_provider.get_eye_gaze_in_camera( |
| stream_id, timestamp_ns, camera_model=camera_model |
| ) |
| ) |
| if ( |
| eye_gaze_reprojection_data is None |
| or not eye_gaze_reprojection_data.any() |
| ): |
| continue |
|
|
| label = ( |
| f"world/device/{stream_id}/eye-gaze_projection" |
| if camera_model == LINEAR |
| else f"world/device/{stream_id}_raw/eye-gaze_projection_raw" |
| ) |
| rr.log( |
| label, |
| rr.Points2D(eye_gaze_reprojection_data, radii=20), |
| |
| ) |
| |
| |
| |
|
|
| |
| if aria_eye_gaze_data is not None: |
| T_device_CPF = self._device_data_provider.get_device_calibration().get_transform_device_cpf() |
| |
| gaze_vector_in_cpf = get_eyegaze_point_at_depth( |
| aria_eye_gaze_data.yaw, aria_eye_gaze_data.pitch, depth_m=0.3 |
| ) |
| |
| rr.log( |
| "world/device/eye-gaze", |
| rr.Arrows3D( |
| origins=[T_device_CPF @ [0, 0, 0]], |
| vectors=[ |
| T_device_CPF @ gaze_vector_in_cpf - T_device_CPF @ [0, 0, 0] |
| ], |
| ), |
| ) |
|
|
| @staticmethod |
| def log_aria_glasses( |
| label: str, |
| device_calibration: DeviceCalibration, |
| use_cad_calibration: bool = True, |
| ) -> None: |
| |
| aria_glasses_point_outline = AriaGlassesOutline( |
| device_calibration, use_cad_calibration |
| ) |
| rr.log(label, rr.LineStrips3D([aria_glasses_point_outline]), static=True) |
|
|
| @staticmethod |
| def log_calibration( |
| label: str, |
| camera_calibration: CameraCalibration, |
| ) -> None: |
| rr.log( |
| label, |
| rr.Pinhole( |
| resolution=[ |
| camera_calibration.get_image_size()[0], |
| camera_calibration.get_image_size()[1], |
| ], |
| focal_length=float(camera_calibration.get_focal_lengths()[0]), |
| ), |
| static=True, |
| ) |
|
|
| @staticmethod |
| def log_pose(label: str, pose: SE3, static=False) -> None: |
| rr.log(label, ToTransform3D(pose, False), static=static) |
|
|
| @staticmethod |
| def log_hands( |
| label: str, |
| hand_data_provider: HandDataProviderBase, |
| hand_poses_with_dt: HandPose3dCollectionWithDt, |
| show_hand_mesh=True, |
| show_hand_vertices=True, |
| show_hand_landmarks=True, |
| ): |
| logged_right_hand_data = False |
| logged_left_hand_data = False |
| if hand_poses_with_dt is None: |
| return |
|
|
| hand_pose_collection = hand_poses_with_dt.pose3d_collection |
|
|
| for hand_pose_data in hand_pose_collection.poses.values(): |
| if hand_pose_data.is_left_hand(): |
| logged_left_hand_data = True |
| elif hand_pose_data.is_right_hand(): |
| logged_right_hand_data = True |
|
|
| handedness_label = hand_pose_data.handedness_label() |
|
|
| |
| if show_hand_landmarks: |
| hand_landmarks = hand_data_provider.get_hand_landmarks(hand_pose_data) |
| |
| |
| points = [ |
| connections |
| for connectivity in LANDMARK_CONNECTIVITY |
| for connections in [ |
| [hand_landmarks[it].numpy().tolist() for it in connectivity] |
| ] |
| ] |
| rr.log( |
| f"{label}/{handedness_label}/joints", |
| rr.LineStrips3D(points, radii=0.002), |
| ) |
|
|
| |
| hand_mesh_vertices = ( |
| hand_data_provider.get_hand_mesh_vertices(hand_pose_data) |
| if show_hand_vertices or show_hand_mesh |
| else None |
| ) |
|
|
| |
| if show_hand_vertices: |
| rr.log( |
| f"{label}/{handedness_label}/mesh", |
| rr.Points3D(hand_mesh_vertices), |
| ) |
|
|
| |
| if show_hand_mesh: |
| [hand_triangles, hand_vertex_normals] = ( |
| hand_data_provider.get_hand_mesh_faces_and_normals(hand_pose_data) |
| ) |
| rr.log( |
| f"{label}/{handedness_label}/mesh_faces", |
| rr.Mesh3D( |
| vertex_positions=hand_mesh_vertices, |
| vertex_normals=hand_vertex_normals, |
| triangle_indices=hand_triangles, |
| ), |
| ) |
| |
| if logged_left_hand_data is False: |
| rr.log(f"{label}/left", rr.Clear.recursive()) |
| if logged_right_hand_data is False: |
| rr.log(f"{label}/right", rr.Clear.recursive()) |
|
|
| @staticmethod |
| def log_object_poses( |
| label: str, |
| object_poses_with_dt: ObjectPose3dCollectionWithDt, |
| object_pose_data_provider: ObjectPose3dProvider, |
| object_library: ObjectLibrary, |
| object_cache_status: Dict[int, bool], |
| ): |
| if object_poses_with_dt is None: |
| return |
|
|
| objects_pose3d_collection = object_poses_with_dt.pose3d_collection |
|
|
| |
| object_uids = object_pose_data_provider.object_uids_with_poses |
| logging_status = {x: False for x in object_uids} |
|
|
| for ( |
| object_uid, |
| object_pose3d, |
| ) in objects_pose3d_collection.poses.items(): |
| object_name = object_library.object_id_to_name_dict[object_uid] |
| object_name = object_name + "_" + str(object_uid) |
| object_cad_asset_filepath = ObjectLibrary.get_cad_asset_path( |
| object_library_folderpath=object_library.asset_folder_name, |
| object_id=object_uid, |
| ) |
|
|
| Hot3DVisualizer.log_pose( |
| f"world/objects/{object_name}", |
| object_pose3d.T_world_object, |
| False, |
| ) |
| |
| logging_status[object_uid] = True |
|
|
| |
| if object_uid not in object_cache_status.keys(): |
| object_cache_status[object_uid] = True |
| rr.log( |
| f"world/objects/{object_name}", |
| rr.Asset3D( |
| path=object_cad_asset_filepath, |
| ), |
| ) |
|
|
| |
| for object_uid, displayed in logging_status.items(): |
| if not displayed: |
| object_name = object_library.object_id_to_name_dict[object_uid] |
| object_name = object_name + "_" + str(object_uid) |
| rr.log( |
| f"world/objects/{object_name}", |
| rr.Clear.recursive(), |
| ) |
| if object_uid in object_cache_status.keys(): |
| del object_cache_status[object_uid] |
|
|
| @staticmethod |
| def log_object_bounding_boxes( |
| stream_id: StreamId, |
| box2d_collection_with_dt: Optional[ObjectBox2dCollectionWithDt], |
| object_box2d_data_provider: ObjectBox2dProvider, |
| object_library: ObjectLibrary, |
| bbox_colors: np.ndarray, |
| ): |
| """ |
| Object bounding boxes (valid for native raw images). |
| - We assume that the image corresponding to the stream_id has been logged beforehand as 'world/device/{stream_id}_raw/' |
| """ |
|
|
| |
| object_uids = list(object_box2d_data_provider.object_uids) |
| logging_status = {x: False for x in object_uids} |
|
|
| if ( |
| box2d_collection_with_dt is None |
| or box2d_collection_with_dt.box2d_collection is None |
| ): |
| |
| rr.log(f"world/device/{stream_id}_raw/bbox", rr.Clear.recursive()) |
| return |
|
|
| object_uids_at_query_timestamp = ( |
| box2d_collection_with_dt.box2d_collection.object_uid_list |
| ) |
|
|
| for object_uid in object_uids_at_query_timestamp: |
| object_name = object_library.object_id_to_name_dict[object_uid] |
| axis_aligned_box2d = box2d_collection_with_dt.box2d_collection.box2ds[ |
| object_uid |
| ] |
| box = axis_aligned_box2d.box2d |
| if box is None: |
| continue |
|
|
| logging_status[object_uid] = True |
| rr.log( |
| f"world/device/{stream_id}_raw/bbox/{object_name}", |
| rr.Boxes2D( |
| mins=[box.left, box.top], |
| sizes=[box.width, box.height], |
| colors=bbox_colors[object_uids.index(object_uid)], |
| ), |
| ) |
| |
| for key, value in logging_status.items(): |
| if not value: |
| object_name = object_library.object_id_to_name_dict[key] |
| rr.log( |
| f"world/device/{stream_id}_raw/bbox/{object_name}", |
| rr.Clear.flat(), |
| ) |
|
|