""" Advanced 3D Reconstruction from Single Images with Responsible AI Features """ import gradio as gr import numpy as np import torch from PIL import Image, ImageDraw from transformers import GLPNForDepthEstimation, GLPNImageProcessor import open3d as o3d import plotly.graph_objects as go import matplotlib.pyplot as plt import io import json import time from pathlib import Path import tempfile import zipfile import hashlib from datetime import datetime # ============================================================================ # RESPONSIBLE AI GUIDELINES # ============================================================================ RESPONSIBLE_AI_NOTICE = """ ## ⚠️ Responsible Use Guidelines ### Privacy & Consent - **Do not upload images containing identifiable people without their explicit consent** - **Do not use for surveillance, tracking, or monitoring individuals** - Facial features may be reconstructed in 3D consider privacy implications - Remove metadata (EXIF) that may contain location or personal information ### Ethical Use - This tool is for **educational, research, and creative purposes only** - **Prohibited uses:** - Creating deepfakes or misleading 3D content - Unauthorized documentation of private property - Circumventing security systems - Generating 3D models for harassment or stalking - Commercial use without proper rights to source images ### Limitations & Bias - Training data is concentrated in indoor Western settings, so accuracy on other regions and architectural styles is not well characterised - Likely to perform less reliably on scenes far from the training distribution - Scale is relative, not absolute, not suitable for precision measurements - Single viewpoint limitations, occluded areas are inferred, not captured ### Data Usage - Images are processed locally during your session - No images are stored or transmitted to external servers - Processing logs contain only technical metrics, no image content - You retain all rights to your uploaded images and generated 3D models **By using this tool, you agree to these responsible use guidelines.** """ # ============================================================================ # BUILT IN SAMPLE IMAGES (real photographs, fetched once at startup) # # Two real, openly licensed demo photographs are downloaded at launch and # cached to a local folder so the sample tab loads instantly on every run. # They are chosen to demonstrate the model's strengths and limits side by side: # # Sample 1 · Indoor scene (two cats on a couch) # The well known COCO image 000000039769, used across Hugging Face vision # tutorials. A real 640x480 indoor photo with clear furniture geometry and # near/far structure, close to the NYU Depth V2 distribution GLPN was # trained on. Start here to see a clean reconstruction. # # Sample 2 · Outdoor street scene # A real driving/street photo from Niantic's monodepth2 repo. Open sky and a # receding road stress test GLPN's known weak spots. Run this, then switch to # DPT (High Quality) and compare the metrics, outlier % and the watertight # flag reveal which model handled the scene better. # # Sources (both from openly licensed research repositories, fetched via raw # GitHub URLs so they work behind most corporate proxies): # - COCO 000000039769 -> huggingface/transformers test fixtures # - monodepth2 sample -> nianticlabs/monodepth2 assets # # If the machine is offline, a labelled placeholder is generated instead so the # app still launches without crashing. # ============================================================================ import urllib.request as _urlreq _SAMPLE_SPECS = { "indoor": { "url": "https://raw.githubusercontent.com/huggingface/transformers/main/tests/fixtures/tests_samples/COCO/000000039769.png", "filename": "sample_indoor_scene.png", "label": "indoor scene", }, "outdoor": { "url": "https://raw.githubusercontent.com/nianticlabs/monodepth2/master/assets/test_image.jpg", "filename": "sample_outdoor_street.jpg", "label": "outdoor street", }, } # Local cache folder for the downloaded samples (also used as gr.Image values). _SAMPLE_DIR = Path(tempfile.gettempdir()) / "depthforge_samples" _SAMPLE_DIR.mkdir(parents=True, exist_ok=True) def _placeholder(label: str) -> Image.Image: """Simple labelled placeholder used only when a download fails (offline).""" img = Image.new("RGB", (640, 480), (235, 230, 220)) d = ImageDraw.Draw(img) d.rectangle([8, 8, 631, 471], outline=(150, 140, 120), width=2) d.text((24, 24), f"sample unavailable offline:\n{label}", fill=(90, 80, 60)) d.text((24, 440), "connect to the internet and restart to load the real photo", fill=(120, 110, 90)) return img def _load_sample(key: str) -> Image.Image: """ Return a real sample photo as a PIL image, downloading + caching on first run. Falls back to a placeholder if the machine has no internet access. """ spec = _SAMPLE_SPECS[key] dest = _SAMPLE_DIR / spec["filename"] # use the cached copy if we already downloaded it this session / earlier if dest.exists() and dest.stat().st_size > 0: try: return Image.open(dest).convert("RGB") except Exception: pass # corrupt cache -> re-download below try: req = _urlreq.Request(spec["url"], headers={"User-Agent": "DepthForge/1.0"}) with _urlreq.urlopen(req, timeout=15) as r: dest.write_bytes(r.read()) print(f"\u2713 sample '{key}' downloaded -> {dest}") return Image.open(dest).convert("RGB") except Exception as e: print(f"\u26a0 could not download sample '{key}' ({e}); using placeholder") return _placeholder(spec["label"]) # Fetch once at startup so the sample tab is instant thereafter SAMPLE_INDOOR = _load_sample("indoor") SAMPLE_OUTDOOR = _load_sample("outdoor") # ============================================================================ # PRIVACY & SAFETY FUNCTIONS # ============================================================================ def check_image_safety(image): warnings = [] width, height = image.size if width * height > 10_000_000: warnings.append("⚠️ Very large image: consider resizing to improve processing speed") if max(width, height) / min(width, height) > 3: warnings.append("⚠️ Unusual aspect ratio detected: ensure image doesn't contain unintended content") try: exif = image.getexif() if exif and any(k for k in exif.keys() if k in [34853, 0x8825]): warnings.append("⚠️ GPS location data detected in image: consider removing EXIF data for privacy") except Exception: pass return True, "\n".join(warnings) if warnings else None def generate_session_id(): return hashlib.sha256(str(datetime.now()).encode()).hexdigest()[:16] def content_policy_check(image): w, h = image.size if w < 100 or h < 100: return False, "Image too small: minimum 100×100 pixels required for meaningful reconstruction" return True, None # ============================================================================ # MODEL LOADING # ============================================================================ print("Loading GLPN model (lightweight)...") try: glpn_processor = GLPNImageProcessor.from_pretrained("vinvino02/glpn-nyu") glpn_model = GLPNForDepthEstimation.from_pretrained("vinvino02/glpn-nyu") print("✓ GLPN model loaded successfully!") except Exception as e: print(f"Error loading model: {e}") glpn_processor = None glpn_model = None dpt_model = None dpt_processor = None # ============================================================================ # CORE 3D RECONSTRUCTION # ============================================================================ def process_image(image, model_choice="GLPN (Recommended)", visualization_type="mesh"): def _quality_assessment(metrics): notes = [] pct = (metrics["outliers_removed"] / metrics["initial_points"]) * 100 notes.append("Very clean depth estimation" if pct < 5 else ("Good depth quality" if pct < 15 else "High noise in depth estimation")) if metrics["is_edge_manifold"] and metrics["is_vertex_manifold"]: notes.append("Excellent topology") elif metrics["is_vertex_manifold"]: notes.append("Good local topology") else: notes.append("Topology issues present") notes.append("Watertight mesh: ready for 3D printing!" if metrics["is_watertight"] else "Not watertight —needs repair for 3D printing") return "\n".join(f"- {n}" for n in notes) if glpn_model is None: return None, None, None, "❌ Model failed to load. Please refresh the page.", None try: # preprocess new_height = 480 if image.height > 480 else image.height new_height -= new_height % 32 new_width = int(new_height * image.width / image.height) diff = new_width % 32 new_width = new_width - diff if diff < 16 else new_width + (32 - diff) image = image.resize((new_width, new_height), Image.LANCZOS) # model selection if model_choice == "GLPN (Recommended)": processor, model = glpn_processor, glpn_model else: global dpt_model, dpt_processor if dpt_model is None: print("Loading DPT model (first time only)…") from transformers import DPTForDepthEstimation, DPTImageProcessor dpt_processor = DPTImageProcessor.from_pretrained("Intel/dpt-large") dpt_model = DPTForDepthEstimation.from_pretrained("Intel/dpt-large") print("✓ DPT model loaded!") processor, model = dpt_processor, dpt_model inputs = processor(images=image, return_tensors="pt") t0 = time.time() with torch.no_grad(): predicted_depth = model(**inputs).predicted_depth depth_time = time.time() - t0 # crop / align pad = 16 output = predicted_depth.squeeze().cpu().numpy() * 1000.0 output = output[pad:-pad, pad:-pad] image = image.crop((pad, pad, image.width - pad, image.height - pad)) dh, dw = output.shape iw, ih = image.size if dh != ih or dw != iw: from scipy import ndimage output = ndimage.zoom(output, (ih / dh, iw / dw), order=1) # depth visualisation fig, ax = plt.subplots(1, 2, figsize=(14, 7)) ax[0].imshow(image); ax[0].set_title("Original Image", fontsize=14, fontweight="bold"); ax[0].axis("off") im = ax[1].imshow(output, cmap="plasma") ax[1].set_title("Estimated Depth Map", fontsize=14, fontweight="bold"); ax[1].axis("off") plt.colorbar(im, ax=ax[1], fraction=0.046, pad=0.04) plt.tight_layout() buf = io.BytesIO(); plt.savefig(buf, format="png", dpi=150, bbox_inches="tight"); buf.seek(0) depth_viz = Image.open(buf) plt.close() # point cloud w, h = image.size if output.shape != (h, w): from scipy import ndimage output = ndimage.zoom(output, (h / output.shape[0], w / output.shape[1]), order=1) depth_img = (output * 255 / np.max(output)).astype(np.uint8) img_arr = np.array(image) rgbd = o3d.geometry.RGBDImage.create_from_color_and_depth( o3d.geometry.Image(img_arr), o3d.geometry.Image(depth_img), convert_rgb_to_intensity=False) cam = o3d.camera.PinholeCameraIntrinsic() cam.set_intrinsics(w, h, 500, 500, w / 2, h / 2) pcd = o3d.geometry.PointCloud.create_from_rgbd_image(rgbd, cam) initial_points = len(pcd.points) _, ind = pcd.remove_statistical_outlier(nb_neighbors=20, std_ratio=2.0) pcd = pcd.select_by_index(ind) pcd.estimate_normals() pcd.orient_normals_to_align_with_direction() # mesh t1 = time.time() mesh = o3d.geometry.TriangleMesh.create_from_point_cloud_poisson(pcd, depth=9, n_threads=1)[0] tree = o3d.geometry.KDTreeFlann(pcd) mesh.vertex_colors = o3d.utility.Vector3dVector( np.array([pcd.colors[tree.search_knn_vector_3d(v, 1)[1][0]] for v in mesh.vertices])) mesh.rotate(mesh.get_rotation_matrix_from_xyz((np.pi, 0, 0)), center=(0, 0, 0)) mesh_time = time.time() - t1 mesh.compute_vertex_normals() # metrics metrics = { "model_used": model_choice, "depth_estimation_time": f"{depth_time:.2f}s", "mesh_reconstruction_time": f"{mesh_time:.2f}s", "total_time": f"{depth_time + mesh_time:.2f}s", "initial_points": initial_points, "outliers_removed": initial_points - len(pcd.points), "final_points": len(pcd.points), "vertices": len(mesh.vertices), "triangles": len(mesh.triangles), "is_edge_manifold": mesh.is_edge_manifold(), "is_vertex_manifold": mesh.is_vertex_manifold(), "is_watertight": mesh.is_watertight(), } try: sa = mesh.get_surface_area() if sa <= 0: verts = np.asarray(mesh.vertices); tris = np.asarray(mesh.triangles) cross = np.cross(verts[tris[:,1]] - verts[tris[:,0]], verts[tris[:,2]] - verts[tris[:,0]]) sa = float(np.sum(0.5 * np.linalg.norm(cross, axis=1))) metrics["surface_area"] = float(sa) except Exception: metrics["surface_area"] = "Unable to compute" metrics["volume"] = float(mesh.get_volume()) if mesh.is_watertight() else None # 3-D visualisation pts = np.asarray(pcd.points) cols = np.asarray(pcd.colors) if visualization_type == "point_cloud": plotly_fig = go.Figure(data=[go.Scatter3d( x=pts[:,0], y=pts[:,1], z=pts[:,2], mode="markers", marker=dict(size=2, color=["rgb({},{},{})".format(int(r*255),int(g*255),int(b*255)) for r,g,b in cols]))]) else: verts = np.asarray(mesh.vertices); tris = np.asarray(mesh.triangles) vcols = np.asarray(mesh.vertex_colors) plotly_fig = go.Figure(data=[go.Mesh3d( x=verts[:,0], y=verts[:,1], z=verts[:,2], i=tris[:,0], j=tris[:,1], k=tris[:,2], vertexcolor=["rgb({},{},{})".format(int(r*255),int(g*255),int(b*255)) for r,g,b in vcols], opacity=0.95)]) plotly_fig.update_layout( scene=dict(xaxis=dict(visible=False), yaxis=dict(visible=False), zaxis=dict(visible=False), aspectmode="data"), height=700) # export tmp = Path(tempfile.mkdtemp()) pcd_path = tmp / "point_cloud.ply"; o3d.io.write_point_cloud(str(pcd_path), pcd) mesh_ply = tmp / "mesh.ply"; o3d.io.write_triangle_mesh(str(mesh_ply), mesh) mesh_obj = tmp / "mesh.obj"; o3d.io.write_triangle_mesh(str(mesh_obj), mesh) mesh_stl = tmp / "mesh.stl"; o3d.io.write_triangle_mesh(str(mesh_stl), mesh) met_path = tmp / "metrics.json"; met_path.write_text(json.dumps(metrics, indent=2, default=str)) zip_path = tmp / "reconstruction_complete.zip" with zipfile.ZipFile(zip_path, "w", zipfile.ZIP_DEFLATED) as zf: for p in [pcd_path, mesh_ply, mesh_obj, mesh_stl, met_path]: zf.write(p, p.name) report = f""" ## Reconstruction Complete! ### Performance - **Processing Time**: {metrics['total_time']} - **Points**: {metrics['final_points']:,} - **Triangles**: {metrics['triangles']:,} ### Quality - **Topology**: {'Good' if metrics['is_vertex_manifold'] else 'Issues'} - **Watertight**: {'Yes' if metrics['is_watertight'] else 'No'} ### Assessment {_quality_assessment(metrics)} **Download the complete package below!** """ return depth_viz, plotly_fig, str(zip_path), report, json.dumps(metrics, indent=2, default=str) except Exception as e: import traceback return None, None, None, f"Error: {e}\n\n{traceback.format_exc()}", None def process_image_with_safeguards(image, model_choice="GLPN (Recommended)", visualization_type="mesh", consent_given=False): session_id = generate_session_id() if not consent_given: return None, None, None, "**You must agree to the Responsible Use Guidelines first.**", None if image is None: return None, None, None, "Please upload an image first.", None _, safety_warning = check_image_safety(image) passes, policy_msg = content_policy_check(image) if not passes: return None, None, None, policy_msg, None depth_viz, plotly_fig, zip_path, report, json_metrics = process_image(image, model_choice, visualization_type) if safety_warning: report = f"**Privacy Notice:**\n{safety_warning}\n\n{report}" if json_metrics: metrics = json.loads(json_metrics) metrics["responsible_ai"] = {"session_id": session_id, "timestamp": datetime.now().isoformat(), "consent_given": True} json_metrics = json.dumps(metrics, indent=2) return depth_viz, plotly_fig, zip_path, report, json_metrics # ============================================================================ # GRADIO INTERFACE # ============================================================================ THEME = gr.themes.Soft( primary_hue=gr.themes.colors.amber, secondary_hue=gr.themes.colors.blue, neutral_hue=gr.themes.colors.stone, font=[gr.themes.GoogleFont("IBM Plex Sans"), "system-ui", "sans-serif"], font_mono=[gr.themes.GoogleFont("IBM Plex Mono"), "monospace"], ) CSS = """ @import url('https://fonts.googleapis.com/css2?family=Fraunces:ital,opsz,wght@0,9..144,600;0,9..144,800;0,9..144,900;1,9..144,500&display=swap'); .gradio-container h1, .gradio-container h2, .gradio-container h3, .gradio-container h4 { font-family: 'Fraunces', Georgia, serif !important; letter-spacing: -.01em; } .gradio-container h2 { border-left: 3px solid var(--color-accent); padding-left: .55rem !important; } .gradio-container h3 { opacity: .85; } .df-header { position: relative; border-radius: 10px; padding: 1.8rem 1.7rem 1.4rem; margin: .3rem 0 1rem; border-bottom: 2px solid var(--border-color-primary); } .df-kicker { font-family: 'IBM Plex Mono', monospace; font-size: .7rem; letter-spacing: .04em; margin-bottom: .3rem; opacity: .6; } .df-title { font-family: 'Fraunces', serif; font-weight: 900; font-size: 2.5rem; line-height: 1; margin: 0 0 .3rem; } .df-title span { background: linear-gradient(180deg, transparent 58%, color-mix(in srgb, var(--color-accent) 35%, transparent) 58%); padding: 0 .05em; } .df-sub { font-family: 'IBM Plex Mono', monospace; font-size: .74rem; opacity: .6; } .df-stamp { position: absolute; top: 1.2rem; right: 1.2rem; transform: rotate(7deg); font-family: 'IBM Plex Mono', monospace; font-size: .62rem; letter-spacing: .08em; text-transform: uppercase; border: 1.5px solid; border-radius: 4px; padding: .2rem .45rem; opacity: .75; color: var(--error-text-color, #c0392b); border-color: var(--error-text-color, #c0392b); } .df-note { margin-top: .9rem; font-family: 'IBM Plex Mono', monospace; font-size: .72rem; border-left: 3px solid var(--color-accent); padding: .5rem .7rem; background: color-mix(in srgb, var(--color-accent) 8%, transparent); border-radius: 0 4px 4px 0; } .df-badge { display: inline-block; font-family: 'IBM Plex Mono', monospace; font-size: .63rem; border: 1px solid var(--border-color-primary); border-radius: 3px; padding: .18rem .48rem; margin: .5rem .35rem 0 0; opacity: .75; } button.primary { text-transform: uppercase !important; letter-spacing: .04em !important; font-family: 'IBM Plex Mono', monospace !important; box-shadow: 3px 3px 0 color-mix(in srgb, var(--color-accent) 60%, transparent) !important; transition: transform .08s, box-shadow .08s !important; } button.primary:hover { transform: translate(1px,1px); box-shadow: 2px 2px 0 color-mix(in srgb, var(--color-accent) 60%, transparent) !important; } .gradio-image, .gradio-plot, [class*="image-container"] { border-radius: 8px !important; border: 1px solid var(--border-color-primary) !important; box-shadow: 3px 3px 0 color-mix(in srgb, var(--body-text-color) 10%, transparent) !important; } .gradio-container :not(pre) > code { background: color-mix(in srgb, var(--color-accent) 25%, transparent) !important; border-radius: 3px; padding: .05em .3em; } .gradio-container pre { border-radius: 8px !important; } .gradio-container blockquote { border-left: 3px solid var(--color-accent) !important; font-family: 'Fraunces', serif; font-style: italic; } button.selected { border-bottom: 2px solid var(--color-accent) !important; } .df-footer { text-align: center; padding: 1.5rem; margin-top: 1.4rem; border-top: 1px solid var(--border-color-primary); font-family: 'IBM Plex Mono', monospace; font-size: .72rem; line-height: 1.7; opacity: .7; } /* sample cards */ .sample-card { border: 1px solid var(--border-color-primary); border-radius: 8px; padding: 1rem 1.1rem; background: color-mix(in srgb, var(--color-accent) 4%, transparent); margin-bottom: .75rem; } .sample-card h4 { font-family: 'Fraunces', Georgia, serif !important; font-size: 1rem; margin: 0 0 .4rem; } .sample-card p { font-family: 'IBM Plex Mono', monospace; font-size: .7rem; opacity: .75; margin: 0 0 .6rem; line-height: 1.6; } .sample-pill { display: inline-block; font-family: 'IBM Plex Mono', monospace; font-size: .6rem; border-radius: 3px; padding: .12rem .38rem; margin-right: .3rem; border: 1px solid; opacity: .85; } .sample-pill.good { color: var(--success-text-color, #2d7a2d); border-color: var(--success-text-color, #2d7a2d); } .sample-pill.warn { color: var(--warning-text-color, #a07000); border-color: var(--warning-text-color, #a07000); } """ # ============================================================================ # LAYOUT # ============================================================================ with gr.Blocks(title="DepthForge") as demo: gr.HTML("""
A real 640×480 photo (two cats resting on a couch the well-known COCO image used across Hugging Face vision tutorials). Clear foreground/background depth layering with distinct near and far objects makes it a good fit for GLPN. Expect a sharp depth map, a dense point cloud, and a clean mesh. Start here for your "hello world" reconstruction.
✓ real photo ✓ GLPN ideal input ✓ clear depth layeringA real outdoor street scene (from Niantic's monodepth2 demo assets). Open sky carries no true depth signal and the receding road blends into the background, so GLPN tends to produce noisier depth here outlier % rises and the mesh may not be watertight. Run it, then switch to DPT (High Quality) and compare the two metrics reports to see how much the model choice matters.
⚡ real photo ⚡ tests model limits ✓ switch to DPT here