Spaces:
Running
on
L4
Running
on
L4
Update app.py
Browse files
app.py
CHANGED
|
@@ -9,7 +9,7 @@ import os
|
|
| 9 |
import cv2
|
| 10 |
import torch
|
| 11 |
import torch.nn.functional as F
|
| 12 |
-
import uuid, threading, time
|
| 13 |
import gradio as gr
|
| 14 |
|
| 15 |
from torchvision.transforms.functional import normalize
|
|
@@ -67,13 +67,16 @@ def imread(img_path):
|
|
| 67 |
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
|
| 68 |
return img
|
| 69 |
|
| 70 |
-
def delayed_remove(path, delay=
|
| 71 |
time.sleep(delay)
|
| 72 |
try:
|
| 73 |
if os.path.exists(path):
|
| 74 |
os.remove(path)
|
|
|
|
|
|
|
|
|
|
| 75 |
except Exception as e:
|
| 76 |
-
print(f"[
|
| 77 |
|
| 78 |
# set enhancer with RealESRGAN
|
| 79 |
def set_realesrgan():
|
|
@@ -176,6 +179,13 @@ def inference(image, face_align, background_enhance, face_upsample, upscale, cod
|
|
| 176 |
# align and warp each face
|
| 177 |
face_helper.align_warp_face()
|
| 178 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 179 |
# face restoration for each cropped face
|
| 180 |
for idx, cropped_face in enumerate(face_helper.cropped_faces):
|
| 181 |
# prepare data
|
|
@@ -232,13 +242,14 @@ def inference(image, face_align, background_enhance, face_upsample, upscale, cod
|
|
| 232 |
#save restored img
|
| 233 |
save_path = f"output/{uuid.uuid4().hex}.png"
|
| 234 |
imwrite(restored_img, save_path)
|
|
|
|
| 235 |
|
| 236 |
threading.Thread(
|
| 237 |
target=delayed_remove,
|
| 238 |
-
args=(save_path,
|
| 239 |
daemon=True
|
| 240 |
).start()
|
| 241 |
-
|
| 242 |
return save_path
|
| 243 |
|
| 244 |
except Exception as error:
|
|
@@ -314,7 +325,8 @@ demo = gr.Interface(
|
|
| 314 |
gr.Number(value=2, label="Rescaling_Factor (up to 4)"),
|
| 315 |
gr.Slider(0, 1, value=0.5, step=0.01, label='Codeformer_Fidelity (0 for better quality, 1 for better identity)')
|
| 316 |
], [
|
| 317 |
-
gr.Image(type="filepath", label="Output")
|
|
|
|
| 318 |
],
|
| 319 |
title=title,
|
| 320 |
description=description,
|
|
|
|
| 9 |
import cv2
|
| 10 |
import torch
|
| 11 |
import torch.nn.functional as F
|
| 12 |
+
import uuid, threading, time, glob
|
| 13 |
import gradio as gr
|
| 14 |
|
| 15 |
from torchvision.transforms.functional import normalize
|
|
|
|
| 67 |
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
|
| 68 |
return img
|
| 69 |
|
| 70 |
+
def delayed_remove(path, delay=60):
|
| 71 |
time.sleep(delay)
|
| 72 |
try:
|
| 73 |
if os.path.exists(path):
|
| 74 |
os.remove(path)
|
| 75 |
+
print(f"[CLEANUP] removed: {path}")
|
| 76 |
+
else:
|
| 77 |
+
print(f"[CLEANUP] already gone: {path}")
|
| 78 |
except Exception as e:
|
| 79 |
+
print(f"[CLEANUP] failed: {path} | {e}")
|
| 80 |
|
| 81 |
# set enhancer with RealESRGAN
|
| 82 |
def set_realesrgan():
|
|
|
|
| 179 |
# align and warp each face
|
| 180 |
face_helper.align_warp_face()
|
| 181 |
|
| 182 |
+
if min(img.shape[:2]) > 1000 and num_det_faces > 15:
|
| 183 |
+
raise gr.Error(
|
| 184 |
+
"Too many faces detected (>15) in a high-resolution image. "
|
| 185 |
+
"To keep the demo responsive, this case is skipped. "
|
| 186 |
+
"For such inputs, please deploy the demo locally without this limit."
|
| 187 |
+
)
|
| 188 |
+
|
| 189 |
# face restoration for each cropped face
|
| 190 |
for idx, cropped_face in enumerate(face_helper.cropped_faces):
|
| 191 |
# prepare data
|
|
|
|
| 242 |
#save restored img
|
| 243 |
save_path = f"output/{uuid.uuid4().hex}.png"
|
| 244 |
imwrite(restored_img, save_path)
|
| 245 |
+
print(f"[SAVE] {save_path} | OUTPUT Number: {len(glob.glob("output/*.png"))}")
|
| 246 |
|
| 247 |
threading.Thread(
|
| 248 |
target=delayed_remove,
|
| 249 |
+
args=(save_path,30),
|
| 250 |
daemon=True
|
| 251 |
).start()
|
| 252 |
+
|
| 253 |
return save_path
|
| 254 |
|
| 255 |
except Exception as error:
|
|
|
|
| 325 |
gr.Number(value=2, label="Rescaling_Factor (up to 4)"),
|
| 326 |
gr.Slider(0, 1, value=0.5, step=0.01, label='Codeformer_Fidelity (0 for better quality, 1 for better identity)')
|
| 327 |
], [
|
| 328 |
+
gr.Image(type="filepath", label="Output"),
|
| 329 |
+
gr.Markdown("Please download the output within 30 seconds.")
|
| 330 |
],
|
| 331 |
title=title,
|
| 332 |
description=description,
|