Miguelpef commited on
Commit
1c6444b
·
verified ·
1 Parent(s): 479bab5

Upload 2 files

Browse files
Files changed (2) hide show
  1. requirements.txt +2 -2
  2. viewer.py +77 -13
requirements.txt CHANGED
@@ -1,4 +1,4 @@
1
- gradio==5.13.1
2
  numpy>=1.24.0
3
  plotly>=5.18.0
4
- py-vox-io
 
1
+ gradio>=5.13.1
2
  numpy>=1.24.0
3
  plotly>=5.18.0
4
+ git+https://github.com/MiguelPeFr/py-vox-io.git
viewer.py CHANGED
@@ -47,12 +47,12 @@ def create_3d_scatter(voxels, palette):
47
  x=x, y=y, z=z,
48
  mode='markers',
49
  marker=dict(
50
- size=6, # Reduced size for better visualization
51
  color=rgb_colors,
52
  opacity=1.0,
53
  symbol='square', # Using square symbol (supported by Plotly)
54
  line=dict(width=0), # Remove border lines completely
55
- sizemode='diameter', # Fixed size regardless of zoom
56
  sizeref=1.0 # Reference scale for consistent size
57
  )
58
  )])
@@ -63,14 +63,14 @@ def create_3d_scatter(voxels, palette):
63
  center_z = (z.max() + z.min()) / 2
64
  max_range = max(x.max() - x.min(), y.max() - y.min(), z.max() - z.min())
65
 
66
- # Update layout for better visualization
67
  fig.update_layout(
68
  scene=dict(
69
  aspectmode='cube', # Force cubic aspect ratio
70
  camera=dict(
71
  up=dict(x=0, y=1, z=0),
72
  center=dict(x=0, y=0, z=0),
73
- eye=dict(x=1.5, y=0.9, z=0.9) # Adjusted camera position for a closer view while maintaining angle
74
  ),
75
  xaxis=dict(range=[center_x - max_range/1.5, center_x + max_range/1.5], showgrid=True, gridwidth=1, gridcolor='rgba(128, 128, 128, 0.2)'),
76
  yaxis=dict(range=[center_y - max_range/1.5, center_y + max_range/1.5], showgrid=True, gridwidth=1, gridcolor='rgba(128, 128, 128, 0.2)'),
@@ -90,19 +90,70 @@ def display_vox_model(vox_file):
90
  try:
91
  # Load the vox model
92
  if not vox_file:
93
- return "Please upload a .vox file"
 
 
 
 
 
 
 
 
 
 
 
 
94
 
95
  if not vox_file.name.endswith('.vox'):
96
- return "Please upload a valid .vox file"
 
 
 
 
 
 
 
 
 
 
 
 
97
 
98
  print(f"Loading vox file: {vox_file.name}")
99
- voxels, palette = load_vox_model(vox_file.name)
100
 
101
  if voxels is None or palette is None:
102
- return "Error: Could not load voxel data from file"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
103
 
104
  if voxels.size == 0:
105
- return "Error: No voxels found in the model"
 
 
 
 
 
 
 
 
 
 
 
 
106
 
107
  print(f"Model loaded successfully. Shape: {voxels.shape}, Palette size: {len(palette)}")
108
 
@@ -112,17 +163,30 @@ def display_vox_model(vox_file):
112
  return fig
113
  except Exception as e:
114
  print(f"Error details: {str(e)}")
115
- return f"Error loading model: {str(e)}"
 
 
 
 
 
 
 
 
 
 
 
 
 
116
 
117
  # Create Gradio interface
118
  interface = gr.Interface(
119
  fn=display_vox_model,
120
  inputs=gr.File(label="Upload .vox file"),
121
- outputs=gr.Plot(label="3D Voxel Model"),
122
  title="Voxel Model Viewer",
123
  description="Upload a .vox file to view the 3D voxelized model.",
124
- examples=[["examples/modelo_optimizado.vox"]] # Updated path
125
  )
126
 
127
  if __name__ == "__main__":
128
- interface.launch()
 
47
  x=x, y=y, z=z,
48
  mode='markers',
49
  marker=dict(
50
+ size=6,
51
  color=rgb_colors,
52
  opacity=1.0,
53
  symbol='square', # Using square symbol (supported by Plotly)
54
  line=dict(width=0), # Remove border lines completely
55
+ sizemode='diameter',
56
  sizeref=1.0 # Reference scale for consistent size
57
  )
58
  )])
 
63
  center_z = (z.max() + z.min()) / 2
64
  max_range = max(x.max() - x.min(), y.max() - y.min(), z.max() - z.min())
65
 
