Commit ·
090676f
1
Parent(s): f563fa2
Initial commit
Browse files46 unmodified lines47484950515253545592 unmodified lines14814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626790 unmodified lines35835936036136236336436536636736836937037137237310 unmodified lines38438538638738838939014 unmodified lines4054064074084094104114124134144154167 unmodified lines42442542642742842943046 unmodified linesfrom mvdiffusion.pipelines.pipeline_mvdiffusion_unclip import StableUnCLIPImg2ImgPipelinefrom utils_folder.face_utils import preprocess_image, preprocess_image_without_cropping# Import convert function for PLY to SPLAT conversionimport convert as ply_to_splat# HuggingFace repository configurationHF_REPO_ID = "wlyu/OpenFaceLift"92 unmodified linesself._models_on_gpu = Trueprint("Models on GPU, xformers enabled!")def _create_viewer_html(self, splat_path):"""Create standalone HTML viewer for the gaussian splat."""import base64# Read the splat file and encode as base64with open(splat_path, 'rb') as f:splat_data = f.read()splat_b64 = base64.b64encode(splat_data).decode('utf-8')# Read the main.js content and modify it to use embedded datawith open(Path(__file__).parent / "main.js", 'r') as f:js_content = f.read()# Replace the URL fetching part with blob URLjs_content = js_content.replace('params.get("url") || "train.splat"','window.EMBEDDED_SPLAT_URL || "train.splat"')html = f"""<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>3D Gaussian Splat Viewer - FaceLift</title><style>body {{margin: 0;padding: 0;overflow: hidden;font-family: Arial, sans-serif;background: #000;}}#canvas {{width: 100vw;height: 100vh;display: block;}}#info {{position: absolute;top: 10px;left: 10px;background: rgba(0, 0, 0, 0.7);color: white;padding: 10px;border-radius: 5px;font-size: 12px;max-width: 300px;z-index: 1000;}}#info h3 {{margin: 0 0 10px 0;font-size: 14px;}}#loading {{position: absolute;top: 50%;left: 50%;transform: translate(-50%, -50%);color: white;font-size: 18px;text-align: center;z-index: 999;}}</style></head><body><canvas id="canvas"></canvas><div id="loading">Loading 3D model...</div><div id="info"><h3>Controls</h3><p><b>Mouse:</b> Click and drag to orbit</p><p><b>Right click/Ctrl+drag:</b> Move forward/back (up/down), strafe (left/right)</p><p><b>Arrow keys:</b> Move forward/back, strafe left/right</p><p><b>WASD:</b> Rotate camera</p><p><b>Space:</b> Jump</p><p><b>Q/E:</b> Roll camera</p></div><script>// Convert base64 to blob and create URLconst SPLAT_DATA_B64 = '{splat_b64}';function b64toBlob(b64Data, contentType='application/octet-stream', sliceSize=512) {{const byteCharacters = atob(b64Data);const byteArrays = [];for (let offset = 0; offset < byteCharacters.length; offset += sliceSize) {{const slice = byteCharacters.slice(offset, offset + sliceSize);const byteNumbers = new Array(slice.length);for (let i = 0; i < slice.length; i++) {{byteNumbers[i] = slice.charCodeAt(i);}}const byteArray = new Uint8Array(byteNumbers);byteArrays.push(byteArray);}}return new Blob(byteArrays, {{type: contentType}});}}// Create blob URL for embedded splat dataconst blob = b64toBlob(SPLAT_DATA_B64);window.EMBEDDED_SPLAT_URL = URL.createObjectURL(blob);// Remove loading message once render startssetTimeout(() => {{const loading = document.getElementById('loading');if (loading) loading.style.display = 'none';}}, 2000);</script><script>{js_content}</script></body></html>"""return html@spaces.GPUdef generate_3d_head(self, image_path, auto_crop=True, guidance_scale=3.0,random_seed=4, num_steps=50):90 unmodified linesply_path = output_dir / "gaussians.ply"filtered_gaussians.save_ply(str(ply_path))# Convert PLY to SPLAT format for web viewersplat_path = output_dir / "gaussians.splat"ply_to_splat.convert(str(ply_path), str(splat_path))# Create HTML viewerviewer_html = self._create_viewer_html(str(splat_path))viewer_path = output_dir / "viewer.html"with open(viewer_path, 'w') as f:f.write(viewer_html)# Save output imagecomp_image = rearrange(comp_image, "x v c h w -> (x h) (v w) c")comp_image = (comp_image.cpu().numpy() * 255.0).clip(0, 255).astype(np.uint8)10 unmodified linesimageseq2video(turntable_frames, str(turntable_path), fps=30)return str(input_path), str(multiview_path), str(output_path), \str(turntable_path), str(viewer_path), str(ply_path)except Exception as e:raise gr.Error(f"Generation failed: {str(e)}")14 unmodified linesfn=pipeline.generate_3d_head,title="FaceLift: Single Image 3D Face Reconstruction",description="""Transform a single portrait image into a complete 3D head model with an interactive WebGL viewer.**Tips:**- Use high-quality portrait images with clear facial features- If face detection fails, try disabling auto-cropping and manually crop to square- Download the Interactive 3D Viewer HTML file to explore your model in full screen""",inputs=[gr.Image(type="filepath", label="Input Portrait Image"),7 unmodified linesgr.Image(label="Multi-view Generation"),gr.Image(label="3D Reconstruction"),gr.PlayableVideo(label="Turntable Animation"),gr.File(label="Interactive 3D Viewer (.html) - Download & Open"),gr.File(label="3D Model (.ply)"),],examples=examples,46 unmodified lines47484950515292 unmodified lines14514614714814915090 unmodified lines24124224324424524610 unmodified lines25725825926026126226314 unmodified lines2782792802812822832842852862872887 unmodified lines29629729829930030146 unmodified linesfrom mvdiffusion.pipelines.pipeline_mvdiffusion_unclip import StableUnCLIPImg2ImgPipelinefrom utils_folder.face_utils import preprocess_image, preprocess_image_without_cropping# HuggingFace repository configurationHF_REPO_ID = "wlyu/OpenFaceLift"92 unmodified linesself._models_on_gpu = Trueprint("Models on GPU, xformers enabled!")@spaces.GPUdef generate_3d_head(self, image_path, auto_crop=True, guidance_scale=3.0,random_seed=4, num_steps=50):90 unmodified linesply_path = output_dir / "gaussians.ply"filtered_gaussians.save_ply(str(ply_path))# Save output imagecomp_image = rearrange(comp_image, "x v c h w -> (x h) (v w) c")comp_image = (comp_image.cpu().numpy() * 255.0).clip(0, 255).astype(np.uint8)10 unmodified linesimageseq2video(turntable_frames, str(turntable_path), fps=30)return str(input_path), str(multiview_path), str(output_path), \str(turntable_path), str(ply_path)except Exception as e:raise gr.Error(f"Generation failed: {str(e)}")14 unmodified linesfn=pipeline.generate_3d_head,title="FaceLift: Single Image 3D Face Reconstruction",description="""Transform a single portrait image into a complete 3D head model.**Tips:**- Use high-quality portrait images with clear facial features- If face detection fails, try disabling auto-cropping and manually crop to square""",inputs=[gr.Image(type="filepath", label="Input Portrait Image"),7 unmodified linesgr.Image(label="Multi-view Generation"),gr.Image(label="3D Reconstruction"),gr.PlayableVideo(label="Turntable Animation"),gr.File(label="3D Model (.ply)"),],examples=examples,
app.py+2-131