Spaces:
Sleeping
Sleeping
Zongsheng
commited on
Commit
Β·
692826c
1
Parent(s):
ea3f9a7
add alinged case
Browse files
app.py
CHANGED
@@ -62,79 +62,92 @@ def predict(im_path, background_enhance, face_upsample, upscale, started_timeste
|
|
62 |
upscale = 2 # avoid momory exceeded due to too large img resolution
|
63 |
configs.detection.upscale = int(upscale)
|
64 |
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
71 |
|
72 |
restored_image_dir = Path('restored_output')
|
73 |
if not restored_image_dir.exists():
|
74 |
restored_image_dir.mkdir()
|
75 |
# save the whole image
|
76 |
save_path = restored_image_dir / Path(im_path).name
|
77 |
-
image_restored = util_image.bgr2rgb(image_restored)
|
78 |
util_image.imwrite(image_restored, save_path, chn='rgb', dtype_in='uint8')
|
79 |
|
80 |
return image_restored, str(save_path)
|
81 |
|
82 |
-
title = "DifFace: Blind Face Restoration with Diffused Error Contraction"
|
83 |
-
description = r"""
|
84 |
-
<b>Official Gradio demo</b> for <a href='https://github.com/zsyOAOA/DifFace' target='_blank'><b>DifFace: Blind Face Restoration with Diffused Error Contraction</b></a>.<br>
|
85 |
-
π₯ DifFace is a robust face restoration algorithm for old or corrupted photos.<br>
|
86 |
-
"""
|
87 |
-
article = r"""
|
88 |
-
If DifFace is helpful for your work, please help to β the <a href='https://github.com/zsyOAOA/DifFace' target='_blank'>Github Repo</a>. Thanks!
|
89 |
-
[![GitHub Stars](https://img.shields.io/github/stars/zsyOAOA/DifFace?affiliations=OWNER&color=green&style=social)](https://github.com/zsyOAOA/DifFace)
|
90 |
-
|
91 |
-
---
|
92 |
-
|
93 |
-
π **Citation**
|
94 |
-
|
95 |
-
If our work is useful for your research, please consider citing:
|
96 |
-
```bibtex
|
97 |
-
@article{yue2022difface,
|
98 |
-
title={DifFace: Blind Face Restoration with Diffused Error Contraction},
|
99 |
-
author={Yue, Zongsheng and Loy, Chen Change},
|
100 |
-
journal={arXiv preprint arXiv:2212.06512},
|
101 |
-
year={2022}
|
102 |
-
}
|
103 |
-
```
|
104 |
-
|
105 |
-
π **License**
|
106 |
-
|
107 |
-
This project is licensed under <a rel="license" href="https://github.com/zsyOAOA/DifFace/blob/master/LICENSE">S-Lab License 1.0</a>.
|
108 |
-
Redistribution and use for non-commercial purposes should follow this license.
|
109 |
-
|
110 |
-
π§ **Contact**
|
111 |
-
If you have any questions, please feel free to contact me via <b>[email protected]</b>.
|
112 |
-
![visitors](https://visitor-badge.laobi.icu/badge?page_id=zsyOAOA/DifFace)
|
113 |
-
"""
|
114 |
-
|
115 |
-
demo = gr.Interface(
|
116 |
-
predict,
|
117 |
-
inputs=[
|
118 |
-
gr.Image(type="filepath", label="Input"),
|
119 |
-
gr.Checkbox(value=True, label="Background_Enhance"),
|
120 |
-
gr.Checkbox(value=True, label="Face_Upsample"),
|
121 |
-
gr.Number(value=2, label="Rescaling_Factor (up to 4)"),
|
122 |
-
gr.Slider(1, 160, value=80, step=10, label='Realism-Fidelity Trade-off
|
123 |
-
],
|
124 |
-
outputs=[
|
125 |
-
gr.Image(type="numpy", label="Output"),
|
126 |
-
gr.outputs.File(label="Download the output")
|
127 |
-
],
|
128 |
-
title=title,
|
129 |
-
description=description,
|
130 |
-
article=article,
|
131 |
-
examples=[
|
132 |
-
['./testdata/whole_imgs/00.jpg', True, True, 2, 80],
|
133 |
-
['./testdata/whole_imgs/01.jpg', True, True, 2, 80],
|
134 |
-
['./testdata/whole_imgs/04.jpg', True, True, 2, 80],
|
135 |
-
['./testdata/whole_imgs/05.jpg', True, True, 2, 80],
|
136 |
-
]
|
137 |
-
)
|
138 |
-
|
139 |
-
demo.queue(concurrency_count=4)
|
140 |
-
demo.launch()
|
|
|
62 |
upscale = 2 # avoid momory exceeded due to too large img resolution
|
63 |
configs.detection.upscale = int(upscale)
|
64 |
|
65 |
+
if background_enhance:
|
66 |
+
image_restored, face_restored, face_cropped = sampler_dist.sample_func_bfr_unaligned(
|
67 |
+
y0=im_lq,
|
68 |
+
start_timesteps=started_timesteps,
|
69 |
+
need_restoration=True,
|
70 |
+
draw_box=False,
|
71 |
+
) # h x w x c, numpy array, [0, 255], uint8, BGR
|
72 |
+
image_restored = util_image.bgr2rgb(image_restored)
|
73 |
+
else:
|
74 |
+
image_restored = sampler_dist.sample_func_ir_aligned(
|
75 |
+
y0=im_lq,
|
76 |
+
start_timesteps=started_timesteps,
|
77 |
+
need_restoration=True,
|
78 |
+
)[0] # b x c x h x w, [0, 1], torch tensor, RGB
|
79 |
+
image_restored = util_image.tensor2img(
|
80 |
+
image_restored.cpu(),
|
81 |
+
rgb2bgr=False,
|
82 |
+
out_type=np.uint8,
|
83 |
+
min_max=(0, 1),
|
84 |
+
) # h x w x c, [0, 255], uint8, RGB, numpy array
|
85 |
|
86 |
restored_image_dir = Path('restored_output')
|
87 |
if not restored_image_dir.exists():
|
88 |
restored_image_dir.mkdir()
|
89 |
# save the whole image
|
90 |
save_path = restored_image_dir / Path(im_path).name
|
|
|
91 |
util_image.imwrite(image_restored, save_path, chn='rgb', dtype_in='uint8')
|
92 |
|
93 |
return image_restored, str(save_path)
|
94 |
|
95 |
+
# title = "DifFace: Blind Face Restoration with Diffused Error Contraction"
|
96 |
+
# description = r"""
|
97 |
+
# <b>Official Gradio demo</b> for <a href='https://github.com/zsyOAOA/DifFace' target='_blank'><b>DifFace: Blind Face Restoration with Diffused Error Contraction</b></a>.<br>
|
98 |
+
# π₯ DifFace is a robust face restoration algorithm for old or corrupted photos.<br>
|
99 |
+
# """
|
100 |
+
# article = r"""
|
101 |
+
# If DifFace is helpful for your work, please help to β the <a href='https://github.com/zsyOAOA/DifFace' target='_blank'>Github Repo</a>. Thanks!
|
102 |
+
# [![GitHub Stars](https://img.shields.io/github/stars/zsyOAOA/DifFace?affiliations=OWNER&color=green&style=social)](https://github.com/zsyOAOA/DifFace)
|
103 |
+
|
104 |
+
# ---
|
105 |
+
|
106 |
+
# π **Citation**
|
107 |
+
|
108 |
+
# If our work is useful for your research, please consider citing:
|
109 |
+
# ```bibtex
|
110 |
+
# @article{yue2022difface,
|
111 |
+
# title={DifFace: Blind Face Restoration with Diffused Error Contraction},
|
112 |
+
# author={Yue, Zongsheng and Loy, Chen Change},
|
113 |
+
# journal={arXiv preprint arXiv:2212.06512},
|
114 |
+
# year={2022}
|
115 |
+
# }
|
116 |
+
# ```
|
117 |
+
|
118 |
+
# π **License**
|
119 |
+
|
120 |
+
# This project is licensed under <a rel="license" href="https://github.com/zsyOAOA/DifFace/blob/master/LICENSE">S-Lab License 1.0</a>.
|
121 |
+
# Redistribution and use for non-commercial purposes should follow this license.
|
122 |
+
|
123 |
+
# π§ **Contact**
|
124 |
+
# If you have any questions, please feel free to contact me via <b>[email protected]</b>.
|
125 |
+
# ![visitors](https://visitor-badge.laobi.icu/badge?page_id=zsyOAOA/DifFace)
|
126 |
+
# """
|
127 |
+
|
128 |
+
# demo = gr.Interface(
|
129 |
+
# predict,
|
130 |
+
# inputs=[
|
131 |
+
# gr.Image(type="filepath", label="Input"),
|
132 |
+
# gr.Checkbox(value=True, label="Background_Enhance"),
|
133 |
+
# gr.Checkbox(value=True, label="Face_Upsample"),
|
134 |
+
# gr.Number(value=2, label="Rescaling_Factor (up to 4)"),
|
135 |
+
# gr.Slider(1, 160, value=80, step=10, label='Realism-Fidelity Trade-off')
|
136 |
+
# ],
|
137 |
+
# outputs=[
|
138 |
+
# gr.Image(type="numpy", label="Output"),
|
139 |
+
# gr.outputs.File(label="Download the output")
|
140 |
+
# ],
|
141 |
+
# title=title,
|
142 |
+
# description=description,
|
143 |
+
# article=article,
|
144 |
+
# examples=[
|
145 |
+
# ['./testdata/whole_imgs/00.jpg', True, True, 2, 80],
|
146 |
+
# ['./testdata/whole_imgs/01.jpg', True, True, 2, 80],
|
147 |
+
# ['./testdata/whole_imgs/04.jpg', True, True, 2, 80],
|
148 |
+
# ['./testdata/whole_imgs/05.jpg', True, True, 2, 80],
|
149 |
+
# ]
|
150 |
+
# )
|
151 |
+
|
152 |
+
# demo.queue(concurrency_count=4)
|
153 |
+
# demo.launch()
|