Spaces:
Runtime error
Runtime error
jens
commited on
Commit
·
7598e8a
1
Parent(s):
9780d7b
3d
Browse files- app.py +4 -2
- requirements.txt +2 -1
- utils.py +53 -0
app.py
CHANGED
@@ -2,15 +2,17 @@ import gradio as gr
|
|
2 |
from segment_anything import SamAutomaticMaskGenerator, sam_model_registry
|
3 |
import supervision as sv
|
4 |
from inference import DepthPredictor, SegmentPredictor
|
5 |
-
|
|
|
6 |
|
7 |
|
8 |
def snap(image, video):
|
9 |
depth_predictor = DepthPredictor()
|
10 |
depth_result = depth_predictor.predict(image)
|
|
|
11 |
#segment_predictor = SegmentPredictor()
|
12 |
#sam_result = segment_predictor.predict(image)
|
13 |
-
return [depth_result,
|
14 |
|
15 |
|
16 |
demo = gr.Interface(
|
|
|
2 |
from segment_anything import SamAutomaticMaskGenerator, sam_model_registry
|
3 |
import supervision as sv
|
4 |
from inference import DepthPredictor, SegmentPredictor
|
5 |
+
from utils import create_3d_obj
|
6 |
+
import numpy as np
|
7 |
|
8 |
|
9 |
def snap(image, video):
|
10 |
depth_predictor = DepthPredictor()
|
11 |
depth_result = depth_predictor.predict(image)
|
12 |
+
gltf_path = create_3d_obj(np.array(image), depth_result)
|
13 |
#segment_predictor = SegmentPredictor()
|
14 |
#sam_result = segment_predictor.predict(image)
|
15 |
+
return [depth_result, gltf_path, gltf_path]
|
16 |
|
17 |
|
18 |
demo = gr.Interface(
|
requirements.txt
CHANGED
@@ -5,4 +5,5 @@ supervision
|
|
5 |
torch
|
6 |
torchvision
|
7 |
opencv-python
|
8 |
-
transformers
|
|
|
|
5 |
torch
|
6 |
torchvision
|
7 |
opencv-python
|
8 |
+
transformers
|
9 |
+
open3d
|
utils.py
ADDED
@@ -0,0 +1,53 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import numpy as np
|
2 |
+
import open3d as o3d
|
3 |
+
|
4 |
+
|
5 |
+
def create_3d_obj(rgb_image, depth_image, depth=10):
|
6 |
+
depth_o3d = o3d.geometry.Image(depth_image)
|
7 |
+
image_o3d = o3d.geometry.Image(rgb_image)
|
8 |
+
rgbd_image = o3d.geometry.RGBDImage.create_from_color_and_depth(
|
9 |
+
image_o3d, depth_o3d, convert_rgb_to_intensity=False)
|
10 |
+
w = int(depth_image.shape[1])
|
11 |
+
h = int(depth_image.shape[0])
|
12 |
+
|
13 |
+
camera_intrinsic = o3d.camera.PinholeCameraIntrinsic()
|
14 |
+
camera_intrinsic.set_intrinsics(w, h, 500, 500, w/2, h/2)
|
15 |
+
|
16 |
+
pcd = o3d.geometry.PointCloud.create_from_rgbd_image(
|
17 |
+
rgbd_image, camera_intrinsic)
|
18 |
+
|
19 |
+
print('normals')
|
20 |
+
pcd.normals = o3d.utility.Vector3dVector(
|
21 |
+
np.zeros((1, 3))) # invalidate existing normals
|
22 |
+
pcd.estimate_normals(
|
23 |
+
search_param=o3d.geometry.KDTreeSearchParamHybrid(radius=0.01, max_nn=30))
|
24 |
+
pcd.orient_normals_towards_camera_location(
|
25 |
+
camera_location=np.array([0., 0., 1000.]))
|
26 |
+
pcd.transform([[1, 0, 0, 0],
|
27 |
+
[0, -1, 0, 0],
|
28 |
+
[0, 0, -1, 0],
|
29 |
+
[0, 0, 0, 1]])
|
30 |
+
pcd.transform([[-1, 0, 0, 0],
|
31 |
+
[0, 1, 0, 0],
|
32 |
+
[0, 0, 1, 0],
|
33 |
+
[0, 0, 0, 1]])
|
34 |
+
|
35 |
+
print('run Poisson surface reconstruction')
|
36 |
+
with o3d.utility.VerbosityContextManager(o3d.utility.VerbosityLevel.Debug) as cm:
|
37 |
+
mesh_raw, densities = o3d.geometry.TriangleMesh.create_from_point_cloud_poisson(
|
38 |
+
pcd, depth=depth, width=0, scale=1.1, linear_fit=True)
|
39 |
+
|
40 |
+
voxel_size = max(mesh_raw.get_max_bound() - mesh_raw.get_min_bound()) / 256
|
41 |
+
print(f'voxel_size = {voxel_size:e}')
|
42 |
+
mesh = mesh_raw.simplify_vertex_clustering(
|
43 |
+
voxel_size=voxel_size,
|
44 |
+
contraction=o3d.geometry.SimplificationContraction.Average)
|
45 |
+
|
46 |
+
# vertices_to_remove = densities < np.quantile(densities, 0.001)
|
47 |
+
# mesh.remove_vertices_by_mask(vertices_to_remove)
|
48 |
+
bbox = pcd.get_axis_aligned_bounding_box()
|
49 |
+
mesh_crop = mesh.crop(bbox)
|
50 |
+
gltf_path = './image.gltf'
|
51 |
+
o3d.io.write_triangle_mesh(
|
52 |
+
gltf_path, mesh_crop, write_triangle_uvs=True)
|
53 |
+
return gltf_path
|