sczhou commited on
Commit
8db78ee
·
verified ·
1 Parent(s): cfcc9cd

change output from numpy to filepath

Browse files
Files changed (1) hide show
  1. app.py +31 -8
app.py CHANGED
@@ -9,6 +9,7 @@ import os
9
  import cv2
10
  import torch
11
  import torch.nn.functional as F
 
12
  import gradio as gr
13
 
14
  from torchvision.transforms.functional import normalize
@@ -66,6 +67,14 @@ def 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 = True if torch.cuda.is_available() else False
@@ -124,7 +133,7 @@ def inference(image, face_align, background_enhance, face_upsample, upscale, cod
124
  img = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
125
 
126
  if img is None:
127
- raise ValueError(f"Failed to read image from {image_path}")
128
 
129
  # print('\timage size:', img.shape)
130
 
@@ -213,14 +222,28 @@ def inference(image, face_align, background_enhance, face_upsample, upscale, cod
213
  restored_img = restored_face
214
 
215
  # save restored img
216
- save_path = f'output/out.png'
217
- imwrite(restored_img, str(save_path))
 
 
 
 
218
 
219
- restored_img = cv2.cvtColor(restored_img, cv2.COLOR_BGR2RGB)
220
- return restored_img
 
 
 
 
 
 
 
 
 
 
221
  except Exception as error:
222
  print('Global exception', error)
223
- return None, None
224
 
225
 
226
  title = "CodeFormer: Robust Face Restoration and Enhancement Network"
@@ -291,7 +314,7 @@ demo = gr.Interface(
291
  gr.Number(value=2, label="Rescaling_Factor (up to 4)"),
292
  gr.Slider(0, 1, value=0.5, step=0.01, label='Codeformer_Fidelity (0 for better quality, 1 for better identity)')
293
  ], [
294
- gr.Image(type="numpy", label="Output")
295
  ],
296
  title=title,
297
  description=description,
@@ -304,7 +327,7 @@ demo = gr.Interface(
304
  ['05.jpg', True, True, True, 2, 0.1],
305
  ['06.png', False, True, True, 1, 0.5]
306
  ],
307
- concurrency_limit=2
308
  )
309
 
310
  DEBUG = os.getenv('DEBUG') == '1'
 
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
  img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
68
  return img
69
 
70
+ def delayed_remove(path, delay=90):
71
+ time.sleep(delay)
72
+ try:
73
+ if os.path.exists(path):
74
+ os.remove(path)
75
+ except Exception as e:
76
+ print(f"[WARN] Failed to remove {path}: {e}")
77
+
78
  # set enhancer with RealESRGAN
79
  def set_realesrgan():
80
  half = True if torch.cuda.is_available() else False
 
133
  img = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
134
 
135
  if img is None:
136
+ raise ValueError("Input image is None")
137
 
138
  # print('\timage size:', img.shape)
139
 
 
222
  restored_img = restored_face
223
 
224
  # save restored img
225
+ # save_path = f'output/out.png'
226
+ # imwrite(restored_img, str(save_path))
227
+
228
+ # restored_img = cv2.cvtColor(restored_img, cv2.COLOR_BGR2RGB)
229
+ # return restored_img
230
+
231
 
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,10),
239
+ daemon=True
240
+ ).start()
241
+
242
+ return save_path
243
+
244
  except Exception as error:
245
  print('Global exception', error)
246
+ return None
247
 
248
 
249
  title = "CodeFormer: Robust Face Restoration and Enhancement Network"
 
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,
 
327
  ['05.jpg', True, True, True, 2, 0.1],
328
  ['06.png', False, True, True, 1, 0.5]
329
  ],
330
+ concurrency_limit=1
331
  )
332
 
333
  DEBUG = os.getenv('DEBUG') == '1'