|
import numpy as np |
|
import os |
|
import pickle |
|
import torch |
|
import open3d as o3d |
|
from manotorch.manolayer import ManoLayer |
|
import glob |
|
|
|
|
|
class VisDemo: |
|
def __init__(self, demo_path, dtype): |
|
|
|
self.demo_path = demo_path |
|
self.dtype = dtype |
|
assert dtype in ["h1o1", "h2o1", "h2o2"], f"Unsupported dtype: {dtype}" |
|
self.demo_list = os.listdir(f"{demo_path}/{dtype}") |
|
self.demo_list = sorted(self.demo_list) |
|
|
|
self.mano_layer_right = ManoLayer( |
|
flat_hand_mean=True, |
|
side="right", |
|
center_idx=0, |
|
mano_assets_root="data/mano_v1_2", |
|
) |
|
self.mano_layer_left = ManoLayer( |
|
flat_hand_mean=True, |
|
side="left", |
|
center_idx=0, |
|
mano_assets_root="data/mano_v1_2", |
|
) |
|
|
|
def visualize(self, idx): |
|
vis = o3d.visualization.Visualizer() |
|
vis.create_window(window_name=f"Demo", width=720, height=720) |
|
vis.add_geometry(o3d.geometry.TriangleMesh.create_coordinate_frame(size=0.1)) |
|
|
|
class HandMesh: |
|
def __init__(self, parent, idx, side): |
|
self.parent = parent |
|
self.side = side |
|
self.data = self._load(parent, idx, side) |
|
self.t = 0 |
|
|
|
mano_layer = getattr(parent, f"mano_layer_{side}") |
|
faces = mano_layer.get_mano_closed_faces() |
|
|
|
pose = torch.tensor(self.data["mano_pose"]) |
|
betas = torch.tensor(self.data["mano_betas"]) |
|
tsl = self.data["mano_tsl"] |
|
self.d_verts = mano_layer(pose.reshape(-1, 16 * 3), betas).verts.numpy() + tsl[:, None] |
|
|
|
self.mesh = o3d.geometry.TriangleMesh() |
|
self.mesh.vertices = o3d.utility.Vector3dVector(self.d_verts[self.t]) |
|
self.mesh.triangles = o3d.utility.Vector3iVector(faces) |
|
if self.side == "right": |
|
self.mesh.paint_uniform_color([0.55, 0.78, 0.78]) |
|
else: |
|
self.mesh.paint_uniform_color([0.31, 0.55, 0.78]) |
|
self.mesh.compute_vertex_normals() |
|
|
|
def _load(self, parent, idx, side): |
|
pkl_path = os.path.join(parent.demo_path, parent.dtype, idx, f"{side}_hand.pkl") |
|
with open(pkl_path, "rb") as f: |
|
return pickle.load(f) |
|
|
|
def update(self): |
|
self.t = (self.t + 1) % len(self.d_verts) |
|
self.mesh.vertices = o3d.utility.Vector3dVector(self.d_verts[self.t]) |
|
self.mesh.compute_vertex_normals() |
|
return self.mesh |
|
|
|
class ObjMesh: |
|
def __init__(self, parent, idx, side=None): |
|
self.parent = parent |
|
self.side = side |
|
self.data = self._load(parent, idx, side) |
|
self.t = 0 |
|
|
|
self.mesh = self._load_mesh(parent, idx, side) |
|
self.mesh.paint_uniform_color([1.0, 0.42, 0.04]) |
|
self.mesh.transform(self.data["obj_trajectory"][self.t]) |
|
|
|
def _load(self, parent, idx, side): |
|
if side is None: |
|
pkl_path = os.path.join(parent.demo_path, parent.dtype, idx, "obj.pkl") |
|
else: |
|
pkl_path = os.path.join(parent.demo_path, parent.dtype, idx, f"{side}_obj.pkl") |
|
with open(pkl_path, "rb") as f: |
|
return pickle.load(f) |
|
|
|
def _load_mesh(self, parent, idx, side): |
|
if side is None: |
|
base = os.path.join(parent.demo_path, parent.dtype, idx, "urdf") |
|
else: |
|
base = os.path.join(parent.demo_path, parent.dtype, idx, f"{side}_urdf") |
|
mesh_path = (glob.glob(os.path.join(base, "*.ply")) or glob.glob(os.path.join(base, "*.obj")))[0] |
|
mesh = o3d.io.read_triangle_mesh(mesh_path) |
|
mesh.compute_vertex_normals() |
|
return mesh |
|
|
|
def update(self): |
|
cur = self.data["obj_trajectory"][self.t] |
|
self.t = (self.t + 1) % len(self.data["obj_trajectory"]) |
|
self.mesh.transform(self.data["obj_trajectory"][self.t] @ np.linalg.inv(cur)) |
|
return self.mesh |
|
|
|
if self.dtype == "h1o1": |
|
only_rh = os.path.exists(os.path.join(self.demo_path, self.dtype, idx, "right_hand.pkl")) |
|
|
|
meshes = [] |
|
if self.dtype != "h1o1" or only_rh: |
|
meshes.append(HandMesh(self, idx, "right")) |
|
if self.dtype != "h1o1" or not only_rh: |
|
meshes.append(HandMesh(self, idx, "left")) |
|
if self.dtype != "h2o1": |
|
if self.dtype == "h2o2" or not only_rh: |
|
meshes.append(ObjMesh(self, idx, side="left")) |
|
if self.dtype == "h2o2" or only_rh: |
|
meshes.append(ObjMesh(self, idx, side="right")) |
|
else: |
|
meshes.append(ObjMesh(self, idx, side=None)) |
|
|
|
for mesh in meshes: |
|
vis.add_geometry(mesh.mesh) |
|
|
|
while True: |
|
should_close = not vis.poll_events() |
|
if should_close: |
|
break |
|
for mesh in meshes: |
|
vis.update_geometry(mesh.update()) |
|
vis.update_renderer() |
|
|
|
vis.destroy_window() |
|
|
|
|
|
if __name__ == "__main__": |
|
demo_path = "data/train_sample" |
|
dtype = "h2o2" |
|
vis_demo = VisDemo(demo_path, dtype) |
|
|
|
for idx in vis_demo.demo_list: |
|
print(f"Visualizing demo: {idx}") |
|
vis_demo.visualize(idx) |
|
|