KenjieDec commited on
Commit
314a753
·
verified ·
1 Parent(s): c8f7fc3

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?

Files changed (3) hide show
  1. README.md +11 -13
  2. app.py +206 -189
  3. 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: 3.36.1
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
- for model_path in ['fbcnn_gray.pth','fbcnn_color.pth']:
14
- if os.path.exists(model_path):
15
- print(f'{model_path} exists.')
16
- else:
17
- print("downloading model")
18
- url = 'https://github.com/jiaxi-jiang/FBCNN/releases/download/v1.0/{}'.format(os.path.basename(model_path))
19
- r = requests.get(url, allow_redirects=True)
20
- open(model_path, 'wb').write(r.content)
21
-
22
- def inference(input_img, is_gray, input_quality, zoom, x_shift, y_shift):
23
-
24
- print("datetime:",datetime.datetime.utcnow())
25
- input_img_width, input_img_height = Image.fromarray(input_img).size
26
- print("img size:",(input_img_width,input_img_height))
27
-
28
- if (input_img_width > 1080) or (input_img_height > 1080):
29
- resize_ratio = min(1080/input_img_width, 1080/input_img_height)
30
- resized_input = Image.fromarray(input_img).resize((int(input_img_width*resize_ratio)+(input_img_width*resize_ratio < 1),
31
- int(input_img_height*resize_ratio)+(input_img_height*resize_ratio < 1)),
32
- resample=Image.BICUBIC)
33
- input_img = np.array(resized_input)
34
- print("input image resized to:", resized_input.size)
35
-
36
- if is_gray:
37
- n_channels = 1 # set 1 for grayscale image, set 3 for color image
38
- model_name = 'fbcnn_gray.pth'
39
- else:
40
- n_channels = 3 # set 1 for grayscale image, set 3 for color image
41
- model_name = 'fbcnn_color.pth'
42
- nc = [64,128,256,512]
43
- nb = 4
44
-
45
-
46
- input_quality = 100 - input_quality
47
-
48
- model_path = model_name
49
-
50
- if os.path.exists(model_path):
51
- print(f'{model_path} already exists.')
52
- else:
53
- print("downloading model")
54
- os.makedirs(os.path.dirname(model_path), exist_ok=True)
55
- url = 'https://github.com/jiaxi-jiang/FBCNN/releases/download/v1.0/{}'.format(os.path.basename(model_path))
56
- r = requests.get(url, allow_redirects=True)
57
- open(model_path, 'wb').write(r.content)
58
-
59
- device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
60
- print("device:",device)
61
-
62
- # ----------------------------------------
63
- # load model
64
- # ----------------------------------------
65
-
66
- print(f'loading model from {model_path}')
67
-
68
- model = net(in_nc=n_channels, out_nc=n_channels, nc=nc, nb=nb, act_mode='R')
69
- print("#model.load_state_dict(torch.load(model_path), strict=True)")
70
- model.load_state_dict(torch.load(model_path), strict=True)
71
- print("#model.eval()")
72
- model.eval()
73
- print("#for k, v in model.named_parameters()")
74
- for k, v in model.named_parameters():
75
- v.requires_grad = False
76
- print("#model.to(device)")
77
- model = model.to(device)
78
- print("Model loaded.")
79
-
80
- test_results = OrderedDict()
81
- test_results['psnr'] = []
82
- test_results['ssim'] = []
83
- test_results['psnrb'] = []
84
-
85
- # ------------------------------------
86
- # (1) img_L
87
- # ------------------------------------
88
-
89
- print("#if n_channels")
90
- if n_channels == 1:
91
- open_cv_image = Image.fromarray(input_img)
92
- open_cv_image = ImageOps.grayscale(open_cv_image)
93
- open_cv_image = np.array(open_cv_image) # PIL to open cv image
94
- img = np.expand_dims(open_cv_image, axis=2) # HxWx1
95
- elif n_channels == 3:
96
- open_cv_image = np.array(input_img) # PIL to open cv image
97
- if open_cv_image.ndim == 2:
98
- open_cv_image = cv2.cvtColor(open_cv_image, cv2.COLOR_GRAY2RGB) # GGG
99
- else:
100
- open_cv_image = cv2.cvtColor(open_cv_image, cv2.COLOR_BGR2RGB) # RGB
101
-
102
- print("#util.uint2tensor4(open_cv_image)")
103
- img_L = util.uint2tensor4(open_cv_image)
104
-
105
- print("#img_L.to(device)")
106
- img_L = img_L.to(device)
107
-
108
- # ------------------------------------
109
- # (2) img_E
110
- # ------------------------------------
111
-
112
- print("#model(img_L)")
113
- img_E,QF = model(img_L)
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
- print("#torch.tensor([[1-input_quality/100]]).cuda() || torch.tensor([[1-input_quality/100]])")
120
- qf_input = torch.tensor([[1-input_quality/100]]).cuda() if device == torch.device('cuda') else torch.tensor([[1-input_quality/100]])
121
- print("#util.single2uint(img_E)")
122
- img_E,QF = model(img_L, qf_input)
123
-
124
- print("#util.tensor2single(img_E)")
125
- img_E = util.tensor2single(img_E)
126
- print("#util.single2uint(img_E)")
127
- img_E = util.single2uint(img_E)
128
-
129
- if img_E.ndim == 3:
130
- img_E = img_E[:, :, [2, 1, 0]]
131
-
132
- print("--inference finished")
133
-
134
- out_img = Image.fromarray(img_E)
135
- out_img_w, out_img_h = out_img.size # output image size
136
- zoom = zoom/100
137
- x_shift = x_shift/100
138
- y_shift = y_shift/100
139
- zoom_w, zoom_h = out_img_w*zoom, out_img_h*zoom
140
- zoom_left, zoom_right = int((out_img_w - zoom_w)*x_shift), int(zoom_w + (out_img_w - zoom_w)*x_shift)
141
- zoom_top, zoom_bottom = int((out_img_h - zoom_h)*y_shift), int(zoom_h + (out_img_h - zoom_h)*y_shift)
142
- in_img = Image.fromarray(input_img)
143
- in_img = in_img.crop((zoom_left, zoom_top, zoom_right, zoom_bottom))
144
- in_img = in_img.resize((int(zoom_w/zoom), int(zoom_h/zoom)), Image.NEAREST)
145
- out_img = out_img.crop((zoom_left, zoom_top, zoom_right, zoom_bottom))
146
- out_img = out_img.resize((int(zoom_w/zoom), int(zoom_h/zoom)), Image.NEAREST)
147
-
148
- print("--generating preview finished")
149
-
150
- return img_E, in_img, out_img
151
-
152
- gr.Interface(
153
- fn = inference,
154
- inputs = [gr.inputs.Image(label="Input Image"),
155
- gr.inputs.Checkbox(label="Grayscale (Check this if your image is grayscale)"),
156
- gr.inputs.Slider(minimum=1, maximum=100, step=1, label="Intensity (Higher = stronger JPEG artifact removal)"),
157
- gr.inputs.Slider(minimum=10, maximum=100, step=1, default=50, label="Zoom Image "
158
- "(Use this to see a copy of the output image up close. "
159
- "100 = original size)"),
160
- gr.inputs.Slider(minimum=0, maximum=100, step=1, label="Zoom horizontal shift "
161
- "(Increase to shift to the right)"),
162
- gr.inputs.Slider(minimum=0, maximum=100, step=1, label="Zoom vertical shift "
163
- "(Increase to shift downwards)")
164
- ],
165
- outputs = [gr.outputs.Image(label="Result"),
166
- gr.outputs.Image(label="Before:"),
167
- gr.outputs.Image(label="After:")],
168
- examples = [["doraemon.jpg",False,60,42,50,50],
169
- ["tomandjerry.jpg",False,60,40,57,44],
170
- ["somepanda.jpg",True,100,30,8,24],
171
- ["cemetry.jpg",False,70,20,76,62],
172
- ["michelangelo_david.jpg",True,30,12,53,27],
173
- ["elon_musk.jpg",False,45,15,33,30],
174
- ["text.jpg",True,70,50,11,29]],
175
- title = "JPEG Artifacts Removal [FBCNN]",
176
- description = "Gradio Demo for JPEG Artifacts Removal. To use it, simply upload your image, "
177
- "or click one of the examples to load them. Check out the paper and the original GitHub repo at the links below. "
178
- "JPEG artifacts are noticeable distortions of images caused by JPEG lossy compression. "
179
- "This is not a super-resolution AI but a JPEG compression artifact remover. "
180
- "Written below the examples are the limitations of the input image. ",
181
- article = "<p style='text-align: left;'>Uploaded images with a length longer than 1080 pixels will be downscaled to a smaller size "
182
- "with a length of 1080 pixels. Uploaded images with transparency will be incorrectly reconstructed at the output.</p>"
183
- "<p style='text-align: center;'><a href='https://github.com/jiaxi-jiang/FBCNN'>FBCNN GitHub Repo</a><br>"
184
- "<a href='https://arxiv.org/abs/2109.14573'>Towards Flexible Blind JPEG Artifacts Removal (FBCNN, ICCV 2021)</a><br>"
185
- "<a href='https://jiaxi-jiang.github.io/'>Jiaxi Jiang, </a>"
186
- "<a href='https://cszn.github.io/'>Kai Zhang, </a>"
187
- "<a href='http://people.ee.ethz.ch/~timofter/'>Radu Timofte</a></p>",
188
- allow_flagging="never"
189
- ).launch(enable_queue=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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