import os import shutil import cv2 import gradio as gr import torch from basicsr.archs.rrdbnet_arch import RRDBNet from gfpgan.utils import GFPGANer from realesrgan.utils import RealESRGANer #os.system("pip freeze") # download weights if not os.path.exists('model_zoo/real/RealESRGAN_x4plus.pth'): os.system("wget https://github.com/xinntao/Real-ESRGAN/releases/download/v0.1.0/RealESRGAN_x4plus.pth -P model_zoo/real") if not os.path.exists('model_zoo/real/RealESRGAN_x2plus.pth'): os.system("wget https://github.com/xinntao/Real-ESRGAN/releases/download/v0.2.1/RealESRGAN_x2plus.pth -P model_zoo/real") if not os.path.exists('model_zoo/gan/GFPGANv1.4.pth'): os.system("wget https://github.com/TencentARC/GFPGAN/releases/download/v1.3.0/GFPGANv1.4.pth -P model_zoo/gan") if not os.path.exists('model_zoo/gan/RestoreFormer.pth'): os.system("wget https://github.com/TencentARC/GFPGAN/releases/download/v1.3.4/RestoreFormer.pth -P model_zoo/gan") def inference(img, version, scale, enhance_face): # background enhancer with RealESRGAN if scale <= 2: model = RRDBNet(num_in_ch=3, num_out_ch=3, num_feat=64, num_block=23, num_grow_ch=32, scale=2) netscale = 2 model_path = 'model_zoo/real/RealESRGAN_x2plus.pth' else: model = RRDBNet(num_in_ch=3, num_out_ch=3, num_feat=64, num_block=23, num_grow_ch=32, scale=4) model_path = 'model_zoo/real/RealESRGAN_x4plus.pth' netscale = 4 print(model_path) tile = 400 if torch.cuda.is_available() else 0 dni_weight = None # restorer upsampler = RealESRGANer( scale=netscale, model_path=model_path, dni_weight=dni_weight, model=model, tile=tile, tile_pad=10, pre_pad=0, half=False, #Use fp32 precision during inference. Default: fp16 (half precision). gpu_id=None) #gpu device to use (default=None) can be 0,1,2 for multi-gpu if enhance_face: if version == 'RestoreFormer': face_enhancer = GFPGANer( model_path='model_zoo/gan/RestoreFormer.pth', upscale=scale, arch='RestoreFormer', channel_multiplier=2, bg_upsampler=upsampler) else: face_enhancer = GFPGANer( model_path='model_zoo/gan/GFPGANv1.4.pth', upscale=scale, arch='clean', channel_multiplier=2, bg_upsampler=upsampler) os.makedirs('output', exist_ok=True) if scale > 4: scale = 4 # avoid too large scale value try: extension = os.path.splitext(os.path.basename(str(img)))[1] img = cv2.imread(img, cv2.IMREAD_UNCHANGED) if len(img.shape) == 3 and img.shape[2] == 4: img_mode = 'RGBA' elif len(img.shape) == 2: # for gray inputs img_mode = None img = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR) else: img_mode = None h, w = img.shape[0:2] if h < 300: img = cv2.resize(img, (w * 2, h * 2), interpolation=cv2.INTER_LANCZOS4) try: if enhance_face: _, _, 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: print('Error', error) try: if scale != 2: interpolation = cv2.INTER_AREA if scale < 2 else cv2.INTER_LANCZOS4 h, w = img.shape[0:2] output = cv2.resize(output, (int(w * scale / 2), int(h * scale / 2)), interpolation=interpolation) except Exception as error: print('wrong scale input.', error) if img_mode == 'RGBA': # RGBA images should be saved in png format extension = 'png' else: extension = 'jpg' save_path = f'output/out.{extension}' cv2.imwrite(save_path, output) output = cv2.cvtColor(output, cv2.COLOR_BGR2RGB) return output, save_path except Exception as error: print('global exception', error) return None, None def clean_folder(folder): for filename in os.listdir(folder): file_path = os.path.join(folder, filename) try: if os.path.isfile(file_path) or os.path.islink(file_path): os.unlink(file_path) elif os.path.isdir(file_path): shutil.rmtree(file_path) except Exception as e: print('Failed to delete %s. Reason: %s' % (file_path, e)) title = "Real Esrgan Restore Ai Face Restoration by appsgenz.com" description = r""" """ article = r""" 😊 """ reminiApp = gr.Interface( inference, [ gr.Image(type="filepath", label="Input"), gr.Radio(['v1.4', 'RestoreFormer'], type="value", value='v1.4', label='version GFPGAN. Note that it work when enable Enhance faces '), gr.Number(label="Rescaling factor", value=1), gr.Checkbox(label="Enhance faces with GFPGAN. Note that it does not work for anime images/vidoes", value=True), ], [ gr.Image(type="numpy", label="Output (The whole image)"), gr.File(label="Download the output image") ], title=title, description=description, article=article) reminiApp.queue(concurrency_count=4) reminiApp.launch(share=False)