66
+ # better visualization
67
  fig.update_layout(
68
  scene=dict(
69
  aspectmode='cube', # Force cubic aspect ratio
70
  camera=dict(
71
  up=dict(x=0, y=1, z=0),
72
  center=dict(x=0, y=0, z=0),
73
+ eye=dict(x=1.5, y=0.9, z=0.9)
74
  ),
75
  xaxis=dict(range=[center_x - max_range/1.5, center_x + max_range/1.5], showgrid=True, gridwidth=1, gridcolor='rgba(128, 128, 128, 0.2)'),
76
  yaxis=dict(range=[center_y - max_range/1.5, center_y + max_range/1.5], showgrid=True, gridwidth=1, gridcolor='rgba(128, 128, 128, 0.2)'),
 
90
  try:
91
  # Load the vox model
92
  if not vox_file:
93
+ fig = go.Figure()
94
+ fig.add_annotation(
95
+ text="Please upload a .vox file",
96
+ xref="paper", yref="paper",
97
+ x=0.5, y=0.5,
98
+ showarrow=False,
99
+ font=dict(size=16, color="white")
100
+ )
101
+ fig.update_layout(
102
+ paper_bgcolor='rgba(0,0,0,1)',
103
+ plot_bgcolor='rgba(0,0,0,1)'
104
+ )
105
+ return fig
106
 
107
  if not vox_file.name.endswith('.vox'):
108
+ fig = go.Figure()
109
+ fig.add_annotation(
110
+ text="Please upload a valid .vox file",
111
+ xref="paper", yref="paper",
112
+ x=0.5, y=0.5,
113
+ showarrow=False,
114
+ font=dict(size=16, color="white")
115
+ )
116
+ fig.update_layout(
117
+ paper_bgcolor='rgba(0,0,0,1)',
118
+ plot_bgcolor='rgba(0,0,0,1)'
119
+ )
120
+ return fig
121
 
122
  print(f"Loading vox file: {vox_file.name}")
123
+ voxels, palette = load_vox_model(vox_file.temp_path if hasattr(vox_file, 'temp_path') else vox_file.name)
124
 
125
  if voxels is None or palette is None:
126
+ fig = go.Figure()
127
+ error_message = "Error: Could not load voxel data from file\n"
128
+ error_message += "This might be due to version incompatibility.\n"
129
+ error_message += "The viewer currently supports .vox files up to version 200."
130
+ fig.add_annotation(
131
+ text=error_message,
132
+ xref="paper", yref="paper",
133
+ x=0.5, y=0.5,
134
+ showarrow=False,
135
+ font=dict(size=16, color="white")
136
+ )
137
+ fig.update_layout(
138
+ paper_bgcolor='rgba(0,0,0,1)',
139
+ plot_bgcolor='rgba(0,0,0,1)'
140
+ )
141
+ return fig
142
 
143
  if voxels.size == 0:
144
+ fig = go.Figure()
145
+ fig.add_annotation(
146
+ text="Error: No voxels found in the model",
147
+ xref="paper", yref="paper",
148
+ x=0.5, y=0.5,
149
+ showarrow=False,
150
+ font=dict(size=16, color="white")
151
+ )
152
+ fig.update_layout(
153
+ paper_bgcolor='rgba(0,0,0,1)',
154
+ plot_bgcolor='rgba(0,0,0,1)'
155
+ )
156
+ return fig
157
 
158
  print(f"Model loaded successfully. Shape: {voxels.shape}, Palette size: {len(palette)}")
159
 
 
163
  return fig
164
  except Exception as e:
165
  print(f"Error details: {str(e)}")
166
+ # Create an empty figure with error message
167
+ fig = go.Figure()
168
+ fig.add_annotation(
169
+ text=f"Error loading model: {str(e)}",
170
+ xref="paper", yref="paper",
171
+ x=0.5, y=0.5,
172
+ showarrow=False,
173
+ font=dict(size=16, color="white")
174
+ )
175
+ fig.update_layout(
176
+ paper_bgcolor='rgba(0,0,0,1)',
177
+ plot_bgcolor='rgba(0,0,0,1)'
178
+ )
179
+ return fig
180
 
181
  # Create Gradio interface
182
  interface = gr.Interface(
183
  fn=display_vox_model,
184
  inputs=gr.File(label="Upload .vox file"),
185
+ outputs=gr.Plot(label="3D Voxel Model"), # Remove the type parameter
186
  title="Voxel Model Viewer",
187
  description="Upload a .vox file to view the 3D voxelized model.",
188
+ examples=[["examples/modelo_optimizado.vox"]]
189
  )
190
 
191
  if __name__ == "__main__":
192
+ interface.launch()