Update app.py
Browse files
app.py
CHANGED
|
@@ -7,7 +7,7 @@ import torch
|
|
| 7 |
import numpy as np
|
| 8 |
from PIL import Image
|
| 9 |
import open3d as o3d
|
| 10 |
-
import
|
| 11 |
from pathlib import Path
|
| 12 |
import os
|
| 13 |
import cv2
|
|
@@ -87,20 +87,6 @@ def process_image(image_path, depth_map_path=None):
|
|
| 87 |
return [img, gltf_path, gltf_path]
|
| 88 |
|
| 89 |
def create_3d_obj(foreground, depth_image, image_path, depth=12, edge_strength=0.15):
|
| 90 |
-
"""
|
| 91 |
-
Generates a 3D model by combining an image and a masked depth map focused only on the main subject.
|
| 92 |
-
|
| 93 |
-
Parameters:
|
| 94 |
-
foreground (np.array): The main subject image (RGB).
|
| 95 |
-
depth_image (np.array): The depth map array for the main subject only.
|
| 96 |
-
image_path (str): Path to save the output model.
|
| 97 |
-
depth (int): Poisson reconstruction depth value for high detail.
|
| 98 |
-
edge_strength (float): Strength of the edge thresholding for sharper subject boundaries.
|
| 99 |
-
|
| 100 |
-
Returns:
|
| 101 |
-
str: Path to the saved 3D model (.gltf).
|
| 102 |
-
"""
|
| 103 |
-
|
| 104 |
# Resize and normalize depth image to align with the foreground dimensions
|
| 105 |
depth_image_resized = np.array(
|
| 106 |
Image.fromarray((depth_image * 255).astype(np.uint8)).resize(foreground.shape[:2][::-1], Image.LANCZOS)
|
|
@@ -110,54 +96,45 @@ def create_3d_obj(foreground, depth_image, image_path, depth=12, edge_strength=0
|
|
| 110 |
depth_image_normalized = ((depth_image_resized - depth_image_resized.min()) /
|
| 111 |
(depth_image_resized.max() - depth_image_resized.min()) * 255.0).astype(np.float32)
|
| 112 |
|
| 113 |
-
# Apply edge detection to enhance the subject’s edges
|
| 114 |
sobel_x = sobel(depth_image_normalized, axis=0)
|
| 115 |
sobel_y = sobel(depth_image_normalized, axis=1)
|
| 116 |
gradient_magnitude = np.hypot(sobel_x, sobel_y)
|
| 117 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 118 |
# Generate an edge mask that captures only strong edges
|
| 119 |
-
edge_mask =
|
| 120 |
|
| 121 |
# Keep depth values only within the edge mask area (isolating the main subject)
|
| 122 |
depth_image_sharpened = np.where(edge_mask, depth_image_normalized, 0)
|
| 123 |
|
| 124 |
-
#
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
pymesh.resolve_self_intersection(mesh_data) # Fix intersecting faces for better 3D cohesion
|
| 130 |
-
|
| 131 |
-
# Convert the PyMesh data to an Open3D point cloud for further refinement
|
| 132 |
-
vertices = mesh_data.vertices
|
| 133 |
-
faces = mesh_data.faces
|
| 134 |
-
|
| 135 |
-
# Create an Open3D triangle mesh from vertices and faces
|
| 136 |
-
mesh = o3d.geometry.TriangleMesh()
|
| 137 |
-
mesh.vertices = o3d.utility.Vector3dVector(vertices)
|
| 138 |
-
mesh.triangles = o3d.utility.Vector3iVector(faces)
|
| 139 |
-
|
| 140 |
-
# Compute vertex normals for better visualization
|
| 141 |
-
mesh.compute_vertex_normals()
|
| 142 |
|
| 143 |
-
#
|
| 144 |
-
mesh
|
|
|
|
|
|
|
| 145 |
|
| 146 |
-
#
|
| 147 |
-
|
| 148 |
-
image_o3d = o3d.geometry.Image(foreground)
|
| 149 |
-
rgbd_image = o3d.geometry.RGBDImage.create_from_color_and_depth(
|
| 150 |
-
image_o3d, depth_o3d, convert_rgb_to_intensity=False
|
| 151 |
-
)
|
| 152 |
|
| 153 |
-
#
|
| 154 |
-
|
| 155 |
-
camera_intrinsic = o3d.camera.PinholeCameraIntrinsic(w, h, 3000, 3000, w / 2, h / 2)
|
| 156 |
|
| 157 |
-
# Generate point cloud
|
| 158 |
-
pcd = o3d.geometry.PointCloud
|
|
|
|
| 159 |
|
| 160 |
-
# Check minimum point count
|
| 161 |
if len(pcd.points) < 100:
|
| 162 |
raise RuntimeError("Insufficient points in the point cloud for normals estimation.")
|
| 163 |
|
|
@@ -184,9 +161,16 @@ def create_3d_obj(foreground, depth_image, image_path, depth=12, edge_strength=0
|
|
| 184 |
contraction=o3d.geometry.SimplificationContraction.Average,
|
| 185 |
)
|
| 186 |
|
| 187 |
-
# Apply
|
| 188 |
mesh = mesh.filter_smooth_laplacian(number_of_iterations=4)
|
| 189 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 190 |
# Crop mesh to bounding box around main subject only
|
| 191 |
mesh_crop = mesh.crop(bbox)
|
| 192 |
|
|
|
|
| 7 |
import numpy as np
|
| 8 |
from PIL import Image
|
| 9 |
import open3d as o3d
|
| 10 |
+
import trimesh
|
| 11 |
from pathlib import Path
|
| 12 |
import os
|
| 13 |
import cv2
|
|
|
|
| 87 |
return [img, gltf_path, gltf_path]
|
| 88 |
|
| 89 |
def create_3d_obj(foreground, depth_image, image_path, depth=12, edge_strength=0.15):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 90 |
# Resize and normalize depth image to align with the foreground dimensions
|
| 91 |
depth_image_resized = np.array(
|
| 92 |
Image.fromarray((depth_image * 255).astype(np.uint8)).resize(foreground.shape[:2][::-1], Image.LANCZOS)
|
|
|
|
| 96 |
depth_image_normalized = ((depth_image_resized - depth_image_resized.min()) /
|
| 97 |
(depth_image_resized.max() - depth_image_resized.min()) * 255.0).astype(np.float32)
|
| 98 |
|
| 99 |
+
# Apply Sobel edge detection to enhance the subject’s edges
|
| 100 |
sobel_x = sobel(depth_image_normalized, axis=0)
|
| 101 |
sobel_y = sobel(depth_image_normalized, axis=1)
|
| 102 |
gradient_magnitude = np.hypot(sobel_x, sobel_y)
|
| 103 |
|
| 104 |
+
# Multi-scale edge detection to refine boundaries further
|
| 105 |
+
edges_high_res = sobel(gaussian_filter(depth_image_normalized, sigma=1), axis=0) + sobel(gaussian_filter(depth_image_normalized, sigma=1), axis=1)
|
| 106 |
+
edges_low_res = sobel(gaussian_filter(depth_image_normalized, sigma=2), axis=0) + sobel(gaussian_filter(depth_image_normalized, sigma=2), axis=1)
|
| 107 |
+
|
| 108 |
+
combined_edges = np.maximum(edges_high_res, edges_low_res)
|
| 109 |
+
|
| 110 |
# Generate an edge mask that captures only strong edges
|
| 111 |
+
edge_mask = combined_edges < (combined_edges.max() * edge_strength)
|
| 112 |
|
| 113 |
# Keep depth values only within the edge mask area (isolating the main subject)
|
| 114 |
depth_image_sharpened = np.where(edge_mask, depth_image_normalized, 0)
|
| 115 |
|
| 116 |
+
# Create mesh data using Trimesh with the refined depth map
|
| 117 |
+
vertices, faces, normals, values = trimesh.voxel.creation.voxelize(
|
| 118 |
+
depth_image_sharpened, pitch=1
|
| 119 |
+
)
|
| 120 |
+
mesh = trimesh.Trimesh(vertices=vertices, faces=faces, process=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 121 |
|
| 122 |
+
# Apply Trimesh filters for further refinement
|
| 123 |
+
mesh.remove_degenerate_faces()
|
| 124 |
+
mesh.remove_duplicate_faces()
|
| 125 |
+
mesh.fill_holes()
|
| 126 |
|
| 127 |
+
# Additional cleanup step: Remove small, low-volume faces
|
| 128 |
+
mesh = mesh.remove_small_faces(min_area=500)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 129 |
|
| 130 |
+
# Smooth the mesh (optional)
|
| 131 |
+
mesh = mesh.smooth()
|
|
|
|
| 132 |
|
| 133 |
+
# Generate Open3D point cloud for additional transformations
|
| 134 |
+
pcd = o3d.geometry.PointCloud()
|
| 135 |
+
pcd.points = o3d.utility.Vector3dVector(vertices)
|
| 136 |
|
| 137 |
+
# Check for minimum point count
|
| 138 |
if len(pcd.points) < 100:
|
| 139 |
raise RuntimeError("Insufficient points in the point cloud for normals estimation.")
|
| 140 |
|
|
|
|
| 161 |
contraction=o3d.geometry.SimplificationContraction.Average,
|
| 162 |
)
|
| 163 |
|
| 164 |
+
# Apply edge-aware smoothing (bilateral filter in 3D)
|
| 165 |
mesh = mesh.filter_smooth_laplacian(number_of_iterations=4)
|
| 166 |
+
|
| 167 |
+
# Apply an edge-aware bilateral filter to retain small features
|
| 168 |
+
pcd_filtered = pcd.crop(bbox) # Crop to the subject's bounds
|
| 169 |
+
pcd_filtered = o3d.geometry.PointCloud()
|
| 170 |
+
pcd_filtered.points = o3d.utility.Vector3dVector(np.array(mesh.vertices)) # Update with filtered points
|
| 171 |
+
|
| 172 |
+
mesh = o3d.geometry.TriangleMesh.create_from_point_cloud_poisson(pcd_filtered, depth=12)
|
| 173 |
+
|
| 174 |
# Crop mesh to bounding box around main subject only
|
| 175 |
mesh_crop = mesh.crop(bbox)
|
| 176 |
|