sczhou commited on
Commit
3f969c1
·
verified ·
1 Parent(s): 9a8ec17

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -19
app.py CHANGED
@@ -7,6 +7,7 @@ import sys
7
  sys.path.append('CodeFormer')
8
  import os
9
  import cv2
 
10
  import torch
11
  import torch.nn.functional as F
12
  import uuid, threading, time, glob
@@ -62,10 +63,11 @@ torch.hub.download_url_to_file(
62
  'https://raw.githubusercontent.com/sczhou/CodeFormer/master/inputs/cropped_faces/0729.png',
63
  '06.png')
64
 
65
- def imread(img_path):
66
- img = cv2.imread(img_path)
67
- img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
68
- return img
 
69
 
70
  def delayed_remove(path, delay=60):
71
  time.sleep(delay)
@@ -124,7 +126,6 @@ def inference(image, face_align, background_enhance, face_upsample, upscale, cod
124
  draw_box = False
125
  detection_model = "retinaface_resnet50"
126
 
127
- print('Inp:', image.shape, background_enhance, face_upsample, upscale, codeformer_fidelity)
128
  face_align = face_align if face_align is not None else True
129
  background_enhance = background_enhance if background_enhance is not None else True
130
  face_upsample = face_upsample if face_upsample is not None else True
@@ -132,13 +133,26 @@ def inference(image, face_align, background_enhance, face_upsample, upscale, cod
132
 
133
  has_aligned = not face_align
134
  upscale = 1 if has_aligned else upscale
 
 
 
 
 
 
 
 
 
 
 
135
 
136
- img = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
137
 
 
 
138
  if img is None:
139
- raise ValueError("Input image is None")
140
-
141
- # print('\timage size:', img.shape)
142
 
143
  upscale = int(upscale) # convert type to int
144
  if upscale > 4: # avoid memory exceeded due to too large upscale
@@ -184,12 +198,12 @@ def inference(image, face_align, background_enhance, face_upsample, upscale, cod
184
  # align and warp each face
185
  face_helper.align_warp_face()
186
 
187
- if min(img.shape[:2]) > 1000 and num_det_faces > 15:
188
- raise gr.Error(
189
- "Too many faces detected (>15) in a high-resolution image. "
190
- "To keep the demo responsive and avoid long queue times, this case is skipped. "
191
- "For such inputs, please deploy this demo locally and remove this limit."
192
- )
193
 
194
 
195
  # face restoration for each cropped face
@@ -257,11 +271,14 @@ def inference(image, face_align, background_enhance, face_upsample, upscale, cod
257
  ).start()
258
 
259
  return save_path, None
 
 
 
260
 
261
  except Exception as error:
262
- print('Global exception', error)
263
- return None, None
264
-
265
 
266
  title = "CodeFormer: Robust Face Restoration and Enhancement Network"
267
 
@@ -324,7 +341,7 @@ td {
324
 
325
  demo = gr.Interface(
326
  inference, [
327
- gr.Image(type="numpy", label="Input"),
328
  gr.Checkbox(value=True, label="Pre_Face_Align"),
329
  gr.Checkbox(value=True, label="Background_Enhance"),
330
  gr.Checkbox(value=True, label="Face_Upsample"),
 
7
  sys.path.append('CodeFormer')
8
  import os
9
  import cv2
10
+ import numpy as np
11
  import torch
12
  import torch.nn.functional as F
13
  import uuid, threading, time, glob
 
63
  'https://raw.githubusercontent.com/sczhou/CodeFormer/master/inputs/cropped_faces/0729.png',
64
  '06.png')
65
 
66
+
67
+ def imread_unicode_safe(path):
68
+ with open(path, "rb") as f:
69
+ data = np.frombuffer(f.read(), dtype=np.uint8)
70
+ return cv2.imdecode(data, cv2.IMREAD_COLOR)
71
 
72
  def delayed_remove(path, delay=60):
73
  time.sleep(delay)
 
126
  draw_box = False
127
  detection_model = "retinaface_resnet50"
128
 
 
129
  face_align = face_align if face_align is not None else True
130
  background_enhance = background_enhance if background_enhance is not None else True
131
  face_upsample = face_upsample if face_upsample is not None else True
 
133
 
134
  has_aligned = not face_align
135
  upscale = 1 if has_aligned else upscale
136
+
137
+ if isinstance(image, dict):
138
+ image_path = image.get("name")
139
+ elif isinstance(image, str):
140
+ image_path = image
141
+ else:
142
+ image_path = None
143
+ raise gr.Error("Invalid input image.")
144
+
145
+ if not os.path.exists(image_path):
146
+ raise gr.Error("Invalid input image.")
147
 
148
+ print('Inp:', image_path, background_enhance, face_upsample, upscale, codeformer_fidelity)
149
 
150
+ img = imread_unicode_safe(image_path)
151
+
152
  if img is None:
153
+ raise gr.Error("Failed to read input image.")
154
+
155
+ print('\timage size:', img.shape)
156
 
157
  upscale = int(upscale) # convert type to int
158
  if upscale > 4: # avoid memory exceeded due to too large upscale
 
198
  # align and warp each face
199
  face_helper.align_warp_face()
200
 
201
+ if min(img.shape[:2]) > 1000 and num_det_faces > 15:
202
+ raise gr.Error(
203
+ "Too many faces detected (>15) in a high-resolution image. "
204
+ "To keep the demo responsive and avoid long queue times, this case is skipped. "
205
+ "For such inputs, please deploy this demo locally and remove this limit."
206
+ )
207
 
208
 
209
  # face restoration for each cropped face
 
271
  ).start()
272
 
273
  return save_path, None
274
+
275
+ except gr.Error:
276
+ raise
277
 
278
  except Exception as error:
279
+ print('[UNEXPECTED ERROR]', error)
280
+ raise gr.Error("Unexpected error. Please try another image.")
281
+
282
 
283
  title = "CodeFormer: Robust Face Restoration and Enhancement Network"
284
 
 
341
 
342
  demo = gr.Interface(
343
  inference, [
344
+ gr.Image(type="filepath", label="Input"),
345
  gr.Checkbox(value=True, label="Pre_Face_Align"),
346
  gr.Checkbox(value=True, label="Background_Enhance"),
347
  gr.Checkbox(value=True, label="Face_Upsample"),