import os
import cv2
import gradio as gr
from utils import get_upsampler, get_face_enhancer
def inference(img, task, model_name, scale):
if scale > 4:
scale = 4 # avoid too large scale value
try:
img = cv2.imread(img, cv2.IMREAD_UNCHANGED)
h, w = img.shape[0:2]
if h > 3500 or w > 3500:
raise gr.Error(f"image too large: {w} * {h}")
if (h < 300 and w < 300) and model_name != "srcnn":
img = cv2.resize(img, (w * 2, h * 2), interpolation=cv2.INTER_LANCZOS4)
return cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
if task == "face":
upsample_model_name = "realesr-general-x4v3"
else:
upsample_model_name = model_name
upsampler = get_upsampler(upsample_model_name)
if task == "face":
face_enhancer = get_face_enhancer(model_name, scale, upsampler)
else:
face_enhancer = None
try:
if face_enhancer is not None:
_, _, output = face_enhancer.enhance(
img, has_aligned=False, only_center_face=False, paste_back=True
)
else:
output, _ = upsampler.enhance(img, outscale=scale)
except RuntimeError as error:
raise gr.Error(error)
output = cv2.cvtColor(output, cv2.COLOR_BGR2RGB)
return output
except Exception as error:
raise gr.Error(f"global exception: {error}")
def on_task_change(task):
if task == "general":
return gr.Dropdown.update(
choices=[
"srcnn",
"RealESRGAN_x2plus",
"RealESRGAN_x4plus",
"RealESRNet_x4plus",
"realesr-general-x4v3",
],
value="realesr-general-x4v3",
)
elif task == "face":
return gr.Dropdown.update(
choices=["GFPGANv1.3", "GFPGANv1.4", "RestoreFormer"], value="GFPGANv1.4"
)
elif task == "anime":
return gr.Dropdown.update(
choices=["srcnn", "RealESRGAN_x4plus_anime_6B", "realesr-animevideov3"],
value="RealESRGAN_x4plus_anime_6B",
)
title = "ISR: General Image Super Resolution"
description = r"""Gradio demo for GFPGAN: Towards Real-World Blind Face Restoration with Generative Facial Prior.
It can be used to restore your **old photos** or improve **AI-generated faces**.
To use it, simply upload your image.
If GFPGAN is helpful, please help to ⭐ the Github Repo and recommend it to your friends 😊
"""
article = r"""