File size: 9,085 Bytes
254216d
c82a68e
1f60aa2
c82a68e
1f60aa2
 
 
c82a68e
1f60aa2
254216d
 
 
044aa6a
254216d
044aa6a
c82a68e
 
044aa6a
c82a68e
 
044aa6a
 
 
254216d
 
 
 
 
c82a68e
044aa6a
c82a68e
 
044aa6a
c82a68e
 
254216d
c82a68e
044aa6a
 
c82a68e
 
044aa6a
 
c82a68e
044aa6a
254216d
c82a68e
 
 
 
 
254216d
 
044aa6a
 
 
254216d
 
044aa6a
 
 
 
 
 
 
 
 
 
c82a68e
044aa6a
 
c82a68e
044aa6a
1f60aa2
 
c82a68e
 
 
254216d
 
c82a68e
 
254216d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c82a68e
 
254216d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c82a68e
044aa6a
254216d
1f60aa2
254216d
 
 
c82a68e
254216d
044aa6a
c82a68e
254216d
c82a68e
044aa6a
 
c82a68e
044aa6a
254216d
 
 
 
 
 
 
 
 
 
 
c82a68e
044aa6a
 
 
c82a68e
254216d
c82a68e
044aa6a
 
 
 
c82a68e
 
044aa6a
c82a68e
1f60aa2
254216d
 
1f60aa2
044aa6a
254216d
1f60aa2
c82a68e
044aa6a
 
 
254216d
 
 
044aa6a
c82a68e
 
 
 
1f60aa2
044aa6a
c82a68e
 
254216d
c82a68e
044aa6a
 
 
c82a68e
 
1f60aa2
7318ab6
c82a68e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
import os, time, datetime
from pathlib import Path
import numpy as np
from PIL import Image

import torch
from transformers import GLPNForDepthEstimation, GLPNImageProcessor
import gradio as gr

# ---- Keep Spaces stable (CPU-safe; quiet threading) ----
os.environ.setdefault("CUDA_VISIBLE_DEVICES", "")
os.environ.setdefault("PYTORCH_ENABLE_MPS_FALLBACK", "1")
os.environ.setdefault("OMP_NUM_THREADS", "1")
os.environ.setdefault("OPENBLAS_NUM_THREADS", "1")

DEVICE = torch.device(
    "cuda" if torch.cuda.is_available()
    else ("mps" if getattr(torch.backends, "mps", None) and torch.backends.mps.is_available() else "cpu")
)

PROC = GLPNImageProcessor.from_pretrained("vinvino02/glpn-nyu")
MODEL = GLPNForDepthEstimation.from_pretrained("vinvino02/glpn-nyu").to(DEVICE).eval()

# Import Open3D (fail fast if missing)
import open3d as o3d

OUT_DIR = Path("outputs")
OUT_DIR.mkdir(parents=True, exist_ok=True)

def _resize_h480_m32(pil_img: Image.Image):
    h = min(pil_img.height, 480)
    h -= (h % 32)
    w = max(1, int(h * pil_img.width / max(1, pil_img.height)))
    return pil_img.resize((w, h), Image.BILINEAR)

def _infer_depth(pil_img: Image.Image, logs):
    t0 = time.time()
    img_proc = _resize_h480_m32(pil_img)
    inputs = PROC(images=img_proc, return_tensors="pt")
    with torch.no_grad():
        inputs = {k: v.to(DEVICE) for k, v in inputs.items()}
        out = MODEL(**inputs)
    pred = getattr(out, "predicted_depth", None)
    if pred is None:
        pred = out[0] if isinstance(out, (tuple, list)) else next(iter(out.values()))
    if pred.dim() == 3:
        pred = pred.unsqueeze(1)
    pred = torch.nn.functional.interpolate(
        pred, size=pil_img.size[::-1], mode="bicubic", align_corners=False
    ).squeeze(0).squeeze(0)
    depth = pred.detach().cpu().float().numpy()
    logs.append(f"[Depth] shape={depth.shape} device={DEVICE} time={time.time()-t0:.2f}s")
    return depth

