codic commited on
Commit
3dacc67
·
verified ·
1 Parent(s): e147b5e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +4 -149
app.py CHANGED
@@ -12,10 +12,10 @@ model = DPTForDepthEstimation.from_pretrained("Intel/dpt-large")
12
  def process_image(image_path, depth_map_path=None):
13
  image_path = Path(image_path)
14
  image_raw = Image.open(image_path)
15
- image = image_raw.resize(
16
- (1600, int(1600 * image_raw.size[1] / image_raw.size[0])),
17
- Image.Resampling.LANCZOS,
18
- )
19
 
20
  if depth_map_path:
21
  # Load and resize the user-provided depth map to match the RGB image size
@@ -59,151 +59,6 @@ image = image_raw.resize(
59
  print("Error reconstructing 3D model")
60
  raise Exception("Error reconstructing 3D model")
61
 
62
-
63
- def create_3d_obj(rgb_image, depth_image, image_path, depth=12):
64
- depth_o3d = o3d.geometry.Image(depth_image)
65
- image_o3d = o3d.geometry.Image(rgb_image)
66
- rgbd_image = o3d.geometry.RGBDImage.create_from_color_and_depth(
67
- image_o3d, depth_o3d, convert_rgb_to_intensity=False
68
- )
69
- w = int(depth_image.shape[1])
70
- h = int(depth_image.shape[0])
71
-
72
- camera_intrinsic = o3d.camera.PinholeCameraIntrinsic()
73
- camera_intrinsic.set_intrinsics(w, h, 1000, 1000, w / 2, h / 2)
74
-
75
- pcd = o3d.geometry.PointCloud.create_from_rgbd_image(rgbd_image, camera_intrinsic)
76
-
77
- print("normals")
78
- pcd.normals = o3d.utility.Vector3dVector(
79
- np.zeros((1, 3))
80
- ) # invalidate existing normals
81
- pcd.estimate_normals(
82
- search_param=o3d.geometry.KDTreeSearchParamHybrid(radius=0.05, max_nn=30)
83
- )
84
- pcd.orient_normals_towards_camera_location(
85
- camera_location=np.array([0.0, 0.0, 1000.0])
86
- )
87
- pcd.transform([[1, 0, 0, 0], [0, -1, 0, 0], [0, 0, -1, 0], [0, 0, 0, 1]])
88
- pcd.transform([[-1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]])
89
-
90
- print("run Poisson surface reconstruction")
91
- with o3d.utility.VerbosityContextManager(o3d.utility.VerbosityLevel.Debug) as cm:
92
- mesh_raw, densities = o3d.geometry.TriangleMesh.create_from_point_cloud_poisson(
93
- pcd, depth=depth, width=0, scale=1.1, linear_fit=True
94
- )
95
-
96
- voxel_size = max(mesh_raw.get_max_bound() - mesh_raw.get_min_bound()) / 512
97
- print(f"voxel_size = {voxel_size:e}")
98
- mesh = mesh_raw.simplify_vertex_clustering(
99
- voxel_size=voxel_size,
100
- contraction=o3d.geometry.SimplificationContraction.Average,
101
- )
102
-
103
- # Apply smoothing
104
- mesh = mesh.filter_smooth_simple(number_of_iterations=3)
105
-
106
- bbox = pcd.get_axis_aligned_bounding_box()
107
- mesh_crop = mesh.crop(bbox)
108
- gltf_path = f"./{image_path.stem}.gltf"
109
- o3d.io.write_triangle_mesh(gltf_path, mesh_crop, write_triangle_uvs=True)
110
- return gltf_path
111
- def create_3d_obj(rgb_image, depth_image, image_path, depth=12):
112
- depth_o3d = o3d.geometry.Image(depth_image)
113
- image_o3d = o3d.geometry.Image(rgb_image)
114
- rgbd_image = o3d.geometry.RGBDImage.create_from_color_and_depth(
115
- image_o3d, depth_o3d, convert_rgb_to_intensity=False
116
- )
117
- w = int(depth_image.shape[1])
118
- h = int(depth_image.shape[0])
119
-
120
- camera_intrinsic = o3d.camera.PinholeCameraIntrinsic()
121
- camera_intrinsic.set_intrinsics(w, h, 1000, 1000, w / 2, h / 2)
122
-
123
- pcd = o3d.geometry.PointCloud.create_from_rgbd_image(rgbd_image, camera_intrinsic)
124
-
125
- print("normals")
126
- pcd.normals = o3d.utility.Vector3dVector(
127
- np.zeros((1, 3))
128
- ) # invalidate existing normals
129
- pcd.estimate_normals(
130
- search_param=o3d.geometry.KDTreeSearchParamHybrid(radius=0.05, max_nn=30)
131
- )
132
- pcd.orient_normals_towards_camera_location(
133
- camera_location=np.array([0.0, 0.0, 1000.0])
134
- )
135
- pcd.transform([[1, 0, 0, 0], [0, -1, 0, 0], [0, 0, -1, 0], [0, 0, 0, 1]])
136
- pcd.transform([[-1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]])
137
-
138
- print("run Poisson surface reconstruction")
139
- with o3d.utility.VerbosityContextManager(o3d.utility.VerbosityLevel.Debug) as cm:
140
- mesh_raw, densities = o3d.geometry.TriangleMesh.create_from_point_cloud_poisson(
141
- pcd, depth=depth, width=0, scale=1.1, linear_fit=True
142
- )
143
-
144
- voxel_size = max(mesh_raw.get_max_bound() - mesh_raw.get_min_bound()) / 512
145
- print(f"voxel_size = {voxel_size:e}")
146
- mesh = mesh_raw.simplify_vertex_clustering(
147
- voxel_size=voxel_size,
148
- contraction=o3d.geometry.SimplificationContraction.Average,
149
- )
150
-
151
- # Apply smoothing
152
- mesh = mesh.filter_smooth_simple(number_of_iterations=3)
153
-
154
- bbox = pcd.get_axis_aligned_bounding_box()
155
- mesh_crop = mesh.crop(bbox)
156
- gltf_path = f"./{image_path.stem}.gltf"
157
- o3d.io.write_triangle_mesh(gltf_path, mesh_crop, write_triangle_uvs=True)
158
- return gltf_path
159
- def create_3d_obj(rgb_image, depth_image, image_path, depth=12):
160
- depth_o3d = o3d.geometry.Image(depth_image)
161
- image_o3d = o3d.geometry.Image(rgb_image)
162
- rgbd_image = o3d.geometry.RGBDImage.create_from_color_and_depth(
163
- image_o3d, depth_o3d, convert_rgb_to_intensity=False
164
- )
165
- w = int(depth_image.shape[1])
166
- h = int(depth_image.shape[0])
167
-
168
- camera_intrinsic = o3d.camera.PinholeCameraIntrinsic()
169
- camera_intrinsic.set_intrinsics(w, h, 1000, 1000, w / 2, h / 2)
170
-
171
- pcd = o3d.geometry.PointCloud.create_from_rgbd_image(rgbd_image, camera_intrinsic)
172
-
173
- print("normals")
174
- pcd.normals = o3d.utility.Vector3dVector(
175
- np.zeros((1, 3))
176
- ) # invalidate existing normals
177
- pcd.estimate_normals(
178
- search_param=o3d.geometry.KDTreeSearchParamHybrid(radius=0.05, max_nn=30)
179
- )
180
- pcd.orient_normals_towards_camera_location(
181
- camera_location=np.array([0.0, 0.0, 1000.0])
182
- )
183
- pcd.transform([[1, 0, 0, 0], [0, -1, 0, 0], [0, 0, -1, 0], [0, 0, 0, 1]])
184
- pcd.transform([[-1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]])
185
-
186
- print("run Poisson surface reconstruction")
187
- with o3d.utility.VerbosityContextManager(o3d.utility.VerbosityLevel.Debug) as cm:
188
- mesh_raw, densities = o3d.geometry.TriangleMesh.create_from_point_cloud_poisson(
189
- pcd, depth=depth, width=0, scale=1.1, linear_fit=True
190
- )
191
-
192
- voxel_size = max(mesh_raw.get_max_bound() - mesh_raw.get_min_bound()) / 512
193
- print(f"voxel_size = {voxel_size:e}")
194
- mesh = mesh_raw.simplify_vertex_clustering(
195
- voxel_size=voxel_size,
196
- contraction=o3d.geometry.SimplificationContraction.Average,
197
- )
198
-
199
- # Apply smoothing
200
- mesh = mesh.filter_smooth_simple(number_of_iterations=3)
201
-
202
- bbox = pcd.get_axis_aligned_bounding_box()
203
- mesh_crop = mesh.crop(bbox)
204
- gltf_path = f"./{image_path.stem}.gltf"
205
- o3d.io.write_triangle_mesh(gltf_path, mesh_crop, write_triangle_uvs=True)
206
- return gltf_path
207
  def create_3d_obj(rgb_image, depth_image, image_path, depth=12):
208
  depth_o3d = o3d.geometry.Image(depth_image)
209
  image_o3d = o3d.geometry.Image(rgb_image)
 
12
  def process_image(image_path, depth_map_path=None):
13
  image_path = Path(image_path)
14
  image_raw = Image.open(image_path)
15
+ image = image_raw.resize(
16
+ (800, int(800 * image_raw.size[1] / image_raw.size[0])),
17
+ Image.Resampling.LANCZOS,
18
+ )
19
 
20
  if depth_map_path:
21
  # Load and resize the user-provided depth map to match the RGB image size
 
59
  print("Error reconstructing 3D model")
60
  raise Exception("Error reconstructing 3D model")
61
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
62
  def create_3d_obj(rgb_image, depth_image, image_path, depth=12):
63
  depth_o3d = o3d.geometry.Image(depth_image)
64
  image_o3d = o3d.geometry.Image(rgb_image)