Jonny001 commited on
Commit
8dc034d
·
verified ·
1 Parent(s): 79d7173

Upload 2 files

Browse files
Files changed (2) hide show
  1. app.py +134 -0
  2. requirements.txt +13 -0
app.py ADDED
@@ -0,0 +1,134 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 gfpgan.utils import GFPGANer
8
+ from realesrgan.utils import RealESRGANer
9
+ import spaces
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('GFPGANv1.2.pth'):
16
+ os.system("wget https://github.com/TencentARC/GFPGAN/releases/download/v1.3.0/GFPGANv1.2.pth -P .")
17
+ if not os.path.exists('GFPGANv1.3.pth'):
18
+ os.system("wget https://github.com/TencentARC/GFPGAN/releases/download/v1.3.0/GFPGANv1.3.pth -P .")
19
+ if not os.path.exists('GFPGANv1.4.pth'):
20
+ os.system("wget https://github.com/TencentARC/GFPGAN/releases/download/v1.3.0/GFPGANv1.4.pth -P .")
21
+ if not os.path.exists('RestoreFormer.pth'):
22
+ os.system("wget https://github.com/TencentARC/GFPGAN/releases/download/v1.3.4/RestoreFormer.pth -P .")
23
+ if not os.path.exists('CodeFormer.pth'):
24
+ os.system("wget https://github.com/TencentARC/GFPGAN/releases/download/v1.3.4/CodeFormer.pth -P .")
25
+
26
+ torch.hub.download_url_to_file(
27
+ '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',
28
+ 'lincoln.jpg')
29
+ torch.hub.download_url_to_file(
30
+ 'https://user-images.githubusercontent.com/17445847/187400315-87a90ac9-d231-45d6-b377-38702bd1838f.jpg',
31
+ 'AI-generate.jpg')
32
+ torch.hub.download_url_to_file(
33
+ 'https://user-images.githubusercontent.com/17445847/187400981-8a58f7a4-ef61-42d9-af80-bc6234cef860.jpg',
34
+ 'Blake_Lively.jpg')
35
+ torch.hub.download_url_to_file(
36
+ 'https://user-images.githubusercontent.com/17445847/187401133-8a3bf269-5b4d-4432-b2f0-6d26ee1d3307.png',
37
+ '10045.png')
38
+
39
+ # background enhancer with RealESRGAN
40
+ model = SRVGGNetCompact(num_in_ch=3, num_out_ch=3, num_feat=64, num_conv=32, upscale=4, act_type='prelu')
41
+ model_path = 'realesr-general-x4v3.pth'
42
+ half = True if torch.cuda.is_available() else False
43
+ upsampler = RealESRGANer(scale=4, model_path=model_path, model=model, tile=0, tile_pad=10, pre_pad=0, half=half)
44
+
45
+ os.makedirs('output', exist_ok=True)
46
+
47
+
48
+ # def inference(img, version, scale, weight):
49
+ @spaces.GPU(enable_queue=True)
50
+ def inference(img, version, scale):
51
+ # weight /= 100
52
+ print(img, version, scale)
53
+ if scale > 4:
54
+ scale = 4 # avoid too large scale value
55
+ try:
56
+ extension = os.path.splitext(os.path.basename(str(img)))[1]
57
+ img = cv2.imread(img, cv2.IMREAD_UNCHANGED)
58
+ if len(img.shape) == 3 and img.shape[2] == 4:
59
+ img_mode = 'RGBA'
60
+ elif len(img.shape) == 2: # for gray inputs
61
+ img_mode = None
62
+ img = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)
63
+ else:
64
+ img_mode = None
65
+
66
+ h, w = img.shape[0:2]
67
+ if h > 3500 or w > 3500:
68
+ print('too large size')
69
+ return None, None
70
+
71
+ if h < 300:
72
+ img = cv2.resize(img, (w * 2, h * 2), interpolation=cv2.INTER_LANCZOS4)
73
+
74
+ if version == 'v1.2':
75
+ face_enhancer = GFPGANer(
76
+ model_path='GFPGANv1.2.pth', upscale=2, arch='clean', channel_multiplier=2, bg_upsampler=upsampler)
77
+ elif version == 'v1.3':
78
+ face_enhancer = GFPGANer(
79
+ model_path='GFPGANv1.3.pth', upscale=2, arch='clean', channel_multiplier=2, bg_upsampler=upsampler)
80
+ elif version == 'v1.4':
81
+ face_enhancer = GFPGANer(
82
+ model_path='GFPGANv1.4.pth', upscale=2, arch='clean', channel_multiplier=2, bg_upsampler=upsampler)
83
+ elif version == 'RestoreFormer':
84
+ face_enhancer = GFPGANer(
85
+ model_path='RestoreFormer.pth', upscale=2, arch='RestoreFormer', channel_multiplier=2, bg_upsampler=upsampler)
86
+ # elif version == 'CodeFormer':
87
+ # face_enhancer = GFPGANer(
88
+ # model_path='CodeFormer.pth', upscale=2, arch='CodeFormer', channel_multiplier=2, bg_upsampler=upsampler)
89
+
90
+ try:
91
+ # _, _, output = face_enhancer.enhance(img, has_aligned=False, only_center_face=False, paste_back=True, weight=weight)
92
+ _, _, output = face_enhancer.enhance(img, has_aligned=False, only_center_face=False, paste_back=True)
93
+ except RuntimeError as error:
94
+ print('Error', error)
95
+
96
+ try:
97
+ if scale != 2:
98
+ interpolation = cv2.INTER_AREA if scale < 2 else cv2.INTER_LANCZOS4
99
+ h, w = img.shape[0:2]
100
+ output = cv2.resize(output, (int(w * scale / 2), int(h * scale / 2)), interpolation=interpolation)
101
+ except Exception as error:
102
+ print('wrong scale input.', error)
103
+ if img_mode == 'RGBA': # RGBA images should be saved in png format
104
+ extension = 'png'
105
+ else:
106
+ extension = 'jpg'
107
+ save_path = f'output/out.{extension}'
108
+ cv2.imwrite(save_path, output)
109
+
110
+ output = cv2.cvtColor(output, cv2.COLOR_BGR2RGB)
111
+ return output, save_path
112
+ except Exception as error:
113
+ print('global exception', error)
114
+ return None, None
115
+
116
+
117
+ description = "⚠ Sorry for the inconvenience. The Space is currently running on the CPU, which might affect performance. We appreciate your understanding."
118
+
119
+ demo = gr.Interface(
120
+ inference, [
121
+ gr.Image(type="filepath", label="Input"),
122
+ # gr.Radio(['v1.2', 'v1.3', 'v1.4', 'RestoreFormer', 'CodeFormer'], type="value", value='v1.4', label='version'),
123
+ gr.Radio(['v1.2', 'v1.3', 'v1.4', 'RestoreFormer'], type="value", value='v1.4', label='version'),
124
+ gr.Number(label="Rescaling factor", value=2),
125
+ # gr.Slider(0, 100, label='Weight, only for CodeFormer. 0 for better quality, 100 for better identity', value=50)
126
+ ], [
127
+ gr.Image(type="numpy", label="Output (The whole image)"),
128
+ gr.File(label="Download the output image")
129
+ ],
130
+ description=description,
131
+ # examples=[['AI-generate.jpg', 'v1.4', 2, 50], ['lincoln.jpg', 'v1.4', 2, 50], ['Blake_Lively.jpg', 'v1.4', 2, 50],
132
+ # ['10045.png', 'v1.4', 2, 50]]).launch()
133
+
134
+ demo.queue(max_size=50).launch()
requirements.txt ADDED
@@ -0,0 +1,13 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ torch==2.0.0
2
+ basicsr>=1.4.2
3
+ facexlib>=0.2.5
4
+ gfpgan>=1.3.7
5
+ realesrgan>=0.2.5
6
+ numpy<2
7
+ opencv-python
8
+ torchvision
9
+ scipy
10
+ tqdm
11
+ lmdb
12
+ pyyaml
13
+ yapf