Upload 3 files
Browse filesminimal complexity
- app.py +27 -42
- tiny_esrgan.py +55 -0
- tiny_gfpgan.py +92 -0
app.py
CHANGED
|
@@ -23,68 +23,53 @@
|
|
| 23 |
# SOFTWARE.
|
| 24 |
#
|
| 25 |
#######################################################################################
|
| 26 |
-
|
| 27 |
-
#
|
|
|
|
| 28 |
# It provides functionality to enhances an image by upscaling it 4 times its original size .
|
| 29 |
-
|
| 30 |
-
|
| 31 |
# Source code is based on or inspired by several projects.
|
| 32 |
# For more details and proper attribution, please refer to the following resources:
|
| 33 |
#
|
| 34 |
-
# - [
|
| 35 |
-
|
| 36 |
|
| 37 |
-
import torch
|
| 38 |
import gradio as gr
|
| 39 |
-
|
| 40 |
-
from
|
| 41 |
-
from realesrgan import RealESRGANer
|
| 42 |
|
| 43 |
-
|
|
|
|
| 44 |
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
'AI-Forever X4+':'AI-Forever_x4plus.pth'
|
| 49 |
-
}
|
| 50 |
|
| 51 |
-
def
|
| 52 |
-
return hf_hub_download(repo_id=REALESRGAN_REPO_ID, filename=file)
|
| 53 |
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
Returns:
|
| 61 |
-
path: File path to the enhanced, upscaled image.
|
| 62 |
-
"""
|
| 63 |
|
| 64 |
-
|
|
|
|
| 65 |
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
half = True if torch.cuda.is_available() else False
|
| 69 |
|
| 70 |
-
output , _ = RealESRGANer(
|
| 71 |
-
scale=4,
|
| 72 |
-
model_path=model_path,
|
| 73 |
-
model=model,
|
| 74 |
-
half=half).enhance(img)
|
| 75 |
|
| 76 |
-
return output
|
| 77 |
|
| 78 |
app = gr.Interface(
|
| 79 |
predict, [
|
| 80 |
-
gr.Image(type="
|
| 81 |
-
gr.Dropdown(list(MODELS.items()),
|
| 82 |
-
type="value", value='RealESRGAN_x4plus.pth', label='Enhancer model'),
|
| 83 |
], [
|
| 84 |
-
gr.Image(type="numpy", label="Image enhanced")
|
| 85 |
],
|
| 86 |
title="Image enhancer",
|
| 87 |
-
description="
|
| 88 |
|
| 89 |
app.launch(share=False, debug=True, show_error=True, mcp_server=True)
|
| 90 |
app.queue()
|
|
|
|
| 23 |
# SOFTWARE.
|
| 24 |
#
|
| 25 |
#######################################################################################
|
| 26 |
+
#
|
| 27 |
+
#
|
| 28 |
+
# This file implements an API endpoint GFPGAN system.
|
| 29 |
# It provides functionality to enhances an image by upscaling it 4 times its original size .
|
| 30 |
+
#
|
| 31 |
+
#
|
| 32 |
# Source code is based on or inspired by several projects.
|
| 33 |
# For more details and proper attribution, please refer to the following resources:
|
| 34 |
#
|
| 35 |
+
# - [GFPGAN] - [https://github.com/TencentARC/GFPGAN]
|
|
|
|
| 36 |
|
|
|
|
| 37 |
import gradio as gr
|
| 38 |
+
import numpy as np
|
| 39 |
+
from facexlib.utils.face_restoration_helper import FaceRestoreHelper
|
|
|
|
| 40 |
|
| 41 |
+
from tiny_esrgan import TinyESRGAN
|
| 42 |
+
from tiny_gfpgan import TinyGFPGAN
|
| 43 |
|
| 44 |
+
face_enhancer = TinyGFPGAN()
|
| 45 |
+
background_enhancer = TinyESRGAN()
|
| 46 |
+
face_helper = FaceRestoreHelper(upscale_factor=4, use_parse=True, model_rootpath='gfpgan/weights')
|
|
|
|
|
|
|
| 47 |
|
| 48 |
+
def predict(img):
|
|
|
|
| 49 |
|
| 50 |
+
img = np.asarray(img)
|
| 51 |
+
face_helper.clean_all()
|
| 52 |
+
face_helper.read_image(img)
|
| 53 |
+
# get face landmarks for each face
|
| 54 |
+
face_helper.get_face_landmarks_5(eye_dist_threshold=5)
|
| 55 |
+
face_helper.align_warp_face()
|
|
|
|
|
|
|
|
|
|
| 56 |
|
| 57 |
+
face_helper.restored_faces = face_enhancer.enhance(face_helper.cropped_faces)
|
| 58 |
+
bg = background_enhancer.enhance(img)
|
| 59 |
|
| 60 |
+
face_helper.get_inverse_affine(None)
|
| 61 |
+
return face_helper.paste_faces_to_input_image(upsample_img=bg)
|
|
|
|
| 62 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
|
|
|
|
| 64 |
|
| 65 |
app = gr.Interface(
|
| 66 |
predict, [
|
| 67 |
+
gr.Image(type="pil", label="Image input"),
|
|
|
|
|
|
|
| 68 |
], [
|
| 69 |
+
gr.Image(type="numpy", label="Image face enhanced")
|
| 70 |
],
|
| 71 |
title="Image enhancer",
|
| 72 |
+
description="GFPGAN")
|
| 73 |
|
| 74 |
app.launch(share=False, debug=True, show_error=True, mcp_server=True)
|
| 75 |
app.queue()
|
tiny_esrgan.py
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#######################################################################################
|
| 2 |
+
#
|
| 3 |
+
# MIT License
|
| 4 |
+
#
|
| 5 |
+
# Copyright (c) [2025] [leonelhs@gmail.com]
|
| 6 |
+
#
|
| 7 |
+
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
| 8 |
+
# of this software and associated documentation files (the "Software"), to deal
|
| 9 |
+
# in the Software without restriction, including without limitation the rights
|
| 10 |
+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
| 11 |
+
# copies of the Software, and to permit persons to whom the Software is
|
| 12 |
+
# furnished to do so, subject to the following conditions:
|
| 13 |
+
#
|
| 14 |
+
# The above copyright notice and this permission notice shall be included in all
|
| 15 |
+
# copies or substantial portions of the Software.
|
| 16 |
+
#
|
| 17 |
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
| 18 |
+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
| 19 |
+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
| 20 |
+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
| 21 |
+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
| 22 |
+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
| 23 |
+
# SOFTWARE.
|
| 24 |
+
#
|
| 25 |
+
#######################################################################################
|
| 26 |
+
|
| 27 |
+
# This file implements an API endpoint ESRGAN system.
|
| 28 |
+
# It provides functionality to enhances an image by upscaling it 4 times its original size .
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
# Source code is based on or inspired by several projects.
|
| 32 |
+
# For more details and proper attribution, please refer to the following resources:
|
| 33 |
+
#
|
| 34 |
+
# This code aims to study ESRGAN image restoration.
|
| 35 |
+
# It has isolated te minimal functionalities from ESRGAN x4 plus project.
|
| 36 |
+
#
|
| 37 |
+
# [Real-ESRGAN] - [https://github.com/xinntao/Real-ESRGAN]
|
| 38 |
+
#
|
| 39 |
+
|
| 40 |
+
import torch
|
| 41 |
+
from basicsr.archs.rrdbnet_arch import RRDBNet
|
| 42 |
+
from huggingface_hub import hf_hub_download
|
| 43 |
+
from realesrgan import RealESRGANer
|
| 44 |
+
|
| 45 |
+
REPO_ID = 'leonelhs/realesrgan'
|
| 46 |
+
|
| 47 |
+
class TinyESRGAN:
|
| 48 |
+
def __init__(self):
|
| 49 |
+
half = True if torch.cuda.is_available() else False
|
| 50 |
+
model_path = hf_hub_download(repo_id=REPO_ID, filename="RealESRGAN_x4plus.pth")
|
| 51 |
+
model = RRDBNet(num_in_ch=3, num_out_ch=3, num_feat=64, num_block=23, num_grow_ch=32, scale=4)
|
| 52 |
+
self.esrgan = RealESRGANer(scale=4, model_path=model_path, model=model, half=half)
|
| 53 |
+
|
| 54 |
+
def enhance(self, img):
|
| 55 |
+
return self.esrgan.enhance(img)[0]
|
tiny_gfpgan.py
ADDED
|
@@ -0,0 +1,92 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#######################################################################################
|
| 2 |
+
#
|
| 3 |
+
# MIT License
|
| 4 |
+
#
|
| 5 |
+
# Copyright (c) [2025] [leonelhs@gmail.com]
|
| 6 |
+
#
|
| 7 |
+
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
| 8 |
+
# of this software and associated documentation files (the "Software"), to deal
|
| 9 |
+
# in the Software without restriction, including without limitation the rights
|
| 10 |
+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
| 11 |
+
# copies of the Software, and to permit persons to whom the Software is
|
| 12 |
+
# furnished to do so, subject to the following conditions:
|
| 13 |
+
#
|
| 14 |
+
# The above copyright notice and this permission notice shall be included in all
|
| 15 |
+
# copies or substantial portions of the Software.
|
| 16 |
+
#
|
| 17 |
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
| 18 |
+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
| 19 |
+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
| 20 |
+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
| 21 |
+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
| 22 |
+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
| 23 |
+
# SOFTWARE.
|
| 24 |
+
#
|
| 25 |
+
#######################################################################################
|
| 26 |
+
|
| 27 |
+
# This file implements an API endpoint GFPGAN system.
|
| 28 |
+
# It provides functionality to enhances an image by upscaling it 4 times its original size .
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
# Source code is based on or inspired by several projects.
|
| 32 |
+
# For more details and proper attribution, please refer to the following resources:
|
| 33 |
+
#
|
| 34 |
+
# - [GFPGAN] - [https://github.com/TencentARC/GFPGAN]
|
| 35 |
+
# - [utils.py] - [https://github.com/TencentARC/GFPGAN/blob/master/gfpgan/utils.py]
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
import torch
|
| 39 |
+
from basicsr.utils import img2tensor, tensor2img
|
| 40 |
+
from gfpgan.archs.gfpganv1_clean_arch import GFPGANv1Clean
|
| 41 |
+
from huggingface_hub import hf_hub_download
|
| 42 |
+
from torchvision.transforms.functional import normalize
|
| 43 |
+
|
| 44 |
+
GFPGAN_REPO_ID = 'leonelhs/gfpgan'
|
| 45 |
+
|
| 46 |
+
class TinyGFPGAN:
|
| 47 |
+
"""
|
| 48 |
+
Minimal functionalities from GFPGAN project.
|
| 49 |
+
|
| 50 |
+
Args:
|
| 51 |
+
channel_multiplier (int): Channel multiplier for large networks of StyleGAN2. Default: 2.
|
| 52 |
+
"""
|
| 53 |
+
|
| 54 |
+
def __init__(self, channel_multiplier=2, device=None):
|
| 55 |
+
|
| 56 |
+
# initialize model
|
| 57 |
+
self.device = torch.device('cuda' if torch.cuda.is_available() else 'cpu') if device is None else device
|
| 58 |
+
# initialize the GFP-GAN
|
| 59 |
+
self.gfpgan = GFPGANv1Clean(
|
| 60 |
+
out_size=512,
|
| 61 |
+
channel_multiplier=channel_multiplier,
|
| 62 |
+
fix_decoder=False,
|
| 63 |
+
input_is_latent=True,
|
| 64 |
+
different_w=True,
|
| 65 |
+
sft_half=True)
|
| 66 |
+
|
| 67 |
+
model_path = hf_hub_download(repo_id=GFPGAN_REPO_ID, filename="GFPGANv1.4.pth")
|
| 68 |
+
loadnet = torch.load(model_path)
|
| 69 |
+
if 'params_ema' in loadnet:
|
| 70 |
+
keyname = 'params_ema'
|
| 71 |
+
else:
|
| 72 |
+
keyname = 'params'
|
| 73 |
+
self.gfpgan.load_state_dict(loadnet[keyname], strict=True)
|
| 74 |
+
self.gfpgan.eval()
|
| 75 |
+
self.gfpgan = self.gfpgan.to(self.device)
|
| 76 |
+
|
| 77 |
+
@torch.no_grad()
|
| 78 |
+
def inference(self, cropped_face, weight=0.5):
|
| 79 |
+
cropped_face_t = img2tensor(cropped_face / 255., bgr2rgb=True, float32=True)
|
| 80 |
+
normalize(cropped_face_t, [0.5, 0.5, 0.5], [0.5, 0.5, 0.5], inplace=True)
|
| 81 |
+
cropped_face_t = cropped_face_t.unsqueeze(0).to(self.device)
|
| 82 |
+
|
| 83 |
+
try:
|
| 84 |
+
output = self.gfpgan(cropped_face_t, return_rgb=False, weight=weight)[0]
|
| 85 |
+
# convert to image
|
| 86 |
+
restored_face = tensor2img(output.squeeze(0), rgb2bgr=True, min_max=(-1, 1))
|
| 87 |
+
return restored_face.astype('uint8')
|
| 88 |
+
except RuntimeError as error:
|
| 89 |
+
raise ValueError(f'\tFailed inference for GFPGAN: {error}.')
|
| 90 |
+
|
| 91 |
+
def enhance(self, cropped_faces):
|
| 92 |
+
return [self.inference(face) for face in cropped_faces]
|