Spaces:
Running
on
Zero
Running
on
Zero
by default dont simplify 3D mesh
Browse files- app.py +24 -31
- history.md +9 -0
app.py
CHANGED
|
@@ -72,7 +72,7 @@ def resize_image(image_path, max_size=1024):
|
|
| 72 |
return temp_file.name
|
| 73 |
|
| 74 |
@spaces.GPU(duration=30) # Increased duration to 30 seconds
|
| 75 |
-
def generate_3d_model(depth, image_path, focallength_px, simplification_factor=0
|
| 76 |
"""
|
| 77 |
Generate a textured 3D mesh from the depth map and the original image.
|
| 78 |
"""
|
|
@@ -131,33 +131,26 @@ def generate_3d_model(depth, image_path, focallength_px, simplification_factor=0
|
|
| 131 |
# Create the mesh using Trimesh with vertex colors
|
| 132 |
mesh = trimesh.Trimesh(vertices=vertices, faces=faces, vertex_colors=colors, process=False)
|
| 133 |
|
| 134 |
-
# Mesh cleaning and improvement steps
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
mesh = mesh.smoothed()
|
| 155 |
-
print("After smoothing - vertices: {}, faces: {}".format(len(mesh.vertices), len(mesh.faces)))
|
| 156 |
-
|
| 157 |
-
print("Removing thin features")
|
| 158 |
-
# 4. Remove thin features
|
| 159 |
-
mesh = remove_thin_features(mesh, thickness_threshold=thin_threshold)
|
| 160 |
-
print("After removing thin features - vertices: {}, faces: {}".format(len(mesh.vertices), len(mesh.faces)))
|
| 161 |
|
| 162 |
# Export the mesh to OBJ files with unique filenames
|
| 163 |
timestamp = int(time.time())
|
|
@@ -334,9 +327,9 @@ with gr.Blocks() as iface:
|
|
| 334 |
download_texture = gr.File(label="Download Texture (PNG)")
|
| 335 |
|
| 336 |
with gr.Row():
|
| 337 |
-
simplification_factor = gr.Slider(minimum=0.1, maximum=1.0, value=0
|
| 338 |
-
smoothing_iterations = gr.Slider(minimum=0, maximum=5, value=
|
| 339 |
-
thin_threshold = gr.Slider(minimum=0
|
| 340 |
|
| 341 |
regenerate_button = gr.Button("Regenerate 3D Model")
|
| 342 |
model_status = gr.Textbox(label="3D Model Status")
|
|
|
|
| 72 |
return temp_file.name
|
| 73 |
|
| 74 |
@spaces.GPU(duration=30) # Increased duration to 30 seconds
|
| 75 |
+
def generate_3d_model(depth, image_path, focallength_px, simplification_factor=1.0, smoothing_iterations=0, thin_threshold=0):
|
| 76 |
"""
|
| 77 |
Generate a textured 3D mesh from the depth map and the original image.
|
| 78 |
"""
|
|
|
|
| 131 |
# Create the mesh using Trimesh with vertex colors
|
| 132 |
mesh = trimesh.Trimesh(vertices=vertices, faces=faces, vertex_colors=colors, process=False)
|
| 133 |
|
| 134 |
+
# Mesh cleaning and improvement steps (only if not using default values)
|
| 135 |
+
if simplification_factor < 1.0 or smoothing_iterations > 0 or thin_threshold > 0:
|
| 136 |
+
print("Original mesh - vertices: {}, faces: {}".format(len(mesh.vertices), len(mesh.faces)))
|
| 137 |
+
|
| 138 |
+
if simplification_factor < 1.0:
|
| 139 |
+
print("Simplifying mesh")
|
| 140 |
+
target_faces = int(len(mesh.faces) * simplification_factor)
|
| 141 |
+
mesh = mesh.simplify_quadric_decimation(face_count=target_faces)
|
| 142 |
+
print("After simplification - vertices: {}, faces: {}".format(len(mesh.vertices), len(mesh.faces)))
|
| 143 |
+
|
| 144 |
+
if smoothing_iterations > 0:
|
| 145 |
+
print("Smoothing mesh")
|
| 146 |
+
for _ in range(smoothing_iterations):
|
| 147 |
+
mesh = mesh.smoothed()
|
| 148 |
+
print("After smoothing - vertices: {}, faces: {}".format(len(mesh.vertices), len(mesh.faces)))
|
| 149 |
+
|
| 150 |
+
if thin_threshold > 0:
|
| 151 |
+
print("Removing thin features")
|
| 152 |
+
mesh = remove_thin_features(mesh, thickness_threshold=thin_threshold)
|
| 153 |
+
print("After removing thin features - vertices: {}, faces: {}".format(len(mesh.vertices), len(mesh.faces)))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 154 |
|
| 155 |
# Export the mesh to OBJ files with unique filenames
|
| 156 |
timestamp = int(time.time())
|
|
|
|
| 327 |
download_texture = gr.File(label="Download Texture (PNG)")
|
| 328 |
|
| 329 |
with gr.Row():
|
| 330 |
+
simplification_factor = gr.Slider(minimum=0.1, maximum=1.0, value=1.0, step=0.1, label="Simplification Factor (1.0 = No simplification)")
|
| 331 |
+
smoothing_iterations = gr.Slider(minimum=0, maximum=5, value=0, step=1, label="Smoothing Iterations (0 = No smoothing)")
|
| 332 |
+
thin_threshold = gr.Slider(minimum=0, maximum=0.1, value=0, step=0.001, label="Thin Feature Threshold (0 = No thin feature removal)")
|
| 333 |
|
| 334 |
regenerate_button = gr.Button("Regenerate 3D Model")
|
| 335 |
model_status = gr.Textbox(label="3D Model Status")
|
history.md
CHANGED
|
@@ -38,3 +38,12 @@
|
|
| 38 |
1. Investigate and fix the texture mapping in the 3D model generation process.
|
| 39 |
2. Clarify the units for the thin threshold and consider adjusting its range.
|
| 40 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
1. Investigate and fix the texture mapping in the 3D model generation process.
|
| 39 |
2. Clarify the units for the thin threshold and consider adjusting its range.
|
| 40 |
|
| 41 |
+
## 2024-10-06 00:30 PST
|
| 42 |
+
### Reverting 3D Model Simplification and Smoothing
|
| 43 |
+
- Problem: The 3D model still lacks proper colorization from the original image.
|
| 44 |
+
- Action: Reverting simplification and smoothing operations to their default state (no processing) to isolate the colorization issue.
|
| 45 |
+
- Changes:
|
| 46 |
+
1. Set default values for simplification_factor, smoothing_iterations, and thin_threshold to 1.0, 0, and 0 respectively.
|
| 47 |
+
2. Adjust the generate_3d_model function to skip simplification and smoothing steps when default values are used.
|
| 48 |
+
- Next steps: Re-test the 3D model generation to verify if colorization is restored without these processing steps.
|
| 49 |
+
|