jens commited on
Commit
9917d3b
·
1 Parent(s): 95036e2

point-cloud

Browse files
Files changed (2) hide show
  1. app.py +2 -2
  2. utils.py +35 -1
app.py CHANGED
@@ -2,14 +2,14 @@ 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
- 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 [image, gltf_path, gltf_path]#[depth_result, gltf_path, gltf_path]
 
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, create_3d_pc
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_pc(np.array(image), depth_result)
13
  #segment_predictor = SegmentPredictor()
14
  #sam_result = segment_predictor.predict(image)
15
  return [image, gltf_path, gltf_path]#[depth_result, gltf_path, gltf_path]
utils.py CHANGED
@@ -50,4 +50,38 @@ def create_3d_obj(rgb_image, depth_image, depth=10):
50
  gltf_path = './image.gltf'
51
  o3d.io.write_triangle_mesh(
52
  gltf_path, mesh_crop, write_triangle_uvs=True)
53
- return gltf_path
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
50
  gltf_path = './image.gltf'
51
  o3d.io.write_triangle_mesh(
52
  gltf_path, mesh_crop, write_triangle_uvs=True)
53
+ return gltf_path
54
+
55
+
56
+ def create_3d_pc(rgb_image, depth_image, image_path, depth=10):
57
+ depth_image = depth_image.astype(np.float32) # Convert depth map to float32
58
+ depth_o3d = o3d.geometry.Image(depth_image)
59
+ image_o3d = o3d.geometry.Image(rgb_image)
60
+ rgbd_image = o3d.geometry.RGBDImage.create_from_color_and_depth(
61
+ image_o3d, depth_o3d, convert_rgb_to_intensity=False)
62
+
63
+ w = int(depth_image.shape[1])
64
+ h = int(depth_image.shape[0])
65
+
66
+ # Specify camera intrinsic parameters (modify based on actual camera)
67
+ fx = 500
68
+ fy = 500
69
+ cx = w / 2
70
+ cy = h / 2
71
+
72
+ camera_intrinsic = o3d.camera.PinholeCameraIntrinsic(w, h, fx, fy, cx, cy)
73
+
74
+ pcd = o3d.geometry.PointCloud.create_from_rgbd_image(
75
+ rgbd_image, camera_intrinsic)
76
+
77
+ print('Estimating normals...')
78
+ pcd.estimate_normals(
79
+ search_param=o3d.geometry.KDTreeSearchParamHybrid(radius=0.01, max_nn=30))
80
+ pcd.orient_normals_towards_camera_location(
81
+ camera_location=np.array([0., 0., 1000.]))
82
+
83
+ # Save the point cloud as a PLY file
84
+ ply_path = image_path.with_suffix('.ply')
85
+ o3d.io.write_point_cloud(str(ply_path), pcd)
86
+
87
+ return str(ply_path) # Return the file path where the PLY file is saved