Spaces:
Runtime error
Runtime error
Create model_generator.py
Browse files- model_generator.py +35 -0
model_generator.py
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import numpy as np
|
| 2 |
+
import trimesh
|
| 3 |
+
from scipy.spatial import Delaunay
|
| 4 |
+
|
| 5 |
+
class ModelGenerator:
|
| 6 |
+
def __init__(self):
|
| 7 |
+
pass
|
| 8 |
+
|
| 9 |
+
def depth_to_points(self, depth_map, scale=1.0):
|
| 10 |
+
"""Convert depth map to 3D points"""
|
| 11 |
+
height, width = depth_map.shape
|
| 12 |
+
x, y = np.meshgrid(np.arange(width), np.arange(height))
|
| 13 |
+
|
| 14 |
+
# Create points array
|
| 15 |
+
points = np.stack([x.flatten(), y.flatten(), depth_map.flatten() * scale], axis=1)
|
| 16 |
+
|
| 17 |
+
return points
|
| 18 |
+
|
| 19 |
+
def generate_mesh(self, points):
|
| 20 |
+
"""Generate a 3D mesh from points"""
|
| 21 |
+
# Project points to 2D for triangulation
|
| 22 |
+
points_2d = points[:, :2]
|
| 23 |
+
|
| 24 |
+
# Create triangulation
|
| 25 |
+
tri = Delaunay(points_2d)
|
| 26 |
+
|
| 27 |
+
# Create mesh
|
| 28 |
+
mesh = trimesh.Trimesh(vertices=points, faces=tri.simplices)
|
| 29 |
+
|
| 30 |
+
return mesh
|
| 31 |
+
|
| 32 |
+
def save_mesh(self, mesh, filename):
|
| 33 |
+
"""Save the mesh to a file"""
|
| 34 |
+
mesh.export(filename)
|
| 35 |
+
|