2023-01-23 06:24:43 +00:00
|
|
|
from PIL import Image
|
|
|
|
import numpy as np
|
|
|
|
|
2023-12-02 15:01:11 +00:00
|
|
|
from modules import scripts_postprocessing, gfpgan_model, ui_components
|
2023-01-23 06:24:43 +00:00
|
|
|
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
class ScriptPostprocessingGfpGan(scripts_postprocessing.ScriptPostprocessing):
|
|
|
|
name = "GFPGAN"
|
|
|
|
order = 2000
|
|
|
|
|
|
|
|
def ui(self):
|
2023-12-02 15:01:11 +00:00
|
|
|
with ui_components.InputAccordion(False, label="GFPGAN") as enable:
|
|
|
|
gfpgan_visibility = gr.Slider(minimum=0.0, maximum=1.0, step=0.001, label="Visibility", value=1.0, elem_id="extras_gfpgan_visibility")
|
2023-01-23 06:24:43 +00:00
|
|
|
|
|
|
|
return {
|
2023-12-02 15:01:11 +00:00
|
|
|
"enable": enable,
|
2023-01-23 06:24:43 +00:00
|
|
|
"gfpgan_visibility": gfpgan_visibility,
|
|
|
|
}
|
|
|
|
|
2023-12-02 15:01:11 +00:00
|
|
|
def process(self, pp: scripts_postprocessing.PostprocessedImage, enable, gfpgan_visibility):
|
|
|
|
if gfpgan_visibility == 0 or not enable:
|
2023-01-23 06:24:43 +00:00
|
|
|
return
|
|
|
|
|
2024-03-20 06:28:32 +00:00
|
|
|
restored_img = gfpgan_model.gfpgan_fix_faces(np.array(pp.image.convert("RGB"), dtype=np.uint8))
|
2023-01-23 06:24:43 +00:00
|
|
|
res = Image.fromarray(restored_img)
|
|
|
|
|
|
|
|
if gfpgan_visibility < 1.0:
|
|
|
|
res = Image.blend(pp.image, res, gfpgan_visibility)
|
|
|
|
|
|
|
|
pp.image = res
|
|
|
|
pp.info["GFPGAN visibility"] = round(gfpgan_visibility, 3)
|