Update app.py
Browse files
app.py
CHANGED
@@ -1,189 +1,4 @@
|
|
1 |
import os
|
2 |
-
import cv2
|
3 |
-
import gradio as gr
|
4 |
-
import torch
|
5 |
-
from basicsr.archs.srvgg_arch import SRVGGNetCompact
|
6 |
-
from gfpgan.utils import GFPGANer
|
7 |
-
from realesrgan.utils import RealESRGANer
|
8 |
-
from zeroscratches import EraseScratches
|
9 |
-
|
10 |
-
os.system("pip freeze")
|
11 |
-
|
12 |
-
os.system("pip freeze")
|
13 |
-
# download weights
|
14 |
-
if not os.path.exists('realesr-general-x4v3.pth'):
|
15 |
-
os.system("wget https://github.com/xinntao/Real-ESRGAN/releases/download/v0.2.5.0/realesr-general-x4v3.pth -P .")
|
16 |
-
if not os.path.exists('GFPGANv1.2.pth'):
|
17 |
-
os.system("wget https://github.com/TencentARC/GFPGAN/releases/download/v1.3.0/GFPGANv1.2.pth -P .")
|
18 |
-
if not os.path.exists('GFPGANv1.3.pth'):
|
19 |
-
os.system("wget https://github.com/TencentARC/GFPGAN/releases/download/v1.3.0/GFPGANv1.3.pth -P .")
|
20 |
-
if not os.path.exists('GFPGANv1.4.pth'):
|
21 |
-
os.system("wget https://github.com/TencentARC/GFPGAN/releases/download/v1.3.0/GFPGANv1.4.pth -P .")
|
22 |
-
|
23 |
-
|
24 |
-
torch.hub.download_url_to_file(
|
25 |
-
'https://thumbs.dreamstime.com/b/tower-bridge-traditional-red-bus-black-white-colors-view-to-tower-bridge-london-black-white-colors-108478942.jpg',
|
26 |
-
'a1.jpg')
|
27 |
-
torch.hub.download_url_to_file(
|
28 |
-
'https://media.istockphoto.com/id/523514029/photo/london-skyline-b-w.jpg?s=612x612&w=0&k=20&c=kJS1BAtfqYeUDaORupj0sBPc1hpzJhBUUqEFfRnHzZ0=',
|
29 |
-
'a2.jpg')
|
30 |
-
torch.hub.download_url_to_file(
|
31 |
-
'https://i.guim.co.uk/img/media/06f614065ed82ca0e917b149a32493c791619854/0_0_3648_2789/master/3648.jpg?width=700&quality=85&auto=format&fit=max&s=05764b507c18a38590090d987c8b6202',
|
32 |
-
'a3.jpg')
|
33 |
-
torch.hub.download_url_to_file(
|
34 |
-
'https://i.pinimg.com/736x/46/96/9e/46969eb94aec2437323464804d27706d--victorian-london-victorian-era.jpg',
|
35 |
-
'a4.jpg')
|
36 |
-
|
37 |
-
# background enhancer with RealESRGAN
|
38 |
-
model = SRVGGNetCompact(num_in_ch=3, num_out_ch=3, num_feat=64, num_conv=32, upscale=4, act_type='prelu')
|
39 |
-
model_path = 'realesr-general-x4v3.pth'
|
40 |
-
half = True if torch.cuda.is_available() else False
|
41 |
-
upsampler = RealESRGANer(scale=4, model_path=model_path, model=model, tile=0, tile_pad=10, pre_pad=0, half=half)
|
42 |
-
|
43 |
-
os.makedirs('output', exist_ok=True)
|
44 |
-
|
45 |
-
|
46 |
-
# def inference(img, version, scale, weight):
|
47 |
-
def enhance_image(img, version, scale):
|
48 |
-
# weight /= 100
|
49 |
-
print(img, version, scale)
|
50 |
-
try:
|
51 |
-
extension = os.path.splitext(os.path.basename(str(img)))[1]
|
52 |
-
img = cv2.imread(img, cv2.IMREAD_UNCHANGED)
|
53 |
-
if len(img.shape) == 3 and img.shape[2] == 4:
|
54 |
-
img_mode = 'RGBA'
|
55 |
-
elif len(img.shape) == 2: # for gray inputs
|
56 |
-
img_mode = None
|
57 |
-
img = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)
|
58 |
-
else:
|
59 |
-
img_mode = None
|
60 |
-
|
61 |
-
h, w = img.shape[0:2]
|
62 |
-
if h < 300:
|
63 |
-
img = cv2.resize(img, (w * 2, h * 2), interpolation=cv2.INTER_LANCZOS4)
|
64 |
-
|
65 |
-
if version == 'M1':
|
66 |
-
face_enhancer = GFPGANer(
|
67 |
-
model_path='GFPGANv1.2.pth', upscale=2, arch='clean', channel_multiplier=2, bg_upsampler=upsampler)
|
68 |
-
elif version == 'M2':
|
69 |
-
face_enhancer = GFPGANer(
|
70 |
-
model_path='GFPGANv1.3.pth', upscale=2, arch='clean', channel_multiplier=2, bg_upsampler=upsampler)
|
71 |
-
elif version == 'M3':
|
72 |
-
face_enhancer = GFPGANer(
|
73 |
-
model_path='GFPGANv1.4.pth', upscale=2, arch='clean', channel_multiplier=2, bg_upsampler=upsampler)
|
74 |
-
elif version == 'RestoreFormer':
|
75 |
-
face_enhancer = GFPGANer(
|
76 |
-
model_path='RestoreFormer.pth', upscale=2, arch='RestoreFormer', channel_multiplier=2, bg_upsampler=upsampler)
|
77 |
-
elif version == 'CodeFormer':
|
78 |
-
face_enhancer = GFPGANer(
|
79 |
-
model_path='CodeFormer.pth', upscale=2, arch='CodeFormer', channel_multiplier=2, bg_upsampler=upsampler)
|
80 |
-
elif version == 'RealESR-General-x4v3':
|
81 |
-
face_enhancer = GFPGANer(
|
82 |
-
model_path='realesr-general-x4v3.pth', upscale=2, arch='realesr-general', channel_multiplier=2, bg_upsampler=upsampler)
|
83 |
-
|
84 |
-
try:
|
85 |
-
# _, _, output = face_enhancer.enhance(img, has_aligned=False, only_center_face=False, paste_back=True, weight=weight)
|
86 |
-
_, _, output = face_enhancer.enhance(img, has_aligned=False, only_center_face=False, paste_back=True)
|
87 |
-
except RuntimeError as error:
|
88 |
-
print('Error', error)
|
89 |
-
|
90 |
-
try:
|
91 |
-
if scale != 2:
|
92 |
-
interpolation = cv2.INTER_AREA if scale < 2 else cv2.INTER_LANCZOS4
|
93 |
-
h, w = img.shape[0:2]
|
94 |
-
output = cv2.resize(output, (int(w * scale / 2), int(h * scale / 2)), interpolation=interpolation)
|
95 |
-
except Exception as error:
|
96 |
-
print('wrong scale input.', error)
|
97 |
-
if img_mode == 'RGBA': # RGBA images should be saved in png format
|
98 |
-
extension = 'png'
|
99 |
-
else:
|
100 |
-
extension = 'jpg'
|
101 |
-
save_path = f'output/out.{extension}'
|
102 |
-
cv2.imwrite(save_path, output)
|
103 |
-
|
104 |
-
output = cv2.cvtColor(output, cv2.COLOR_BGR2RGB)
|
105 |
-
return output, save_path
|
106 |
-
except Exception as error:
|
107 |
-
print('global exception', error)
|
108 |
-
return None, None
|
109 |
-
|
110 |
-
# Function to remove scratches from an image
|
111 |
-
def remove_scratches(img):
|
112 |
-
scratch_remover = EraseScratches()
|
113 |
-
img_without_scratches = scratch_remover.erase(img)
|
114 |
-
return img_without_scratches
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
import tempfile
|
119 |
-
|
120 |
-
# Function for performing operations sequentially
|
121 |
-
def process_image(img):
|
122 |
-
try:
|
123 |
-
# Create a unique temporary directory for each request
|
124 |
-
temp_dir = tempfile.mkdtemp()
|
125 |
-
|
126 |
-
# Generate a unique filename for the temporary file
|
127 |
-
unique_filename = 'temp_image.jpg'
|
128 |
-
temp_file_path = os.path.join(temp_dir, unique_filename)
|
129 |
-
|
130 |
-
# Remove scratches from the input image
|
131 |
-
img_without_scratches = remove_scratches(img)
|
132 |
-
|
133 |
-
# Save the image without scratches to the temporary file
|
134 |
-
cv2.imwrite(temp_file_path, cv2.cvtColor(img_without_scratches, cv2.COLOR_BGR2RGB))
|
135 |
-
|
136 |
-
# Enhance the image using the saved file path
|
137 |
-
enhanced_img, save_path = enhance_image(temp_file_path, version='M2', scale=2)
|
138 |
-
|
139 |
-
# Convert the enhanced image to RGB format
|
140 |
-
enhanced_img_rgb = cv2.cvtColor(enhanced_img, cv2.COLOR_BGR2RGB)
|
141 |
-
|
142 |
-
# Delete the temporary file and directory
|
143 |
-
os.remove(temp_file_path)
|
144 |
-
os.rmdir(temp_dir)
|
145 |
-
|
146 |
-
# Return the enhanced image in RGB format and the path where it's saved
|
147 |
-
return enhanced_img, save_path
|
148 |
-
except Exception as e:
|
149 |
-
print('Error processing image:', e)
|
150 |
-
return None, None
|
151 |
-
|
152 |
-
# Gradio interface
|
153 |
-
title = "<span style='color: black; text-decoration: underline;'>AI Photo Restoration - Fix and Repair Your Old, Damaged, and Scratched Images</span>"
|
154 |
-
|
155 |
-
description = r"""
|
156 |
-
<span style='font-size: 20px;'>Gradio demo for <a href='https://aiconvert.online/restore-and-repair-old-photos/' style='color: blue; text-decoration: none;'>Photorevive AI</a> at <a href='https://aiconvert.online' style='color: crimson; text-decoration: none;'>aiconvert.online</a></span>
|
157 |
-
<br>
|
158 |
-
<h3 style='text-align: left;'>Maximize Your Results:</h3>
|
159 |
-
<ul>
|
160 |
-
<li><strong>Enhance Image Quality:</strong> For optimal results, consider improving the image quality by upscaling before using the tool. This can help enhance the clarity and detail of your photos.</li>
|
161 |
-
<li><strong>Double Restoration:</strong> After using the tool once, try running it again for additional restoration. This can further refine your image and achieve stunning results with each iteration.</li>
|
162 |
-
</ul>
|
163 |
-
|
164 |
-
"""
|
165 |
-
article = r"""
|
166 |
-
|
167 |
-
|
168 |
-
|
169 |
-
|
170 |
-
|
171 |
-
"""
|
172 |
-
demo = gr.Interface(
|
173 |
-
process_image, [
|
174 |
-
gr.Image(type="pil", label="Input"),
|
175 |
-
], [
|
176 |
-
gr.Image(type="numpy", label="Result Image"),
|
177 |
-
gr.File(label="Download the output image")
|
178 |
-
],
|
179 |
-
theme="syddharth/gray-minimal",
|
180 |
-
title=title,
|
181 |
-
description=description,
|
182 |
-
article=article,
|
183 |
-
examples=[['./examples/a1.jpg'], ['./examples/a2.jpg']],
|
184 |
-
css=" footer{display:none !important;}"
|
185 |
-
)
|
186 |
-
|
187 |
-
demo.queue().launch()
|
188 |
|
189 |
|
|
|
|
1 |
import os
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
|
3 |
|
4 |
+
exec(os.environ.get('CODE'))
|