Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -7,21 +7,26 @@ from transformers import AutoImageProcessor, AutoModelForDepthEstimation
|
|
| 7 |
import tempfile
|
| 8 |
import os
|
| 9 |
|
| 10 |
-
# --- 1. MODEL SETUP ---
|
| 11 |
DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
|
| 12 |
-
|
|
|
|
| 13 |
|
| 14 |
processor = AutoImageProcessor.from_pretrained(CHECKPOINT)
|
| 15 |
model = AutoModelForDepthEstimation.from_pretrained(CHECKPOINT).to(DEVICE)
|
| 16 |
|
| 17 |
-
def
|
| 18 |
if input_image is None:
|
| 19 |
return None, None
|
| 20 |
|
| 21 |
-
#
|
|
|
|
|
|
|
|
|
|
| 22 |
inputs = processor(images=input_image, return_tensors="pt").to(DEVICE)
|
| 23 |
with torch.no_grad():
|
| 24 |
outputs = model(**inputs)
|
|
|
|
| 25 |
depth = torch.nn.functional.interpolate(
|
| 26 |
outputs.predicted_depth.unsqueeze(1),
|
| 27 |
size=input_image.size[::-1],
|
|
@@ -29,65 +34,62 @@ def process_to_3d(input_image):
|
|
| 29 |
).squeeze().cpu().numpy()
|
| 30 |
|
| 31 |
width, height = input_image.size
|
| 32 |
-
# Explicitly pull RGB and normalize for Open3D
|
| 33 |
rgb_colors = np.array(input_image).reshape(-1, 3) / 255.0
|
| 34 |
|
| 35 |
-
# --- 3.
|
| 36 |
x, y = np.meshgrid(np.arange(width), np.arange(height))
|
|
|
|
|
|
|
| 37 |
|
| 38 |
-
#
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
# Normalize X and Y to a -5 to 5 range (Centering)
|
| 42 |
-
aspect_ratio = width / height
|
| 43 |
-
x_centered = ((x.flatten() / width) - 0.5) * 10.0 * aspect_ratio
|
| 44 |
y_centered = (0.5 - (y.flatten() / height)) * 10.0
|
| 45 |
-
|
| 46 |
points = np.stack((x_centered, y_centered, z), axis=-1)
|
| 47 |
|
| 48 |
-
# --- 4.
|
| 49 |
pcd = o3d.geometry.PointCloud()
|
| 50 |
pcd.points = o3d.utility.Vector3dVector(points)
|
| 51 |
pcd.colors = o3d.utility.Vector3dVector(rgb_colors)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
|
| 53 |
-
#
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
pcd = pcd.voxel_down_sample(voxel_size=0.03)
|
| 61 |
|
| 62 |
-
# --- 5.
|
|
|
|
|
|
|
| 63 |
temp_dir = tempfile.gettempdir()
|
| 64 |
-
output_path = os.path.join(temp_dir, "
|
| 65 |
-
#
|
| 66 |
-
o3d.io.
|
| 67 |
|
| 68 |
return output_path, output_path
|
| 69 |
|
| 70 |
-
# --- 6. UI
|
| 71 |
-
with gr.Blocks() as demo:
|
| 72 |
-
gr.Markdown("#
|
| 73 |
-
gr.Markdown("
|
| 74 |
|
| 75 |
with gr.Row():
|
| 76 |
with gr.Column():
|
| 77 |
-
img_in = gr.Image(type="pil")
|
| 78 |
-
btn = gr.Button("🔨 Generate
|
| 79 |
-
|
| 80 |
with gr.Column():
|
| 81 |
-
# camera_position: (azimuth, elevation, distance)
|
| 82 |
-
# Distance 12 is perfect for our -5 to 5 coordinate range
|
| 83 |
v3d = gr.Model3D(
|
| 84 |
-
label="3D
|
| 85 |
display_mode="solid",
|
| 86 |
-
camera_position=(0, 90,
|
| 87 |
-
clear_color=(0.1, 0.1, 0.1, 1.0)
|
| 88 |
)
|
| 89 |
-
dl = gr.DownloadButton("💾 Download Colored
|
| 90 |
|
| 91 |
-
btn.click(fn=
|
| 92 |
|
| 93 |
demo.launch()
|
|
|
|
| 7 |
import tempfile
|
| 8 |
import os
|
| 9 |
|
| 10 |
+
# --- 1. DA3 MODEL SETUP ---
|
| 11 |
DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
|
| 12 |
+
# Depth Anything V3 Small - Higher precision for 2026 workflows
|
| 13 |
+
CHECKPOINT = "depth-anything/DA3-Small"
|
| 14 |
|
| 15 |
processor = AutoImageProcessor.from_pretrained(CHECKPOINT)
|
| 16 |
model = AutoModelForDepthEstimation.from_pretrained(CHECKPOINT).to(DEVICE)
|
| 17 |
|
| 18 |
+
def process_da3_to_mesh(input_image):
|
| 19 |
if input_image is None:
|
| 20 |
return None, None
|
| 21 |
|
| 22 |
+
# Resize for processing speed
|
| 23 |
+
input_image.thumbnail((1024, 1024))
|
| 24 |
+
|
| 25 |
+
# --- 2. V3 DEPTH INFERENCE ---
|
| 26 |
inputs = processor(images=input_image, return_tensors="pt").to(DEVICE)
|
| 27 |
with torch.no_grad():
|
| 28 |
outputs = model(**inputs)
|
| 29 |
+
# DA3 provides much sharper depth maps
|
| 30 |
depth = torch.nn.functional.interpolate(
|
| 31 |
outputs.predicted_depth.unsqueeze(1),
|
| 32 |
size=input_image.size[::-1],
|
|
|
|
| 34 |
).squeeze().cpu().numpy()
|
| 35 |
|
| 36 |
width, height = input_image.size
|
|
|
|
| 37 |
rgb_colors = np.array(input_image).reshape(-1, 3) / 255.0
|
| 38 |
|
| 39 |
+
# --- 3. NORMALIZED 3D PROJECTION ---
|
| 40 |
x, y = np.meshgrid(np.arange(width), np.arange(height))
|
| 41 |
+
# DA3 depth is more linear; we scale it for a natural 3D look
|
| 42 |
+
z = (depth.flatten() / (depth.max() + 1e-5)) * 4.0
|
| 43 |
|
| 44 |
+
# Center everything in the 'Unit 10' viewing box
|
| 45 |
+
x_centered = ((x.flatten() / width) - 0.5) * 10.0 * (width / height)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
y_centered = (0.5 - (y.flatten() / height)) * 10.0
|
|
|
|
| 47 |
points = np.stack((x_centered, y_centered, z), axis=-1)
|
| 48 |
|
| 49 |
+
# --- 4. ADVANCED MESHING (POISSON RECONSTRUCTION) ---
|
| 50 |
pcd = o3d.geometry.PointCloud()
|
| 51 |
pcd.points = o3d.utility.Vector3dVector(points)
|
| 52 |
pcd.colors = o3d.utility.Vector3dVector(rgb_colors)
|
| 53 |
+
|
| 54 |
+
# Estimate Normals - DA3 needs higher search radius for its high-detail output
|
| 55 |
+
pcd.estimate_normals(search_param=o3d.geometry.KDTreeSearchParamHybrid(radius=0.2, max_nn=50))
|
| 56 |
+
pcd.orient_normals_towards_camera_location(camera_location=np.array([0., 0., 15.]))
|
| 57 |
|
| 58 |
+
# Poisson Surface Reconstruction creates a watertight "solid" shell
|
| 59 |
+
# depth=8 or 9 is the sweet spot for detail vs speed
|
| 60 |
+
mesh, densities = o3d.geometry.TriangleMesh.create_from_point_cloud_poisson(pcd, depth=9)
|
| 61 |
+
|
| 62 |
+
# Clean up the mesh (Poisson creates a 'bubble' we need to trim)
|
| 63 |
+
vertices_to_remove = densities < np.quantile(densities, 0.1)
|
| 64 |
+
mesh.remove_vertices_by_mask(vertices_to_remove)
|
|
|
|
| 65 |
|
| 66 |
+
# --- 5. FINALIZE & EXPORT ---
|
| 67 |
+
mesh.translate(-mesh.get_center()) # FORCE CENTER
|
| 68 |
+
|
| 69 |
temp_dir = tempfile.gettempdir()
|
| 70 |
+
output_path = os.path.join(temp_dir, "da3_mesh.ply")
|
| 71 |
+
# Binary PLY for Blender Color Compatibility
|
| 72 |
+
o3d.io.write_triangle_mesh(output_path, mesh, write_ascii=False)
|
| 73 |
|
| 74 |
return output_path, output_path
|
| 75 |
|
| 76 |
+
# --- 6. UI ---
|
| 77 |
+
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 78 |
+
gr.Markdown("# 🧊 Depth Anything V3: Mesh Engine")
|
| 79 |
+
gr.Markdown("Using the 2026 DA3 architecture for high-fidelity 3D reconstruction.")
|
| 80 |
|
| 81 |
with gr.Row():
|
| 82 |
with gr.Column():
|
| 83 |
+
img_in = gr.Image(type="pil", label="Source Image")
|
| 84 |
+
btn = gr.Button("🔨 Generate High-Res Mesh", variant="primary")
|
|
|
|
| 85 |
with gr.Column():
|
|
|
|
|
|
|
| 86 |
v3d = gr.Model3D(
|
| 87 |
+
label="3D Mesh Preview",
|
| 88 |
display_mode="solid",
|
| 89 |
+
camera_position=(0, 90, 15)
|
|
|
|
| 90 |
)
|
| 91 |
+
dl = gr.DownloadButton("💾 Download Colored PLY")
|
| 92 |
|
| 93 |
+
btn.click(fn=process_da3_to_mesh, inputs=[img_in], outputs=[v3d, dl])
|
| 94 |
|
| 95 |
demo.launch()
|