MalikShehram commited on
Commit
f624bf7
·
verified ·
1 Parent(s): 22b6029

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -27
app.py CHANGED
@@ -4,62 +4,58 @@ import numpy as np
4
  import tempfile
5
  import os
6
 
7
- def reconstruct_and_download(input_file):
8
  if input_file is None:
9
  return None, None
10
 
11
  try:
12
- # 1. Load the uploaded STL file
13
  mesh = trimesh.load(input_file.name)
14
 
15
- # Handle cases where the STL might be loaded as a Scene
16
  if isinstance(mesh, trimesh.Scene):
17
  mesh = mesh.dump(concatenate=True)
18
 
19
- # 2. Geometric Reconstruction (Convex Hull)
20
- # This fills the irregular cuts by calculating the smallest convex
21
- # shape that encloses all existing points—perfect for restoring a cube.
22
- reconstructed_mesh = mesh.convex_hull
 
 
23
 
24
- # 3. Create a temporary path for the output STL
25
  temp_dir = tempfile.gettempdir()
26
- output_filename = "perfect_cube_generated.stl"
27
  output_path = os.path.join(temp_dir, output_filename)
28
 
29
- # 4. Export the reconstructed mesh
30
- reconstructed_mesh.export(output_path)
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
- # --- Custom Gradio Interface ---
40
- with gr.Blocks(title="3D Shape Completion AI", theme=gr.themes.Default()) as demo:
41
- gr.Markdown("# 🧊 AI 3D Cube Reconstructor")
42
- gr.Markdown("Upload an STL cube with irregular cuts. The AI will reconstruct the missing geometry and provide a downloadable 'proper' cube.")
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("🔨 Reconstruct Perfect Shape", variant="primary")
49
 
50
- # Output Section
51
  with gr.Column():
52
- model_viewer = gr.Model3D(label="3D Preview (Completed Shape)")
53
- download_file = gr.File(label="Download Generated STL")
54
 
55
- # Action trigger
56
  generate_btn.click(
57
- fn=reconstruct_and_download,
58
  inputs=file_input,
59
- outputs=[model_viewer, download_file]
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()