Spaces:
Running
on
Zero
Running
on
Zero
| import plotly.graph_objects as go | |
| import open3d as o3d | |
| import numpy as np | |
| import textwrap | |
| import torch | |
| import random | |
| import os | |
| import subprocess | |
| def install_cuda_toolkit(): | |
| CUDA_TOOLKIT_URL = "https://developer.download.nvidia.com/compute/cuda/11.8.0/local_installers/cuda_11.8.0_520.61.05_linux.run" | |
| #CUDA_TOOLKIT_URL = "https://developer.download.nvidia.com/compute/cuda/12.2.0/local_installers/cuda_12.2.0_535.54.03_linux.run" | |
| CUDA_TOOLKIT_FILE = "/tmp/%s" % os.path.basename(CUDA_TOOLKIT_URL) | |
| subprocess.call(["wget", "-q", CUDA_TOOLKIT_URL, "-O", CUDA_TOOLKIT_FILE]) | |
| subprocess.call(["chmod", "+x", CUDA_TOOLKIT_FILE]) | |
| subprocess.call([CUDA_TOOLKIT_FILE, "--silent", "--toolkit"]) | |
| os.environ["CUDA_HOME"] = "/usr/local/cuda" | |
| os.environ["PATH"] = "%s/bin:%s" % (os.environ["CUDA_HOME"], os.environ["PATH"]) | |
| os.environ["LD_LIBRARY_PATH"] = "%s/lib:%s" % ( | |
| os.environ["CUDA_HOME"], | |
| "" if "LD_LIBRARY_PATH" not in os.environ else os.environ["LD_LIBRARY_PATH"], | |
| ) | |
| # Fix: arch_list[-1] += '+PTX'; IndexError: list index out of range | |
| os.environ["TORCH_CUDA_ARCH_LIST"] = "8.0;8.6" | |
| def set_seed(): | |
| seed = 123 | |
| torch.manual_seed(seed) | |
| torch.cuda.manual_seed(seed) | |
| torch.cuda.manual_seed_all(seed) | |
| np.random.seed(seed) | |
| random.seed(seed) | |
| def read_pcd(pcd_path): | |
| pcd = o3d.io.read_point_cloud(pcd_path) | |
| xyz = np.asarray(pcd.points) | |
| rgb = np.asarray(pcd.colors) | |
| if not pcd.has_normals(): | |
| pcd.estimate_normals(search_param=o3d.geometry.KDTreeSearchParamHybrid(radius=0.1, max_nn=100)) | |
| normal = np.asarray(pcd.normals) | |
| return xyz, rgb, normal | |
| def render_pcd_file(pcd_path): | |
| pcd = o3d.io.read_point_cloud(pcd_path) | |
| xyz = np.asarray(pcd.points) | |
| rgb = np.asarray(pcd.colors) | |
| return render_point_cloud(xyz, rgb) | |
| def render_point_cloud(xyz, rgb, legend=None): | |
| x = xyz[:, 0] | |
| y = xyz[:, 1] | |
| z = xyz[:, 2] | |
| rgb = rgb * 255 | |
| hex_colors = [f'#{int(r):02x}{int(g):02x}{int(b):02x}' for r, g, b in rgb] | |
| fig = go.Figure(data=[go.Scatter3d(x=x, y=y, z=z, mode='markers', | |
| marker=dict(size=2, color=hex_colors, colorscale='Viridis', opacity=0.8))]) | |
| if legend: | |
| fig.add_annotation(x=0.5, y=1.25, text="<br>".join(textwrap.wrap(legend, width=30)), showarrow=False, xref="paper", yref="paper") | |
| # Customize layout | |
| fig.update_layout( | |
| scene=dict( | |
| xaxis=dict(title='x', showgrid=False, zeroline=False, visible=False), | |
| yaxis=dict(title='y', showgrid=False, zeroline=False, visible=False), | |
| zaxis=dict(title='z', showgrid=False, zeroline=False, visible=False), | |
| aspectmode='manual'), | |
| scene_camera=dict( | |
| up=dict(x=0, y=1, z=0), # Adjust these values for your point cloud | |
| eye=dict(x=0, y=-0.9, z=2), # Increase the values to move further away | |
| center = dict(x=0,y=0,z=0) | |
| ) | |
| ) | |
| # fig.update_layout( | |
| # height=450, | |
| # autosize=True, | |
| # hovermode=False, | |
| # margin=go.layout.Margin(l=0, r=0, b=0, t=0), | |
| # showlegend=False, | |
| # legend=dict( | |
| # yanchor='bottom', | |
| # y=0.01, | |
| # xanchor='right', | |
| # x=0.99, | |
| # ), | |
| # scene=dict( | |
| # aspectmode='manual', | |
| # aspectratio=dict(x=1, y=1, z=1.0), | |
| # camera=dict( | |
| # eye=dict(x=base_radius - 1.6, y=0.0, z=0.6), | |
| # center=dict(x=0.0, y=0.0, z=0.0), | |
| # up=dict(x=0.0, y=0.0, z=1.0)), | |
| # xaxis_title='', | |
| # yaxis_title='', | |
| # zaxis_title='', | |
| # xaxis=dict( | |
| # range=[-scene_bounds, scene_bounds], | |
| # showticklabels=False, | |
| # showgrid=True, | |
| # zeroline=False, | |
| # showbackground=True, | |
| # showspikes=False, | |
| # showline=False, | |
| # ticks=''), | |
| # yaxis=dict( | |
| # range=[-scene_bounds, scene_bounds], | |
| # showticklabels=False, | |
| # showgrid=True, | |
| # zeroline=False, | |
| # showbackground=True, | |
| # showspikes=False, | |
| # showline=False, | |
| # ticks=''), | |
| # zaxis=dict( | |
| # range=[-scene_bounds, scene_bounds], | |
| # showticklabels=False, | |
| # showgrid=True, | |
| # zeroline=False, | |
| # showbackground=True, | |
| # showspikes=False, | |
| # showline=False, | |
| # ticks=''))) | |
| return fig |