Update app.py
Browse files
app.py
CHANGED
|
@@ -1,6 +1,7 @@
|
|
| 1 |
-
from skimage import
|
| 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
|
|
@@ -42,7 +43,7 @@ def process_image(image_path, depth_map_path=None):
|
|
| 42 |
# Normalize depth image to [0, 1] range
|
| 43 |
depth_image = (depth_image - np.min(depth_image)) / (np.max(depth_image) - np.min(depth_image))
|
| 44 |
depth_image = np.clip(depth_image, 0, 1)
|
| 45 |
-
depth_image =
|
| 46 |
else:
|
| 47 |
# Generate depth map using DPT model
|
| 48 |
encoding = feature_extractor(foreground, return_tensors="pt")
|
|
@@ -61,7 +62,7 @@ def process_image(image_path, depth_map_path=None):
|
|
| 61 |
# Normalize depth image to [0, 1] range before converting
|
| 62 |
depth_image = (depth_image - np.min(depth_image)) / (np.max(depth_image) - np.min(depth_image))
|
| 63 |
depth_image = np.clip(depth_image, 0, 1)
|
| 64 |
-
depth_image =
|
| 65 |
|
| 66 |
# Resize and apply lighter Gaussian smoothing for smoother transitions
|
| 67 |
depth_image = cv2.resize(depth_image, (image.size[0], image.size[1]), interpolation=cv2.INTER_CUBIC)
|
|
@@ -72,7 +73,7 @@ def process_image(image_path, depth_map_path=None):
|
|
| 72 |
depth_image = cv2.cvtColor(depth_image, cv2.COLOR_BGR2GRAY)
|
| 73 |
if len(mask_blurred.shape) == 3:
|
| 74 |
mask_blurred = cv2.cvtColor(mask_blurred, cv2.COLOR_BGR2GRAY)
|
| 75 |
-
|
| 76 |
mask_blurred_resized = cv2.resize(mask_blurred, (depth_image.shape[1], depth_image.shape[0]), interpolation=cv2.INTER_LINEAR)
|
| 77 |
|
| 78 |
# Blend depth_image with mask_blurred_resized for smoother transition
|
|
@@ -93,31 +94,41 @@ def process_image(image_path, depth_map_path=None):
|
|
| 93 |
img = Image.fromarray(blended_depth_image)
|
| 94 |
return [img, gltf_path, gltf_path]
|
| 95 |
|
| 96 |
-
|
|
|
|
|
|
|
| 97 |
def create_3d_obj(foreground, depth_image, background, image_path, depth=10):
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 105 |
|
| 106 |
-
depth_o3d = o3d.geometry.Image(
|
| 107 |
image_o3d = o3d.geometry.Image(foreground)
|
|
|
|
| 108 |
rgbd_image = o3d.geometry.RGBDImage.create_from_color_and_depth(
|
| 109 |
image_o3d, depth_o3d, convert_rgb_to_intensity=False
|
| 110 |
)
|
| 111 |
-
w, h = foreground.shape[1], foreground.shape[0]
|
| 112 |
|
|
|
|
|
|
|
| 113 |
camera_intrinsic = o3d.camera.PinholeCameraIntrinsic()
|
| 114 |
camera_intrinsic.set_intrinsics(w, h, 1500, 1500, w / 2, h / 2)
|
| 115 |
|
|
|
|
| 116 |
pcd = o3d.geometry.PointCloud.create_from_rgbd_image(rgbd_image, camera_intrinsic)
|
| 117 |
|
| 118 |
if len(pcd.points) < 100:
|
| 119 |
raise RuntimeError("Insufficient points in the point cloud for normals estimation.")
|
| 120 |
|
|
|
|
| 121 |
if not pcd.has_normals():
|
| 122 |
pcd.estimate_normals(search_param=o3d.geometry.KDTreeSearchParamHybrid(radius=0.25, max_nn=50))
|
| 123 |
|
|
@@ -126,23 +137,31 @@ def create_3d_obj(foreground, depth_image, background, image_path, depth=10):
|
|
| 126 |
else:
|
| 127 |
raise RuntimeError("Failed to estimate normals for the point cloud.")
|
| 128 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 129 |
mesh_raw, densities = o3d.geometry.TriangleMesh.create_from_point_cloud_poisson(
|
| 130 |
-
pcd, depth=
|
| 131 |
)
|
| 132 |
|
|
|
|
| 133 |
voxel_size = max(mesh_raw.get_max_bound() - mesh_raw.get_min_bound()) / 400
|
| 134 |
mesh = mesh_raw.simplify_vertex_clustering(
|
| 135 |
voxel_size=voxel_size,
|
| 136 |
contraction=o3d.geometry.SimplificationContraction.Average,
|
| 137 |
)
|
| 138 |
mesh = mesh.filter_smooth_simple(number_of_iterations=3)
|
|
|
|
|
|
|
| 139 |
bbox = pcd.get_axis_aligned_bounding_box()
|
| 140 |
mesh_crop = mesh.crop(bbox)
|
| 141 |
|
|
|
|
| 142 |
gltf_path = f"./{image_path.stem}.gltf"
|
| 143 |
o3d.io.write_triangle_mesh(gltf_path, mesh_crop, write_triangle_uvs=True)
|
| 144 |
return gltf_path
|
| 145 |
-
|
| 146 |
# Gradio Interface
|
| 147 |
title = "Depth Estimation & 3D Reconstruction Demo"
|
| 148 |
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."
|
|
@@ -166,4 +185,4 @@ iface = gr.Interface(
|
|
| 166 |
allow_flagging="never",
|
| 167 |
cache_examples=False,
|
| 168 |
)
|
| 169 |
-
iface.launch(debug=True, show_api=True, share=True)
|
|
|
|
| 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
|
|
|
|
| 43 |
# Normalize depth image to [0, 1] range
|
| 44 |
depth_image = (depth_image - np.min(depth_image)) / (np.max(depth_image) - np.min(depth_image))
|
| 45 |
depth_image = np.clip(depth_image, 0, 1)
|
| 46 |
+
depth_image = img_as_uint(depth_image) # Changed to uint16
|
| 47 |
else:
|
| 48 |
# Generate depth map using DPT model
|
| 49 |
encoding = feature_extractor(foreground, return_tensors="pt")
|
|
|
|
| 62 |
# Normalize depth image to [0, 1] range before converting
|
| 63 |
depth_image = (depth_image - np.min(depth_image)) / (np.max(depth_image) - np.min(depth_image))
|
| 64 |
depth_image = np.clip(depth_image, 0, 1)
|
| 65 |
+
depth_image = img_as_uint(depth_image) # Changed to uint16
|
| 66 |
|
| 67 |
# Resize and apply lighter Gaussian smoothing for smoother transitions
|
| 68 |
depth_image = cv2.resize(depth_image, (image.size[0], image.size[1]), interpolation=cv2.INTER_CUBIC)
|
|
|
|
| 73 |
depth_image = cv2.cvtColor(depth_image, cv2.COLOR_BGR2GRAY)
|
| 74 |
if len(mask_blurred.shape) == 3:
|
| 75 |
mask_blurred = cv2.cvtColor(mask_blurred, cv2.COLOR_BGR2GRAY)
|
| 76 |
+
|
| 77 |
mask_blurred_resized = cv2.resize(mask_blurred, (depth_image.shape[1], depth_image.shape[0]), interpolation=cv2.INTER_LINEAR)
|
| 78 |
|
| 79 |
# Blend depth_image with mask_blurred_resized for smoother transition
|
|
|
|
| 94 |
img = Image.fromarray(blended_depth_image)
|
| 95 |
return [img, gltf_path, gltf_path]
|
| 96 |
|
| 97 |
+
|
| 98 |
+
import cv2 # Make sure OpenCV is installed
|
| 99 |
+
|
| 100 |
def create_3d_obj(foreground, depth_image, background, image_path, depth=10):
|
| 101 |
+
if depth_image.ndim != 2:
|
| 102 |
+
raise ValueError("Depth image should be a 2D array, but got: {}".format(depth_image.shape))
|
| 103 |
+
|
| 104 |
+
depth_image = depth_image.astype(np.uint16)
|
| 105 |
+
|
| 106 |
+
# Convert depth image to Open3D compatible format without PIL if needed
|
| 107 |
+
depth_image_resized = cv2.resize(depth_image, (foreground.shape[1], foreground.shape[0]), interpolation=cv2.INTER_LINEAR)
|
| 108 |
+
|
| 109 |
+
# Check for valid shape and dtype
|
| 110 |
+
if depth_image_resized.ndim != 2 or depth_image_resized.shape[0] == 0 or depth_image_resized.shape[1] == 0:
|
| 111 |
+
raise ValueError(f"Resized depth image is not valid. Shape: {depth_image_resized.shape}")
|
| 112 |
|
| 113 |
+
depth_o3d = o3d.geometry.Image(depth_image_resized)
|
| 114 |
image_o3d = o3d.geometry.Image(foreground)
|
| 115 |
+
|
| 116 |
rgbd_image = o3d.geometry.RGBDImage.create_from_color_and_depth(
|
| 117 |
image_o3d, depth_o3d, convert_rgb_to_intensity=False
|
| 118 |
)
|
|
|
|
| 119 |
|
| 120 |
+
# Set camera intrinsic parameters
|
| 121 |
+
w, h = foreground.shape[1], foreground.shape[0]
|
| 122 |
camera_intrinsic = o3d.camera.PinholeCameraIntrinsic()
|
| 123 |
camera_intrinsic.set_intrinsics(w, h, 1500, 1500, w / 2, h / 2)
|
| 124 |
|
| 125 |
+
# Create point cloud from RGBD image
|
| 126 |
pcd = o3d.geometry.PointCloud.create_from_rgbd_image(rgbd_image, camera_intrinsic)
|
| 127 |
|
| 128 |
if len(pcd.points) < 100:
|
| 129 |
raise RuntimeError("Insufficient points in the point cloud for normals estimation.")
|
| 130 |
|
| 131 |
+
# Estimate normals
|
| 132 |
if not pcd.has_normals():
|
| 133 |
pcd.estimate_normals(search_param=o3d.geometry.KDTreeSearchParamHybrid(radius=0.25, max_nn=50))
|
| 134 |
|
|
|
|
| 137 |
else:
|
| 138 |
raise RuntimeError("Failed to estimate normals for the point cloud.")
|
| 139 |
|
| 140 |
+
# Increase the depth parameter dynamically based on image size for finer details in complex images
|
| 141 |
+
adjusted_depth = depth + int(min(w, h) / 500)
|
| 142 |
+
|
| 143 |
+
# Create mesh using Poisson reconstruction
|
| 144 |
mesh_raw, densities = o3d.geometry.TriangleMesh.create_from_point_cloud_poisson(
|
| 145 |
+
pcd, depth=adjusted_depth, width=0, scale=1.2, linear_fit=True
|
| 146 |
)
|
| 147 |
|
| 148 |
+
# Simplify mesh
|
| 149 |
voxel_size = max(mesh_raw.get_max_bound() - mesh_raw.get_min_bound()) / 400
|
| 150 |
mesh = mesh_raw.simplify_vertex_clustering(
|
| 151 |
voxel_size=voxel_size,
|
| 152 |
contraction=o3d.geometry.SimplificationContraction.Average,
|
| 153 |
)
|
| 154 |
mesh = mesh.filter_smooth_simple(number_of_iterations=3)
|
| 155 |
+
|
| 156 |
+
# Crop mesh to point cloud bounding box
|
| 157 |
bbox = pcd.get_axis_aligned_bounding_box()
|
| 158 |
mesh_crop = mesh.crop(bbox)
|
| 159 |
|
| 160 |
+
# Save as GLTF
|
| 161 |
gltf_path = f"./{image_path.stem}.gltf"
|
| 162 |
o3d.io.write_triangle_mesh(gltf_path, mesh_crop, write_triangle_uvs=True)
|
| 163 |
return gltf_path
|
| 164 |
+
|
| 165 |
# Gradio Interface
|
| 166 |
title = "Depth Estimation & 3D Reconstruction Demo"
|
| 167 |
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."
|
|
|
|
| 185 |
allow_flagging="never",
|
| 186 |
cache_examples=False,
|
| 187 |
)
|
| 188 |
+
iface.launch(debug=True, show_api=True, share=True)
|