forked from Lifelong-Robot-Learning/LIBERO
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvisualizer.py
168 lines (146 loc) · 6.74 KB
/
visualizer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
import plotly.graph_objs as go
import plotly.io as pio
from flask import Flask, render_template_string
class Visualizer:
'''From the DP3 Repos Codebase'''
def __init__(self):
self.app = Flask(__name__)
self.pointclouds = []
def _generate_trace(self, pointcloud, color:tuple=None, size=5, opacity=0.7):
x_coords = pointcloud[:, 0]
y_coords = pointcloud[:, 1]
z_coords = pointcloud[:, 2]
if pointcloud.shape[1] == 3:
if color is None:
# design a colorful point cloud based on 3d coordinates
# Normalize coordinates to range [0, 1]
min_coords = pointcloud.min(axis=0)
max_coords = pointcloud.max(axis=0)
normalized_coords = (pointcloud - min_coords) / (max_coords - min_coords)
try:
# Use normalized coordinates as RGB values
colors = ['rgb({},{},{})'.format(int(r*255), int(g*255), int(b*255)) for r, g, b in normalized_coords]
except: # maybe meet NaN error
# use simple cyan color
colors = ['rgb(0,255,255)' for _ in range(len(x_coords))]
else:
colors = ['rgb({},{},{})'.format(color[0], color[1], color[2]) for _ in range(len(x_coords))]
else:
colors = ['rgb({},{},{})'.format(int(r), int(g), int(b)) for r, g, b in pointcloud[:, 3:6]]
return go.Scatter3d(
x=x_coords,
y=y_coords,
z=z_coords,
mode='markers',
marker=dict(
size=size,
opacity=opacity,
color=colors
)
)
def colorize(self, pointcloud):
if pointcloud.shape[1] == 3:
# design a colorful point cloud based on 3d coordinates
# Normalize coordinates to range [0, 1]
min_coords = pointcloud.min(axis=0)
max_coords = pointcloud.max(axis=0)
normalized_coords = (pointcloud - min_coords) / (max_coords - min_coords)
try:
# Use normalized coordinates as RGB values
colors = ['rgb({},{},{})'.format(int(r*255), int(g*255), int(b*255)) for r, g, b in normalized_coords]
except: # maybe meet NaN error
# use simple cyan color
x_coords = pointcloud[:, 0]
colors = ['rgb(0,255,255)' for _ in range(len(x_coords))]
else:
colors = ['rgb({},{},{})'.format(int(r), int(g), int(b)) for r, g, b in pointcloud[:, 3:6]]
return colors
def visualize_pointcloud(self, pointcloud, color:tuple=None):
trace = self._generate_trace(pointcloud, color=color, size=6, opacity=1.0)
layout = go.Layout(margin=dict(l=0, r=0, b=0, t=0))
fig = go.Figure(data=[trace], layout=layout)
fig.update_layout(
scene=dict(
# aspectmode='cube',
xaxis=dict(
showbackground=False, # 隐藏背景网格
showgrid=True, # 隐藏网格
showline=True, # 显示轴线
linecolor='grey', # 设置轴线颜色为灰色
zerolinecolor='grey', # 设置0线颜色为灰色
zeroline=False, # 关闭0线
gridcolor='grey', # 设置网格颜色为灰色
),
yaxis=dict(
showbackground=False,
showgrid=True,
showline=True,
linecolor='grey',
zerolinecolor='grey',
zeroline=False, # 关闭0线
gridcolor='grey', # 设置网格颜色为灰色
),
zaxis=dict(
showbackground=False,
showgrid=True,
showline=True,
linecolor='grey',
zerolinecolor='grey',
zeroline=False, # 关闭0线
gridcolor='grey', # 设置网格颜色为灰色
),
bgcolor='white' # 设置背景色为白色
)
)
div = pio.to_html(fig, full_html=False)
@self.app.route('/')
def index():
return render_template_string('''<div>{{ div|safe }}</div>''', div=div)
self.app.run(debug=True, use_reloader=False)
def visualize_pointcloud_and_save(self, pointcloud, color:tuple=None, save_path=None):
trace = self._generate_trace(pointcloud, color=color, size=6, opacity=1.0)
layout = go.Layout(margin=dict(l=0, r=0, b=0, t=0))
fig = go.Figure(data=[trace], layout=layout)
fig.update_layout(
scene=dict(
# aspectmode='cube',
xaxis=dict(
showbackground=False, # 隐藏背景网格
showgrid=True, # 隐藏网格
showline=True, # 显示轴线
linecolor='grey', # 设置轴线颜色为灰色
zerolinecolor='grey', # 设置0线颜色为灰色
zeroline=False, # 关闭0线
gridcolor='grey', # 设置网格颜色为灰色
),
yaxis=dict(
showbackground=False,
showgrid=True,
showline=True,
linecolor='grey',
zerolinecolor='grey',
zeroline=False, # 关闭0线
gridcolor='grey', # 设置网格颜色为灰色
),
zaxis=dict(
showbackground=False,
showgrid=True,
showline=True,
linecolor='grey',
zerolinecolor='grey',
zeroline=False, # 关闭0线
gridcolor='grey', # 设置网格颜色为灰色
),
bgcolor='white' # 设置背景色为白色
)
)
# save
fig.write_image(save_path, width=800, height=600)
def save_visualization_to_file(self, pointcloud, file_path, color:tuple=None):
# visualize pointcloud and save as html
trace = self._generate_trace(pointcloud, color=color)
layout = go.Layout(margin=dict(l=0, r=0, b=0, t=0))
fig_html = pio.to_html(go.Figure(data=[trace], layout=layout), full_html=True)
with open(file_path, 'w') as file:
file.write(fig_html)
print(f"Visualization saved to {file_path}")