def _depth_preview(depth: np.ndarray) -> Image.Image:
    d = depth - float(depth.min())
    rng = float(d.max()) + 1e-8
    d /= rng
    return Image.fromarray((d * 255).astype(np.uint8))

def _to_u16(depth: np.ndarray) -> np.ndarray:
    d = depth - float(depth.min())
    d /= (float(d.max()) + 1e-8)
    out = (d * 65535.0).astype(np.uint16)
    out[out == 0] = 1
    return out

def _rgbd_intrinsics(rgb: np.ndarray, depth_u16: np.ndarray, fx, fy):
    h, w = depth_u16.shape
    color = o3d.geometry.Image(rgb.astype(np.uint8))
    depth = o3d.geometry.Image(depth_u16)
    rgbd = o3d.geometry.RGBDImage.create_from_color_and_depth(
        color, depth, convert_rgb_to_intensity=False, depth_trunc=65535.0, depth_scale=1.0
    )
    intr = o3d.camera.PinholeCameraIntrinsic()
    intr.set_intrinsics(w, h, fx, fy, w/2.0, h/2.0)
    return rgbd, intr

def _make_pointcloud(rgbd, intr, logs, nb_neighbors=20, std_ratio=20.0, down_voxel=0.0):
    t0 = time.time()
    pcd = o3d.geometry.PointCloud.create_from_rgbd_image(rgbd, intr)

    # If extremely sparse, skip aggressive outlier removal
    if np.asarray(pcd.points).shape[0] > 500:
        _, ind = pcd.remove_statistical_outlier(nb_neighbors=nb_neighbors, std_ratio=std_ratio)
        if len(ind) > 50:  # keep at least some points
            pcd = pcd.select_by_index(ind)
        else:
            logs.append("[PCD] Outlier removal would drop almost all points; skipping.")
    else:
        logs.append("[PCD] Too few points for outlier removal; skipping.")

    if down_voxel and down_voxel > 0:
        pcd = pcd.voxel_down_sample(voxel_size=float(down_voxel))

    npts = np.asarray(pcd.points).shape[0]
    logs.append(f"[PCD] points={npts} time={time.time()-t0:.2f}s (voxel={down_voxel})")
    return pcd

def _make_mesh_with_fallback(pcd, poisson_depth, logs, method="poisson"):
    t0 = time.time()
    if np.asarray(pcd.points).shape[0] < 30:
        raise RuntimeError("Point cloud too small for meshing.")

    pcd.estimate_normals()
    pcd.orient_normals_to_align_with_direction()

    try:
        if method == "poisson":
            # Many Open3D wheels don’t support n_threads kwarg; don’t pass it.
            mesh = o3d.geometry.TriangleMesh.create_from_point_cloud_poisson(
                pcd, depth=int(poisson_depth)
            )[0]
            used = "Poisson"
        else:
            # Ball-Pivoting fallback
            distances = pcd.compute_nearest_neighbor_distance()
            if not distances:
                raise RuntimeError("No neighbor distances for Ball-Pivoting.")
            avg = float(sum(distances)) / len(distances)
            radii = [avg * r for r in (1.5, 2.5)]
            mesh = o3d.geometry.TriangleMesh.create_from_point_cloud_ball_pivoting(
                pcd, o3d.utility.DoubleVector(radii)
            )
            used = "Ball-Pivoting"

        # Post clean & orient
        mesh.remove_duplicated_vertices()
        mesh.remove_duplicated_triangles()
        mesh.remove_degenerate_triangles()
        mesh.remove_non_manifold_edges()
        R = mesh.get_rotation_matrix_from_xyz((np.pi, 0, 0))
        mesh.rotate(R, center=(0, 0, 0))

        v = np.asarray(mesh.vertices).shape[0]
        f = np.asarray(mesh.triangles).shape[0]
        logs.append(f"[Mesh] method={used} V={v} F={f} time={time.time()-t0:.2f}s")
        return mesh
    except Exception as e:
        if method == "poisson":
            logs.append(f"[Mesh] Poisson failed: {e}. Falling back to Ball-Pivoting…")
            return _make_mesh_with_fallback(pcd, poisson_depth, logs, method="ball")
        raise

