Spaces:
Sleeping
Sleeping
Download app.py from 002CyberTiger/I2Img: direct link, hf CLI and curl.
- Browser
- Download file 3.02 kB
-
https://huggingface.co/spaces/002CyberTiger/I2Img/resolve/main/app.py
- Command line
-
hf download hf://spaces/002CyberTiger/I2Img/app.py
-
curl -L -o app.py https://huggingface.co/spaces/002CyberTiger/I2Img/resolve/main/app.py
3.02 kB
| import sys | |
| import types | |
| # Patch for newer torchvision compatibility with GFPGAN | |
| try: | |
| import torchvision.transforms.functional as F | |
| functional_tensor = types.ModuleType("torchvision.transforms.functional_tensor") | |
| functional_tensor.rgb_to_grayscale = F.rgb_to_grayscale | |
| sys.modules["torchvision.transforms.functional_tensor"] = functional_tensor | |
| except Exception: | |
| pass | |
| import gradio as gr | |
| import cv2 | |
| import insightface | |
| from insightface.app import FaceAnalysis | |
| from huggingface_hub import hf_hub_download | |
| from gfpgan import GFPGANer | |
| import spaces | |
| app = None | |
| swapper = None | |
| restorer = None | |
| def load_models(): | |
| global app, swapper, restorer | |
| if app is None: | |
| app = FaceAnalysis(name="buffalo_l", providers=["CPUExecutionProvider"]) | |
| app.prepare(ctx_id=-1, det_size=(640, 640)) | |
| if swapper is None: | |
| swapper_path = hf_hub_download( | |
| repo_id="ezioruan/inswapper_128.onnx", | |
| filename="inswapper_128.onnx" | |
| ) | |
| swapper = insightface.model_zoo.get_model(swapper_path, download=False) | |
| if restorer is None: | |
| gfpgan_path = hf_hub_download( | |
| repo_id="gmk123/GFPGAN", | |
| filename="GFPGANv1.4.pth" | |
| ) | |
| restorer = GFPGANer( | |
| model_path=gfpgan_path, | |
| upscale=1, | |
| arch="clean", | |
| channel_multiplier=2, | |
| bg_upsampler=None | |
| ) | |
| def swap_faces(source_img, target_img, enhance_face): | |
| if source_img is None or target_img is None: | |
| return None | |
| load_models() | |
| source_bgr = cv2.cvtColor(source_img, cv2.COLOR_RGB2BGR) | |
| target_bgr = cv2.cvtColor(target_img, cv2.COLOR_RGB2BGR) | |
| source_faces = app.get(source_bgr) | |
| target_faces = app.get(target_bgr) | |
| if not source_faces or not target_faces: | |
| return target_img | |
| source_face = sorted(source_faces, key=lambda x: x.bbox[0])[0] | |
| target_face = sorted(target_faces, key=lambda x: x.bbox[0])[0] | |
| # 1. Perform the face swap | |
| result_bgr = swapper.get(target_bgr, target_face, source_face, paste_back=True) | |
| # 2. Sharpen and restore the face with GFPGAN if checked | |
| if enhance_face: | |
| _, _, result_bgr = restorer.enhance( | |
| result_bgr, | |
| has_aligned=False, | |
| only_center_face=False, | |
| paste_back=True | |
| ) | |
| return cv2.cvtColor(result_bgr, cv2.COLOR_BGR2RGB) | |
| with gr.Blocks() as demo: | |
| gr.Markdown("# 🎭 Private Face Swapper + GFPGAN Enhancer") | |
| with gr.Row(): | |
| with gr.Column(): | |
| src = gr.Image(label="Source Image (Face to Copy)", type="numpy") | |
| tgt = gr.Image(label="Target Image (Body / Scene)", type="numpy") | |
| enhance = gr.Checkbox(label="Enhance & Sharpen Face (GFPGAN v1.4)", value=True) | |
| btn = gr.Button("Swap Face", variant="primary") | |
| with gr.Column(): | |
| out = gr.Image(label="Swapped Result", type="numpy") | |
| btn.click(fn=swap_faces, inputs=[src, tgt, enhance], outputs=out) | |
| demo.queue().launch() |