codic commited on
Commit
c7b362a
·
verified ·
1 Parent(s): 18951ff

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -61
app.py CHANGED
@@ -87,68 +87,40 @@ def inpaint_background(image, original_image):
87
  inpainted_image = cv2.inpaint(original_image, mask, inpaintRadius=3, flags=cv2.INPAINT_TELEA)
88
  return inpainted_image
89
 
90
- def create_3d_obj(rgb_image, depth_image, image_path, inpainted_bg, depth=12):
91
- try:
92
- # Create RGBD image in Open3D format
93
- depth_o3d = o3d.geometry.Image(depth_image)
94
- image_o3d = o3d.geometry.Image(rgb_image)
95
- rgbd_image = o3d.geometry.RGBDImage.create_from_color_and_depth(
96
- image_o3d, depth_o3d, convert_rgb_to_intensity=False
97
- )
98
-
99
- w, h = depth_image.shape[1], depth_image.shape[0]
100
-
101
- # Set camera intrinsic parameters
102
- camera_intrinsic = o3d.camera.PinholeCameraIntrinsic()
103
- camera_intrinsic.set_intrinsics(w, h, 1000, 1000, w / 2, h / 2)
104
-
105
- # Create a 3D point cloud from the RGBD image
106
- pcd = o3d.geometry.PointCloud.create_from_rgbd_image(rgbd_image, camera_intrinsic)
107
- if not pcd.has_points():
108
- raise ValueError("Point cloud generation failed: No points were created from the depth image.")
109
-
110
- # Apply offset to main subject for panorama effect
111
- pcd.transform([[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, -0.05], [0, 0, 0, 1]])
112
-
113
- # Orient normals towards the camera
114
- pcd.normals = o3d.utility.Vector3dVector(np.zeros((1, 3)))
115
- pcd.estimate_normals(
116
- search_param=o3d.geometry.KDTreeSearchParamHybrid(radius=0.05, max_nn=30)
117
- )
118
- pcd.orient_normals_towards_camera_location(camera_location=np.array([0.0, 0.0, 1000.0]))
119
-
120
- # Poisson reconstruction with a flat back surface
121
- print("Starting Poisson reconstruction...")
122
- with o3d.utility.VerbosityContextManager(o3d.utility.VerbosityLevel.Debug):
123
- mesh_raw, densities = o3d.geometry.TriangleMesh.create_from_point_cloud_poisson(
124
- pcd, depth=depth, width=0, scale=1.1, linear_fit=True
125
- )
126
-
127
- if not mesh_raw.has_triangles():
128
- raise ValueError("Poisson reconstruction failed: No triangles were created in the mesh.")
129
-
130
- # Flatten the back surface of the main subject
131
- for vertex in np.asarray(mesh_raw.vertices):
132
- if vertex[2] < -0.05:
133
- vertex[2] = -0.05
134
-
135
- # Simplify the mesh while keeping details
136
- voxel_size = max(mesh_raw.get_max_bound() - mesh_raw.get_min_bound()) / 512
137
- mesh = mesh_raw.simplify_vertex_clustering(
138
- voxel_size=voxel_size,
139
- contraction=o3d.geometry.SimplificationContraction.Average,
140
- )
141
-
142
- # Save model
143
- gltf_path = f"./{image_path.stem}.gltf"
144
- o3d.io.write_triangle_mesh(gltf_path, mesh, write_triangle_uvs=True)
145
- print("3D model successfully reconstructed and saved.")
146
- return gltf_path
147
 
148
- except Exception as e:
149
- print(f"Error encountered: {e}")
150
- raise Exception("Error reconstructing 3D model") from e
151
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
152
 
153
  title = "Enhanced Depth Estimation & 3D Reconstruction with Panorama Background"
154
  description = (
 
87
  inpainted_image = cv2.inpaint(original_image, mask, inpaintRadius=3, flags=cv2.INPAINT_TELEA)
88
  return inpainted_image
89
 
90
+ def extract_subject_depth(depth_image, image):
91
+ # Assuming we have a mask or logic to isolate the main subject in the depth map
92
+ # Only retain depth of main subject, setting background to zero
93
+ mask = np.array(image.convert("L").point(lambda x: 255 if x == 0 else 0, mode="1")).astype(np.uint8)
94
+ subject_depth_map = np.where(mask == 255, depth_image, 0) # Set bg depth to zero
95
+ return subject_depth_map
96
+
97
+ def create_3d_obj(image, depth_image, image_path, inpainted_bg, main_subject_offset=0.2):
98
+ # Ensure depth_image has only main subject depth and no background depth
99
+ depth_image = np.array(depth_image)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
100
 
101
+ # Convert image and depth_image to Open3D format
102
+ rgbd_image = o3d.geometry.RGBDImage.create_from_color_and_depth(
103
+ o3d.geometry.Image(image),
104
+ o3d.geometry.Image(depth_image),
105
+ convert_rgb_to_intensity=False
106
+ )
107
+ pcd = o3d.geometry.PointCloud.create_from_rgbd_image(rgbd_image, o3d.camera.PinholeCameraIntrinsic(
108
+ width=image.shape[1], height=image.shape[0], fx=1.0, fy=1.0, cx=image.shape[1] / 2, cy=image.shape[0] / 2))
109
+
110
+ # Flatten the back of the main subject
111
+ for point in pcd.points:
112
+ if point[2] < main_subject_offset:
113
+ point[2] = main_subject_offset # Make the back flat
114
+
115
+ # Bring main subject closer to the viewer
116
+ for point in pcd.points:
117
+ if point[2] > main_subject_offset:
118
+ point[2] += main_subject_offset # Offset main subject depth
119
+
120
+ # Avoid cropping: retain original size and save the entire model
121
+ gltf_path = f"./{image_path.stem}.gltf"
122
+ o3d.io.write_triangle_mesh(gltf_path, pcd, write_triangle_uvs=True)
123
+ return gltf_path
124
 
125
  title = "Enhanced Depth Estimation & 3D Reconstruction with Panorama Background"
126
  description = (