def _timestamped(name: str, ext: str) -> Path:
    ts = datetime.datetime.utcnow().strftime("%Y%m%d_%H%M%S")
    return OUT_DIR / f"{name}_{ts}.{ext}"

def run(pil_img: Image.Image, fx: int, fy: int, poisson_depth: int, down_voxel: float, verbose: bool):
    logs = []
    try:
        if pil_img is None:
            return None, None, None, None, None, None, "Upload an image."

        # 1) Depth
        depth = _infer_depth(pil_img, logs)
        depth_prev = _depth_preview(depth)

        # 2) RGBD + intrinsics
        rgb = np.array(pil_img.convert("RGB"))
        depth_u16 = _to_u16(depth)
        rgbd, intr = _rgbd_intrinsics(rgb, depth_u16, fx, fy)

        # 3) Point cloud
        pcd = _make_pointcloud(rgbd, intr, logs, down_voxel=down_voxel)
        if np.asarray(pcd.points).shape[0] < 30:
            raise RuntimeError("Got < 30 points after filtering; try lowering outlier removal or increasing voxel size to 0.")

        # 4) Mesh with fallback
        mesh = _make_mesh_with_fallback(pcd, poisson_depth, logs)

        # 5) Save artifacts (persistent + timestamped)
        depth_png = _timestamped("depth_preview", "png")
        pcd_ply   = _timestamped("pointcloud", "ply")
        mesh_ply  = _timestamped("mesh", "ply")

        depth_prev.save(depth_png)
        o3d.io.write_point_cloud(str(pcd_ply), pcd, write_ascii=False)
        o3d.io.write_triangle_mesh(str(mesh_ply), mesh, write_ascii=False)

        log_txt = "\n".join(logs if verbose else logs[-20:])
        return (
            depth_prev,           # preview image
            str(pcd_ply),         # for Model3D viewer
            str(mesh_ply),        # for Model3D viewer
            str(depth_png),       # download depth
            str(pcd_ply),         # download pcd
            str(mesh_ply),        # download mesh
            log_txt
        )
    except Exception as e:
        logs.append(f"[ERROR] {type(e).__name__}: {e}")
        return None, None, None, None, None, None, "\n".join(logs)

with gr.Blocks(title="Room 3D Reconstruction (GLPN + Open3D)") as demo:
    gr.Markdown("### Room 3D Reconstruction — GLPN → RGB-D → Point Cloud → Mesh\nUpload a room photo. If Poisson fails, we auto-fallback to Ball-Pivoting.")
    with gr.Row():
        with gr.Column():
            inp = gr.Image(type="pil", label="Input room image")
            fx = gr.Slider(200, 1200, value=500, step=10, label="fx (px)")
            fy = gr.Slider(200, 1200, value=500, step=10, label="fy (px)")
            pdepth = gr.Slider(6, 11, value=9, step=1, label="Poisson depth (lower = faster/stabler)")
            down = gr.Slider(0.0, 0.02, value=0.01, step=0.002, label="Voxel downsample (m)")
            verbose = gr.Checkbox(value=True, label="Verbose logs")
            btn = gr.Button("Reconstruct 3D", variant="primary")
        with gr.Column():
            depth_img = gr.Image(label="Depth preview", interactive=False)
            pcd_view = gr.Model3D(label="Point Cloud (.ply)")
            mesh_view = gr.Model3D(label="Mesh (.ply)")
    with gr.Row():
        depth_file = gr.File(label="Download depth (PNG)")
        pcd_file = gr.File(label="Download point cloud (.ply)")
        mesh_file = gr.File(label="Download mesh (.ply)")
    logs = gr.Textbox(label="Logs", max_lines=48, lines=20)

    btn.click(
        run,
        inputs=[inp, fx, fy, pdepth, down, verbose],
        outputs=[depth_img, pcd_view, mesh_view, depth_file, pcd_file, mesh_file, logs]
    )

if __name__ == "__main__":
    demo.queue().launch()