Update app.py
Browse files
app.py
CHANGED
|
@@ -1,6 +1,7 @@
|
|
| 1 |
from skimage import img_as_uint
|
| 2 |
from skimage.filters import gaussian
|
| 3 |
from skimage.segmentation import clear_border
|
|
|
|
| 4 |
import gradio as gr
|
| 5 |
from transformers import DPTFeatureExtractor, DPTForDepthEstimation
|
| 6 |
import torch
|
|
@@ -96,23 +97,24 @@ def process_image(image_path, depth_map_path=None):
|
|
| 96 |
|
| 97 |
import cv2 # Make sure OpenCV is installed
|
| 98 |
|
| 99 |
-
|
| 100 |
def create_3d_obj(foreground, depth_image, background, image_path, depth=10):
|
| 101 |
-
# Resize depth_image with PIL
|
| 102 |
depth_image_resized = np.array(
|
| 103 |
-
Image.fromarray(
|
| 104 |
-
foreground.shape[:2][::-1], Image.Resampling.
|
| 105 |
)
|
| 106 |
)
|
|
|
|
|
|
|
|
|
|
| 107 |
|
| 108 |
-
#
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
(depth_image_resized.max() - depth_image_resized.min()) * 65535).astype(np.uint16)
|
| 114 |
|
| 115 |
-
# Convert the depth image to
|
| 116 |
depth_o3d = o3d.geometry.Image(depth_image_normalized)
|
| 117 |
image_o3d = o3d.geometry.Image(foreground)
|
| 118 |
rgbd_image = o3d.geometry.RGBDImage.create_from_color_and_depth(
|
|
@@ -139,9 +141,12 @@ def create_3d_obj(foreground, depth_image, background, image_path, depth=10):
|
|
| 139 |
else:
|
| 140 |
raise RuntimeError("Failed to estimate normals for the point cloud.")
|
| 141 |
|
|
|
|
|
|
|
|
|
|
| 142 |
# Create mesh using Poisson reconstruction
|
| 143 |
mesh_raw, densities = o3d.geometry.TriangleMesh.create_from_point_cloud_poisson(
|
| 144 |
-
pcd, depth=
|
| 145 |
)
|
| 146 |
|
| 147 |
# Simplify mesh
|
|
@@ -159,8 +164,7 @@ def create_3d_obj(foreground, depth_image, background, image_path, depth=10):
|
|
| 159 |
# Save as GLTF
|
| 160 |
gltf_path = f"./{image_path.stem}.gltf"
|
| 161 |
o3d.io.write_triangle_mesh(gltf_path, mesh_crop, write_triangle_uvs=True)
|
| 162 |
-
return gltf_path
|
| 163 |
-
|
| 164 |
# Gradio Interface
|
| 165 |
title = "Depth Estimation & 3D Reconstruction Demo"
|
| 166 |
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."
|
|
|
|
| 1 |
from skimage import img_as_uint
|
| 2 |
from skimage.filters import gaussian
|
| 3 |
from skimage.segmentation import clear_border
|
| 4 |
+
from scipy.ndimage import gaussian_filter
|
| 5 |
import gradio as gr
|
| 6 |
from transformers import DPTFeatureExtractor, DPTForDepthEstimation
|
| 7 |
import torch
|
|
|
|
| 97 |
|
| 98 |
import cv2 # Make sure OpenCV is installed
|
| 99 |
|
|
|
|
| 100 |
def create_3d_obj(foreground, depth_image, background, image_path, depth=10):
|
| 101 |
+
# Step 1: Resize depth_image with PIL using bilinear interpolation for smoother resizing
|
| 102 |
depth_image_resized = np.array(
|
| 103 |
+
Image.fromarray(depth_image.astype(np.uint16), mode='I;16').resize(
|
| 104 |
+
foreground.shape[:2][::-1], Image.Resampling.BILINEAR
|
| 105 |
)
|
| 106 |
)
|
| 107 |
+
|
| 108 |
+
# Step 2: Apply a slight Gaussian blur to the depth map to smooth out artifacts
|
| 109 |
+
depth_image_smoothed = gaussian_filter(depth_image_resized, sigma=1)
|
| 110 |
|
| 111 |
+
# Step 3: Normalize the depth image to 0-65535 range without over-compressing details
|
| 112 |
+
depth_image_normalized = (
|
| 113 |
+
(depth_image_smoothed - depth_image_smoothed.min()) /
|
| 114 |
+
(depth_image_smoothed.max() - depth_image_smoothed.min()) * 65535
|
| 115 |
+
).astype(np.uint16)
|
|
|
|
| 116 |
|
| 117 |
+
# Step 4: Convert the normalized depth image to Open3D Image object
|
| 118 |
depth_o3d = o3d.geometry.Image(depth_image_normalized)
|
| 119 |
image_o3d = o3d.geometry.Image(foreground)
|
| 120 |
rgbd_image = o3d.geometry.RGBDImage.create_from_color_and_depth(
|
|
|
|
| 141 |
else:
|
| 142 |
raise RuntimeError("Failed to estimate normals for the point cloud.")
|
| 143 |
|
| 144 |
+
# Increase the depth parameter dynamically based on image size for finer details in complex images
|
| 145 |
+
adjusted_depth = depth + int(min(w, h) / 500)
|
| 146 |
+
|
| 147 |
# Create mesh using Poisson reconstruction
|
| 148 |
mesh_raw, densities = o3d.geometry.TriangleMesh.create_from_point_cloud_poisson(
|
| 149 |
+
pcd, depth=adjusted_depth, width=0, scale=1.2, linear_fit=True
|
| 150 |
)
|
| 151 |
|
| 152 |
# Simplify mesh
|
|
|
|
| 164 |
# Save as GLTF
|
| 165 |
gltf_path = f"./{image_path.stem}.gltf"
|
| 166 |
o3d.io.write_triangle_mesh(gltf_path, mesh_crop, write_triangle_uvs=True)
|
| 167 |
+
return gltf_path
|
|
|
|
| 168 |
# Gradio Interface
|
| 169 |
title = "Depth Estimation & 3D Reconstruction Demo"
|
| 170 |
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."
|