Team Ai
Apppublic

CVPR/GFPGAN-example

sourceHugging Faceapache-2.0updated 5y agoView on Hugging Face
6likes
app.py68 linesDownload Raw Back to root
1import os2os.system("pip install gfpgan")3 4os.system("pip freeze")5os.system("wget https://github.com/TencentARC/GFPGAN/releases/download/v0.2.0/GFPGANCleanv1-NoCE-C2.pth -P .")6import random7import gradio as gr8from PIL import Image9import torch10torch.hub.download_url_to_file('https://upload.wikimedia.org/wikipedia/commons/thumb/a/ab/Abraham_Lincoln_O-77_matte_collodion_print.jpg/1024px-Abraham_Lincoln_O-77_matte_collodion_print.jpg', 'lincoln.jpg')11torch.hub.download_url_to_file('https://upload.wikimedia.org/wikipedia/commons/5/50/Albert_Einstein_%28Nobel%29.png', 'einstein.png')12torch.hub.download_url_to_file('https://upload.wikimedia.org/wikipedia/commons/thumb/9/9d/Thomas_Edison2.jpg/1024px-Thomas_Edison2.jpg', 'edison.jpg')13torch.hub.download_url_to_file('https://upload.wikimedia.org/wikipedia/commons/thumb/a/a9/Henry_Ford_1888.jpg/1024px-Henry_Ford_1888.jpg', 'Henry.jpg')14torch.hub.download_url_to_file('https://upload.wikimedia.org/wikipedia/commons/thumb/0/06/Frida_Kahlo%2C_by_Guillermo_Kahlo.jpg/800px-Frida_Kahlo%2C_by_Guillermo_Kahlo.jpg', 'Frida.jpg')15 16 17 18 19import cv220import glob21import numpy as np22from basicsr.utils import imwrite23from gfpgan import GFPGANer24 25import warnings26warnings.warn('The unoptimized RealESRGAN is very slow on CPU. We do not use it. '27              'If you really want to use it, please modify the corresponding codes.')28bg_upsampler = None29 30 31 32# set up GFPGAN restorer33restorer = GFPGANer(34    model_path='GFPGANCleanv1-NoCE-C2.pth',35    upscale=2,36    arch='clean',37    channel_multiplier=2,38    bg_upsampler=bg_upsampler)39 40 41 42 43 44def inference(img):45    input_img = cv2.imread(img, cv2.IMREAD_COLOR)46    cropped_faces, restored_faces, restored_img = restorer.enhance(47        input_img, has_aligned=False, only_center_face=False, paste_back=True)48    49    return Image.fromarray(restored_faces[0][:,:,::-1])50        51title = "GFP-GAN Example"52description = "Gradio demo for GFP-GAN: Towards Real-World Blind Face Restoration with Generative Facial Prior. To use it, simply upload your image, or click one of the examples to load them. Read more at the links below. Please click submit only once"53article = "<p style='text-align: center'><a href='https://arxiv.org/abs/2101.04061' target='_blank'>Towards Real-World Blind Face Restoration with Generative Facial Prior</a> | <a href='https://github.com/TencentARC/GFPGAN' target='_blank'>Github Repo</a></p><center><img src='https://visitor-badge.glitch.me/badge?page_id=cvpr_GFPGAN_example' alt='visitor badge'></center>"54gr.Interface(55    inference, 56    [gr.inputs.Image(type="filepath", label="Input")], 57    gr.outputs.Image(type="pil", label="Output"),58    title=title,59    description=description,60    article=article,61    examples=[62    ['lincoln.jpg'],63    ['einstein.png'],64    ['edison.jpg'],65    ['Henry.jpg'],66    ['Frida.jpg']67    ]68    ).launch(enable_queue=True,cache_examples=True)