Update app.py
Browse files
app.py
CHANGED
|
@@ -46,7 +46,7 @@ def process_image(image_path, depth_map_path=None):
|
|
| 46 |
|
| 47 |
# Normalize depth image
|
| 48 |
depth_image = (depth_image - np.min(depth_image)) / (np.max(depth_image) - np.min(depth_image))
|
| 49 |
-
depth_image =
|
| 50 |
else:
|
| 51 |
# Generate depth map using DPT model
|
| 52 |
encoding = feature_extractor(foreground, return_tensors="pt")
|
|
@@ -60,7 +60,8 @@ def process_image(image_path, depth_map_path=None):
|
|
| 60 |
mode="bicubic",
|
| 61 |
align_corners=False,
|
| 62 |
).squeeze()
|
| 63 |
-
depth_image = (prediction.cpu().numpy()
|
|
|
|
| 64 |
|
| 65 |
# Resize and apply Gaussian smoothing for smoother transitions
|
| 66 |
depth_image = cv2.resize(depth_image, (image.size[0], image.size[1]), interpolation=cv2.INTER_CUBIC)
|
|
@@ -96,6 +97,9 @@ def process_image(image_path, depth_map_path=None):
|
|
| 96 |
return [img, gltf_path, gltf_path]
|
| 97 |
|
| 98 |
def create_3d_obj(foreground, depth_image, background, image_path, depth=10):
|
|
|
|
|
|
|
|
|
|
| 99 |
depth_image_resized = np.array(
|
| 100 |
Image.fromarray(depth_image).resize(foreground.shape[:2][::-1], Image.LANCZOS)
|
| 101 |
)
|
|
@@ -108,7 +112,13 @@ def create_3d_obj(foreground, depth_image, background, image_path, depth=10):
|
|
| 108 |
# Convert to float32 for depth processing
|
| 109 |
depth_image_normalized = depth_image_normalized.astype(np.float32)
|
| 110 |
|
| 111 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 112 |
image_o3d = o3d.geometry.Image(foreground)
|
| 113 |
rgbd_image = o3d.geometry.RGBDImage.create_from_color_and_depth(
|
| 114 |
image_o3d, depth_o3d, convert_rgb_to_intensity=False
|
|
@@ -153,6 +163,7 @@ def create_3d_obj(foreground, depth_image, background, image_path, depth=10):
|
|
| 153 |
o3d.io.write_triangle_mesh(gltf_path, mesh_crop, write_triangle_uvs=True)
|
| 154 |
return gltf_path
|
| 155 |
|
|
|
|
| 156 |
# Gradio Interface
|
| 157 |
title = "Depth Estimation & 3D Reconstruction Demo"
|
| 158 |
description = "Upload an image and optionally a depth map (in .npy or image format) to generate a 3D model. If no depth map is provided, the DPT model will generate it."
|
|
|
|
| 46 |
|
| 47 |
# Normalize depth image
|
| 48 |
depth_image = (depth_image - np.min(depth_image)) / (np.max(depth_image) - np.min(depth_image))
|
| 49 |
+
depth_image = depth_image.astype('float32') # Directly convert to float32
|
| 50 |
else:
|
| 51 |
# Generate depth map using DPT model
|
| 52 |
encoding = feature_extractor(foreground, return_tensors="pt")
|
|
|
|
| 60 |
mode="bicubic",
|
| 61 |
align_corners=False,
|
| 62 |
).squeeze()
|
| 63 |
+
depth_image = (prediction.cpu().numpy() - np.min(prediction.cpu().numpy())) / (np.max(prediction.cpu().numpy()) - np.min(prediction.cpu().numpy())) # Normalize depth values
|
| 64 |
+
depth_image = depth_image.astype("float32") # Changed to float32
|
| 65 |
|
| 66 |
# Resize and apply Gaussian smoothing for smoother transitions
|
| 67 |
depth_image = cv2.resize(depth_image, (image.size[0], image.size[1]), interpolation=cv2.INTER_CUBIC)
|
|
|
|
| 97 |
return [img, gltf_path, gltf_path]
|
| 98 |
|
| 99 |
def create_3d_obj(foreground, depth_image, background, image_path, depth=10):
|
| 100 |
+
# Resize background to match the original image size (no cropping)
|
| 101 |
+
background_resized = cv2.resize(background, (foreground.shape[1], foreground.shape[0]), interpolation=cv2.INTER_LINEAR)
|
| 102 |
+
|
| 103 |
depth_image_resized = np.array(
|
| 104 |
Image.fromarray(depth_image).resize(foreground.shape[:2][::-1], Image.LANCZOS)
|
| 105 |
)
|
|
|
|
| 112 |
# Convert to float32 for depth processing
|
| 113 |
depth_image_normalized = depth_image_normalized.astype(np.float32)
|
| 114 |
|
| 115 |
+
# Create flat background depth with a small gap for empty space
|
| 116 |
+
background_depth = np.zeros_like(depth_image_normalized) + 1.5 # Adjust value for the empty space
|
| 117 |
+
|
| 118 |
+
# Combine depths to create the illusion of an empty space
|
| 119 |
+
combined_depth = np.maximum(depth_image_normalized * 0.5, background_depth)
|
| 120 |
+
|
| 121 |
+
depth_o3d = o3d.geometry.Image(combined_depth)
|
| 122 |
image_o3d = o3d.geometry.Image(foreground)
|
| 123 |
rgbd_image = o3d.geometry.RGBDImage.create_from_color_and_depth(
|
| 124 |
image_o3d, depth_o3d, convert_rgb_to_intensity=False
|
|
|
|
| 163 |
o3d.io.write_triangle_mesh(gltf_path, mesh_crop, write_triangle_uvs=True)
|
| 164 |
return gltf_path
|
| 165 |
|
| 166 |
+
|
| 167 |
# Gradio Interface
|
| 168 |
title = "Depth Estimation & 3D Reconstruction Demo"
|
| 169 |
description = "Upload an image and optionally a depth map (in .npy or image format) to generate a 3D model. If no depth map is provided, the DPT model will generate it."
|