Fixed Demo | Improved Zoom Image
Browse files- Used pngwn's `ImageSlider` to better allow users to compare input/output image
- Slider Changes | Before: Click Run, and wait for a long time, especially if your image is quite *high-res*
- Slider Changes | Now: Just change the slider, the image will output faster because it doesn't run the whole `FBCNN` thingy, it's just resizing the existing output and input
- Gradio updated to **4.44.0** ( Not the latest )
- Others?
- README.md +11 -13
- app.py +206 -189
- requirements.txt +7 -4
README.md
CHANGED
@@ -1,13 +1,11 @@
|
|
1 |
-
---
|
2 |
-
title: JPEG Artifacts Removal
|
3 |
-
emoji: 🖼️
|
4 |
-
colorFrom: indigo
|
5 |
-
colorTo: blue
|
6 |
-
sdk: gradio
|
7 |
-
sdk_version:
|
8 |
-
app_file: app.py
|
9 |
-
pinned: false
|
10 |
-
license: apache-2.0
|
11 |
-
---
|
12 |
-
|
13 |
-
Check out the configuration reference at https://huggingface.co/docs/hub/spaces#reference
|
|
|
1 |
+
---
|
2 |
+
title: JPEG Artifacts Removal
|
3 |
+
emoji: 🖼️
|
4 |
+
colorFrom: indigo
|
5 |
+
colorTo: blue
|
6 |
+
sdk: gradio
|
7 |
+
sdk_version: 4.44.1
|
8 |
+
app_file: app.py
|
9 |
+
pinned: false
|
10 |
+
license: apache-2.0
|
11 |
+
---
|
|
|
|
app.py
CHANGED
@@ -1,189 +1,206 @@
|
|
1 |
-
import gradio as gr
|
2 |
-
import os.path
|
3 |
-
import numpy as np
|
4 |
-
from collections import OrderedDict
|
5 |
-
import torch
|
6 |
-
import cv2
|
7 |
-
from PIL import Image, ImageOps
|
8 |
-
import utils_image as util
|
9 |
-
from network_fbcnn import FBCNN as net
|
10 |
-
import requests
|
11 |
-
import datetime
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
print(
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
print("
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
n_channels =
|
41 |
-
model_name = '
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
print(
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
model
|
69 |
-
print("#model.
|
70 |
-
model.
|
71 |
-
print("#model.
|
72 |
-
model.
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
print("
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
test_results =
|
81 |
-
test_results['
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
open_cv_image
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
print("#
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
# (
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
print("#util.tensor2single(img_E)")
|
115 |
-
img_E = util.tensor2single(img_E)
|
116 |
-
print("#util.single2uint(img_E)")
|
117 |
-
img_E = util.single2uint(img_E)
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
print("
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
|
157 |
-
|
158 |
-
|
159 |
-
|
160 |
-
|
161 |
-
|
162 |
-
|
163 |
-
|
164 |
-
|
165 |
-
|
166 |
-
|
167 |
-
|
168 |
-
|
169 |
-
|
170 |
-
|
171 |
-
|
172 |
-
|
173 |
-
|
174 |
-
|
175 |
-
|
176 |
-
|
177 |
-
|
178 |
-
|
179 |
-
|
180 |
-
|
181 |
-
|
182 |
-
|
183 |
-
|
184 |
-
|
185 |
-
|
186 |
-
|
187 |
-
|
188 |
-
|
189 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import os.path
|
3 |
+
import numpy as np
|
4 |
+
from collections import OrderedDict
|
5 |
+
import torch
|
6 |
+
import cv2
|
7 |
+
from PIL import Image, ImageOps
|
8 |
+
import utils_image as util
|
9 |
+
from network_fbcnn import FBCNN as net
|
10 |
+
import requests
|
11 |
+
import datetime
|
12 |
+
from gradio_imageslider import ImageSlider
|
13 |
+
|
14 |
+
current_output = None
|
15 |
+
for model_path in ['fbcnn_gray.pth','fbcnn_color.pth']:
|
16 |
+
if os.path.exists(model_path):
|
17 |
+
print(f'{model_path} exists.')
|
18 |
+
else:
|
19 |
+
print("downloading model")
|
20 |
+
url = 'https://github.com/jiaxi-jiang/FBCNN/releases/download/v1.0/{}'.format(os.path.basename(model_path))
|
21 |
+
r = requests.get(url, allow_redirects=True)
|
22 |
+
open(model_path, 'wb').write(r.content)
|
23 |
+
|
24 |
+
def inference(input_img, is_gray, input_quality, zoom, x_shift, y_shift):
|
25 |
+
|
26 |
+
print("datetime:", datetime.datetime.utcnow())
|
27 |
+
input_img_width, input_img_height = Image.fromarray(input_img).size
|
28 |
+
print("img size:", (input_img_width, input_img_height))
|
29 |
+
|
30 |
+
if (input_img_width > 1080) or (input_img_height > 1080):
|
31 |
+
resize_ratio = min(1080/input_img_width, 1080/input_img_height)
|
32 |
+
resized_input = Image.fromarray(input_img).resize(
|
33 |
+
(int(input_img_width*resize_ratio) + (input_img_width*resize_ratio < 1),
|
34 |
+
int(input_img_height*resize_ratio) + (input_img_height*resize_ratio < 1)),
|
35 |
+
resample=Image.BICUBIC)
|
36 |
+
input_img = np.array(resized_input)
|
37 |
+
print("input image resized to:", resized_input.size)
|
38 |
+
|
39 |
+
if is_gray:
|
40 |
+
n_channels = 1
|
41 |
+
model_name = 'fbcnn_gray.pth'
|
42 |
+
else:
|
43 |
+
n_channels = 3
|
44 |
+
model_name = 'fbcnn_color.pth'
|
45 |
+
nc = [64,128,256,512]
|
46 |
+
nb = 4
|
47 |
+
|
48 |
+
input_quality = 100 - input_quality
|
49 |
+
|
50 |
+
model_path = model_name
|
51 |
+
|
52 |
+
if os.path.exists(model_path):
|
53 |
+
print(f'{model_path} already exists.')
|
54 |
+
else:
|
55 |
+
print("downloading model")
|
56 |
+
os.makedirs(os.path.dirname(model_path), exist_ok=True)
|
57 |
+
url = 'https://github.com/jiaxi-jiang/FBCNN/releases/download/v1.0/{}'.format(os.path.basename(model_path))
|
58 |
+
r = requests.get(url, allow_redirects=True)
|
59 |
+
open(model_path, 'wb').write(r.content)
|
60 |
+
|
61 |
+
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
|
62 |
+
print("device:", device)
|
63 |
+
|
64 |
+
print(f'loading model from {model_path}')
|
65 |
+
|
66 |
+
model = net(in_nc=n_channels, out_nc=n_channels, nc=nc, nb=nb, act_mode='R')
|
67 |
+
print("#model.load_state_dict(torch.load(model_path), strict=True)")
|
68 |
+
model.load_state_dict(torch.load(model_path), strict=True)
|
69 |
+
print("#model.eval()")
|
70 |
+
model.eval()
|
71 |
+
print("#for k, v in model.named_parameters()")
|
72 |
+
for k, v in model.named_parameters():
|
73 |
+
v.requires_grad = False
|
74 |
+
print("#model.to(device)")
|
75 |
+
model = model.to(device)
|
76 |
+
print("Model loaded.")
|
77 |
+
|
78 |
+
test_results = OrderedDict()
|
79 |
+
test_results['psnr'] = []
|
80 |
+
test_results['ssim'] = []
|
81 |
+
test_results['psnrb'] = []
|
82 |
+
|
83 |
+
print("#if n_channels")
|
84 |
+
if n_channels == 1:
|
85 |
+
open_cv_image = Image.fromarray(input_img)
|
86 |
+
open_cv_image = ImageOps.grayscale(open_cv_image)
|
87 |
+
open_cv_image = np.array(open_cv_image)
|
88 |
+
img = np.expand_dims(open_cv_image, axis=2)
|
89 |
+
elif n_channels == 3:
|
90 |
+
open_cv_image = np.array(input_img)
|
91 |
+
if open_cv_image.ndim == 2:
|
92 |
+
open_cv_image = cv2.cvtColor(open_cv_image, cv2.COLOR_GRAY2RGB)
|
93 |
+
else:
|
94 |
+
open_cv_image = cv2.cvtColor(open_cv_image, cv2.COLOR_BGR2RGB)
|
95 |
+
|
96 |
+
print("#util.uint2tensor4(open_cv_image)")
|
97 |
+
img_L = util.uint2tensor4(open_cv_image)
|
98 |
+
|
99 |
+
print("#img_L.to(device)")
|
100 |
+
img_L = img_L.to(device)
|
101 |
+
|
102 |
+
print("#model(img_L)")
|
103 |
+
img_E, QF = model(img_L)
|
104 |
+
print("#util.tensor2single(img_E)")
|
105 |
+
img_E = util.tensor2single(img_E)
|
106 |
+
print("#util.single2uint(img_E)")
|
107 |
+
img_E = util.single2uint(img_E)
|
108 |
+
|
109 |
+
print("#torch.tensor([[1-input_quality/100]]).cuda() || torch.tensor([[1-input_quality/100]])")
|
110 |
+
qf_input = torch.tensor([[1-input_quality/100]]).cuda() if device == torch.device('cuda') else torch.tensor([[1-input_quality/100]])
|
111 |
+
print("#util.single2uint(img_E)")
|
112 |
+
img_E, QF = model(img_L, qf_input)
|
113 |
+
|
114 |
+
print("#util.tensor2single(img_E)")
|
115 |
+
img_E = util.tensor2single(img_E)
|
116 |
+
print("#util.single2uint(img_E)")
|
117 |
+
img_E = util.single2uint(img_E)
|
118 |
+
|
119 |
+
if img_E.ndim == 3:
|
120 |
+
img_E = img_E[:, :, [2, 1, 0]]
|
121 |
+
|
122 |
+
global current_output
|
123 |
+
current_output = img_E.copy()
|
124 |
+
print("--inference finished")
|
125 |
+
|
126 |
+
(in_img, out_img) = zoom_image(zoom, x_shift, y_shift, input_img, img_E)
|
127 |
+
print("--generating preview finished")
|
128 |
+
|
129 |
+
return img_E, (in_img, out_img)
|
130 |
+
|
131 |
+
def zoom_image(zoom, x_shift, y_shift, input_img, output_img = None):
|
132 |
+
global current_output
|
133 |
+
if output_img is None:
|
134 |
+
if current_output is None:
|
135 |
+
return None
|
136 |
+
output_img = current_output
|
137 |
+
|
138 |
+
img = Image.fromarray(input_img)
|
139 |
+
out_img = Image.fromarray(output_img)
|
140 |
+
|
141 |
+
img_w, img_h = img.size
|
142 |
+
zoom_factor = (100 - zoom) / 100
|
143 |
+
x_shift /= 100
|
144 |
+
y_shift /= 100
|
145 |
+
|
146 |
+
zoom_w, zoom_h = int(img_w * zoom_factor), int(img_h * zoom_factor)
|
147 |
+
x_offset = int((img_w - zoom_w) * x_shift)
|
148 |
+
y_offset = int((img_h - zoom_h) * y_shift)
|
149 |
+
|
150 |
+
crop_box = (x_offset, y_offset, x_offset + zoom_w, y_offset + zoom_h)
|
151 |
+
img = img.crop(crop_box).resize((img_w, img_h), Image.BILINEAR)
|
152 |
+
out_img = out_img.crop(crop_box).resize((img_w, img_h), Image.BILINEAR)
|
153 |
+
|
154 |
+
return (img, out_img)
|
155 |
+
|
156 |
+
with gr.Blocks() as demo:
|
157 |
+
gr.Markdown("# JPEG Artifacts Removal [FBCNN]")
|
158 |
+
|
159 |
+
with gr.Row():
|
160 |
+
input_img = gr.Image(label="Input Image")
|
161 |
+
output_img = gr.Image(label="Result")
|
162 |
+
|
163 |
+
is_gray = gr.Checkbox(label="Grayscale (Check this if your image is grayscale)")
|
164 |
+
input_quality = gr.Slider(1, 100, step=1, label="Intensity (Higher = stronger JPEG artifact removal)")
|
165 |
+
zoom = gr.Slider(10, 100, step=1, value=50, label="Zoom Percentage (0 = original size)")
|
166 |
+
x_shift = gr.Slider(0, 100, step=1, label="Horizontal shift Percentage (Before/After)")
|
167 |
+
y_shift = gr.Slider(0, 100, step=1, label="Vertical shift Percentage (Before/After)")
|
168 |
+
|
169 |
+
run = gr.Button("Run")
|
170 |
+
|
171 |
+
with gr.Row():
|
172 |
+
before_after = ImageSlider(label="Before/After", type="pil", value=None)
|
173 |
+
|
174 |
+
run.click(
|
175 |
+
inference,
|
176 |
+
inputs=[input_img, is_gray, input_quality, zoom, x_shift, y_shift],
|
177 |
+
outputs=[output_img, before_after]
|
178 |
+
)
|
179 |
+
|
180 |
+
gr.Examples([
|
181 |
+
["doraemon.jpg", False, 60, 58, 50, 50],
|
182 |
+
["tomandjerry.jpg", False, 60, 60, 57, 44],
|
183 |
+
["somepanda.jpg", True, 100, 70, 8, 24],
|
184 |
+
["cemetry.jpg", False, 70, 80, 76, 62],
|
185 |
+
["michelangelo_david.jpg", True, 30, 88, 53, 27],
|
186 |
+
["elon_musk.jpg", False, 45, 75, 33, 30],
|
187 |
+
["text.jpg", True, 70, 50, 11, 29]
|
188 |
+
], inputs=[input_img, is_gray, input_quality, zoom, x_shift, y_shift])
|
189 |
+
|
190 |
+
zoom.release(zoom_image, inputs=[zoom, x_shift, y_shift, input_img], outputs=[before_after])
|
191 |
+
x_shift.release(zoom_image, inputs=[zoom, x_shift, y_shift, input_img], outputs=[before_after])
|
192 |
+
y_shift.release(zoom_image, inputs=[zoom, x_shift, y_shift, input_img], outputs=[before_after])
|
193 |
+
|
194 |
+
gr.Markdown("""
|
195 |
+
JPEG Artifacts are noticeable distortions of images caused by JPEG lossy compression.
|
196 |
+
Note that this is not an AI Upscaler, but just a JPEG Compression Artifact Remover.
|
197 |
+
|
198 |
+
[Original Demo](https://huggingface.co/spaces/danielsapit/JPEG_Artifacts_Removal)
|
199 |
+
[FBCNN GitHub Repo](https://github.com/jiaxi-jiang/FBCNN)
|
200 |
+
[Towards Flexible Blind JPEG Artifacts Removal (FBCNN, ICCV 2021)](https://arxiv.org/abs/2109.14573)
|
201 |
+
[Jiaxi Jiang](https://jiaxi-jiang.github.io/),
|
202 |
+
[Kai Zhang](https://cszn.github.io/),
|
203 |
+
[Radu Timofte](http://people.ee.ethz.ch/~timofter/)
|
204 |
+
""")
|
205 |
+
|
206 |
+
demo.launch()
|
requirements.txt
CHANGED
@@ -1,4 +1,7 @@
|
|
1 |
-
torch
|
2 |
-
opencv-python
|
3 |
-
torchvision
|
4 |
-
gradio
|
|
|
|
|
|
|
|
1 |
+
torch
|
2 |
+
opencv-python
|
3 |
+
torchvision
|
4 |
+
gradio
|
5 |
+
jinja2
|
6 |
+
matplotlib
|
7 |
+
gradio_imageslider
|