Update app.py
Browse files
app.py
CHANGED
|
@@ -2,86 +2,78 @@ import cv2
|
|
| 2 |
import numpy as np
|
| 3 |
import scipy as sp
|
| 4 |
import scipy.sparse.linalg
|
| 5 |
-
from numba import
|
| 6 |
import gradio as gr
|
| 7 |
|
| 8 |
-
@
|
| 9 |
-
def neighbours(
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
-
@
|
| 19 |
-
def
|
|
|
|
|
|
|
|
|
|
| 20 |
im2var = np.arange(img_h * img_w).reshape(img_h, img_w)
|
| 21 |
-
A_data = []
|
| 22 |
-
A_row = []
|
| 23 |
-
A_col = []
|
| 24 |
-
b = np.zeros(img_h*img_w*5)
|
| 25 |
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
A_row.append(e)
|
| 31 |
-
A_col.append(im2var[y, x])
|
| 32 |
-
e += 1
|
| 33 |
-
|
| 34 |
-
for n_y, n_x in neighbours(y, x, img_h-1, img_w-1):
|
| 35 |
-
A_data.append(1)
|
| 36 |
-
A_row.append(e)
|
| 37 |
-
A_col.append(im2var[y, x])
|
| 38 |
-
|
| 39 |
-
A_data.append(-1)
|
| 40 |
-
A_row.append(e)
|
| 41 |
-
A_col.append(im2var[n_y, n_x])
|
| 42 |
-
|
| 43 |
-
e += 1
|
| 44 |
|
| 45 |
-
return A_data, A_row, A_col, b,
|
| 46 |
|
| 47 |
-
@
|
| 48 |
-
def
|
| 49 |
e = 0
|
| 50 |
for y in prange(img_h):
|
| 51 |
for x in range(img_w):
|
| 52 |
-
|
|
|
|
|
|
|
|
|
|
| 53 |
e += 1
|
| 54 |
|
| 55 |
for n_y, n_x in neighbours(y, x, img_h-1, img_w-1):
|
| 56 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
e += 1
|
| 58 |
-
|
| 59 |
-
def poisson_sharpening(img: np.ndarray, alpha: float) -> np.ndarray:
|
| 60 |
-
"""
|
| 61 |
-
Returns a sharpened image with strength of alpha.
|
| 62 |
-
:param img: the image
|
| 63 |
-
:param alpha: edge threshold and gradient scaler
|
| 64 |
-
"""
|
| 65 |
-
img_h, img_w = img.shape[:2]
|
| 66 |
-
|
| 67 |
-
A_data, A_row, A_col, b, e = build_poisson_matrix(img_h, img_w, alpha)
|
| 68 |
-
fill_b_vector(b, img, alpha, img_h, img_w)
|
| 69 |
|
| 70 |
-
A = sp.sparse.csr_matrix((A_data, (A_row, A_col)), shape=(e, img_h*img_w))
|
| 71 |
v = sp.sparse.linalg.lsqr(A, b[:e])[0]
|
| 72 |
|
| 73 |
return np.clip(v.reshape(img_h, img_w), 0, 1)
|
| 74 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
def get_image(img):
|
| 76 |
return cv2.cvtColor(img, cv2.COLOR_BGR2RGB).astype('float32') / 255.0
|
| 77 |
|
| 78 |
def sharpen_image(input_img, alpha):
|
| 79 |
img = get_image(input_img)
|
| 80 |
-
|
| 81 |
-
sharpen_img = np.zeros(img.shape)
|
| 82 |
-
for b in range(3):
|
| 83 |
-
sharpen_img[:,:,b] = poisson_sharpening(img[:,:,b], alpha)
|
| 84 |
-
|
| 85 |
return (sharpen_img * 255).astype(np.uint8)
|
| 86 |
|
| 87 |
# Create examples list
|
|
|
|
| 2 |
import numpy as np
|
| 3 |
import scipy as sp
|
| 4 |
import scipy.sparse.linalg
|
| 5 |
+
from numba import njit, prange
|
| 6 |
import gradio as gr
|
| 7 |
|
| 8 |
+
@njit
|
| 9 |
+
def neighbours(y, x, max_y, max_x):
|
| 10 |
+
neighbors = []
|
| 11 |
+
if y > 0:
|
| 12 |
+
neighbors.append((y-1, x))
|
| 13 |
+
if y < max_y:
|
| 14 |
+
neighbors.append((y+1, x))
|
| 15 |
+
if x > 0:
|
| 16 |
+
neighbors.append((y, x-1))
|
| 17 |
+
if x < max_x:
|
| 18 |
+
neighbors.append((y, x+1))
|
| 19 |
+
return neighbors
|
| 20 |
|
| 21 |
+
@njit
|
| 22 |
+
def poisson_sharpening(img, alpha):
|
| 23 |
+
img_h, img_w = img.shape
|
| 24 |
+
img_s = img.copy()
|
| 25 |
+
|
| 26 |
im2var = np.arange(img_h * img_w).reshape(img_h, img_w)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
+
A_data = np.zeros(img_h * img_w * 5, dtype=np.float64)
|
| 29 |
+
A_row = np.zeros(img_h * img_w * 5, dtype=np.int32)
|
| 30 |
+
A_col = np.zeros(img_h * img_w * 5, dtype=np.int32)
|
| 31 |
+
b = np.zeros(img_h * img_w * 5, dtype=np.float64)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
+
return _poisson_sharpening_inner(img_s, alpha, im2var, A_data, A_row, A_col, b, img_h, img_w)
|
| 34 |
|
| 35 |
+
@njit(parallel=True)
|
| 36 |
+
def _poisson_sharpening_inner(img_s, alpha, im2var, A_data, A_row, A_col, b, img_h, img_w):
|
| 37 |
e = 0
|
| 38 |
for y in prange(img_h):
|
| 39 |
for x in range(img_w):
|
| 40 |
+
A_data[e] = 1
|
| 41 |
+
A_row[e] = e
|
| 42 |
+
A_col[e] = im2var[y, x]
|
| 43 |
+
b[e] = img_s[y, x]
|
| 44 |
e += 1
|
| 45 |
|
| 46 |
for n_y, n_x in neighbours(y, x, img_h-1, img_w-1):
|
| 47 |
+
A_data[e] = 1
|
| 48 |
+
A_row[e] = e
|
| 49 |
+
A_col[e] = im2var[y, x]
|
| 50 |
+
e += 1
|
| 51 |
+
|
| 52 |
+
A_data[e] = -1
|
| 53 |
+
A_row[e] = e - 1
|
| 54 |
+
A_col[e] = im2var[n_y, n_x]
|
| 55 |
+
|
| 56 |
+
b[e-1] = alpha * (img_s[y, x] - img_s[n_y, n_x])
|
| 57 |
e += 1
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
|
| 59 |
+
A = sp.sparse.csr_matrix((A_data[:e], (A_row[:e], A_col[:e])), shape=(e, img_h * img_w))
|
| 60 |
v = sp.sparse.linalg.lsqr(A, b[:e])[0]
|
| 61 |
|
| 62 |
return np.clip(v.reshape(img_h, img_w), 0, 1)
|
| 63 |
|
| 64 |
+
@njit(parallel=True)
|
| 65 |
+
def sharpen_image_channels(img, alpha):
|
| 66 |
+
sharpen_img = np.zeros_like(img)
|
| 67 |
+
for b in prange(3):
|
| 68 |
+
sharpen_img[:,:,b] = poisson_sharpening(img[:,:,b], alpha)
|
| 69 |
+
return sharpen_img
|
| 70 |
+
|
| 71 |
def get_image(img):
|
| 72 |
return cv2.cvtColor(img, cv2.COLOR_BGR2RGB).astype('float32') / 255.0
|
| 73 |
|
| 74 |
def sharpen_image(input_img, alpha):
|
| 75 |
img = get_image(input_img)
|
| 76 |
+
sharpen_img = sharpen_image_channels(img, alpha)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
return (sharpen_img * 255).astype(np.uint8)
|
| 78 |
|
| 79 |
# Create examples list
|