Skip to content

Instantly share code, notes, and snippets.

@dilaragokay
Created March 28, 2021 23:31
Show Gist options
  • Save dilaragokay/8f052b528b92d0bd2b99b7359cf7b482 to your computer and use it in GitHub Desktop.
Save dilaragokay/8f052b528b92d0bd2b99b7359cf7b482 to your computer and use it in GitHub Desktop.
Render depth map and RGB image of ModelNet10 chair dataset from various view points
'''
Render depth map and RGB image of ModelNet10 chair dataset from various view points
'''
import open3d as o3d
import numpy as np
import json
NUM_VIEWS = 6
with open('front.json') as json_file:
data = json.load(json_file)
train = open('debug_train.txt').readlines()
test = open('debug_test.txt').readlines()
all_files = train + test
all_files = ["ModelNet10/chair/train/" + f[:-1] + ".off" if f.endswith('\n') else "ModelNet10/chair/train/" + f + ".off" for f in all_files]
vis = o3d.visualization.Visualizer()
vis.create_window(visible=False)
for file in all_files:
mesh = o3d.io.read_triangle_mesh(file)
# subtract mean
ver = np.asarray(mesh.vertices)
for i, v in enumerate(ver):
ver[i] = v - ver.mean(0)
# normalize
dist = [np.linalg.norm(v) for v in ver]
ver = ver / max(dist)
mesh.vertices = o3d.utility.Vector3dVector(ver)
# sample points
degrees = np.arange(0, 2*np.pi, np.pi/3)
vis.add_geometry(mesh)
for i in range(NUM_VIEWS):
R = mesh.get_rotation_matrix_from_xyz((0, 0, 2 * np.pi / NUM_VIEWS))
mesh.rotate(R, center=(0, 0, 0))
# viewpoint set here
ctr = vis.get_view_control()
ctr.set_front(np.array(data['trajectory'][0]['front']))
ctr.set_lookat(np.array(data['trajectory'][0]['lookat']))
ctr.set_up(np.array(data['trajectory'][0]['up']))
vis.update_geometry(mesh)
vis.poll_events()
vis.update_renderer()
current_file = file[:-4] + "_" + str(i) + ".ply"
vis.capture_depth_point_cloud(current_file, convert_to_world_coordinate=True)
print(current_file)
vis.remove_geometry(mesh)
vis.destroy_window()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment