wzhouxiff commited on
Commit
d4f6669
1 Parent(s): cf2b504

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +106 -38
app.py CHANGED
@@ -1,40 +1,37 @@
1
- import os
2
-
3
  import cv2
4
  import gradio as gr
5
  import torch
6
  from basicsr.archs.srvgg_arch import SRVGGNetCompact
7
  from realesrgan.utils import RealESRGANer
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8
 
9
- from RestoreFormer import RestoreFormer
10
-
11
- os.system("pip freeze")
12
- # download weights
13
- if not os.path.exists('realesr-general-x4v3.pth'):
14
- os.system("wget https://github.com/xinntao/Real-ESRGAN/releases/download/v0.2.5.0/realesr-general-x4v3.pth -P .")
15
- if not os.path.exists('RestoreFormer.ckpt'):
16
- os.system("wget https://github.com/wzhouxiff/RestoreFormerPlusPlus/releases/download/v1.0.0/RestoreFormer.ckpt -P .")
17
- if not os.path.exists('RestoreFormer++.ckpt'):
18
- os.system("wget https://github.com/wzhouxiff/RestoreFormerPlusPlus/releases/download/v1.0.0/RestoreFormer++.ckpt -P .")
19
-
20
- # torch.hub.download_url_to_file(
21
- # 'https://upload.wikimedia.org/wikipedia/commons/thumb/a/ab/Abraham_Lincoln_O-77_matte_collodion_print.jpg/1024px-Abraham_Lincoln_O-77_matte_collodion_print.jpg',
22
- # 'lincoln.jpg')
23
- # torch.hub.download_url_to_file(
24
- # 'https://user-images.githubusercontent.com/17445847/187400315-87a90ac9-d231-45d6-b377-38702bd1838f.jpg',
25
- # 'AI-generate.jpg')
26
- # torch.hub.download_url_to_file(
27
- # 'https://user-images.githubusercontent.com/17445847/187400981-8a58f7a4-ef61-42d9-af80-bc6234cef860.jpg',
28
- # 'Blake_Lively.jpg')
29
- # torch.hub.download_url_to_file(
30
- # 'https://user-images.githubusercontent.com/17445847/187401133-8a3bf269-5b4d-4432-b2f0-6d26ee1d3307.png',
31
- # '10045.png')
32
 
33
  # background enhancer with RealESRGAN
34
  model = SRVGGNetCompact(num_in_ch=3, num_out_ch=3, num_feat=64, num_conv=32, upscale=4, act_type='prelu')
35
- model_path = 'realesr-general-x4v3.pth'
36
  half = True if torch.cuda.is_available() else False
37
- upsampler = RealESRGANer(scale=4, model_path=model_path, model=model, tile=0, tile_pad=10, pre_pad=0, half=half)
38
 
39
  os.makedirs('output', exist_ok=True)
40
 
@@ -66,10 +63,10 @@ def inference(img, version, scale):
66
 
67
  if version == 'RestoreFormer':
68
  face_enhancer = RestoreFormer(
69
- model_path='RestoreFormer.ckpt', upscale=2, arch='RestoreFormer', bg_upsampler=upsampler)
70
  elif version == 'RestoreFormer++':
71
  face_enhancer = RestoreFormer(
72
- model_path='RestoreFormer++.ckpt', upscale=2, arch='RestoreFormer++', bg_upsampler=upsampler)
73
 
74
  try:
75
  # _, _, output = face_enhancer.enhance(img, has_aligned=False, only_center_face=False, paste_back=True, weight=weight)
@@ -98,16 +95,90 @@ def inference(img, version, scale):
98
  return None, None
99
 
100
 
101
- title = "RestoreFormer: Blind Face Restoration Algorithm"
102
- description = r"""Gradio demo for <a href='https://github.com/wzhouxiff/RestoreFormerPlusPlus' target='_blank'><b>RestoreFormer++: Towards Real-World Blind Face Restoration from Undegraded Key-Value Paris</b></a>.<br>
103
- It is used to restore your **old photos**.<br>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
104
  To use it, simply upload your image.<br>
105
  """
 
106
  article = r"""
107
- [![arXiv](https://img.shields.io/badge/arXiv-Paper-<COLOR>.svg)](https://arxiv.org/pdf/2308.07228.pdf)
108
- [![arXiv](https://img.shields.io/badge/arXiv-Paper-<COLOR>.svg)](https://openaccess.thecvf.com/content/CVPR2022/papers/Wang_RestoreFormer_High-Quality_Blind_Face_Restoration_From_Undegraded_Key-Value_Pairs_CVPR_2022_paper.pdf)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
109
  If you have any question, please email 📧 `[email protected]`.
110
  """
 
 
 
 
 
