Update app.py
Browse files
app.py
CHANGED
|
@@ -6,6 +6,8 @@ from PIL import Image
|
|
| 6 |
import open3d as o3d
|
| 7 |
from pathlib import Path
|
| 8 |
import os
|
|
|
|
|
|
|
| 9 |
|
| 10 |
# Initialize model and feature extractor for depth estimation
|
| 11 |
feature_extractor = DPTFeatureExtractor.from_pretrained("Intel/dpt-large")
|
|
@@ -19,6 +21,10 @@ def process_image(image_path, depth_map_path=None):
|
|
| 19 |
Image.Resampling.LANCZOS,
|
| 20 |
)
|
| 21 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
# Check if user-provided depth map is available
|
| 23 |
if depth_map_path:
|
| 24 |
depth_image_raw = Image.open(depth_map_path)
|
|
@@ -31,7 +37,7 @@ def process_image(image_path, depth_map_path=None):
|
|
| 31 |
|
| 32 |
else:
|
| 33 |
# Generate depth map using DPT model
|
| 34 |
-
encoding = feature_extractor(
|
| 35 |
with torch.no_grad():
|
| 36 |
outputs = model(**encoding)
|
| 37 |
predicted_depth = outputs.predicted_depth
|
|
@@ -44,14 +50,17 @@ def process_image(image_path, depth_map_path=None):
|
|
| 44 |
).squeeze()
|
| 45 |
depth_image = (prediction.cpu().numpy() * 255 / np.max(prediction.cpu().numpy())).astype("uint8")
|
| 46 |
|
|
|
|
|
|
|
|
|
|
| 47 |
try:
|
| 48 |
-
gltf_path = create_3d_obj(np.array(image),
|
| 49 |
-
img = Image.fromarray(
|
| 50 |
return [img, gltf_path, gltf_path]
|
| 51 |
except Exception as e:
|
| 52 |
print("Error with default depth. Retrying with a shallower depth.")
|
| 53 |
-
gltf_path = create_3d_obj(np.array(image),
|
| 54 |
-
img = Image.fromarray(
|
| 55 |
return [img, gltf_path, gltf_path]
|
| 56 |
except:
|
| 57 |
print("Error reconstructing 3D model")
|
|
@@ -82,7 +91,7 @@ def create_3d_obj(rgb_image, depth_image, image_path, depth=12):
|
|
| 82 |
pcd, depth=depth, width=0, scale=1.1, linear_fit=True
|
| 83 |
)
|
| 84 |
|
| 85 |
-
# Mesh simplification and smoothing
|
| 86 |
voxel_size = max(mesh_raw.get_max_bound() - mesh_raw.get_min_bound()) / 512
|
| 87 |
mesh = mesh_raw.simplify_vertex_clustering(
|
| 88 |
voxel_size=voxel_size,
|
|
|
|
| 6 |
import open3d as o3d
|
| 7 |
from pathlib import Path
|
| 8 |
import os
|
| 9 |
+
import cv2
|
| 10 |
+
from rembg import remove # Import the rembg library for background removal
|
| 11 |
|
| 12 |
# Initialize model and feature extractor for depth estimation
|
| 13 |
feature_extractor = DPTFeatureExtractor.from_pretrained("Intel/dpt-large")
|
|
|
|
| 21 |
Image.Resampling.LANCZOS,
|
| 22 |
)
|
| 23 |
|
| 24 |
+
# Remove background using rembg
|
| 25 |
+
foreground = remove(image_raw) # Remove background
|
| 26 |
+
foreground = Image.fromarray(np.array(foreground)) # Convert back to PIL Image
|
| 27 |
+
|
| 28 |
# Check if user-provided depth map is available
|
| 29 |
if depth_map_path:
|
| 30 |
depth_image_raw = Image.open(depth_map_path)
|
|
|
|
| 37 |
|
| 38 |
else:
|
| 39 |
# Generate depth map using DPT model
|
| 40 |
+
encoding = feature_extractor(foreground, return_tensors="pt")
|
| 41 |
with torch.no_grad():
|
| 42 |
outputs = model(**encoding)
|
| 43 |
predicted_depth = outputs.predicted_depth
|
|
|
|
| 50 |
).squeeze()
|
| 51 |
depth_image = (prediction.cpu().numpy() * 255 / np.max(prediction.cpu().numpy())).astype("uint8")
|
| 52 |
|
| 53 |
+
# Step 1: Apply Gaussian smoothing on the depth map
|
| 54 |
+
smoothed_depth_map = cv2.GaussianBlur(depth_image, (5, 5), 0)
|
| 55 |
+
|
| 56 |
try:
|
| 57 |
+
gltf_path = create_3d_obj(np.array(image), smoothed_depth_map, image_path)
|
| 58 |
+
img = Image.fromarray(smoothed_depth_map)
|
| 59 |
return [img, gltf_path, gltf_path]
|
| 60 |
except Exception as e:
|
| 61 |
print("Error with default depth. Retrying with a shallower depth.")
|
| 62 |
+
gltf_path = create_3d_obj(np.array(image), smoothed_depth_map, image_path, depth=8)
|
| 63 |
+
img = Image.fromarray(smoothed_depth_map)
|
| 64 |
return [img, gltf_path, gltf_path]
|
| 65 |
except:
|
| 66 |
print("Error reconstructing 3D model")
|
|
|
|
| 91 |
pcd, depth=depth, width=0, scale=1.1, linear_fit=True
|
| 92 |
)
|
| 93 |
|
| 94 |
+
# Step 3: Mesh simplification and smoothing
|
| 95 |
voxel_size = max(mesh_raw.get_max_bound() - mesh_raw.get_min_bound()) / 512
|
| 96 |
mesh = mesh_raw.simplify_vertex_clustering(
|
| 97 |
voxel_size=voxel_size,
|