Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,8 +1,3 @@
|
|
| 1 |
-
"""
|
| 2 |
-
This file is used for deploying hugging face demo:
|
| 3 |
-
https://huggingface.co/spaces/sczhou/CodeFormer
|
| 4 |
-
"""
|
| 5 |
-
|
| 6 |
import sys
|
| 7 |
sys.path.append('CodeFormer')
|
| 8 |
import os
|
|
@@ -12,90 +7,49 @@ import torch.nn.functional as F
|
|
| 12 |
import gradio as gr
|
| 13 |
|
| 14 |
from torchvision.transforms.functional import normalize
|
| 15 |
-
|
| 16 |
from basicsr.utils import imwrite, img2tensor, tensor2img
|
| 17 |
from basicsr.utils.download_util import load_file_from_url
|
| 18 |
from facelib.utils.face_restoration_helper import FaceRestoreHelper
|
| 19 |
from basicsr.archs.rrdbnet_arch import RRDBNet
|
| 20 |
from basicsr.utils.realesrgan_utils import RealESRGANer
|
| 21 |
from facelib.utils.misc import is_gray
|
| 22 |
-
|
| 23 |
from basicsr.utils.registry import ARCH_REGISTRY
|
| 24 |
|
| 25 |
-
|
| 26 |
-
os.system("pip freeze")
|
| 27 |
-
|
| 28 |
pretrain_model_url = {
|
| 29 |
'codeformer': 'https://github.com/sczhou/CodeFormer/releases/download/v0.1.0/codeformer.pth',
|
| 30 |
'detection': 'https://github.com/sczhou/CodeFormer/releases/download/v0.1.0/detection_Resnet50_Final.pth',
|
| 31 |
'parsing': 'https://github.com/sczhou/CodeFormer/releases/download/v0.1.0/parsing_parsenet.pth',
|
| 32 |
'realesrgan': 'https://github.com/sczhou/CodeFormer/releases/download/v0.1.0/RealESRGAN_x2plus.pth'
|
| 33 |
}
|
| 34 |
-
# download weights
|
| 35 |
-
if not os.path.exists('CodeFormer/weights/CodeFormer/codeformer.pth'):
|
| 36 |
-
load_file_from_url(url=pretrain_model_url['codeformer'], model_dir='CodeFormer/weights/CodeFormer', progress=True, file_name=None)
|
| 37 |
-
if not os.path.exists('CodeFormer/weights/facelib/detection_Resnet50_Final.pth'):
|
| 38 |
-
load_file_from_url(url=pretrain_model_url['detection'], model_dir='CodeFormer/weights/facelib', progress=True, file_name=None)
|
| 39 |
-
if not os.path.exists('CodeFormer/weights/facelib/parsing_parsenet.pth'):
|
| 40 |
-
load_file_from_url(url=pretrain_model_url['parsing'], model_dir='CodeFormer/weights/facelib', progress=True, file_name=None)
|
| 41 |
-
if not os.path.exists('CodeFormer/weights/realesrgan/RealESRGAN_x2plus.pth'):
|
| 42 |
-
load_file_from_url(url=pretrain_model_url['realesrgan'], model_dir='CodeFormer/weights/realesrgan', progress=True, file_name=None)
|
| 43 |
|
| 44 |
-
#
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
'https://replicate.com/api/models/sczhou/codeformer/files/a1daba8e-af14-4b00-86a4-69cec9619b53/04.jpg',
|
| 50 |
-
'02.jpg')
|
| 51 |
-
torch.hub.download_url_to_file(
|
| 52 |
-
'https://replicate.com/api/models/sczhou/codeformer/files/542d64f9-1712-4de7-85f7-3863009a7c3d/03.jpg',
|
| 53 |
-
'03.jpg')
|
| 54 |
-
torch.hub.download_url_to_file(
|
| 55 |
-
'https://replicate.com/api/models/sczhou/codeformer/files/a11098b0-a18a-4c02-a19a-9a7045d68426/010.jpg',
|
| 56 |
-
'04.jpg')
|
| 57 |
-
torch.hub.download_url_to_file(
|
| 58 |
-
'https://replicate.com/api/models/sczhou/codeformer/files/7cf19c2c-e0cf-4712-9af8-cf5bdbb8d0ee/012.jpg',
|
| 59 |
-
'05.jpg')
|
| 60 |
-
torch.hub.download_url_to_file(
|
| 61 |
-
'https://raw.githubusercontent.com/sczhou/CodeFormer/master/inputs/cropped_faces/0729.png',
|
| 62 |
-
'06.png')
|
| 63 |
|
|
|
|
| 64 |
def imread(img_path):
|
| 65 |
img = cv2.imread(img_path)
|
| 66 |
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
|
| 67 |
return img
|
| 68 |
|
| 69 |
-
# set enhancer with RealESRGAN
|
| 70 |
def set_realesrgan():
|
| 71 |
-
half =
|
| 72 |
-
model = RRDBNet(
|
| 73 |
-
num_in_ch=3,
|
| 74 |
-
num_out_ch=3,
|
| 75 |
-
num_feat=64,
|
| 76 |
-
num_block=23,
|
| 77 |
-
num_grow_ch=32,
|
| 78 |
-
scale=2,
|
| 79 |
-
)
|
| 80 |
upsampler = RealESRGANer(
|
| 81 |
-
scale=2,
|
| 82 |
-
|
| 83 |
-
model=model,
|
| 84 |
-
tile=400,
|
| 85 |
-
tile_pad=40,
|
| 86 |
-
pre_pad=0,
|
| 87 |
-
half=half,
|
| 88 |
)
|
| 89 |
return upsampler
|
| 90 |
|
|
|
|
| 91 |
upsampler = set_realesrgan()
|
| 92 |
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
|
| 93 |
codeformer_net = ARCH_REGISTRY.get("CodeFormer")(
|
| 94 |
-
dim_embd=512,
|
| 95 |
-
|
| 96 |
-
n_head=8,
|
| 97 |
-
n_layers=9,
|
| 98 |
-
connect_list=["32", "64", "128", "256"],
|
| 99 |
).to(device)
|
| 100 |
ckpt_path = "CodeFormer/weights/CodeFormer/codeformer.pth"
|
| 101 |
checkpoint = torch.load(ckpt_path)["params_ema"]
|
|
@@ -104,205 +58,69 @@ codeformer_net.eval()
|
|
| 104 |
|
| 105 |
os.makedirs('output', exist_ok=True)
|
| 106 |
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
try:
|
| 110 |
-
# take the default setting for the demo
|
| 111 |
only_center_face = False
|
| 112 |
-
draw_box = False
|
| 113 |
detection_model = "retinaface_resnet50"
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
face_align = face_align if face_align is not None else True
|
| 117 |
-
background_enhance = background_enhance if background_enhance is not None else True
|
| 118 |
-
face_upsample = face_upsample if face_upsample is not None else True
|
| 119 |
-
upscale = upscale if (upscale is not None and upscale > 0) else 2
|
| 120 |
-
|
| 121 |
-
has_aligned = not face_align
|
| 122 |
-
upscale = 1 if has_aligned else upscale
|
| 123 |
-
|
| 124 |
img = cv2.imread(str(image), cv2.IMREAD_COLOR)
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
upscale = int(upscale) # convert type to int
|
| 128 |
-
if upscale > 4: # avoid memory exceeded due to too large upscale
|
| 129 |
-
upscale = 4
|
| 130 |
-
if upscale > 2 and max(img.shape[:2])>1000: # avoid memory exceeded due to too large img resolution
|
| 131 |
-
upscale = 2
|
| 132 |
-
if max(img.shape[:2]) > 1500: # avoid memory exceeded due to too large img resolution
|
| 133 |
-
upscale = 1
|
| 134 |
-
background_enhance = False
|
| 135 |
-
face_upsample = False
|
| 136 |
|
| 137 |
face_helper = FaceRestoreHelper(
|
| 138 |
-
upscale,
|
| 139 |
-
|
| 140 |
-
crop_ratio=(1, 1),
|
| 141 |
-
det_model=detection_model,
|
| 142 |
-
save_ext="png",
|
| 143 |
-
use_parse=True,
|
| 144 |
-
device=device,
|
| 145 |
)
|
|
|
|
| 146 |
bg_upsampler = upsampler if background_enhance else None
|
| 147 |
face_upsampler = upsampler if face_upsample else None
|
| 148 |
|
| 149 |
if has_aligned:
|
| 150 |
-
# the input faces are already cropped and aligned
|
| 151 |
img = cv2.resize(img, (512, 512), interpolation=cv2.INTER_LINEAR)
|
| 152 |
face_helper.is_gray = is_gray(img, threshold=5)
|
| 153 |
-
if face_helper.is_gray:
|
| 154 |
-
print('\tgrayscale input: True')
|
| 155 |
face_helper.cropped_faces = [img]
|
| 156 |
else:
|
| 157 |
face_helper.read_image(img)
|
| 158 |
-
|
| 159 |
-
num_det_faces = face_helper.get_face_landmarks_5(
|
| 160 |
-
only_center_face=only_center_face, resize=640, eye_dist_threshold=5
|
| 161 |
-
)
|
| 162 |
-
print(f'\tdetect {num_det_faces} faces')
|
| 163 |
-
# align and warp each face
|
| 164 |
face_helper.align_warp_face()
|
| 165 |
|
| 166 |
-
|
| 167 |
-
for idx, cropped_face in enumerate(face_helper.cropped_faces):
|
| 168 |
-
# prepare data
|
| 169 |
cropped_face_t = img2tensor(cropped_face / 255., bgr2rgb=True, float32=True)
|
| 170 |
normalize(cropped_face_t, (0.5, 0.5, 0.5), (0.5, 0.5, 0.5), inplace=True)
|
| 171 |
cropped_face_t = cropped_face_t.unsqueeze(0).to(device)
|
| 172 |
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
)[0]
|
| 178 |
-
restored_face = tensor2img(output, rgb2bgr=True, min_max=(-1, 1))
|
| 179 |
-
del output
|
| 180 |
-
torch.cuda.empty_cache()
|
| 181 |
-
except RuntimeError as error:
|
| 182 |
-
print(f"Failed inference for CodeFormer: {error}")
|
| 183 |
-
restored_face = tensor2img(cropped_face_t, rgb2bgr=True, min_max=(-1, 1))
|
| 184 |
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
if not has_aligned:
|
| 190 |
-
# upsample the background
|
| 191 |
-
if bg_upsampler is not None:
|
| 192 |
-
# Now only support RealESRGAN for upsampling background
|
| 193 |
-
bg_img = bg_upsampler.enhance(img, outscale=upscale)[0]
|
| 194 |
-
else:
|
| 195 |
-
bg_img = None
|
| 196 |
-
face_helper.get_inverse_affine(None)
|
| 197 |
-
# paste each restored face to the input image
|
| 198 |
-
if face_upsample and face_upsampler is not None:
|
| 199 |
-
restored_img = face_helper.paste_faces_to_input_image(
|
| 200 |
-
upsample_img=bg_img,
|
| 201 |
-
draw_box=draw_box,
|
| 202 |
-
face_upsampler=face_upsampler,
|
| 203 |
-
)
|
| 204 |
-
else:
|
| 205 |
-
restored_img = face_helper.paste_faces_to_input_image(
|
| 206 |
-
upsample_img=bg_img, draw_box=draw_box
|
| 207 |
-
)
|
| 208 |
-
else:
|
| 209 |
-
restored_img = restored_face
|
| 210 |
-
|
| 211 |
-
# save restored img
|
| 212 |
-
save_path = f'output/out.png'
|
| 213 |
-
imwrite(restored_img, str(save_path))
|
| 214 |
|
| 215 |
-
|
| 216 |
-
|
|
|
|
| 217 |
except Exception as error:
|
| 218 |
-
print('
|
| 219 |
-
return None
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
title = "CodeFormer: Robust Face Restoration and Enhancement Network"
|
| 223 |
-
|
| 224 |
-
description = r"""<center><img src='https://user-images.githubusercontent.com/14334509/189166076-94bb2cac-4f4e-40fb-a69f-66709e3d98f5.png' alt='CodeFormer logo'></center>
|
| 225 |
-
<br>
|
| 226 |
-
<b>Official Gradio demo</b> for <a href='https://github.com/sczhou/CodeFormer' target='_blank'><b>Towards Robust Blind Face Restoration with Codebook Lookup Transformer (NeurIPS 2022)</b></a><br>
|
| 227 |
-
🔥 CodeFormer is a robust face restoration algorithm for old photos or AI-generated faces.<br>
|
| 228 |
-
🤗 Try CodeFormer for improved stable-diffusion generation!<br>
|
| 229 |
-
"""
|
| 230 |
-
|
| 231 |
-
article = r"""
|
| 232 |
-
If CodeFormer is helpful, please help to ⭐ the <a href='https://github.com/sczhou/CodeFormer' target='_blank'>Github Repo</a>. Thanks!
|
| 233 |
-
[](https://github.com/sczhou/CodeFormer)
|
| 234 |
-
|
| 235 |
-
---
|
| 236 |
-
|
| 237 |
-
📝 **Citation**
|
| 238 |
-
|
| 239 |
-
If our work is useful for your research, please consider citing:
|
| 240 |
-
```bibtex
|
| 241 |
-
@inproceedings{zhou2022codeformer,
|
| 242 |
-
author = {Zhou, Shangchen and Chan, Kelvin C.K. and Li, Chongyi and Loy, Chen Change},
|
| 243 |
-
title = {Towards Robust Blind Face Restoration with Codebook Lookup TransFormer},
|
| 244 |
-
booktitle = {NeurIPS},
|
| 245 |
-
year = {2022}
|
| 246 |
-
}
|
| 247 |
-
```
|
| 248 |
-
|
| 249 |
-
📋 **License**
|
| 250 |
-
|
| 251 |
-
This project is licensed under <a rel="license" href="https://github.com/sczhou/CodeFormer/blob/master/LICENSE">S-Lab License 1.0</a>.
|
| 252 |
-
Redistribution and use for non-commercial purposes should follow this license.
|
| 253 |
-
|
| 254 |
-
📧 **Contact**
|
| 255 |
-
|
| 256 |
-
If you have any questions, please feel free to reach me out at <b>shangchenzhou@gmail.com</b>.
|
| 257 |
-
|
| 258 |
-
🤗 **Find Me:**
|
| 259 |
-
<style type="text/css">
|
| 260 |
-
td {
|
| 261 |
-
padding-right: 0px !important;
|
| 262 |
-
}
|
| 263 |
-
|
| 264 |
-
.gradio-container-4-37-2 .prose table, .gradio-container-4-37-2 .prose tr, .gradio-container-4-37-2 .prose td, .gradio-container-4-37-2 .prose th {
|
| 265 |
-
border: 0px solid #ffffff;
|
| 266 |
-
border-bottom: 0px solid #ffffff;
|
| 267 |
-
}
|
| 268 |
-
|
| 269 |
-
</style>
|
| 270 |
-
|
| 271 |
-
<table>
|
| 272 |
-
<tr>
|
| 273 |
-
<td><a href="https://github.com/sczhou"><img style="margin:-0.8em 0 2em 0" src="https://img.shields.io/github/followers/sczhou?style=social" alt="Github Follow"></a></td>
|
| 274 |
-
<td><a href="https://twitter.com/ShangchenZhou"><img style="margin:-0.8em 0 2em 0" src="https://img.shields.io/twitter/follow/ShangchenZhou?label=%40ShangchenZhou&style=social" alt="Twitter Follow"></a></td>
|
| 275 |
-
</tr>
|
| 276 |
-
</table>
|
| 277 |
-
|
| 278 |
-
<center><img src='https://api.infinitescript.com/badgen/count?name=sczhou/CodeFormer<ext=Visitors&color=6dc9aa' alt='visitors'></center>
|
| 279 |
-
"""
|
| 280 |
|
|
|
|
| 281 |
demo = gr.Interface(
|
| 282 |
-
inference,
|
|
|
|
| 283 |
gr.Image(type="filepath", label="Input"),
|
| 284 |
gr.Checkbox(value=True, label="Pre_Face_Align"),
|
| 285 |
gr.Checkbox(value=True, label="Background_Enhance"),
|
| 286 |
gr.Checkbox(value=True, label="Face_Upsample"),
|
| 287 |
gr.Number(value=2, label="Rescaling_Factor (up to 4)"),
|
| 288 |
-
gr.Slider(0, 1, value=0.5, step=0.01, label='Codeformer_Fidelity
|
| 289 |
-
],
|
| 290 |
-
|
| 291 |
-
|
| 292 |
-
|
| 293 |
-
description=description,
|
| 294 |
-
article=article,
|
| 295 |
-
examples=[
|
| 296 |
-
['01.png', True, True, True, 2, 0.7],
|
| 297 |
-
['02.jpg', True, True, True, 2, 0.7],
|
| 298 |
-
['03.jpg', True, True, True, 2, 0.7],
|
| 299 |
-
['04.jpg', True, True, True, 2, 0.1],
|
| 300 |
-
['05.jpg', True, True, True, 2, 0.1],
|
| 301 |
-
['06.png', False, True, True, 1, 0.5]
|
| 302 |
-
],
|
| 303 |
-
concurrency_limit=2
|
| 304 |
-
)
|
| 305 |
|
| 306 |
-
|
| 307 |
-
# demo.launch(debug=DEBUG)
|
| 308 |
-
demo.launch(debug=DEBUG, share=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import sys
|
| 2 |
sys.path.append('CodeFormer')
|
| 3 |
import os
|
|
|
|
| 7 |
import gradio as gr
|
| 8 |
|
| 9 |
from torchvision.transforms.functional import normalize
|
|
|
|
| 10 |
from basicsr.utils import imwrite, img2tensor, tensor2img
|
| 11 |
from basicsr.utils.download_util import load_file_from_url
|
| 12 |
from facelib.utils.face_restoration_helper import FaceRestoreHelper
|
| 13 |
from basicsr.archs.rrdbnet_arch import RRDBNet
|
| 14 |
from basicsr.utils.realesrgan_utils import RealESRGANer
|
| 15 |
from facelib.utils.misc import is_gray
|
|
|
|
| 16 |
from basicsr.utils.registry import ARCH_REGISTRY
|
| 17 |
|
| 18 |
+
# Model weight URLs
|
|
|
|
|
|
|
| 19 |
pretrain_model_url = {
|
| 20 |
'codeformer': 'https://github.com/sczhou/CodeFormer/releases/download/v0.1.0/codeformer.pth',
|
| 21 |
'detection': 'https://github.com/sczhou/CodeFormer/releases/download/v0.1.0/detection_Resnet50_Final.pth',
|
| 22 |
'parsing': 'https://github.com/sczhou/CodeFormer/releases/download/v0.1.0/parsing_parsenet.pth',
|
| 23 |
'realesrgan': 'https://github.com/sczhou/CodeFormer/releases/download/v0.1.0/RealESRGAN_x2plus.pth'
|
| 24 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
+
# Download weights if not already present
|
| 27 |
+
for key, url in pretrain_model_url.items():
|
| 28 |
+
file_path = f"CodeFormer/weights/{key}/{url.split('/')[-1]}"
|
| 29 |
+
if not os.path.exists(file_path):
|
| 30 |
+
load_file_from_url(url=url, model_dir=os.path.dirname(file_path), progress=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
+
# Helper functions
|
| 33 |
def imread(img_path):
|
| 34 |
img = cv2.imread(img_path)
|
| 35 |
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
|
| 36 |
return img
|
| 37 |
|
|
|
|
| 38 |
def set_realesrgan():
|
| 39 |
+
half = torch.cuda.is_available()
|
| 40 |
+
model = RRDBNet(num_in_ch=3, num_out_ch=3, num_feat=64, num_block=23, num_grow_ch=32, scale=2)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
upsampler = RealESRGANer(
|
| 42 |
+
scale=2, model_path="CodeFormer/weights/realesrgan/RealESRGAN_x2plus.pth",
|
| 43 |
+
model=model, tile=400, tile_pad=40, pre_pad=0, half=half
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
)
|
| 45 |
return upsampler
|
| 46 |
|
| 47 |
+
# Model setup
|
| 48 |
upsampler = set_realesrgan()
|
| 49 |
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
|
| 50 |
codeformer_net = ARCH_REGISTRY.get("CodeFormer")(
|
| 51 |
+
dim_embd=512, codebook_size=1024, n_head=8, n_layers=9,
|
| 52 |
+
connect_list=["32", "64", "128", "256"]
|
|
|
|
|
|
|
|
|
|
| 53 |
).to(device)
|
| 54 |
ckpt_path = "CodeFormer/weights/CodeFormer/codeformer.pth"
|
| 55 |
checkpoint = torch.load(ckpt_path)["params_ema"]
|
|
|
|
| 58 |
|
| 59 |
os.makedirs('output', exist_ok=True)
|
| 60 |
|
| 61 |
+
# Inference function
|
| 62 |
+
def inference(image, face_align=True, background_enhance=True, face_upsample=True, upscale=2, codeformer_fidelity=0.5):
|
| 63 |
+
try:
|
|
|
|
| 64 |
only_center_face = False
|
|
|
|
| 65 |
detection_model = "retinaface_resnet50"
|
| 66 |
+
|
| 67 |
+
# Load image and set parameters
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
img = cv2.imread(str(image), cv2.IMREAD_COLOR)
|
| 69 |
+
has_aligned = not face_align
|
| 70 |
+
upscale = min(max(1, int(upscale)), 4)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
|
| 72 |
face_helper = FaceRestoreHelper(
|
| 73 |
+
upscale, face_size=512, crop_ratio=(1, 1), det_model=detection_model,
|
| 74 |
+
save_ext="png", use_parse=True, device=device
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
)
|
| 76 |
+
|
| 77 |
bg_upsampler = upsampler if background_enhance else None
|
| 78 |
face_upsampler = upsampler if face_upsample else None
|
| 79 |
|
| 80 |
if has_aligned:
|
|
|
|
| 81 |
img = cv2.resize(img, (512, 512), interpolation=cv2.INTER_LINEAR)
|
| 82 |
face_helper.is_gray = is_gray(img, threshold=5)
|
|
|
|
|
|
|
| 83 |
face_helper.cropped_faces = [img]
|
| 84 |
else:
|
| 85 |
face_helper.read_image(img)
|
| 86 |
+
num_det_faces = face_helper.get_face_landmarks_5(only_center_face=only_center_face, resize=640, eye_dist_threshold=5)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 87 |
face_helper.align_warp_face()
|
| 88 |
|
| 89 |
+
for cropped_face in face_helper.cropped_faces:
|
|
|
|
|
|
|
| 90 |
cropped_face_t = img2tensor(cropped_face / 255., bgr2rgb=True, float32=True)
|
| 91 |
normalize(cropped_face_t, (0.5, 0.5, 0.5), (0.5, 0.5, 0.5), inplace=True)
|
| 92 |
cropped_face_t = cropped_face_t.unsqueeze(0).to(device)
|
| 93 |
|
| 94 |
+
with torch.no_grad():
|
| 95 |
+
output = codeformer_net(cropped_face_t, w=codeformer_fidelity, adain=True)[0]
|
| 96 |
+
restored_face = tensor2img(output, rgb2bgr=True, min_max=(-1, 1))
|
| 97 |
+
face_helper.add_restored_face(restored_face.astype("uint8"), cropped_face)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 98 |
|
| 99 |
+
restored_img = face_helper.paste_faces_to_input_image(
|
| 100 |
+
upsample_img=bg_upsampler.enhance(img, outscale=upscale)[0] if bg_upsampler else None,
|
| 101 |
+
face_upsampler=face_upsampler
|
| 102 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 103 |
|
| 104 |
+
save_path = 'output/out.png'
|
| 105 |
+
imwrite(restored_img, save_path)
|
| 106 |
+
return cv2.cvtColor(restored_img, cv2.COLOR_BGR2RGB)
|
| 107 |
except Exception as error:
|
| 108 |
+
print('Error during inference:', error)
|
| 109 |
+
return None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 110 |
|
| 111 |
+
# Gradio Interface
|
| 112 |
demo = gr.Interface(
|
| 113 |
+
fn=inference,
|
| 114 |
+
inputs=[
|
| 115 |
gr.Image(type="filepath", label="Input"),
|
| 116 |
gr.Checkbox(value=True, label="Pre_Face_Align"),
|
| 117 |
gr.Checkbox(value=True, label="Background_Enhance"),
|
| 118 |
gr.Checkbox(value=True, label="Face_Upsample"),
|
| 119 |
gr.Number(value=2, label="Rescaling_Factor (up to 4)"),
|
| 120 |
+
gr.Slider(0, 1, value=0.5, step=0.01, label='Codeformer_Fidelity')
|
| 121 |
+
],
|
| 122 |
+
outputs=gr.Image(type="numpy", label="Output"),
|
| 123 |
+
title="CodeFormer: Robust Face Restoration and Enhancement Network"
|
| 124 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 125 |
|
| 126 |
+
demo.launch(debug=os.getenv('DEBUG') == '1', share=True)
|
|
|
|
|
|