hongxiaoy commited on
Commit
a57b6ab
·
verified ·
1 Parent(s): d5fed7d

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +222 -0
app.py ADDED
@@ -0,0 +1,222 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import copy
2
+ import gradio as gr
3
+ import os
4
+ import pickle
5
+ from matplotlib import pyplot as plt
6
+ import plotly.express as px
7
+ import plotly.graph_objects as go
8
+ import numpy as np
9
+
10
+
11
+
12
+ all_test_scenes = sorted(os.listdir('iso_output/NYU'))
13
+
14
+
15
+ def get_grid_coords(dims, resolution):
16
+ """
17
+ :param dims: the dimensions of the grid [x, y, z] (i.e. [256, 256, 32])
18
+ :return coords_grid: is the center coords of voxels in the grid
19
+ """
20
+
21
+ g_xx = np.arange(0, dims[0] + 1)
22
+ g_yy = np.arange(0, dims[1] + 1)
23
+
24
+ g_zz = np.arange(0, dims[2] + 1)
25
+
26
+ # Obtaining the grid with coords...
27
+ xx, yy, zz = np.meshgrid(g_xx[:-1], g_yy[:-1], g_zz[:-1])
28
+ coords_grid = np.array([xx.flatten(), yy.flatten(), zz.flatten()]).T
29
+ # coords_grid = coords_grid.astype(np.float)
30
+
31
+ coords_grid = (coords_grid * resolution) + resolution / 2
32
+
33
+ temp = np.copy(coords_grid)
34
+ temp[:, 0] = coords_grid[:, 1]
35
+ temp[:, 1] = coords_grid[:, 0]
36
+ coords_grid = np.copy(temp)
37
+
38
+ return coords_grid
39
+
40
+
41
+ def draw(
42
+ voxels,
43
+ cam_pose,
44
+ vox_origin,
45
+ voxel_size=0.08,
46
+ d=0.75, # 0.75m - determine the size of the mesh representing the camera
47
+ ):
48
+ # Compute the coordinates of the mesh representing camera
49
+ y = d * 480 / (2 * 518.8579)
50
+ x = d * 640 / (2 * 518.8579)
51
+ tri_points = np.array(
52
+ [
53
+ [0, 0, 0],
54
+ [x, y, d],
55
+ [-x, y, d],
56
+ [-x, -y, d],
57
+ [x, -y, d],
58
+ ]
59
+ )
60
+ tri_points = np.hstack([tri_points, np.ones((5, 1))])
61
+
62
+ tri_points = (cam_pose @ tri_points.T).T
63
+ x = tri_points[:, 0] - vox_origin[0]
64
+ y = tri_points[:, 1] - vox_origin[1]
65
+ z = tri_points[:, 2] - vox_origin[2]
66
+ triangles = [
67
+ (0, 1, 2),
68
+ (0, 1, 4),
69
+ (0, 3, 4),
70
+ (0, 2, 3),
71
+ ]
72
+
73
+ # Compute the voxels coordinates
74
+ grid_coords = get_grid_coords(
75
+ [voxels.shape[0], voxels.shape[2], voxels.shape[1]], voxel_size
76
+ )
77
+
78
+ # Attach the predicted class to every voxel
79
+ grid_coords = np.vstack(
80
+ (grid_coords.T, np.moveaxis(voxels, [0, 1, 2], [0, 2, 1]).reshape(-1))
81
+ ).T
82
+
83
+ # Remove empty and unknown voxels
84
+ occupied_voxels = grid_coords[(grid_coords[:, 3] > 0) & (grid_coords[:, 3] < 255)]
85
+
86
+
87
+ colors = np.array(
88
+ [
89
+ [22, 191, 206, 255],
90
+ [214, 38, 40, 255],
91
+ [43, 160, 43, 255],
92
+ [158, 216, 229, 255],
93
+ [114, 158, 206, 255],
94
+ [204, 204, 91, 255],
95
+ [255, 186, 119, 255],
96
+ [147, 102, 188, 255],
97
+ [30, 119, 181, 255],
98
+ [188, 188, 33, 255],
99
+ [255, 127, 12, 255],
100
+ [196, 175, 214, 255],
101
+ [153, 153, 153, 255],
102
+ [255, 255, 255, 255],
103
+ ]
104
+ )
105
+
106
+ pts_colors = [f'rgb({colors[int(i)][0]}, {colors[int(i)][1]}, {colors[int(i)][2]})' for i in occupied_voxels[:, 3]]
107
+
108
+
109
+
110
+ fig = go.Figure(data=[go.Scatter3d(x=occupied_voxels[:, 0], y=occupied_voxels[:, 1], z=occupied_voxels[:, 2],mode='markers',
111
+ marker=dict(
112
+ size=5,
113
+ color=pts_colors, # set color to an array/list of desired values
114
+ # colorscale='Viridis', # choose a colorscale
115
+ opacity=1.0,
116
+ symbol='square'
117
+ ))])
118
+ fig.update_layout(
119
+ autosize=True,
120
+ scene = dict(
121
+ aspectmode='data',
122
+ xaxis = dict(
123
+ backgroundcolor="rgb(255, 255, 255)",
124
+ gridcolor="black",
125
+ showbackground=True,
126
+ zerolinecolor="black",
127
+ nticks=4,
128
+ visible=False,
129
+ range=[-5,5],),
130
+ yaxis = dict(
131
+ backgroundcolor="rgb(255, 255, 255)",
132
+ gridcolor="black",
133
+ showbackground=True,
134
+ zerolinecolor="black",
135
+ visible=False,
136
+ nticks=4, range=[-5,5],),
137
+ zaxis = dict(
138
+ backgroundcolor="rgb(255, 255, 255)",
139
+ gridcolor="black",
140
+ showbackground=True,
141
+ zerolinecolor="black",
142
+ visible=False,
143
+ nticks=4, range=[-5,5],),
144
+ bgcolor="black",
145
+ ),
146
+
147
+ )
148
+
149
+ return fig
150
+
151
+
152
+ def predict(scan):
153
+ if scan is None:
154
+ return None, None, None
155
+ scan = 'iso_output/NYU/' + scan
156
+ with open(scan, "rb") as handle:
157
+ b = pickle.load(handle)
158
+
159
+ cam_pose = b["cam_pose"]
160
+ vox_origin = b["vox_origin"]
161
+ gt_scene = b["target"]
162
+ pred_scene = b["y_pred"]
163
+ scan = os.path.basename(scan)[:12]
164
+ img = plt.imread('iso_input/'+scan+'_color.jpg')
165
+
166
+ pred_scene[(gt_scene == 255)] = 255 # only draw scene inside the room
167
+
168
+ fig = draw(
169
+ pred_scene,
170
+ cam_pose,
171
+ vox_origin,
172
+ voxel_size=0.08,
173
+ d=0.75,
174
+ )
175
+
176
+ fig2 = draw(
177
+ gt_scene,
178
+ cam_pose,
179
+ vox_origin,
180
+ voxel_size=0.08,
181
+ d=0.75,
182
+ )
183
+
184
+ return fig, fig2, img
185
+
186
+ description = """
187
+ ISO Demo on NYUv2 test set.
188
+
189
+ For a fast rendering, we generate the output of test set scenes offline, and just provide a interface for plotting the output result.
190
+ We recommend you try visualization scripts locally in your computer for a better interaction.
191
+
192
+ <center>
193
+ <a href="https://hongxiaoy.github.io/ISO/">
194
+ <img style="display:inline" alt="Project page" src="https://img.shields.io/badge/Project%20Page-ISO-blue">
195
+ </a>
196
+ <a href="https://arxiv.org/abs/2407.11730"><img style="display:inline" src="https://img.shields.io/badge/arXiv-ISO-red"></a>
197
+ <a href="https://github.com/hongxiaoy/ISO"><img style="display:inline" src="https://img.shields.io/github/stars/hongxiaoy/ISO?style=social"></a>
198
+ </center>
199
+ """
200
+ title = """
201
+ <center>
202
+ <h1>Monocular Occupancy Prediction for Scalable Indoor Scenes</h1>
203
+ </center>
204
+ """
205
+
206
+ with gr.Blocks() as demo:
207
+ gr.Markdown(title)
208
+ gr.Markdown(description)
209
+ with gr.Row():
210
+ with gr.Column():
211
+ input = gr.Dropdown(all_test_scenes, label='input scan')
212
+ submit_btn = gr.Button("Submit", render=True)
213
+ img = gr.Image(label='color image')
214
+ with gr.Column():
215
+ output = gr.Plot(label='prediction')
216
+ label = gr.Plot(label='ground truth')
217
+
218
+ submit_btn.click(fn=predict, inputs=input, outputs=[output, label, img])
219
+
220
+ # demo = gr.Interface(fn=predict, inputs=gr.Dropdown(all_test_scenes), outputs=gr.Plot(), title=title, description=description)
221
+
222
+ demo.launch()