Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -7,7 +7,7 @@ from transformers import AutoImageProcessor, AutoModelForDepthEstimation
|
|
| 7 |
import tempfile
|
| 8 |
import os
|
| 9 |
|
| 10 |
-
# --- 1.
|
| 11 |
DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
|
| 12 |
CHECKPOINT = "depth-anything/Depth-Anything-V2-Small-hf"
|
| 13 |
|
|
@@ -18,11 +18,7 @@ def process_to_3d(input_image):
|
|
| 18 |
if input_image is None:
|
| 19 |
return None, None
|
| 20 |
|
| 21 |
-
#
|
| 22 |
-
if max(input_image.size) > 1024:
|
| 23 |
-
input_image.thumbnail((1024, 1024))
|
| 24 |
-
|
| 25 |
-
# --- 2. DEPTH INFERENCE ---
|
| 26 |
inputs = processor(images=input_image, return_tensors="pt").to(DEVICE)
|
| 27 |
with torch.no_grad():
|
| 28 |
outputs = model(**inputs)
|
|
@@ -32,60 +28,65 @@ def process_to_3d(input_image):
|
|
| 32 |
mode="bicubic",
|
| 33 |
).squeeze().cpu().numpy()
|
| 34 |
|
| 35 |
-
# --- 3. COLOR & COORDINATE CALCULATION ---
|
| 36 |
width, height = input_image.size
|
| 37 |
-
|
|
|
|
| 38 |
|
| 39 |
-
#
|
| 40 |
x, y = np.meshgrid(np.arange(width), np.arange(height))
|
| 41 |
|
| 42 |
-
#
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
|
|
|
|
|
|
| 47 |
|
| 48 |
-
points = np.stack((
|
| 49 |
|
| 50 |
-
# --- 4.
|
| 51 |
pcd = o3d.geometry.PointCloud()
|
| 52 |
pcd.points = o3d.utility.Vector3dVector(points)
|
| 53 |
-
pcd.colors = o3d.utility.Vector3dVector(
|
| 54 |
|
| 55 |
-
#
|
| 56 |
-
|
|
|
|
|
|
|
| 57 |
pcd.translate(-pcd.get_center())
|
| 58 |
|
| 59 |
-
#
|
| 60 |
-
pcd = pcd.voxel_down_sample(voxel_size=0.
|
| 61 |
|
| 62 |
-
# --- 5.
|
| 63 |
temp_dir = tempfile.gettempdir()
|
| 64 |
output_path = os.path.join(temp_dir, "model.ply")
|
| 65 |
-
|
| 66 |
-
# write_ascii=False is required for Binary PLY (Colors work best here)
|
| 67 |
o3d.io.write_point_cloud(output_path, pcd, write_ascii=False)
|
| 68 |
|
| 69 |
return output_path, output_path
|
| 70 |
|
| 71 |
-
# --- 6. UI ---
|
| 72 |
with gr.Blocks() as demo:
|
| 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 |
-
#
|
|
|
|
| 82 |
v3d = gr.Model3D(
|
| 83 |
label="3D Viewport",
|
| 84 |
display_mode="solid",
|
| 85 |
-
camera_position=(0, 90,
|
| 86 |
-
clear_color=(0.
|
| 87 |
)
|
| 88 |
-
dl = gr.DownloadButton("💾 Download .PLY")
|
| 89 |
|
| 90 |
btn.click(fn=process_to_3d, inputs=[img_in], outputs=[v3d, dl])
|
| 91 |
|
|
|
|
| 7 |
import tempfile
|
| 8 |
import os
|
| 9 |
|
| 10 |
+
# --- 1. MODEL SETUP ---
|
| 11 |
DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
|
| 12 |
CHECKPOINT = "depth-anything/Depth-Anything-V2-Small-hf"
|
| 13 |
|
|
|
|
| 18 |
if input_image is None:
|
| 19 |
return None, None
|
| 20 |
|
| 21 |
+
# --- 2. DEPTH & COLOR ---
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
inputs = processor(images=input_image, return_tensors="pt").to(DEVICE)
|
| 23 |
with torch.no_grad():
|
| 24 |
outputs = model(**inputs)
|
|
|
|
| 28 |
mode="bicubic",
|
| 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. THE "PERFECT FOCUS" PROJECTION ---
|
| 36 |
x, y = np.meshgrid(np.arange(width), np.arange(height))
|
| 37 |
|
| 38 |
+
# Normalize depth to a 0-5 range
|
| 39 |
+
z = (depth.flatten() / (depth.max() + 1e-5)) * 5.0
|
| 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. OPEN3D COLOR BINDING ---
|
| 49 |
pcd = o3d.geometry.PointCloud()
|
| 50 |
pcd.points = o3d.utility.Vector3dVector(points)
|
| 51 |
+
pcd.colors = o3d.utility.Vector3dVector(rgb_colors)
|
| 52 |
|
| 53 |
+
# Statistical Outlier Removal (Cleans up "ghost" points that mess up camera focus)
|
| 54 |
+
pcd, _ = pcd.remove_statistical_outlier(nb_neighbors=20, std_ratio=2.0)
|
| 55 |
+
|
| 56 |
+
# Center the model's pivot point at exactly 0,0,0
|
| 57 |
pcd.translate(-pcd.get_center())
|
| 58 |
|
| 59 |
+
# Voxelize to make points "solid"
|
| 60 |
+
pcd = pcd.voxel_down_sample(voxel_size=0.03)
|
| 61 |
|
| 62 |
+
# --- 5. SAVE BINARY PLY ---
|
| 63 |
temp_dir = tempfile.gettempdir()
|
| 64 |
output_path = os.path.join(temp_dir, "model.ply")
|
| 65 |
+
# write_ascii=False ensures color data is encoded in a way Gradio understands
|
|
|
|
| 66 |
o3d.io.write_point_cloud(output_path, pcd, write_ascii=False)
|
| 67 |
|
| 68 |
return output_path, output_path
|
| 69 |
|
| 70 |
+
# --- 6. UI WITH CAMERA FOCUS ---
|
| 71 |
with gr.Blocks() as demo:
|
| 72 |
+
gr.Markdown("## 🧊 High-Fidelity 3D Splat View")
|
| 73 |
+
gr.Markdown("The camera is pre-focused on the object. Click and drag to rotate.")
|
| 74 |
|
| 75 |
with gr.Row():
|
| 76 |
with gr.Column():
|
| 77 |
+
img_in = gr.Image(type="pil")
|
| 78 |
+
btn = gr.Button("🔨 Generate & Focus", variant="primary")
|
| 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 Viewport",
|
| 85 |
display_mode="solid",
|
| 86 |
+
camera_position=(0, 90, 12),
|
| 87 |
+
clear_color=(0.1, 0.1, 0.1, 1.0)
|
| 88 |
)
|
| 89 |
+
dl = gr.DownloadButton("💾 Download Colored .PLY")
|
| 90 |
|
| 91 |
btn.click(fn=process_to_3d, inputs=[img_in], outputs=[v3d, dl])
|
| 92 |
|