2024-08-21 17:11:56 +08:00
|
|
|
import os
|
|
|
|
import numpy as np
|
|
|
|
import json
|
|
|
|
import cv2
|
2024-08-29 13:54:13 -05:00
|
|
|
import trimesh
|
2024-08-21 17:11:56 +08:00
|
|
|
|
|
|
|
class DataLoadUtil:
|
|
|
|
|
|
|
|
@staticmethod
|
2024-08-29 13:54:13 -05:00
|
|
|
def get_path(root, scene_name, frame_idx):
|
|
|
|
path = os.path.join(root, scene_name, f"{frame_idx}")
|
2024-08-21 17:11:56 +08:00
|
|
|
return path
|
|
|
|
|
2024-08-22 20:27:21 +08:00
|
|
|
@staticmethod
|
2024-08-29 13:54:13 -05:00
|
|
|
def get_label_path(root, scene_name):
|
|
|
|
path = os.path.join(root,scene_name, f"label.json")
|
2024-08-22 20:27:21 +08:00
|
|
|
return path
|
|
|
|
|
|
|
|
@staticmethod
|
2024-08-30 16:49:21 +08:00
|
|
|
def get_sampled_model_points_path(root, scene_name):
|
|
|
|
path = os.path.join(root,scene_name, f"sampled_model_points.txt")
|
|
|
|
return path
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def get_scene_seq_length(root, scene_name):
|
|
|
|
camera_params_path = os.path.join(root, scene_name, "camera_params")
|
|
|
|
return len(os.listdir(camera_params_path))
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def load_downsampled_world_model_points(root, scene_name):
|
|
|
|
model_path = DataLoadUtil.get_sampled_model_points_path(root, scene_name)
|
|
|
|
model_points = np.loadtxt(model_path)
|
|
|
|
return model_points
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def save_downsampled_world_model_points(root, scene_name, model_points):
|
|
|
|
model_path = DataLoadUtil.get_sampled_model_points_path(root, scene_name)
|
|
|
|
np.savetxt(model_path, model_points)
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def load_original_model_points(model_dir, object_name):
|
|
|
|
model_path = os.path.join(model_dir, object_name, "mesh.obj")
|
2024-08-29 13:54:13 -05:00
|
|
|
mesh = trimesh.load(model_path)
|
|
|
|
return mesh.vertices
|
2024-08-30 16:49:21 +08:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def load_scene_info(root, scene_name):
|
|
|
|
scene_info_path = os.path.join(root, scene_name, "scene_info.json")
|
|
|
|
with open(scene_info_path, "r") as f:
|
|
|
|
scene_info = json.load(f)
|
|
|
|
return scene_info
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def load_target_object_pose(root, scene_name):
|
|
|
|
scene_info = DataLoadUtil.load_scene_info(root, scene_name)
|
|
|
|
target_name = scene_info["target_name"]
|
|
|
|
transformation = scene_info[target_name]
|
|
|
|
location = transformation["location"]
|
|
|
|
rotation_euler = transformation["rotation_euler"]
|
|
|
|
pose_mat = trimesh.transformations.euler_matrix(*rotation_euler)
|
|
|
|
pose_mat[:3, 3] = location
|
|
|
|
return pose_mat
|
|
|
|
|
2024-08-21 17:11:56 +08:00
|
|
|
@staticmethod
|
|
|
|
def load_depth(path):
|
2024-08-29 13:54:13 -05:00
|
|
|
depth_path = os.path.join(os.path.dirname(path), "depth", os.path.basename(path) + ".png")
|
|
|
|
depth = cv2.imread(depth_path, cv2.IMREAD_UNCHANGED)
|
|
|
|
depth = depth.astype(np.float32) / 65535.0
|
|
|
|
min_depth = 0.01
|
|
|
|
max_depth = 5.0
|
|
|
|
depth_meters = min_depth + (max_depth - min_depth) * depth
|
|
|
|
return depth_meters
|
2024-08-21 17:11:56 +08:00
|
|
|
|
2024-08-22 22:28:20 +08:00
|
|
|
@staticmethod
|
|
|
|
def load_label(path):
|
|
|
|
with open(path, 'r') as f:
|
|
|
|
label_data = json.load(f)
|
|
|
|
return label_data
|
|
|
|
|
2024-08-21 17:11:56 +08:00
|
|
|
@staticmethod
|
|
|
|
def load_rgb(path):
|
2024-08-29 13:54:13 -05:00
|
|
|
rgb_path = os.path.join(os.path.dirname(path), "rgb", os.path.basename(path) + ".png")
|
2024-08-21 17:11:56 +08:00
|
|
|
rgb_image = cv2.imread(rgb_path, cv2.IMREAD_COLOR)
|
|
|
|
return rgb_image
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def load_seg(path):
|
2024-08-29 13:54:13 -05:00
|
|
|
mask_path = os.path.join(os.path.dirname(path), "mask", os.path.basename(path) + ".png")
|
|
|
|
mask_image = cv2.imread(mask_path, cv2.IMREAD_GRAYSCALE)
|
|
|
|
return mask_image
|
2024-08-21 17:11:56 +08:00
|
|
|
|
|
|
|
@staticmethod
|
2024-08-29 13:54:13 -05:00
|
|
|
def cam_pose_transformation(cam_pose_before):
|
2024-08-21 17:11:56 +08:00
|
|
|
offset = np.asarray([
|
|
|
|
[1, 0, 0, 0],
|
|
|
|
[0, -1, 0, 0],
|
2024-08-29 13:54:13 -05:00
|
|
|
[0, 0, -1, 0],
|
2024-08-21 17:11:56 +08:00
|
|
|
[0, 0, 0, 1]])
|
2024-08-29 13:54:13 -05:00
|
|
|
cam_pose_after = cam_pose_before @ offset
|
|
|
|
return cam_pose_after
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def load_cam_info(path):
|
|
|
|
camera_params_path = os.path.join(os.path.dirname(path), "camera_params", os.path.basename(path) + ".json")
|
|
|
|
with open(camera_params_path, 'r') as f:
|
|
|
|
label_data = json.load(f)
|
|
|
|
cam_to_world = np.asarray(label_data["extrinsic"])
|
|
|
|
cam_to_world = DataLoadUtil.cam_pose_transformation(cam_to_world)
|
|
|
|
cam_intrinsic = np.asarray(label_data["intrinsic"])
|
2024-08-21 17:11:56 +08:00
|
|
|
return {
|
|
|
|
"cam_to_world": cam_to_world,
|
|
|
|
"cam_intrinsic": cam_intrinsic
|
|
|
|
}
|
|
|
|
|
|
|
|
@staticmethod
|
2024-08-29 13:54:13 -05:00
|
|
|
def get_target_point_cloud(depth, cam_intrinsic, cam_extrinsic, mask, target_mask_label=255):
|
2024-08-21 17:11:56 +08:00
|
|
|
h, w = depth.shape
|
|
|
|
i, j = np.meshgrid(np.arange(w), np.arange(h), indexing='xy')
|
|
|
|
|
|
|
|
z = depth
|
|
|
|
x = (i - cam_intrinsic[0, 2]) * z / cam_intrinsic[0, 0]
|
|
|
|
y = (j - cam_intrinsic[1, 2]) * z / cam_intrinsic[1, 1]
|
|
|
|
|
|
|
|
points_camera = np.stack((x, y, z), axis=-1).reshape(-1, 3)
|
2024-08-30 16:49:21 +08:00
|
|
|
mask = mask.reshape(-1)
|
|
|
|
|
|
|
|
target_mask = mask == target_mask_label
|
2024-08-29 13:54:13 -05:00
|
|
|
target_points_camera = points_camera[target_mask]
|
|
|
|
target_points_camera_aug = np.concatenate([target_points_camera, np.ones((target_points_camera.shape[0], 1))], axis=-1)
|
2024-08-21 17:11:56 +08:00
|
|
|
|
2024-08-29 13:54:13 -05:00
|
|
|
target_points_world = np.dot(cam_extrinsic, target_points_camera_aug.T).T[:, :3]
|
2024-08-21 17:11:56 +08:00
|
|
|
return {
|
2024-08-29 13:54:13 -05:00
|
|
|
"points_world": target_points_world,
|
|
|
|
"points_camera": target_points_camera
|
2024-08-21 17:11:56 +08:00
|
|
|
}
|
2024-08-30 19:21:18 +08:00
|
|
|
|
2024-08-21 17:11:56 +08:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def get_point_cloud_world_from_path(path):
|
|
|
|
cam_info = DataLoadUtil.load_cam_info(path)
|
|
|
|
depth = DataLoadUtil.load_depth(path)
|
|
|
|
mask = DataLoadUtil.load_seg(path)
|
|
|
|
point_cloud = DataLoadUtil.get_target_point_cloud(depth, cam_info['cam_intrinsic'], cam_info['cam_to_world'], mask)
|
|
|
|
return point_cloud['points_world']
|
|
|
|
|
|
|
|
@staticmethod
|
2024-08-30 16:49:21 +08:00
|
|
|
def get_point_cloud_list_from_seq(root, scene_name, num_frames):
|
2024-08-21 17:11:56 +08:00
|
|
|
point_cloud_list = []
|
2024-08-30 16:49:21 +08:00
|
|
|
for frame_idx in range(num_frames):
|
|
|
|
path = DataLoadUtil.get_path(root, scene_name, frame_idx)
|
2024-08-21 17:11:56 +08:00
|
|
|
point_cloud = DataLoadUtil.get_point_cloud_world_from_path(path)
|
|
|
|
point_cloud_list.append(point_cloud)
|
|
|
|
return point_cloud_list
|
|
|
|
|