sudo-soldier commited on
Commit
baa52c5
·
verified ·
1 Parent(s): 9fac7e4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -28
app.py CHANGED
@@ -43,42 +43,47 @@ def process_image(image_path):
43
  else:
44
  depth_image = np.zeros_like(output, dtype='uint8') # Handle empty output
45
 
46
- try:
47
- gltf_path = create_3d_obj(np.array(image), depth_image, image_path)
48
- except Exception:
49
- gltf_path = create_3d_obj(np.array(image), depth_image, image_path, depth=8)
50
 
51
- return Image.fromarray(depth_image), gltf_path, gltf_path
 
 
 
52
 
53
- def create_3d_obj(rgb_image, depth_image, image_path, depth=10):
54
- depth_o3d = o3d.geometry.Image(depth_image)
55
- image_o3d = o3d.geometry.Image(rgb_image)
56
- rgbd_image = o3d.geometry.RGBDImage.create_from_color_and_depth(
57
- image_o3d, depth_o3d, convert_rgb_to_intensity=False)
 
58
 
59
- w, h = depth_image.shape[1], depth_image.shape[0]
60
- camera_intrinsic = o3d.camera.PinholeCameraIntrinsic()
61
- camera_intrinsic.set_intrinsics(w, h, 500, 500, w / 2, h / 2)
62
 
63
- pcd = o3d.geometry.PointCloud.create_from_rgbd_image(rgbd_image, camera_intrinsic)
64
- pcd.estimate_normals(
65
- search_param=o3d.geometry.KDTreeSearchParamHybrid(radius=0.01, max_nn=30))
66
- pcd.orient_normals_towards_camera_location(camera_location=np.array([0., 0., 1000.]))
67
 
68
- with o3d.utility.VerbosityContextManager(o3d.utility.VerbosityLevel.Debug):
69
  mesh_raw, _ = o3d.geometry.TriangleMesh.create_from_point_cloud_poisson(
70
- pcd, depth=depth, width=0, scale=1.1, linear_fit=True)
71
 
72
- voxel_size = max(mesh_raw.get_max_bound() - mesh_raw.get_min_bound()) / 256
73
- mesh = mesh_raw.simplify_vertex_clustering(voxel_size=voxel_size)
 
 
 
 
74
 
75
- bbox = pcd.get_axis_aligned_bounding_box()
76
- mesh_crop = mesh.crop(bbox)
77
-
78
- gltf_path = f'./{image_path.stem}.gltf'
79
- o3d.io.write_triangle_mesh(gltf_path, mesh_crop, write_triangle_uvs=True)
80
-
81
- return gltf_path
 
82
 
83
  title = "Zero-shot Depth Estimation with DPT + 3D Model Preview"
84
  description = "Upload an image to generate a depth map and reconstruct a 3D model in .gltf format."
@@ -109,3 +114,4 @@ if __name__ == "__main__":
109
 
110
 
111
 
 
 
43
  else:
44
  depth_image = np.zeros_like(output, dtype='uint8') # Handle empty output
45
 
46
+ gltf_path = create_3d_obj(np.array(image), depth_image, image_path)
 
 
 
47
 
48
+ if gltf_path and Path(gltf_path).exists():
49
+ return Image.fromarray(depth_image), gltf_path, gltf_path
50
+ else:
51
+ return Image.fromarray(depth_image), None, "3D model generation failed"
52
 
53
+ def create_3d_obj(rgb_image, depth_image, image_path):
54
+ try:
55
+ depth_o3d = o3d.geometry.Image(depth_image)
56
+ image_o3d = o3d.geometry.Image(rgb_image)
57
+ rgbd_image = o3d.geometry.RGBDImage.create_from_color_and_depth(
58
+ image_o3d, depth_o3d, convert_rgb_to_intensity=False)
59
 
60
+ w, h = depth_image.shape[1], depth_image.shape[0]
61
+ camera_intrinsic = o3d.camera.PinholeCameraIntrinsic()
62
+ camera_intrinsic.set_intrinsics(w, h, 500, 500, w / 2, h / 2)
63
 
64
+ pcd = o3d.geometry.PointCloud.create_from_rgbd_image(rgbd_image, camera_intrinsic)
65
+ pcd.estimate_normals(
66
+ search_param=o3d.geometry.KDTreeSearchParamHybrid(radius=0.01, max_nn=30))
67
+ pcd.orient_normals_towards_camera_location(camera_location=np.array([0., 0., 1000.]))
68
 
 
69
  mesh_raw, _ = o3d.geometry.TriangleMesh.create_from_point_cloud_poisson(
70
+ pcd, depth=10, width=0, scale=1.1, linear_fit=True)
71
 
72
+ if not mesh_raw.has_triangles():
73
+ return None # Mesh generation failed
74
+
75
+ # Center the mesh for better preview
76
+ bbox = pcd.get_axis_aligned_bounding_box()
77
+ mesh_raw.translate(-bbox.get_center())
78
 
79
+ # Save the 3D model
80
+ gltf_path = f'./{image_path.stem}.gltf'
81
+ o3d.io.write_triangle_mesh(gltf_path, mesh_raw, write_triangle_uvs=True)
82
+ return gltf_path
83
+
84
+ except Exception as e:
85
+ print(f"3D model generation failed: {e}")
86
+ return None
87
 
88
  title = "Zero-shot Depth Estimation with DPT + 3D Model Preview"
89
  description = "Upload an image to generate a depth map and reconstruct a 3D model in .gltf format."
 
114
 
115
 
116
 
117
+