Spaces:
Runtime error
Runtime error
Commit
·
3b909cb
1
Parent(s):
ac8e0f7
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,136 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
+
from utils import utils_logger
|
| 9 |
+
from utils import utils_image as util
|
| 10 |
+
from models.network_fbcnn import FBCNN as net
|
| 11 |
+
import requests
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
def inference(input_img, is_gray, input_quality, enable_zoom, zoom, x_shift, y_shift, state):
|
| 15 |
+
|
| 16 |
+
if is_gray:
|
| 17 |
+
n_channels = 1 # set 1 for grayscale image, set 3 for color image
|
| 18 |
+
model_name = 'fbcnn_gray.pth'
|
| 19 |
+
else:
|
| 20 |
+
n_channels = 3 # set 1 for grayscale image, set 3 for color image
|
| 21 |
+
model_name = 'fbcnn_color.pth'
|
| 22 |
+
nc = [64,128,256,512]
|
| 23 |
+
nb = 4
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
input_quality = 100 - input_quality
|
| 27 |
+
|
| 28 |
+
#model_pool = 'model_zoo' # fixed
|
| 29 |
+
model_pool = '/content/FBCNN/model_zoo' # fixed
|
| 30 |
+
model_path = os.path.join(model_pool, model_name)
|
| 31 |
+
if os.path.exists(model_path):
|
| 32 |
+
print(f'loading model from {model_path}')
|
| 33 |
+
else:
|
| 34 |
+
os.makedirs(os.path.dirname(model_path), exist_ok=True)
|
| 35 |
+
url = 'https://github.com/jiaxi-jiang/FBCNN/releases/download/v1.0/{}'.format(os.path.basename(model_path))
|
| 36 |
+
r = requests.get(url, allow_redirects=True)
|
| 37 |
+
open(model_path, 'wb').write(r.content)
|
| 38 |
+
|
| 39 |
+
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
|
| 40 |
+
|
| 41 |
+
# ----------------------------------------
|
| 42 |
+
# load model
|
| 43 |
+
# ----------------------------------------
|
| 44 |
+
if (not enable_zoom) or (state[1] is None):
|
| 45 |
+
model = net(in_nc=n_channels, out_nc=n_channels, nc=nc, nb=nb, act_mode='R')
|
| 46 |
+
model.load_state_dict(torch.load(model_path), strict=True)
|
| 47 |
+
model.eval()
|
| 48 |
+
for k, v in model.named_parameters():
|
| 49 |
+
v.requires_grad = False
|
| 50 |
+
model = model.to(device)
|
| 51 |
+
|
| 52 |
+
test_results = OrderedDict()
|
| 53 |
+
test_results['psnr'] = []
|
| 54 |
+
test_results['ssim'] = []
|
| 55 |
+
test_results['psnrb'] = []
|
| 56 |
+
|
| 57 |
+
# ------------------------------------
|
| 58 |
+
# (1) img_L
|
| 59 |
+
# ------------------------------------
|
| 60 |
+
|
| 61 |
+
if n_channels == 1:
|
| 62 |
+
open_cv_image = Image.fromarray(input_img)
|
| 63 |
+
open_cv_image = ImageOps.grayscale(open_cv_image)
|
| 64 |
+
open_cv_image = np.array(open_cv_image) # PIL to open cv image
|
| 65 |
+
img = np.expand_dims(open_cv_image, axis=2) # HxWx1
|
| 66 |
+
elif n_channels == 3:
|
| 67 |
+
open_cv_image = np.array(input_img) # PIL to open cv image
|
| 68 |
+
if open_cv_image.ndim == 2:
|
| 69 |
+
open_cv_image = cv2.cvtColor(open_cv_image, cv2.COLOR_GRAY2RGB) # GGG
|
| 70 |
+
else:
|
| 71 |
+
open_cv_image = cv2.cvtColor(open_cv_image, cv2.COLOR_BGR2RGB) # RGB
|
| 72 |
+
|
| 73 |
+
img_L = util.uint2tensor4(open_cv_image)
|
| 74 |
+
img_L = img_L.to(device)
|
| 75 |
+
|
| 76 |
+
# ------------------------------------
|
| 77 |
+
# (2) img_E
|
| 78 |
+
# ------------------------------------
|
| 79 |
+
|
| 80 |
+
img_E,QF = model(img_L)
|
| 81 |
+
QF = 1- QF
|
| 82 |
+
img_E = util.tensor2single(img_E)
|
| 83 |
+
img_E = util.single2uint(img_E)
|
| 84 |
+
|
| 85 |
+
qf_input = torch.tensor([[1-input_quality/100]]).cuda() if device == torch.device('cuda') else torch.tensor([[1-input_quality/100]])
|
| 86 |
+
img_E,QF = model(img_L, qf_input)
|
| 87 |
+
QF = 1- QF
|
| 88 |
+
img_E = util.tensor2single(img_E)
|
| 89 |
+
img_E = util.single2uint(img_E)
|
| 90 |
+
|
| 91 |
+
if img_E.ndim == 3:
|
| 92 |
+
img_E = img_E[:, :, [2, 1, 0]]
|
| 93 |
+
if (state[1] is not None) and enable_zoom:
|
| 94 |
+
img_E = state[1]
|
| 95 |
+
out_img = Image.fromarray(img_E)
|
| 96 |
+
out_img_w, out_img_h = out_img.size # output image size
|
| 97 |
+
zoom = zoom/100
|
| 98 |
+
x_shift = x_shift/100
|
| 99 |
+
y_shift = y_shift/100
|
| 100 |
+
zoom_w, zoom_h = out_img_w*zoom, out_img_h*zoom
|
| 101 |
+
zoom_left, zoom_right = int((out_img_w - zoom_w)*x_shift), int(zoom_w + (out_img_w - zoom_w)*x_shift)
|
| 102 |
+
zoom_top, zoom_bottom = int((out_img_h - zoom_h)*y_shift), int(zoom_h + (out_img_h - zoom_h)*y_shift)
|
| 103 |
+
if (state[0] is None) or not enable_zoom:
|
| 104 |
+
in_img = Image.fromarray(input_img)
|
| 105 |
+
state[0] = input_img
|
| 106 |
+
else:
|
| 107 |
+
in_img = Image.fromarray(state[0])
|
| 108 |
+
in_img = in_img.crop((zoom_left, zoom_top, zoom_right, zoom_bottom))
|
| 109 |
+
in_img = in_img.resize((int(zoom_w/zoom), int(zoom_h/zoom)), Image.NEAREST)
|
| 110 |
+
out_img = out_img.crop((zoom_left, zoom_top, zoom_right, zoom_bottom))
|
| 111 |
+
out_img = out_img.resize((int(zoom_w/zoom), int(zoom_h/zoom)), Image.NEAREST)
|
| 112 |
+
|
| 113 |
+
return img_E, in_img, out_img, [state[0],img_E]
|
| 114 |
+
|
| 115 |
+
interface = gr.Interface(
|
| 116 |
+
fn = inference,
|
| 117 |
+
inputs = [gr.inputs.Image(),
|
| 118 |
+
gr.inputs.Checkbox(label="Grayscale (Check this if your image is grayscale)"),
|
| 119 |
+
gr.inputs.Slider(minimum=1, maximum=100, step=1, label="Intensity (Higher = more JPEG artifact removal)"),
|
| 120 |
+
gr.inputs.Checkbox(default=False, label="Edit Zoom preview \nThis is optional. "
|
| 121 |
+
"Check this after the image result is loaded to edit zoom parameters\n"
|
| 122 |
+
"without processing the input image."),
|
| 123 |
+
gr.inputs.Slider(minimum=10, maximum=100, step=1, default=50, label="Zoom Image \n"
|
| 124 |
+
"Use this to see the image quality up close \n"
|
| 125 |
+
"100 = original size"),
|
| 126 |
+
gr.inputs.Slider(minimum=0, maximum=100, step=1, label="Zoom preview horizontal shift \n"
|
| 127 |
+
"Increase to shift to the right"),
|
| 128 |
+
gr.inputs.Slider(minimum=0, maximum=100, step=1, label="Zoom preview vertical shift \n"
|
| 129 |
+
"Increase to shift downwards"),
|
| 130 |
+
gr.inputs.State(default=[None,None])
|
| 131 |
+
],
|
| 132 |
+
outputs = [gr.outputs.Image(label="Result"),
|
| 133 |
+
gr.outputs.Image(label="Before:"),
|
| 134 |
+
gr.outputs.Image(label="After:"),
|
| 135 |
+
"state"]
|
| 136 |
+
).launch(enable_queue=True,cache_examples=True)
|