Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,62 +4,58 @@ import numpy as np
|
|
| 4 |
import tempfile
|
| 5 |
import os
|
| 6 |
|
| 7 |
-
def
|
| 8 |
if input_file is None:
|
| 9 |
return None, None
|
| 10 |
|
| 11 |
try:
|
| 12 |
-
# 1. Load the
|
| 13 |
mesh = trimesh.load(input_file.name)
|
| 14 |
|
| 15 |
-
# Handle
|
| 16 |
if isinstance(mesh, trimesh.Scene):
|
| 17 |
mesh = mesh.dump(concatenate=True)
|
| 18 |
|
| 19 |
-
# 2.
|
| 20 |
-
#
|
| 21 |
-
#
|
| 22 |
-
|
|
|
|
|
|
|
| 23 |
|
| 24 |
-
# 3. Create a temporary path for the output
|
| 25 |
temp_dir = tempfile.gettempdir()
|
| 26 |
-
output_filename = "
|
| 27 |
output_path = os.path.join(temp_dir, output_filename)
|
| 28 |
|
| 29 |
-
# 4. Export the
|
| 30 |
-
|
| 31 |
|
| 32 |
-
# Return the path twice: once for the 3D viewer, once for the Download component
|
| 33 |
return output_path, output_path
|
| 34 |
|
| 35 |
except Exception as e:
|
| 36 |
print(f"Error: {e}")
|
| 37 |
return None, None
|
| 38 |
|
| 39 |
-
# ---
|
| 40 |
-
with gr.Blocks(title="3D
|
| 41 |
-
gr.Markdown("# 🧊
|
| 42 |
-
gr.Markdown("
|
| 43 |
|
| 44 |
with gr.Row():
|
| 45 |
-
# Input Section
|
| 46 |
with gr.Column():
|
| 47 |
-
file_input = gr.File(label="Upload Irregular STL", file_types=[".stl"])
|
| 48 |
-
generate_btn = gr.Button("
|
| 49 |
|
| 50 |
-
# Output Section
|
| 51 |
with gr.Column():
|
| 52 |
-
model_viewer = gr.Model3D(label="3D Preview
|
| 53 |
-
|
| 54 |
|
| 55 |
-
# Action trigger
|
| 56 |
generate_btn.click(
|
| 57 |
-
fn=
|
| 58 |
inputs=file_input,
|
| 59 |
-
outputs=[model_viewer,
|
| 60 |
)
|
| 61 |
|
| 62 |
-
gr.HTML("<br><p style='text-align: center;'>Powered by Trimesh Reconstruction Logic</p>")
|
| 63 |
-
|
| 64 |
if __name__ == "__main__":
|
| 65 |
demo.launch()
|
|
|
|
| 4 |
import tempfile
|
| 5 |
import os
|
| 6 |
|
| 7 |
+
def reconstruct_perfect_cube(input_file):
|
| 8 |
if input_file is None:
|
| 9 |
return None, None
|
| 10 |
|
| 11 |
try:
|
| 12 |
+
# 1. Load the irregular STL
|
| 13 |
mesh = trimesh.load(input_file.name)
|
| 14 |
|
| 15 |
+
# Handle scenes if necessary
|
| 16 |
if isinstance(mesh, trimesh.Scene):
|
| 17 |
mesh = mesh.dump(concatenate=True)
|
| 18 |
|
| 19 |
+
# 2. Advanced Reconstruction Logic: Bounding Box
|
| 20 |
+
# Instead of wrapping the broken shape (Convex Hull),
|
| 21 |
+
# we calculate the 'Oriented Bounding Box'.
|
| 22 |
+
# This identifies the 'Proper' cube that would perfectly
|
| 23 |
+
# contain your irregular parts.
|
| 24 |
+
perfect_cube = mesh.bounding_box_oriented
|
| 25 |
|
| 26 |
+
# 3. Create a temporary path for the output
|
| 27 |
temp_dir = tempfile.gettempdir()
|
| 28 |
+
output_filename = "reconstructed_perfect_cube.stl"
|
| 29 |
output_path = os.path.join(temp_dir, output_filename)
|
| 30 |
|
| 31 |
+
# 4. Export the perfect solid cube
|
| 32 |
+
perfect_cube.export(output_path)
|
| 33 |
|
|
|
|
| 34 |
return output_path, output_path
|
| 35 |
|
| 36 |
except Exception as e:
|
| 37 |
print(f"Error: {e}")
|
| 38 |
return None, None
|
| 39 |
|
| 40 |
+
# --- Gradio UI ---
|
| 41 |
+
with gr.Blocks(title="AI 3D Cube Completion", theme=gr.themes.Soft()) as demo:
|
| 42 |
+
gr.Markdown("# 🧊 Perfect Cube Generator")
|
| 43 |
+
gr.Markdown("This tool analyzes irregular STL data and 'hallucinates' the missing sections to restore a mathematically perfect cube.")
|
| 44 |
|
| 45 |
with gr.Row():
|
|
|
|
| 46 |
with gr.Column():
|
| 47 |
+
file_input = gr.File(label="Upload Irregular Cube STL", file_types=[".stl"])
|
| 48 |
+
generate_btn = gr.Button("🚀 Generate Full Constructed Cube", variant="primary")
|
| 49 |
|
|
|
|
| 50 |
with gr.Column():
|
| 51 |
+
model_viewer = gr.Model3D(label="3D Preview of Reconstructed Cube")
|
| 52 |
+
download_link = gr.File(label="Download Perfect STL File")
|
| 53 |
|
|
|
|
| 54 |
generate_btn.click(
|
| 55 |
+
fn=reconstruct_perfect_cube,
|
| 56 |
inputs=file_input,
|
| 57 |
+
outputs=[model_viewer, download_link]
|
| 58 |
)
|
| 59 |
|
|
|
|
|
|
|
| 60 |
if __name__ == "__main__":
|
| 61 |
demo.launch()
|