Spaces:
Running
Running
Update app_2.py
Browse files
app_2.py
CHANGED
|
@@ -10,16 +10,15 @@ import cv2
|
|
| 10 |
import torch
|
| 11 |
import torch.nn.functional as F
|
| 12 |
import gradio as gr
|
| 13 |
-
from itertools import chain
|
| 14 |
|
| 15 |
from torchvision.transforms.functional import normalize
|
| 16 |
|
| 17 |
from basicsr.utils import imwrite, img2tensor, tensor2img
|
| 18 |
from basicsr.utils.download_util import load_file_from_url
|
| 19 |
from facelib.utils.face_restoration_helper import FaceRestoreHelper
|
| 20 |
-
from facelib.utils.misc import is_gray
|
| 21 |
from basicsr.archs.rrdbnet_arch import RRDBNet
|
| 22 |
from basicsr.utils.realesrgan_utils import RealESRGANer
|
|
|
|
| 23 |
|
| 24 |
from basicsr.utils.registry import ARCH_REGISTRY
|
| 25 |
|
|
@@ -58,6 +57,9 @@ torch.hub.download_url_to_file(
|
|
| 58 |
torch.hub.download_url_to_file(
|
| 59 |
'https://replicate.com/api/models/sczhou/codeformer/files/7cf19c2c-e0cf-4712-9af8-cf5bdbb8d0ee/012.jpg',
|
| 60 |
'05.jpg')
|
|
|
|
|
|
|
|
|
|
| 61 |
|
| 62 |
def imread(img_path):
|
| 63 |
img = cv2.imread(img_path)
|
|
@@ -102,19 +104,22 @@ codeformer_net.eval()
|
|
| 102 |
|
| 103 |
os.makedirs('output', exist_ok=True)
|
| 104 |
|
| 105 |
-
def inference(image, background_enhance, face_upsample, upscale, codeformer_fidelity):
|
| 106 |
"""Run a single prediction on the model"""
|
| 107 |
try: # global try
|
| 108 |
# take the default setting for the demo
|
| 109 |
-
has_aligned = False
|
| 110 |
only_center_face = False
|
| 111 |
draw_box = False
|
| 112 |
detection_model = "retinaface_resnet50"
|
|
|
|
| 113 |
print('Inp:', image, background_enhance, face_upsample, upscale, codeformer_fidelity)
|
| 114 |
-
|
| 115 |
-
if background_enhance is None
|
| 116 |
-
if face_upsample is None
|
| 117 |
-
if upscale is None
|
|
|
|
|
|
|
|
|
|
| 118 |
|
| 119 |
img = cv2.imread(str(image), cv2.IMREAD_COLOR)
|
| 120 |
print('\timage size:', img.shape)
|
|
@@ -161,9 +166,7 @@ def inference(image, background_enhance, face_upsample, upscale, codeformer_fide
|
|
| 161 |
# face restoration for each cropped face
|
| 162 |
for idx, cropped_face in enumerate(face_helper.cropped_faces):
|
| 163 |
# prepare data
|
| 164 |
-
cropped_face_t = img2tensor(
|
| 165 |
-
cropped_face / 255.0, bgr2rgb=True, float32=True
|
| 166 |
-
)
|
| 167 |
normalize(cropped_face_t, (0.5, 0.5, 0.5), (0.5, 0.5, 0.5), inplace=True)
|
| 168 |
cropped_face_t = cropped_face_t.unsqueeze(0).to(device)
|
| 169 |
|
|
@@ -177,12 +180,10 @@ def inference(image, background_enhance, face_upsample, upscale, codeformer_fide
|
|
| 177 |
torch.cuda.empty_cache()
|
| 178 |
except RuntimeError as error:
|
| 179 |
print(f"Failed inference for CodeFormer: {error}")
|
| 180 |
-
restored_face = tensor2img(
|
| 181 |
-
cropped_face_t, rgb2bgr=True, min_max=(-1, 1)
|
| 182 |
-
)
|
| 183 |
|
| 184 |
restored_face = restored_face.astype("uint8")
|
| 185 |
-
face_helper.add_restored_face(restored_face)
|
| 186 |
|
| 187 |
# paste_back
|
| 188 |
if not has_aligned:
|
|
@@ -204,6 +205,8 @@ def inference(image, background_enhance, face_upsample, upscale, codeformer_fide
|
|
| 204 |
restored_img = face_helper.paste_faces_to_input_image(
|
| 205 |
upsample_img=bg_img, draw_box=draw_box
|
| 206 |
)
|
|
|
|
|
|
|
| 207 |
|
| 208 |
# save restored img
|
| 209 |
save_path = f'output/out.png'
|
|
@@ -257,6 +260,12 @@ If you have any questions, please feel free to reach me out at <b>shangchenzhou@
|
|
| 257 |
td {
|
| 258 |
padding-right: 0px !important;
|
| 259 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 260 |
</style>
|
| 261 |
|
| 262 |
<table>
|
|
@@ -269,38 +278,31 @@ td {
|
|
| 269 |
<center><img src='https://api.infinitescript.com/badgen/count?name=sczhou/CodeFormer<ext=Visitors&color=6dc9aa' alt='visitors'></center>
|
| 270 |
"""
|
| 271 |
|
| 272 |
-
|
| 273 |
-
|
| 274 |
-
|
| 275 |
-
|
| 276 |
-
|
| 277 |
-
|
| 278 |
-
|
| 279 |
-
|
| 280 |
-
|
| 281 |
-
|
| 282 |
-
|
| 283 |
-
|
| 284 |
-
|
| 285 |
-
|
| 286 |
-
|
| 287 |
-
|
| 288 |
-
|
| 289 |
-
|
| 290 |
-
['
|
| 291 |
-
['
|
| 292 |
-
['
|
| 293 |
-
['04.jpg', True, True, 2, 0.1],
|
| 294 |
-
['05.jpg', True, True, 2, 0.1]
|
| 295 |
],
|
| 296 |
-
|
| 297 |
-
|
| 298 |
-
|
| 299 |
-
cache_examples=True)
|
| 300 |
-
|
| 301 |
-
gr.Markdown(article)
|
| 302 |
-
|
| 303 |
-
|
| 304 |
DEBUG = os.getenv('DEBUG') == '1'
|
| 305 |
-
demo.
|
| 306 |
-
demo.launch(debug=DEBUG)
|
|
|
|
| 10 |
import torch
|
| 11 |
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 |
|
|
|
|
| 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)
|
|
|
|
| 104 |
|
| 105 |
os.makedirs('output', exist_ok=True)
|
| 106 |
|
| 107 |
+
def inference(image, face_align, background_enhance, face_upsample, upscale, codeformer_fidelity):
|
| 108 |
"""Run a single prediction on the model"""
|
| 109 |
try: # global 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 |
print('Inp:', image, background_enhance, face_upsample, upscale, codeformer_fidelity)
|
| 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 |
print('\timage size:', img.shape)
|
|
|
|
| 166 |
# face restoration for each cropped face
|
| 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 |
|
|
|
|
| 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 |
restored_face = restored_face.astype("uint8")
|
| 186 |
+
face_helper.add_restored_face(restored_face, cropped_face)
|
| 187 |
|
| 188 |
# paste_back
|
| 189 |
if not has_aligned:
|
|
|
|
| 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'
|
|
|
|
| 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>
|
|
|
|
| 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 (0 for better quality, 1 for better identity)')
|
| 289 |
+
], [
|
| 290 |
+
gr.Image(type="numpy", label="Output")
|
| 291 |
+
],
|
| 292 |
+
title=title,
|
| 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 |
DEBUG = os.getenv('DEBUG') == '1'
|
| 307 |
+
# demo.launch(debug=DEBUG)
|
| 308 |
+
demo.launch(debug=DEBUG, share=True)
|