Update app.py
Browse files
app.py
CHANGED
|
@@ -11,6 +11,7 @@ from pathlib import Path
|
|
| 11 |
import os
|
| 12 |
import cv2
|
| 13 |
from rembg import remove
|
|
|
|
| 14 |
|
| 15 |
# Initialize model and feature extractor for depth estimation
|
| 16 |
feature_extractor = DPTFeatureExtractor.from_pretrained("Intel/dpt-large")
|
|
@@ -64,8 +65,9 @@ def process_image(image_path, depth_map_path=None):
|
|
| 64 |
depth_image = img_as_ubyte(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)
|
| 68 |
-
mask_blurred = cv2.GaussianBlur(depth_image, (3, 3),
|
| 69 |
|
| 70 |
# Ensure matching size and single channel for blending
|
| 71 |
if len(depth_image.shape) == 3:
|
|
@@ -87,7 +89,7 @@ def process_image(image_path, depth_map_path=None):
|
|
| 87 |
img = Image.fromarray(blended_depth_image)
|
| 88 |
return [img, gltf_path, gltf_path]
|
| 89 |
except Exception as e:
|
| 90 |
-
print(f"Error with depth=8: {str(e)}. Retrying with depth=
|
| 91 |
gltf_path = create_3d_obj(np.array(foreground), blended_depth_image, None, image_path, depth=12)
|
| 92 |
img = Image.fromarray(blended_depth_image)
|
| 93 |
return [img, gltf_path, gltf_path]
|
|
@@ -135,7 +137,11 @@ def create_3d_obj(foreground, depth_image, background, image_path, depth=14):
|
|
| 135 |
pcd, depth=depth, width=1, scale=2, linear_fit=True
|
| 136 |
)
|
| 137 |
except RuntimeError as e:
|
| 138 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 139 |
|
| 140 |
# Simplify and crop mesh
|
| 141 |
voxel_size = max(mesh_raw.get_max_bound() - mesh_raw.get_min_bound()) / 512
|
|
@@ -147,8 +153,9 @@ def create_3d_obj(foreground, depth_image, background, image_path, depth=14):
|
|
| 147 |
bbox = pcd.get_axis_aligned_bounding_box()
|
| 148 |
mesh_crop = mesh.crop(bbox)
|
| 149 |
|
| 150 |
-
# Save to glTF
|
| 151 |
-
|
|
|
|
| 152 |
o3d.io.write_triangle_mesh(gltf_path, mesh_crop, write_triangle_uvs=True)
|
| 153 |
return gltf_path
|
| 154 |
|
|
|
|
| 11 |
import os
|
| 12 |
import cv2
|
| 13 |
from rembg import remove
|
| 14 |
+
import time # For unique file names
|
| 15 |
|
| 16 |
# Initialize model and feature extractor for depth estimation
|
| 17 |
feature_extractor = DPTFeatureExtractor.from_pretrained("Intel/dpt-large")
|
|
|
|
| 65 |
depth_image = img_as_ubyte(depth_image)
|
| 66 |
|
| 67 |
# Resize and apply lighter Gaussian smoothing for smoother transitions
|
| 68 |
+
sigma_value = max(0.9, min(image.size[0] / 1024, 3.0))
|
| 69 |
depth_image = cv2.resize(depth_image, (image.size[0], image.size[1]), interpolation=cv2.INTER_CUBIC)
|
| 70 |
+
mask_blurred = cv2.GaussianBlur(depth_image, (3, 3), sigma_value) # Dynamically set blur for transition
|
| 71 |
|
| 72 |
# Ensure matching size and single channel for blending
|
| 73 |
if len(depth_image.shape) == 3:
|
|
|
|
| 89 |
img = Image.fromarray(blended_depth_image)
|
| 90 |
return [img, gltf_path, gltf_path]
|
| 91 |
except Exception as e:
|
| 92 |
+
print(f"Error with depth=8: {str(e)}. Retrying with depth=12.")
|
| 93 |
gltf_path = create_3d_obj(np.array(foreground), blended_depth_image, None, image_path, depth=12)
|
| 94 |
img = Image.fromarray(blended_depth_image)
|
| 95 |
return [img, gltf_path, gltf_path]
|
|
|
|
| 137 |
pcd, depth=depth, width=1, scale=2, linear_fit=True
|
| 138 |
)
|
| 139 |
except RuntimeError as e:
|
| 140 |
+
if depth > 2:
|
| 141 |
+
print(f"Retrying Poisson reconstruction with lower depth={depth-2}")
|
| 142 |
+
return create_3d_obj(foreground, depth_image, background, image_path, depth=depth-2)
|
| 143 |
+
else:
|
| 144 |
+
raise RuntimeError(f"Poisson reconstruction failed with minimum depth setting. Original error: {e}")
|
| 145 |
|
| 146 |
# Simplify and crop mesh
|
| 147 |
voxel_size = max(mesh_raw.get_max_bound() - mesh_raw.get_min_bound()) / 512
|
|
|
|
| 153 |
bbox = pcd.get_axis_aligned_bounding_box()
|
| 154 |
mesh_crop = mesh.crop(bbox)
|
| 155 |
|
| 156 |
+
# Save to glTF with unique identifier
|
| 157 |
+
unique_name = f"{image_path.stem}_{int(time.time())}.gltf"
|
| 158 |
+
gltf_path = f"./{unique_name}"
|
| 159 |
o3d.io.write_triangle_mesh(gltf_path, mesh_crop, write_triangle_uvs=True)
|
| 160 |
return gltf_path
|
| 161 |
|