#!/usr/bin/env python # -*- coding:utf-8 -*- # Power by Zongsheng Yue 2022-12-16 16:17:14 import os import torch import argparse import numpy as np import gradio as gr from pathlib import Path from einops import rearrange from omegaconf import OmegaConf from skimage import img_as_ubyte from utils import util_opts from utils import util_image from utils import util_common from sampler import DifIRSampler from ResizeRight.resize_right import resize from basicsr.utils.download_util import load_file_from_url # setting configurations cfg_path = 'configs/sample/iddpm_ffhq512_swinir.yaml' configs = OmegaConf.load(cfg_path) configs.aligned = False # build the sampler for diffusion sampler_dist = DifIRSampler(configs) def predict(im_path, background_enhance, face_upsample, upscale, started_timesteps): assert isinstance(im_path, str) print(f'Processing image: {im_path}...') configs.background_enhance = background_enhance configs.face_upsample = face_upsample started_timesteps = int(started_timesteps) assert started_timesteps < int(configs.diffusion.params.timestep_respacing) # prepare the checkpoint if not Path(configs.model.ckpt_path).exists(): load_file_from_url( url="https://github.com/zsyOAOA/DifFace/releases/download/V1.0/iddpm_ffhq512_ema500000.pth", model_dir=str(Path(configs.model.ckpt_path).parent), progress=True, file_name=Path(configs.model.ckpt_path).name, ) if not Path(configs.model_ir.ckpt_path).exists(): load_file_from_url( url="https://github.com/zsyOAOA/DifFace/releases/download/V1.0/General_Face_ffhq512.pth", model_dir=str(Path(configs.model_ir.ckpt_path).parent), progress=True, file_name=Path(configs.model_ir.ckpt_path).name, ) # Load image im_lq = util_image.imread(im_path, chn='bgr', dtype='uint8') if upscale > 4: upscale = 4 # avoid momory exceeded due to too large upscale if upscale > 2 and min(im_lq.shape[:2])>1280: upscale = 2 # avoid momory exceeded due to too large img resolution configs.detection.upscale = int(upscale) image_restored, face_restored, face_cropped = sampler_dist.sample_func_bfr_unaligned( y0=im_lq, start_timesteps=started_timesteps, need_restoration=True, draw_box=False, ) restored_image_dir = Path('restored_output') if not restored_image_dir.exists(): restored_image_dir.mkdir() # save the whole image save_path = restored_image_dir / Path(im_path).name util_image.imwrite(image_restored, save_path, chn='rgb', dtype_in='uint8') return image_restored, str(save_path) title = "DifFace: Blind Face Restoration with Diffused Error Contraction" description = r"""