Update app.py
Browse files
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 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
|
| 20 |
if depth_map_path:
|
| 21 |
# Load and resize the user-provided depth map to match the RGB image size
|
|
@@ -60,7 +60,103 @@ def process_image(image_path, depth_map_path=None):
|
|
| 60 |
raise Exception("Error reconstructing 3D model")
|
| 61 |
|
| 62 |
|
| 63 |
-
def create_3d_obj(rgb_image, depth_image, image_path, depth=
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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(
|
|
@@ -70,7 +166,7 @@ def create_3d_obj(rgb_image, depth_image, image_path, depth=10):
|
|
| 70 |
h = int(depth_image.shape[0])
|
| 71 |
|
| 72 |
camera_intrinsic = o3d.camera.PinholeCameraIntrinsic()
|
| 73 |
-
camera_intrinsic.set_intrinsics(w, h,
|
| 74 |
|
| 75 |
pcd = o3d.geometry.PointCloud.create_from_rgbd_image(rgbd_image, camera_intrinsic)
|
| 76 |
|
|
@@ -79,7 +175,7 @@ def create_3d_obj(rgb_image, depth_image, image_path, depth=10):
|
|
| 79 |
np.zeros((1, 3))
|
| 80 |
) # invalidate existing normals
|
| 81 |
pcd.estimate_normals(
|
| 82 |
-
search_param=o3d.geometry.KDTreeSearchParamHybrid(radius=0.
|
| 83 |
)
|
| 84 |
pcd.orient_normals_towards_camera_location(
|
| 85 |
camera_location=np.array([0.0, 0.0, 1000.0])
|
|
@@ -93,18 +189,70 @@ def create_3d_obj(rgb_image, depth_image, image_path, depth=10):
|
|
| 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()) /
|
| 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 |
bbox = pcd.get_axis_aligned_bounding_box()
|
| 104 |
mesh_crop = mesh.crop(bbox)
|
| 105 |
gltf_path = f"./{image_path.stem}.gltf"
|
| 106 |
o3d.io.write_triangle_mesh(gltf_path, mesh_crop, write_triangle_uvs=True)
|
| 107 |
return gltf_path
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 108 |
|
| 109 |
|
| 110 |
title = "Demo: Depth Estimation & 3D Reconstruction with DPT + Point Cloud"
|
|
|
|
| 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
|
|
|
|
| 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(
|
|
|
|
| 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 |
|
|
|
|
| 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])
|
|
|
|
| 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)
|
| 210 |
+
rgbd_image = o3d.geometry.RGBDImage.create_from_color_and_depth(
|
| 211 |
+
image_o3d, depth_o3d, convert_rgb_to_intensity=False
|
| 212 |
+
)
|
| 213 |
+
w = int(depth_image.shape[1])
|
| 214 |
+
h = int(depth_image.shape[0])
|
| 215 |
+
|
| 216 |
+
camera_intrinsic = o3d.camera.PinholeCameraIntrinsic()
|
| 217 |
+
camera_intrinsic.set_intrinsics(w, h, 1000, 1000, w / 2, h / 2)
|
| 218 |
+
|
| 219 |
+
pcd = o3d.geometry.PointCloud.create_from_rgbd_image(rgbd_image, camera_intrinsic)
|
| 220 |
+
|
| 221 |
+
print("normals")
|
| 222 |
+
pcd.normals = o3d.utility.Vector3dVector(
|
| 223 |
+
np.zeros((1, 3))
|
| 224 |
+
) # invalidate existing normals
|
| 225 |
+
pcd.estimate_normals(
|
| 226 |
+
search_param=o3d.geometry.KDTreeSearchParamHybrid(radius=0.05, max_nn=30)
|
| 227 |
+
)
|
| 228 |
+
pcd.orient_normals_towards_camera_location(
|
| 229 |
+
camera_location=np.array([0.0, 0.0, 1000.0])
|
| 230 |
+
)
|
| 231 |
+
pcd.transform([[1, 0, 0, 0], [0, -1, 0, 0], [0, 0, -1, 0], [0, 0, 0, 1]])
|
| 232 |
+
pcd.transform([[-1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]])
|
| 233 |
+
|
| 234 |
+
print("run Poisson surface reconstruction")
|
| 235 |
+
with o3d.utility.VerbosityContextManager(o3d.utility.VerbosityLevel.Debug) as cm:
|
| 236 |
+
mesh_raw, densities = o3d.geometry.TriangleMesh.create_from_point_cloud_poisson(
|
| 237 |
+
pcd, depth=depth, width=0, scale=1.1, linear_fit=True
|
| 238 |
+
)
|
| 239 |
+
|
| 240 |
+
voxel_size = max(mesh_raw.get_max_bound() - mesh_raw.get_min_bound()) / 512
|
| 241 |
+
print(f"voxel_size = {voxel_size:e}")
|
| 242 |
+
mesh = mesh_raw.simplify_vertex_clustering(
|
| 243 |
+
voxel_size=voxel_size,
|
| 244 |
+
contraction=o3d.geometry.SimplificationContraction.Average,
|
| 245 |
+
)
|
| 246 |
+
|
| 247 |
+
# Apply smoothing
|
| 248 |
+
mesh = mesh.filter_smooth_simple(number_of_iterations=3)
|
| 249 |
+
|
| 250 |
+
bbox = pcd.get_axis_aligned_bounding_box()
|
| 251 |
+
mesh_crop = mesh.crop(bbox)
|
| 252 |
+
gltf_path = f"./{image_path.stem}.gltf"
|
| 253 |
+
o3d.io.write_triangle_mesh(gltf_path, mesh_crop, write_triangle_uvs=True)
|
| 254 |
+
return gltf_path
|
| 255 |
+
|
| 256 |
|
| 257 |
|
| 258 |
title = "Demo: Depth Estimation & 3D Reconstruction with DPT + Point Cloud"
|