111
  demo = gr.Interface(
112
  inference, [
113
  gr.Image(type="filepath", label="Input"),
@@ -120,9 +191,6 @@ demo = gr.Interface(
120
  title=title,
121
  description=description,
122
  article=article,
123
- # examples=[['AI-generate.jpg', 'v1.4', 2, 50], ['lincoln.jpg', 'v1.4', 2, 50], ['Blake_Lively.jpg', 'v1.4', 2, 50],
124
- # ['10045.png', 'v1.4', 2, 50]]).launch()
125
- # examples=[['AI-generate.jpg', 'v1.4', 2], ['lincoln.jpg', 'v1.4', 2], ['Blake_Lively.jpg', 'v1.4', 2],
126
- # ['10045.png', 'v1.4', 2]]
127
  )
 
128
  demo.queue().launch()
 
1
+ import os, sys
2
+ import argparse
3
  import cv2
4
  import gradio as gr
5
  import torch
6
  from basicsr.archs.srvgg_arch import SRVGGNetCompact
7
  from realesrgan.utils import RealESRGANer
8
+ from glob import glob
9
+
10
+ sys.path.insert(1, os.path.join(sys.path[0], '..'))
11
+ from gradio_demo.RestoreFormer import RestoreFormer
12
+
13
+ if not os.path.exists('experiments/pretrained_models'):
14
+ os.makedirs('experiments/pretrained_models')
15
+ realesr_model_path = 'experiments/pretrained_models/RealESRGAN_x4plus.pth'
16
+ if not os.path.exists(realesr_model_path):
17
+ os.system("wget https://github.com/xinntao/Real-ESRGAN/releases/download/v0.2.5.0/realesr-general-x4v3.pth -O experiments/pretrained_models/RealESRGAN_x4plus.pth")
18
+
19
+ if not os.path.exists('experiments/RestoreFormer/'):
20
+ os.makedirs('experiments/RestoreFormer/')
21
+ restoreformer_model_path = 'experiments/RestoreFormer/last.ckpt'
22
+ if not os.path.exists(restoreformer_model_path):
23
+ os.system("wget https://github.com/wzhouxiff/RestoreFormerPlusPlus/releases/download/v1.0.0/RestoreFormer.ckpt -O experiments/RestoreFormer/last.ckpt")
24
 
25
+ if not os.path.exists('experiments/RestoreFormerPlusPlus/'):
26
+ os.makedirs('experiments/RestoreFormerPlusPlus/')
27
+ restoreformerplusplus_model_path = 'experiments/RestoreFormerPlusPlus/last.ckpt'
28
+ if not os.path.exists(restoreformerplusplus_model_path):
29
+ os.system("wget https://github.com/wzhouxiff/RestoreFormerPlusPlus/releases/download/v1.0.0/RestoreFormer++.ckpt -O experiments/RestoreFormerPlusPlus/last.ckpt")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
30
 
31
  # background enhancer with RealESRGAN
32
  model = SRVGGNetCompact(num_in_ch=3, num_out_ch=3, num_feat=64, num_conv=32, upscale=4, act_type='prelu')
 
33
  half = True if torch.cuda.is_available() else False
34
+ upsampler = RealESRGANer(scale=4, model_path=realesr_model_path, model=model, tile=0, tile_pad=10, pre_pad=0, half=half)
35
 
36
  os.makedirs('output', exist_ok=True)
37
 
 
63
 
64
  if version == 'RestoreFormer':
65
  face_enhancer = RestoreFormer(
66
+ model_path=restoreformer_model_path, upscale=2, arch='RestoreFormer', bg_upsampler=upsampler)
67
  elif version == 'RestoreFormer++':
68
  face_enhancer = RestoreFormer(
69
+ model_path=restoreformerplusplus_model_path, upscale=2, arch='RestoreFormer++', bg_upsampler=upsampler)
70
 
71
  try:
72
  # _, _, output = face_enhancer.enhance(img, has_aligned=False, only_center_face=False, paste_back=True, weight=weight)
 
95
  return None, None
96
 
97
 
98
+ title = "RestoreFormer++: Towards Real-World Blind Face Restoration from Undegraded Key-Value Paris"
99
+ important_links=r'''
100
+ <div align='center'>
101
+ [![paper_RestroeForemer++](https://img.shields.io/badge/TPAMI-Restorformer%2B%2B-green
102
+ )](https://openaccess.thecvf.com/content/CVPR2022/papers/Wang_RestoreFormer_High-Quality_Blind_Face_Restoration_From_Undegraded_Key-Value_Pairs_CVPR_2022_paper.pdf)
103
+ &nbsp;
104
+ [![paere_RestroeForemer](https://img.shields.io/badge/CVPR22-Restorformer-green)](https://openaccess.thecvf.com/content/CVPR2022/papers/Wang_RestoreFormer_High-Quality_Blind_Face_Restoration_From_Undegraded_Key-Value_Pairs_CVPR_2022_paper.pdf)
105
+ &nbsp;
106
+ [![code_RestroeForemer++](https://img.shields.io/badge/GitHub-RestoreFormer%2B%2B-red
107
+
108
+ )](https://github.com/wzhouxiff/RestoreFormerPlusPlus)
109
+ &nbsp;
110
+ [![code_RestroeForemer](https://img.shields.io/badge/GitHub-RestoreFormer-red)](https://github.com/wzhouxiff/RestoreFormer)
111
+ &nbsp;
112
+ [![demo](https://img.shields.io/badge/Demo-Gradio-orange
113
+ )](https://gradio.app/hub/wzhouxiff/RestoreFormerPlusPlus)
114
+ </div>
115
+ '''
116
+ description = r"""
117
+ <div align='center'>
118
+ <a target='_blank' href='https://arxiv.org/pdf/2308.07228.pdf' style='float: left'>
119
+ <img src='https://img.shields.io/badge/TPAMI-RestorFormer%2B%2B-green' alt='paper_RestroeForemer++'>
120
+ </a>
121
+ &ensp;&ensp;&ensp;&ensp;&ensp;&ensp;
122
+ <a target='_blank' href='https://openaccess.thecvf.com/content/CVPR2022/papers/Wang_RestoreFormer_High-Quality_Blind_Face_Restoration_From_Undegraded_Key-Value_Pairs_CVPR_2022_paper.pdf' style='float: left'>
123
+ <img src='https://img.shields.io/badge/CVPR22-RestorFormer-green' alt='paere_RestroeForemer' >
124
+ </a>
125
+ &ensp;&ensp;&ensp;&ensp;&ensp;&ensp;
126
+ <a target='_blank' href='https://github.com/wzhouxiff/RestoreFormerPlusPlus' style='float: left'>
127
+ <img src='https://img.shields.io/badge/GitHub-RestoreFormer%2B%2B-red' alt='code_RestroeForemer++'>
128
+ </a>
129
+ &ensp;&ensp;&ensp;&ensp;&ensp;&ensp;
130
+ <a target='_blank' href='https://github.com/wzhouxiff/RestoreFormer' style='float: left'>
131
+ <img src='https://img.shields.io/badge/GitHub-RestoreFormer-red' alt='code_RestroeForemer' >
132
+ </a>
133
+ &ensp;&ensp;&ensp;&ensp;&ensp;&ensp;
134
+ <a target='_blank' href='https://huggingface.co/spaces/wzhouxiff/RestoreFormerPlusPlus' style='float: left' >
135
+ <img src='https://img.shields.io/badge/Demo-Gradio-orange' alt='demo' >
136
+ </a>
137
+ &ensp;&ensp;&ensp;&ensp;&ensp;&ensp;
138
+ </div>
139
+ <br>
140
+ Gradio demo for <a href='https://github.com/wzhouxiff/RestoreFormerPlusPlus' target='_blank'><b>RestoreFormer++: Towards Real-World Blind Face Restoration from Undegraded Key-Value Paris</b></a>.
141
+ <br>
142
+ It is used to restore your Old Photos.
143
+ <br>
144
  To use it, simply upload your image.<br>
145
  """
146
+
147
  article = r"""
148
+ If the proposed algorithm is helpful, please help to ⭐ the GitHub Repositories: <a href='https://github.com/wzhouxiff/RestoreFormer' target='_blank'>RestoreFormer</a> and
149
+ <a href='https://github.com/wzhouxiff/RestoreFormerPlusPlus' target='_blank'>RestoreFormer++</a>. Thanks!
150
+ [![GitHub Stars](https://img.shields.io/github/stars/wzhouxiff%2FRestoreFormer
151
+ )](https://github.com/wzhouxiff/RestoreFormer)
152
+ [![GitHub Stars](https://img.shields.io/github/stars/wzhouxiff%2FRestoreFormerPlusPlus
153
+ )](https://github.com/wzhouxiff/RestoreFormerPlusPlus)
154
+
155
+ ---
156
+
157
+ 📝 **Citation**
158
+ <br>
159
+ If our work is useful for your research, please consider citing:
160
+ ```bibtex
161
+ @article{wang2023restoreformer++,
162
+ title={RestoreFormer++: Towards Real-World Blind Face Restoration from Undegraded Key-Value Paris},
163
+ author={Wang, Zhouxia and Zhang, Jiawei and Chen, Tianshui and Wang, Wenping and Luo, Ping},
164
+ booktitle={IEEE Transactions on Pattern Analysis and Machine Intelligence (T-PAMI)},
165
+ year={2023}
166
+ }
167
+ @article{wang2022restoreformer,
168
+ title={RestoreFormer: High-Quality Blind Face Restoration from Undegraded Key-Value Pairs},
169
+ author={Wang, Zhouxia and Zhang, Jiawei and Chen, Runjian and Wang, Wenping and Luo, Ping},
170
+ booktitle={The IEEE Conference on Computer Vision and Pattern Recognition (CVPR)},
171
+ year={2022}
172
+ }
173
+ ```
174
+
175
  If you have any question, please email 📧 `[email protected]`.
176
  """
177
+
178
+ css=r"""
179
+
180
+ """
181
+
182
  demo = gr.Interface(
183
  inference, [
184
  gr.Image(type="filepath", label="Input"),
 
191
  title=title,
192
  description=description,
193
  article=article,
 
 
 
 
194
  )
195
+
196
  demo.queue().launch()