Stable-X commited on
Commit
46bebed
1 Parent(s): 01ea510

fix: Update geometry_export

Browse files
Files changed (1) hide show
  1. app.py +65 -8
app.py CHANGED
@@ -40,18 +40,75 @@ class Examples(GradioExamples):
40
  self.cached_file = Path(self.cached_folder) / "log.csv"
41
  self.create()
42
 
43
- def export_geometry(geometry):
44
- output_path = tempfile.mktemp(suffix='.obj')
45
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
46
  # Apply rotation
47
  rot = np.eye(4)
48
  rot[:3, :3] = Rotation.from_euler('y', np.deg2rad(180)).as_matrix()
49
  transform = np.linalg.inv(OPENGL @ rot)
50
- geometry.transform(transform)
51
-
52
- o3d.io.write_triangle_mesh(output_path, geometry, write_ascii=False, compressed=True)
53
 
54
- return output_path
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
55
 
56
 
57
  def extract_frames(video_path: str, duration: float = 20.0, fps: float = 3.0) -> str:
@@ -316,7 +373,7 @@ def reconstruct(video_path, conf_thresh, kf_every,
316
  # Create 3D model result using gaussian splatting
317
  return coarse_output_path, gs_output_path
318
  else:
319
- pcd_output_path = export_geometry(pcd_combined)
320
  return coarse_output_path, pcd_output_path
321
 
322
  # Clean up temporary directory
 
40
  self.cached_file = Path(self.cached_folder) / "log.csv"
41
  self.create()
42
 
43
+ def export_geometry(geometry, file_format='obj'):
44
+ """
45
+ Export Open3D geometry (triangle mesh or point cloud) to a file.
46
+
47
+ Args:
48
+ geometry: Open3D geometry object (TriangleMesh or PointCloud)
49
+ file_format: str, output format ('obj', 'ply', 'pcd')
50
+
51
+ Returns:
52
+ str: Path to the exported file
53
+
54
+ Raises:
55
+ ValueError: If geometry type is not supported or file format is invalid
56
+ """
57
+ # Validate geometry type
58
+ if not isinstance(geometry, (o3d.geometry.TriangleMesh, o3d.geometry.PointCloud)):
59
+ raise ValueError("Geometry must be either TriangleMesh or PointCloud")
60
+
61
+ # Validate and set file format
62
+ supported_formats = {
63
+ 'obj': '.obj',
64
+ 'ply': '.ply',
65
+ 'pcd': '.pcd'
66
+ }
67
+
68
+ if file_format.lower() not in supported_formats:
69
+ raise ValueError(f"Unsupported file format. Supported formats: {list(supported_formats.keys())}")
70
+
71
+ # Create temporary file with appropriate extension
72
+ output_path = tempfile.mktemp(suffix=supported_formats[file_format.lower()])
73
+
74
+ # Create a copy of the geometry to avoid modifying the original
75
+ geometry_copy = geometry.clone()
76
+
77
  # Apply rotation
78
  rot = np.eye(4)
79
  rot[:3, :3] = Rotation.from_euler('y', np.deg2rad(180)).as_matrix()
80
  transform = np.linalg.inv(OPENGL @ rot)
 
 
 
81
 
82
+ # Transform geometry
83
+ geometry_copy.transform(transform)
84
+
85
+ # Export based on geometry type and format
86
+ try:
87
+ if isinstance(geometry_copy, o3d.geometry.TriangleMesh):
88
+ if file_format.lower() == 'obj':
89
+ o3d.io.write_triangle_mesh(output_path, geometry_copy,
90
+ write_ascii=False, compressed=True)
91
+ elif file_format.lower() == 'ply':
92
+ o3d.io.write_triangle_mesh(output_path, geometry_copy,
93
+ write_ascii=False, compressed=True)
94
+
95
+ elif isinstance(geometry_copy, o3d.geometry.PointCloud):
96
+ if file_format.lower() == 'pcd':
97
+ o3d.io.write_point_cloud(output_path, geometry_copy,
98
+ write_ascii=False, compressed=True)
99
+ elif file_format.lower() == 'ply':
100
+ o3d.io.write_point_cloud(output_path, geometry_copy,
101
+ write_ascii=False, compressed=True)
102
+ else:
103
+ raise ValueError(f"Format {file_format} not supported for point clouds. Use 'ply' or 'pcd'")
104
+
105
+ return output_path
106
+
107
+ except Exception as e:
108
+ # Clean up temporary file if export fails
109
+ if os.path.exists(output_path):
110
+ os.remove(output_path)
111
+ raise RuntimeError(f"Failed to export geometry: {str(e)}")
112
 
113
 
114
  def extract_frames(video_path: str, duration: float = 20.0, fps: float = 3.0) -> str:
 
373
  # Create 3D model result using gaussian splatting
374
  return coarse_output_path, gs_output_path
375
  else:
376
+ pcd_output_path = export_geometry(pcd_combined, file_format='ply')
377
  return coarse_output_path, pcd_output_path
378
 
379
  # Clean up temporary directory