Spaces:
Runtime error
Runtime error
update hugging face demo.
Browse files- app.py +29 -12
- basicsr/utils/realesrgan_utils.py +16 -13
app.py
CHANGED
@@ -109,8 +109,21 @@ def inference(image, background_enhance, face_upsample, upscale, codeformer_fide
|
|
109 |
only_center_face = False
|
110 |
draw_box = False
|
111 |
detection_model = "retinaface_resnet50"
|
|
|
|
|
|
|
|
|
112 |
|
113 |
upscale = int(upscale) # covert type to int
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
114 |
face_helper = FaceRestoreHelper(
|
115 |
upscale,
|
116 |
face_size=512,
|
@@ -123,14 +136,12 @@ def inference(image, background_enhance, face_upsample, upscale, codeformer_fide
|
|
123 |
bg_upsampler = upsampler if background_enhance else None
|
124 |
face_upsampler = upsampler if face_upsample else None
|
125 |
|
126 |
-
img = cv2.imread(str(image), cv2.IMREAD_COLOR)
|
127 |
-
|
128 |
if has_aligned:
|
129 |
# the input faces are already cropped and aligned
|
130 |
img = cv2.resize(img, (512, 512), interpolation=cv2.INTER_LINEAR)
|
131 |
face_helper.is_gray = is_gray(img, threshold=5)
|
132 |
if face_helper.is_gray:
|
133 |
-
print('
|
134 |
face_helper.cropped_faces = [img]
|
135 |
else:
|
136 |
face_helper.read_image(img)
|
@@ -138,7 +149,7 @@ def inference(image, background_enhance, face_upsample, upscale, codeformer_fide
|
|
138 |
num_det_faces = face_helper.get_face_landmarks_5(
|
139 |
only_center_face=only_center_face, resize=640, eye_dist_threshold=5
|
140 |
)
|
141 |
-
print(f
|
142 |
# align and warp each face
|
143 |
face_helper.align_warp_face()
|
144 |
|
@@ -159,8 +170,8 @@ def inference(image, background_enhance, face_upsample, upscale, codeformer_fide
|
|
159 |
restored_face = tensor2img(output, rgb2bgr=True, min_max=(-1, 1))
|
160 |
del output
|
161 |
torch.cuda.empty_cache()
|
162 |
-
except
|
163 |
-
print(f"
|
164 |
restored_face = tensor2img(
|
165 |
cropped_face_t, rgb2bgr=True, min_max=(-1, 1)
|
166 |
)
|
@@ -196,7 +207,7 @@ def inference(image, background_enhance, face_upsample, upscale, codeformer_fide
|
|
196 |
restored_img = cv2.cvtColor(restored_img, cv2.COLOR_BGR2RGB)
|
197 |
return restored_img, save_path
|
198 |
except Exception as error:
|
199 |
-
print('
|
200 |
return None, None
|
201 |
|
202 |
|
@@ -233,7 +244,13 @@ Redistribution and use for non-commercial purposes should follow this license.
|
|
233 |
|
234 |
If you have any questions, please feel free to reach me out at <b>[email protected]</b>.
|
235 |
|
236 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
237 |
"""
|
238 |
|
239 |
demo = gr.Interface(
|
@@ -241,8 +258,8 @@ demo = gr.Interface(
|
|
241 |
gr.inputs.Image(type="filepath", label="Input"),
|
242 |
gr.inputs.Checkbox(default=True, label="Background_Enhance"),
|
243 |
gr.inputs.Checkbox(default=True, label="Face_Upsample"),
|
244 |
-
gr.inputs.Number(default=2, label="Rescaling_Factor"),
|
245 |
-
gr.Slider(0, 1, value=0.5, step=0.01, label='Codeformer_Fidelity
|
246 |
], [
|
247 |
gr.outputs.Image(type="numpy", label="Output"),
|
248 |
gr.outputs.File(label="Download the output")
|
@@ -257,7 +274,7 @@ demo = gr.Interface(
|
|
257 |
['04.jpg', True, True, 2, 0.1],
|
258 |
['05.jpg', True, True, 2, 0.1]
|
259 |
]
|
260 |
-
)
|
261 |
|
262 |
-
demo.queue(concurrency_count=
|
263 |
demo.launch()
|
|
|
109 |
only_center_face = False
|
110 |
draw_box = False
|
111 |
detection_model = "retinaface_resnet50"
|
112 |
+
print('Inp:', image, background_enhance, face_upsample, upscale, codeformer_fidelity)
|
113 |
+
|
114 |
+
img = cv2.imread(str(image), cv2.IMREAD_COLOR)
|
115 |
+
print('\timage size:', img.shape)
|
116 |
|
117 |
upscale = int(upscale) # covert type to int
|
118 |
+
if upscale > 4: # avoid momory exceeded due to too large upscale
|
119 |
+
upscale = 4
|
120 |
+
if upscale > 2 and max(img.shape[:2])>1000: # avoid momory exceeded due to too large img resolution
|
121 |
+
upscale = 2
|
122 |
+
if max(img.shape[:2]) > 1500: # avoid momory exceeded due to too large img resolution
|
123 |
+
upscale = 1
|
124 |
+
background_enhance = False
|
125 |
+
face_upsample = False
|
126 |
+
|
127 |
face_helper = FaceRestoreHelper(
|
128 |
upscale,
|
129 |
face_size=512,
|
|
|
136 |
bg_upsampler = upsampler if background_enhance else None
|
137 |
face_upsampler = upsampler if face_upsample else None
|
138 |
|
|
|
|
|
139 |
if has_aligned:
|
140 |
# the input faces are already cropped and aligned
|
141 |
img = cv2.resize(img, (512, 512), interpolation=cv2.INTER_LINEAR)
|
142 |
face_helper.is_gray = is_gray(img, threshold=5)
|
143 |
if face_helper.is_gray:
|
144 |
+
print('\tgrayscale input: True')
|
145 |
face_helper.cropped_faces = [img]
|
146 |
else:
|
147 |
face_helper.read_image(img)
|
|
|
149 |
num_det_faces = face_helper.get_face_landmarks_5(
|
150 |
only_center_face=only_center_face, resize=640, eye_dist_threshold=5
|
151 |
)
|
152 |
+
print(f'\tdetect {num_det_faces} faces')
|
153 |
# align and warp each face
|
154 |
face_helper.align_warp_face()
|
155 |
|
|
|
170 |
restored_face = tensor2img(output, rgb2bgr=True, min_max=(-1, 1))
|
171 |
del output
|
172 |
torch.cuda.empty_cache()
|
173 |
+
except RuntimeError as error:
|
174 |
+
print(f"Failed inference for CodeFormer: {error}")
|
175 |
restored_face = tensor2img(
|
176 |
cropped_face_t, rgb2bgr=True, min_max=(-1, 1)
|
177 |
)
|
|
|
207 |
restored_img = cv2.cvtColor(restored_img, cv2.COLOR_BGR2RGB)
|
208 |
return restored_img, save_path
|
209 |
except Exception as error:
|
210 |
+
print('Global exception', error)
|
211 |
return None, None
|
212 |
|
213 |
|
|
|
244 |
|
245 |
If you have any questions, please feel free to reach me out at <b>[email protected]</b>.
|
246 |
|
247 |
+
<div>
|
248 |
+
🤗 Find Me:
|
249 |
+
<a href="https://twitter.com/ShangchenZhou"><img style="margin-top:0.5em; margin-bottom:0.5em" src="https://img.shields.io/twitter/follow/ShangchenZhou?label=%40ShangchenZhou&style=social" alt="Twitter Follow"></a>
|
250 |
+
<a href="https://github.com/sczhou"><img style="margin-top:0.5em; margin-bottom:2em" src="https://img.shields.io/github/followers/sczhou?style=social" alt="Github Follow"></a>
|
251 |
+
</div>
|
252 |
+
|
253 |
+
<center><img src='https://visitor-badge.laobi.icu/badge?page_id=sczhou/CodeFormer' alt='visitors'></center>
|
254 |
"""
|
255 |
|
256 |
demo = gr.Interface(
|
|
|
258 |
gr.inputs.Image(type="filepath", label="Input"),
|
259 |
gr.inputs.Checkbox(default=True, label="Background_Enhance"),
|
260 |
gr.inputs.Checkbox(default=True, label="Face_Upsample"),
|
261 |
+
gr.inputs.Number(default=2, label="Rescaling_Factor (up to 4)"),
|
262 |
+
gr.Slider(0, 1, value=0.5, step=0.01, label='Codeformer_Fidelity (0 for better quality, 1 for better identity)')
|
263 |
], [
|
264 |
gr.outputs.Image(type="numpy", label="Output"),
|
265 |
gr.outputs.File(label="Download the output")
|
|
|
274 |
['04.jpg', True, True, 2, 0.1],
|
275 |
['05.jpg', True, True, 2, 0.1]
|
276 |
]
|
277 |
+
)
|
278 |
|
279 |
+
demo.queue(concurrency_count=2)
|
280 |
demo.launch()
|
basicsr/utils/realesrgan_utils.py
CHANGED
@@ -196,19 +196,22 @@ class RealESRGANer():
|
|
196 |
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
|
197 |
|
198 |
# ------------------- process image (without the alpha channel) ------------------- #
|
199 |
-
|
200 |
-
|
201 |
-
|
202 |
-
self.
|
203 |
-
|
204 |
-
|
205 |
-
|
206 |
-
|
207 |
-
|
208 |
-
|
209 |
-
|
210 |
-
|
211 |
-
|
|
|
|
|
|
|
212 |
|
213 |
# ------------------- process the alpha channel if necessary ------------------- #
|
214 |
if img_mode == 'RGBA':
|
|
|
196 |
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
|
197 |
|
198 |
# ------------------- process image (without the alpha channel) ------------------- #
|
199 |
+
try:
|
200 |
+
with torch.no_grad():
|
201 |
+
self.pre_process(img)
|
202 |
+
if self.tile_size > 0:
|
203 |
+
self.tile_process()
|
204 |
+
else:
|
205 |
+
self.process()
|
206 |
+
output_img_t = self.post_process()
|
207 |
+
output_img = output_img_t.data.squeeze().float().cpu().clamp_(0, 1).numpy()
|
208 |
+
output_img = np.transpose(output_img[[2, 1, 0], :, :], (1, 2, 0))
|
209 |
+
if img_mode == 'L':
|
210 |
+
output_img = cv2.cvtColor(output_img, cv2.COLOR_BGR2GRAY)
|
211 |
+
del output_img_t
|
212 |
+
torch.cuda.empty_cache()
|
213 |
+
except RuntimeError as error:
|
214 |
+
print(f"Failed inference for RealESRGAN: {error}")
|
215 |
|
216 |
# ------------------- process the alpha channel if necessary ------------------- #
|
217 |
if img_mode == 'RGBA':
|