pryanshusharma commited on
Commit
1f18152
β€’
1 Parent(s): 812ff97

Upload 4 files

Browse files
Files changed (4) hide show
  1. README.md +5 -6
  2. app.py +123 -0
  3. packages.txt +3 -0
  4. requirements.txt +13 -0
README.md CHANGED
@@ -1,12 +1,11 @@
1
  ---
2
- title: Remaster1
3
- emoji: πŸŒ–
4
- colorFrom: blue
5
  colorTo: green
6
  sdk: gradio
7
- sdk_version: 4.7.1
 
8
  app_file: app.py
9
  pinned: false
10
  ---
11
-
12
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
1
  ---
2
+ title: REMASTER
3
+ emoji: 😌
4
+ colorFrom: yellow
5
  colorTo: green
6
  sdk: gradio
7
+ sdk_version: 3.43.2
8
+ python_version: 3.8.18
9
  app_file: app.py
10
  pinned: false
11
  ---
 
 
app.py ADDED
@@ -0,0 +1,123 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+
10
+ os.system("pip freeze")
11
+ # download weights
12
+ if not os.path.exists('realesr-general-x4v3.pth'):
13
+ os.system("wget https://github.com/xinntao/Real-ESRGAN/releases/download/v0.2.5.0/realesr-general-x4v3.pth -P .")
14
+ if not os.path.exists('GFPGANv1.2.pth'):
15
+ os.system("wget https://github.com/TencentARC/GFPGAN/releases/download/v1.3.0/GFPGANv1.2.pth -P .")
16
+ if not os.path.exists('GFPGANv1.3.pth'):
17
+ os.system("wget https://github.com/TencentARC/GFPGAN/releases/download/v1.3.0/GFPGANv1.3.pth -P .")
18
+ if not os.path.exists('GFPGANv1.4.pth'):
19
+ os.system("wget https://github.com/TencentARC/GFPGAN/releases/download/v1.3.0/GFPGANv1.4.pth -P .")
20
+ if not os.path.exists('RestoreFormer.pth'):
21
+ os.system("wget https://github.com/TencentARC/GFPGAN/releases/download/v1.3.4/RestoreFormer.pth -P .")
22
+ if not os.path.exists('CodeFormer.pth'):
23
+ os.system("wget https://github.com/TencentARC/GFPGAN/releases/download/v1.3.4/CodeFormer.pth -P .")
24
+
25
+ torch.hub.download_url_to_file(
26
+ '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',
27
+ 'lincoln.jpg')
28
+ torch.hub.download_url_to_file(
29
+ 'https://user-images.githubusercontent.com/17445847/187400315-87a90ac9-d231-45d6-b377-38702bd1838f.jpg',
30
+ 'AI-generate.jpg')
31
+ torch.hub.download_url_to_file(
32
+ 'https://user-images.githubusercontent.com/17445847/187400981-8a58f7a4-ef61-42d9-af80-bc6234cef860.jpg',
33
+ 'Blake_Lively.jpg')
34
+ torch.hub.download_url_to_file(
35
+ 'https://user-images.githubusercontent.com/17445847/187401133-8a3bf269-5b4d-4432-b2f0-6d26ee1d3307.png',
36
+ '10045.png')
37
+
38
+ # background enhancer with RealESRGAN
39
+ model = SRVGGNetCompact(num_in_ch=3, num_out_ch=3, num_feat=64, num_conv=32, upscale=4, act_type='prelu')
40
+ model_path = 'realesr-general-x4v3.pth'
41
+ half = True if torch.cuda.is_available() else False
42
+ upsampler = RealESRGANer(scale=4, model_path=model_path, model=model, tile=0, tile_pad=10, pre_pad=0, half=half)
43
+
44
+ os.makedirs('output', exist_ok=True)
45
+
46
+ def inference(img, version, scale, weight):
47
+ weight /= 100
48
+ print(img, version, scale, weight)
49
+ try:
50
+ extension = os.path.splitext(os.path.basename(str(img)))[1]
51
+ img = cv2.imread(img, cv2.IMREAD_UNCHANGED)
52
+ if len(img.shape) == 3 and img.shape[2] == 4:
53
+ img_mode = 'RGBA'
54
+ elif len(img.shape) == 2: # for gray inputs
55
+ img_mode = None
56
+ img = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)
57
+ else:
58
+ img_mode = None
59
+
60
+ if version == 'v1.2':
61
+ face_enhancer = GFPGANer(
62
+ model_path='GFPGANv1.2.pth', upscale=2, arch='clean', channel_multiplier=2, bg_upsampler=upsampler)
63
+ elif version == 'v1.3':
64
+ face_enhancer = GFPGANer(
65
+ model_path='GFPGANv1.3.pth', upscale=2, arch='clean', channel_multiplier=2, bg_upsampler=upsampler)
66
+ elif version == 'v1.4':
67
+ face_enhancer = GFPGANer(
68
+ model_path='GFPGANv1.4.pth', upscale=2, arch='clean', channel_multiplier=2, bg_upsampler=upsampler)
69
+ elif version == 'RestoreFormer':
70
+ face_enhancer = GFPGANer(
71
+ model_path='RestoreFormer.pth', upscale=2, arch='RestoreFormer', channel_multiplier=2, bg_upsampler=upsampler)
72
+ elif version == 'CodeFormer':
73
+ face_enhancer = GFPGANer(
74
+ model_path='CodeFormer.pth', upscale=2, arch='CodeFormer', channel_multiplier=2, bg_upsampler=upsampler)
75
+
76
+ try:
77
+ _, _, output = face_enhancer.enhance(img, has_aligned=False, only_center_face=False, paste_back=True, weight=weight)
78
+ except RuntimeError as error:
79
+ print('Error', error)
80
+
81
+ try:
82
+ interpolation = cv2.INTER_AREA if scale < 2 else cv2.INTER_LANCZOS4
83
+ h, w = img.shape[0:2]
84
+ output = cv2.resize(output, (int(w * scale), int(h * scale)), interpolation=interpolation)
85
+ except Exception as error:
86
+ print('wrong scale input.', error)
87
+ if img_mode == 'RGBA': # RGBA images should be saved in png format
88
+ extension = 'png'
89
+ else:
90
+ extension = 'jpg'
91
+ save_path = f'output/out.{extension}'
92
+ cv2.imwrite(save_path, output)
93
+
94
+ output = cv2.cvtColor(output, cv2.COLOR_BGR2RGB)
95
+ return output, save_path
96
+ except Exception as error:
97
+ print('global exception', error)
98
+ return None, None
99
+
100
+
101
+ title = "GFPGAN: Practical Face Restoration Algorithm"
102
+ description = r"""Gradio demo for remasting the face
103
+ """
104
+ article = r"""
105
+
106
+ """
107
+ demo = gr.Interface(
108
+ inference, [
109
+ gr.inputs.Image(type="filepath", label="Input"),
110
+ gr.inputs.Radio(['v1.2', 'v1.3', 'v1.4', 'RestoreFormer', 'CodeFormer'], type="value", default='v1.4', label='version'),
111
+ gr.inputs.Number(label="Rescaling factor", default=2),
112
+ gr.Slider(0, 100, label='Weight, only for CodeFormer. 0 for better quality, 100 for better identity', default=50)
113
+ ], [
114
+ gr.outputs.Image(type="numpy", label="Output (The whole image)"),
115
+ gr.outputs.File(label="Download the output image")
116
+ ],
117
+ title=title,
118
+ description=description,
119
+ article=article,
120
+ examples=[['AI-generate.jpg', 'v1.4', 2, 50], ['lincoln.jpg', 'v1.4', 2, 50], ['Blake_Lively.jpg', 'v1.4', 2, 50],
121
+ ['10045.png', 'v1.4', 2, 50]])
122
+ demo.queue()
123
+ demo.launch()
packages.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ ffmpeg
2
+ libsm6
3
+ libxext6
requirements.txt ADDED
@@ -0,0 +1,13 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ torch>=1.7
2
+ basicsr>=1.4.2
3
+ facexlib>=0.2.5
4
+ gfpgan==1.3.7
5
+ realesrgan>=0.2.5
6
+ numpy
7
+ opencv-python
8
+ torchvision
9
+ scipy
10
+ tqdm
11
+ lmdb
12
+ pyyaml
13
+ yapf