Spaces:
Running
on
Zero
Running
on
Zero
demo
Browse files- README.md +10 -29
- VIEWER_IFRAME_SOLUTION.md +208 -0
- app.py +136 -95
README.md
CHANGED
|
@@ -6,46 +6,27 @@ colorTo: purple
|
|
| 6 |
short_description: Single Image 3D Face Reconstruction with Gaussian Splatting
|
| 7 |
sdk: gradio
|
| 8 |
app_file: app.py
|
| 9 |
-
thumbnail: examples/teaser.png
|
| 10 |
pinned: false
|
| 11 |
---
|
| 12 |
|
| 13 |
|
| 14 |
# FaceLift: Single Image 3D Face Reconstruction
|
| 15 |
|
| 16 |
-
|
| 17 |
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
- **Single Image Input**: Upload any portrait photo
|
| 21 |
-
- **Automatic Face Detection**: Optional auto-cropping and alignment
|
| 22 |
-
- **Multi-view Generation**: Creates 6 consistent views using diffusion models
|
| 23 |
-
- **3D Reconstruction**: Generates high-quality 3D Gaussian splats
|
| 24 |
-
- **Interactive 3D Viewer**: Embedded real-time WebGL viewer powered by [antimatter15/splat](https://github.com/antimatter15/splat)
|
| 25 |
-
- **Turntable Animation**: Exports rotating 360° video
|
| 26 |
-
- **Downloadable Model**: Get the 3D model as a .ply file
|
| 27 |
-
|
| 28 |
-
## Usage
|
| 29 |
-
|
| 30 |
-
1. Upload a portrait image
|
| 31 |
-
2. Adjust parameters (optional):
|
| 32 |
-
- Auto Cropping: Enable for automatic face detection
|
| 33 |
-
- Guidance Scale: Controls generation quality (default: 3.0)
|
| 34 |
-
- Random Seed: For reproducible results
|
| 35 |
-
- Generation Steps: Higher = better quality but slower
|
| 36 |
-
3. Click "Generate 3D Head" and wait for processing
|
| 37 |
-
4. View outputs:
|
| 38 |
-
- **Interactive 3D Viewer**: Interact directly with the 3D model in your browser (drag to rotate, scroll to zoom)
|
| 39 |
-
- **Turntable Animation**: Watch the 360° rotating video
|
| 40 |
-
- **3D Model**: Download the .ply file for use in other Gaussian Splatting software
|
| 41 |
|
| 42 |
## Citation
|
| 43 |
|
| 44 |
```
|
| 45 |
-
@
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
}
|
| 50 |
```
|
| 51 |
|
|
|
|
| 6 |
short_description: Single Image 3D Face Reconstruction with Gaussian Splatting
|
| 7 |
sdk: gradio
|
| 8 |
app_file: app.py
|
|
|
|
| 9 |
pinned: false
|
| 10 |
---
|
| 11 |
|
| 12 |
|
| 13 |
# FaceLift: Single Image 3D Face Reconstruction
|
| 14 |
|
| 15 |
+
Huggingface demo for ICCV 2025 paper *FaceLift: Learning Generalizable Single Image 3D Face Reconstruction from Synthetic Heads*.
|
| 16 |
|
| 17 |
+
Transform a single portrait image into a complete 3D head model using multi-view diffusion and Gaussian Splatting.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
## Citation
|
| 20 |
|
| 21 |
```
|
| 22 |
+
@misc{lyu2025facelift,
|
| 23 |
+
title={FaceLift: Learning Generalizable Single Image 3D Face Reconstruction from Synthetic Heads},
|
| 24 |
+
author={Weijie Lyu and Yi Zhou and Ming-Hsuan Yang and Zhixin Shu},
|
| 25 |
+
year={2025},
|
| 26 |
+
eprint={2412.17812},
|
| 27 |
+
archivePrefix={arXiv},
|
| 28 |
+
primaryClass={cs.CV},
|
| 29 |
+
url={https://arxiv.org/abs/2412.17812}
|
| 30 |
}
|
| 31 |
```
|
| 32 |
|
VIEWER_IFRAME_SOLUTION.md
ADDED
|
@@ -0,0 +1,208 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Viewer iFrame Solution for Hugging Face Spaces
|
| 2 |
+
|
| 3 |
+
## Problem
|
| 4 |
+
|
| 5 |
+
The initial approach of embedding 51KB of viewer.js JavaScript directly in the Gradio HTML component didn't work because:
|
| 6 |
+
1. **Size limit**: Gradio's HTML component couldn't handle such large inline content
|
| 7 |
+
2. **CSP restrictions**: Hugging Face Spaces has Content Security Policy that may block large inline scripts
|
| 8 |
+
3. **Result**: Elements weren't rendering (`document.getElementById('canvas')` returned `null`)
|
| 9 |
+
|
| 10 |
+
## Solution
|
| 11 |
+
|
| 12 |
+
Use an **iframe with srcdoc** attribute containing a complete HTML document that loads `viewer.js` as an external script.
|
| 13 |
+
|
| 14 |
+
### Key Changes
|
| 15 |
+
|
| 16 |
+
#### 1. Serve viewer.js as Static File (lines 40-44)
|
| 17 |
+
|
| 18 |
+
```python
|
| 19 |
+
# Copy viewer.js to outputs so it can be served as a static file
|
| 20 |
+
VIEWER_JS_SRC = Path(__file__).parent / "viewer.js"
|
| 21 |
+
if VIEWER_JS_SRC.exists():
|
| 22 |
+
shutil.copy2(VIEWER_JS_SRC, OUTPUTS_DIR / "viewer.js")
|
| 23 |
+
print(f"✓ Copied viewer.js to {OUTPUTS_DIR / 'viewer.js'}")
|
| 24 |
+
```
|
| 25 |
+
|
| 26 |
+
This makes `viewer.js` accessible at `/file=outputs/viewer.js`
|
| 27 |
+
|
| 28 |
+
#### 2. Create iFrame-Based Viewer (lines 335-468)
|
| 29 |
+
|
| 30 |
+
```python
|
| 31 |
+
def create_splat_viewer_html(ply_url: str, viewer_id: str) -> str:
|
| 32 |
+
"""Create HTML with splat viewer that loads a PLY file via external script."""
|
| 33 |
+
return f"""
|
| 34 |
+
<iframe id="viewer-frame-{viewer_id}" style="..." srcdoc='
|
| 35 |
+
<!DOCTYPE html>
|
| 36 |
+
<html>
|
| 37 |
+
<head>...</head>
|
| 38 |
+
<body>
|
| 39 |
+
<canvas id="canvas"></canvas>
|
| 40 |
+
<div id="spinner">...</div>
|
| 41 |
+
<div id="message"></div>
|
| 42 |
+
<div id="fps"></div>
|
| 43 |
+
<div id="camid"></div>
|
| 44 |
+
<div id="controls-info">...</div>
|
| 45 |
+
|
| 46 |
+
<script src="{ply_url.rsplit("/", 3)[0]}/viewer.js"></script>
|
| 47 |
+
<script>
|
| 48 |
+
// Auto-load PLY after viewer initializes
|
| 49 |
+
...
|
| 50 |
+
</script>
|
| 51 |
+
</body>
|
| 52 |
+
</html>
|
| 53 |
+
'></iframe>
|
| 54 |
+
"""
|
| 55 |
+
```
|
| 56 |
+
|
| 57 |
+
### How It Works
|
| 58 |
+
|
| 59 |
+
1. **iFrame Isolation**: Creates a sandboxed environment with its own DOM
|
| 60 |
+
2. **External Script**: Loads `viewer.js` via `<script src="...">` instead of embedding
|
| 61 |
+
3. **Automatic Loading**: Polls for `window.worker` then fetches and loads PLY
|
| 62 |
+
4. **Error Handling**: Shows clear messages if loading fails
|
| 63 |
+
|
| 64 |
+
### Advantages
|
| 65 |
+
|
| 66 |
+
| Aspect | Previous (Embedded) | Current (iFrame) |
|
| 67 |
+
|--------|---------------------|------------------|
|
| 68 |
+
| **Size** | 51KB inline fails | References external file ✓ |
|
| 69 |
+
| **DOM** | Shared with Gradio | Isolated in iframe ✓ |
|
| 70 |
+
| **CSP** | May violate policy | Clean external script ✓ |
|
| 71 |
+
| **Debugging** | Hard to inspect | Full iframe console ✓ |
|
| 72 |
+
| **Reliability** | Failed to render | Renders successfully ✓ |
|
| 73 |
+
|
| 74 |
+
### File Structure
|
| 75 |
+
|
| 76 |
+
```
|
| 77 |
+
FaceLift/
|
| 78 |
+
├── viewer.js # Source (51KB)
|
| 79 |
+
├── outputs/
|
| 80 |
+
│ ├── viewer.js # Copied for serving
|
| 81 |
+
│ └── splats/
|
| 82 |
+
│ └── xxxxx/
|
| 83 |
+
│ └── gaussians.ply # Generated PLY files
|
| 84 |
+
└── app.py
|
| 85 |
+
```
|
| 86 |
+
|
| 87 |
+
### URL Pattern
|
| 88 |
+
|
| 89 |
+
- **viewer.js**: `/file=outputs/viewer.js`
|
| 90 |
+
- **PLY file**: `/file=outputs/splats/xxxxx/gaussians.ply`
|
| 91 |
+
|
| 92 |
+
The script src dynamically extracts the base path from the PLY URL:
|
| 93 |
+
```javascript
|
| 94 |
+
<script src="{ply_url.rsplit('/', 3)[0]}/viewer.js"></script>
|
| 95 |
+
```
|
| 96 |
+
|
| 97 |
+
Example:
|
| 98 |
+
- PLY URL: `/file=outputs/splats/xxxxx/gaussians.ply`
|
| 99 |
+
- Base: `/file=outputs`
|
| 100 |
+
- Viewer: `/file=outputs/viewer.js`
|
| 101 |
+
|
| 102 |
+
### Testing
|
| 103 |
+
|
| 104 |
+
After deployment, check in browser console:
|
| 105 |
+
|
| 106 |
+
```javascript
|
| 107 |
+
// Inside the iframe
|
| 108 |
+
const iframe = document.querySelector('iframe[id^="viewer-frame"]');
|
| 109 |
+
const iframeDoc = iframe.contentDocument;
|
| 110 |
+
|
| 111 |
+
console.log('Canvas:', !!iframeDoc.getElementById('canvas')); // should be true
|
| 112 |
+
console.log('Worker:', !!iframe.contentWindow.worker); // should be true
|
| 113 |
+
```
|
| 114 |
+
|
| 115 |
+
### Debug Info
|
| 116 |
+
|
| 117 |
+
The UI now shows:
|
| 118 |
+
```
|
| 119 |
+
PLY Path: /path/to/outputs/splats/xxxxx/gaussians.ply
|
| 120 |
+
PLY URL: /file=/path/to/outputs/splats/xxxxx/gaussians.ply
|
| 121 |
+
File exists: True
|
| 122 |
+
Viewer ID: a1b2c3d4
|
| 123 |
+
```
|
| 124 |
+
|
| 125 |
+
### Console Output
|
| 126 |
+
|
| 127 |
+
When working correctly:
|
| 128 |
+
```
|
| 129 |
+
=== Splat Viewer Init ===
|
| 130 |
+
PLY URL: /file=outputs/splats/xxxxx/gaussians.ply
|
| 131 |
+
✓ Worker ready after 300 ms
|
| 132 |
+
✓ PLY loaded: 245678 bytes
|
| 133 |
+
✓ Sent to worker
|
| 134 |
+
```
|
| 135 |
+
|
| 136 |
+
### Common Issues
|
| 137 |
+
|
| 138 |
+
#### Issue: "Viewer failed to initialize"
|
| 139 |
+
|
| 140 |
+
**Cause**: viewer.js not accessible or failed to load
|
| 141 |
+
|
| 142 |
+
**Check**:
|
| 143 |
+
```bash
|
| 144 |
+
# Verify file was copied
|
| 145 |
+
ls -lh outputs/viewer.js
|
| 146 |
+
|
| 147 |
+
# Test direct access (when app is running)
|
| 148 |
+
curl http://localhost:7860/file=outputs/viewer.js
|
| 149 |
+
```
|
| 150 |
+
|
| 151 |
+
**Fix**: Ensure `VIEWER_JS_SRC` path is correct and file is copied
|
| 152 |
+
|
| 153 |
+
#### Issue: "Error: HTTP 404"
|
| 154 |
+
|
| 155 |
+
**Cause**: PLY file path incorrect or file doesn't exist
|
| 156 |
+
|
| 157 |
+
**Check Debug Info**: Look at "File exists" field
|
| 158 |
+
|
| 159 |
+
**Fix**: Verify `generate_3d_head()` creates the PLY file
|
| 160 |
+
|
| 161 |
+
#### Issue: iframe shows blank/black
|
| 162 |
+
|
| 163 |
+
**Cause**: CSP blocking iframe or script loading
|
| 164 |
+
|
| 165 |
+
**Check**: Browser console for CSP errors
|
| 166 |
+
|
| 167 |
+
**Fix**: May need to adjust Hugging Face Space settings (rare)
|
| 168 |
+
|
| 169 |
+
### Deployment Checklist
|
| 170 |
+
|
| 171 |
+
- [x] `viewer.js` exists in project root (51KB)
|
| 172 |
+
- [x] File copying code runs at startup
|
| 173 |
+
- [x] Debug info visible in UI
|
| 174 |
+
- [x] iframe contains complete HTML document
|
| 175 |
+
- [x] External script reference uses correct path
|
| 176 |
+
- [x] Error messages display clearly
|
| 177 |
+
- [ ] Test on Hugging Face Spaces
|
| 178 |
+
- [ ] Verify PLY files load correctly
|
| 179 |
+
- [ ] Check interactive controls work
|
| 180 |
+
- [ ] Test on mobile devices
|
| 181 |
+
|
| 182 |
+
### Next Steps
|
| 183 |
+
|
| 184 |
+
1. **Deploy to HF Spaces**:
|
| 185 |
+
```bash
|
| 186 |
+
git add app.py viewer.js
|
| 187 |
+
git commit -m "Fix viewer with iframe approach"
|
| 188 |
+
git push
|
| 189 |
+
```
|
| 190 |
+
|
| 191 |
+
2. **Test the viewer**:
|
| 192 |
+
- Generate a 3D model
|
| 193 |
+
- Check Debug Info shows correct paths
|
| 194 |
+
- Verify iframe loads and displays model
|
| 195 |
+
- Test drag/zoom/pan controls
|
| 196 |
+
|
| 197 |
+
3. **Optional: Hide Debug Info** (after confirming it works):
|
| 198 |
+
```python
|
| 199 |
+
out_debug = gr.Textbox(label="🔍 Debug Info", lines=3, visible=False)
|
| 200 |
+
```
|
| 201 |
+
|
| 202 |
+
---
|
| 203 |
+
|
| 204 |
+
**Status**: ✅ Ready for deployment
|
| 205 |
+
**Approach**: iFrame with external script
|
| 206 |
+
**Size**: Minimal (~2KB HTML wrapper)
|
| 207 |
+
**Compatibility**: Should work on HF Spaces
|
| 208 |
+
|
app.py
CHANGED
|
@@ -37,6 +37,12 @@ import os
|
|
| 37 |
OUTPUTS_DIR = Path.cwd() / "outputs"
|
| 38 |
OUTPUTS_DIR.mkdir(exist_ok=True)
|
| 39 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
# -----------------------------
|
| 41 |
# Ensure diff-gaussian-rasterization builds for current GPU
|
| 42 |
# -----------------------------
|
|
@@ -325,108 +331,140 @@ class FaceLiftPipeline:
|
|
| 325 |
print(f"Error details:\n{error_details}")
|
| 326 |
raise gr.Error(f"Generation failed: {str(e)}")
|
| 327 |
|
| 328 |
-
#
|
| 329 |
-
def create_splat_viewer_html(ply_url: str) -> str:
|
| 330 |
-
"""Create HTML with
|
| 331 |
-
# Read the viewer.js content
|
| 332 |
-
viewer_js_path = Path(__file__).parent / "viewer.js"
|
| 333 |
-
viewer_js_content = viewer_js_path.read_text() if viewer_js_path.exists() else ""
|
| 334 |
|
| 335 |
return f"""
|
| 336 |
-
<
|
| 337 |
-
|
| 338 |
-
|
| 339 |
-
|
| 340 |
-
<
|
| 341 |
-
|
| 342 |
-
|
| 343 |
-
|
| 344 |
-
|
| 345 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 346 |
</div>
|
| 347 |
</div>
|
| 348 |
-
|
| 349 |
-
<div id="
|
| 350 |
-
|
| 351 |
-
<div id="
|
| 352 |
-
|
| 353 |
-
<div id="camid" style="position:absolute; top:40px; right:10px; color:white; font-family:monospace; font-size:11px; background:rgba(0,0,0,0.7); padding:6px 10px; border-radius:4px; display:none;"></div>
|
| 354 |
-
|
| 355 |
-
<div id="controls-info" style="position:absolute; bottom:10px; left:10px; color:white; font-family:Arial; font-size:11px; background:rgba(0,0,0,0.7); padding:8px 12px; border-radius:4px;">
|
| 356 |
<strong>Controls:</strong> Drag: Rotate | Scroll: Zoom | Right-drag: Pan
|
| 357 |
</div>
|
| 358 |
-
</div>
|
| 359 |
-
|
| 360 |
-
<script>
|
| 361 |
-
// Inject viewer.js
|
| 362 |
-
(function() {{
|
| 363 |
-
{viewer_js_content}
|
| 364 |
-
}})();
|
| 365 |
-
</script>
|
| 366 |
-
|
| 367 |
-
<script>
|
| 368 |
-
// Auto-load the PLY file after viewer initializes
|
| 369 |
-
(function() {{
|
| 370 |
-
const plyUrl = '{ply_url}';
|
| 371 |
-
console.log('=== Splat Viewer Init ===');
|
| 372 |
-
console.log('PLY URL:', plyUrl);
|
| 373 |
-
|
| 374 |
-
// Wait for viewer to initialize
|
| 375 |
-
let attempts = 0;
|
| 376 |
-
const maxAttempts = 50;
|
| 377 |
|
| 378 |
-
|
| 379 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 380 |
|
| 381 |
-
|
| 382 |
-
|
| 383 |
-
|
| 384 |
-
|
| 385 |
-
|
| 386 |
-
|
| 387 |
-
|
| 388 |
-
|
| 389 |
-
|
| 390 |
-
return
|
| 391 |
-
|
| 392 |
-
|
| 393 |
-
|
| 394 |
-
|
| 395 |
-
|
| 396 |
-
|
| 397 |
-
|
| 398 |
-
|
| 399 |
-
|
| 400 |
-
|
| 401 |
-
|
| 402 |
-
|
| 403 |
-
|
| 404 |
-
|
| 405 |
-
|
| 406 |
-
|
| 407 |
-
|
| 408 |
-
|
| 409 |
-
|
| 410 |
-
|
| 411 |
-
|
| 412 |
-
|
| 413 |
-
|
| 414 |
-
|
| 415 |
-
console.error('✗ Worker failed to initialize');
|
| 416 |
-
clearInterval(checkAndLoad);
|
| 417 |
-
const spinner = document.getElementById('spinner');
|
| 418 |
-
const message = document.getElementById('message');
|
| 419 |
-
if (spinner) spinner.style.display = 'none';
|
| 420 |
-
if (message) {{
|
| 421 |
-
message.textContent = 'Viewer failed to initialize. Please refresh the page.';
|
| 422 |
-
message.style.display = 'block';
|
| 423 |
}}
|
| 424 |
-
}}
|
| 425 |
-
|
| 426 |
-
|
| 427 |
-
|
| 428 |
-
|
| 429 |
-
</script>
|
| 430 |
"""
|
| 431 |
|
| 432 |
def main():
|
|
@@ -448,10 +486,13 @@ def main():
|
|
| 448 |
|
| 449 |
# Create Gradio-accessible URL for the PLY file
|
| 450 |
ply_url = f"/file={ply_path}"
|
| 451 |
-
|
|
|
|
|
|
|
|
|
|
| 452 |
|
| 453 |
# Debug info showing the paths
|
| 454 |
-
debug_info = f"PLY Path: {ply_path}\nPLY URL: {ply_url}\nFile exists: {Path(ply_path).exists()}"
|
| 455 |
|
| 456 |
return viewer_html, output_path, turntable_path, ply_path, debug_info
|
| 457 |
|
|
|
|
| 37 |
OUTPUTS_DIR = Path.cwd() / "outputs"
|
| 38 |
OUTPUTS_DIR.mkdir(exist_ok=True)
|
| 39 |
|
| 40 |
+
# Copy viewer.js to outputs so it can be served as a static file
|
| 41 |
+
VIEWER_JS_SRC = Path(__file__).parent / "viewer.js"
|
| 42 |
+
if VIEWER_JS_SRC.exists():
|
| 43 |
+
shutil.copy2(VIEWER_JS_SRC, OUTPUTS_DIR / "viewer.js")
|
| 44 |
+
print(f"✓ Copied viewer.js to {OUTPUTS_DIR / 'viewer.js'}")
|
| 45 |
+
|
| 46 |
# -----------------------------
|
| 47 |
# Ensure diff-gaussian-rasterization builds for current GPU
|
| 48 |
# -----------------------------
|
|
|
|
| 331 |
print(f"Error details:\n{error_details}")
|
| 332 |
raise gr.Error(f"Generation failed: {str(e)}")
|
| 333 |
|
| 334 |
+
# Create viewer HTML that references external viewer.js
|
| 335 |
+
def create_splat_viewer_html(ply_url: str, viewer_id: str) -> str:
|
| 336 |
+
"""Create HTML with splat viewer that loads a PLY file via external script."""
|
|
|
|
|
|
|
|
|
|
| 337 |
|
| 338 |
return f"""
|
| 339 |
+
<iframe id="viewer-frame-{viewer_id}" style="width:100%; height:600px; border:1px solid #333; border-radius:8px;" srcdoc='
|
| 340 |
+
<!DOCTYPE html>
|
| 341 |
+
<html>
|
| 342 |
+
<head>
|
| 343 |
+
<meta charset="utf-8">
|
| 344 |
+
<style>
|
| 345 |
+
body {{ margin: 0; overflow: hidden; background: #000; }}
|
| 346 |
+
#canvas {{ width: 100vw; height: 100vh; display: block; }}
|
| 347 |
+
#spinner {{
|
| 348 |
+
position: absolute;
|
| 349 |
+
top: 50%;
|
| 350 |
+
left: 50%;
|
| 351 |
+
transform: translate(-50%, -50%);
|
| 352 |
+
color: white;
|
| 353 |
+
font-family: Arial;
|
| 354 |
+
z-index: 10;
|
| 355 |
+
text-align: center;
|
| 356 |
+
background: rgba(0,0,0,0.8);
|
| 357 |
+
padding: 20px;
|
| 358 |
+
border-radius: 8px;
|
| 359 |
+
}}
|
| 360 |
+
#progress {{
|
| 361 |
+
background: #4CAF50;
|
| 362 |
+
height: 4px;
|
| 363 |
+
width: 0%;
|
| 364 |
+
transition: width 0.3s;
|
| 365 |
+
}}
|
| 366 |
+
#message {{
|
| 367 |
+
position: absolute;
|
| 368 |
+
top: 50%;
|
| 369 |
+
left: 50%;
|
| 370 |
+
transform: translate(-50%, -50%);
|
| 371 |
+
color: #ff4444;
|
| 372 |
+
font-family: Arial;
|
| 373 |
+
font-size: 14px;
|
| 374 |
+
background: rgba(0,0,0,0.9);
|
| 375 |
+
padding: 20px;
|
| 376 |
+
border-radius: 8px;
|
| 377 |
+
display: none;
|
| 378 |
+
z-index: 11;
|
| 379 |
+
}}
|
| 380 |
+
#fps, #camid {{
|
| 381 |
+
position: absolute;
|
| 382 |
+
right: 10px;
|
| 383 |
+
color: white;
|
| 384 |
+
font-family: monospace;
|
| 385 |
+
font-size: 11px;
|
| 386 |
+
background: rgba(0,0,0,0.7);
|
| 387 |
+
padding: 6px 10px;
|
| 388 |
+
border-radius: 4px;
|
| 389 |
+
display: none;
|
| 390 |
+
}}
|
| 391 |
+
#fps {{ top: 10px; }}
|
| 392 |
+
#camid {{ top: 40px; }}
|
| 393 |
+
#controls-info {{
|
| 394 |
+
position: absolute;
|
| 395 |
+
bottom: 10px;
|
| 396 |
+
left: 10px;
|
| 397 |
+
color: white;
|
| 398 |
+
font-family: Arial;
|
| 399 |
+
font-size: 11px;
|
| 400 |
+
background: rgba(0,0,0,0.7);
|
| 401 |
+
padding: 8px 12px;
|
| 402 |
+
border-radius: 4px;
|
| 403 |
+
}}
|
| 404 |
+
</style>
|
| 405 |
+
</head>
|
| 406 |
+
<body>
|
| 407 |
+
<canvas id="canvas"></canvas>
|
| 408 |
+
<div id="spinner">
|
| 409 |
+
<div style="font-size:14px; margin-bottom:10px;">Loading 3D Viewer...</div>
|
| 410 |
+
<div style="background:#333; height:4px; width:200px; border-radius:2px; overflow:hidden;">
|
| 411 |
+
<div id="progress"></div>
|
| 412 |
</div>
|
| 413 |
</div>
|
| 414 |
+
<div id="message"></div>
|
| 415 |
+
<div id="fps"></div>
|
| 416 |
+
<div id="camid"></div>
|
| 417 |
+
<div id="controls-info">
|
|
|
|
|
|
|
|
|
|
|
|
|
| 418 |
<strong>Controls:</strong> Drag: Rotate | Scroll: Zoom | Right-drag: Pan
|
| 419 |
</div>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 420 |
|
| 421 |
+
<script src="{ply_url.rsplit("/", 3)[0]}/viewer.js"></script>
|
| 422 |
+
<script>
|
| 423 |
+
// Auto-load PLY after viewer initializes
|
| 424 |
+
const plyUrl = "{ply_url}";
|
| 425 |
+
console.log("=== Splat Viewer Init ===");
|
| 426 |
+
console.log("PLY URL:", plyUrl);
|
| 427 |
|
| 428 |
+
let attempts = 0;
|
| 429 |
+
const checkAndLoad = setInterval(function() {{
|
| 430 |
+
attempts++;
|
| 431 |
+
|
| 432 |
+
if (window.worker) {{
|
| 433 |
+
console.log("✓ Worker ready after", attempts * 100, "ms");
|
| 434 |
+
clearInterval(checkAndLoad);
|
| 435 |
+
|
| 436 |
+
fetch(plyUrl)
|
| 437 |
+
.then(r => {{ if (!r.ok) throw new Error("HTTP " + r.status); return r.arrayBuffer(); }})
|
| 438 |
+
.then(buffer => {{
|
| 439 |
+
console.log("✓ PLY loaded:", buffer.byteLength, "bytes");
|
| 440 |
+
const file = new File([buffer], "model.ply");
|
| 441 |
+
const reader = new FileReader();
|
| 442 |
+
reader.onload = () => {{
|
| 443 |
+
window.worker.postMessage({{ ply: reader.result }});
|
| 444 |
+
console.log("✓ Sent to worker");
|
| 445 |
+
}};
|
| 446 |
+
reader.readAsArrayBuffer(file);
|
| 447 |
+
}})
|
| 448 |
+
.catch(err => {{
|
| 449 |
+
console.error("✗ Error:", err);
|
| 450 |
+
document.getElementById("spinner").style.display = "none";
|
| 451 |
+
const msg = document.getElementById("message");
|
| 452 |
+
msg.textContent = "Error: " + err.message;
|
| 453 |
+
msg.style.display = "block";
|
| 454 |
+
}});
|
| 455 |
+
}} else if (attempts >= 50) {{
|
| 456 |
+
console.error("✗ Worker timeout");
|
| 457 |
+
clearInterval(checkAndLoad);
|
| 458 |
+
document.getElementById("spinner").style.display = "none";
|
| 459 |
+
const msg = document.getElementById("message");
|
| 460 |
+
msg.textContent = "Viewer failed to initialize";
|
| 461 |
+
msg.style.display = "block";
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 462 |
}}
|
| 463 |
+
}}, 100);
|
| 464 |
+
</script>
|
| 465 |
+
</body>
|
| 466 |
+
</html>
|
| 467 |
+
'></iframe>
|
|
|
|
| 468 |
"""
|
| 469 |
|
| 470 |
def main():
|
|
|
|
| 486 |
|
| 487 |
# Create Gradio-accessible URL for the PLY file
|
| 488 |
ply_url = f"/file={ply_path}"
|
| 489 |
+
|
| 490 |
+
# Generate unique viewer ID
|
| 491 |
+
viewer_id = str(uuid.uuid4())[:8]
|
| 492 |
+
viewer_html = create_splat_viewer_html(ply_url, viewer_id)
|
| 493 |
|
| 494 |
# Debug info showing the paths
|
| 495 |
+
debug_info = f"PLY Path: {ply_path}\nPLY URL: {ply_url}\nFile exists: {Path(ply_path).exists()}\nViewer ID: {viewer_id}"
|
| 496 |
|
| 497 |
return viewer_html, output_path, turntable_path, ply_path, debug_info
|
| 498 |
|