gokaygokay commited on
Commit
04fbf12
·
verified ·
1 Parent(s): 0d65eb5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -47
app.py CHANGED
@@ -1,62 +1,35 @@
1
  import cv2
2
  import numpy as np
3
- import scipy as sp
4
- import scipy.sparse.linalg
5
  import gradio as gr
6
 
7
- def get_image(img):
8
- return cv2.cvtColor(img, cv2.COLOR_BGR2RGB).astype('double') / 255.0
9
-
10
- def neighbours(i, j, max_i, max_j):
11
- pairs = []
12
- for n in [-1, 1]:
13
- if 0 <= i+n <= max_i:
14
- pairs.append((i+n, j))
15
- if 0 <= j+n <= max_j:
16
- pairs.append((i, j+n))
17
- return pairs
18
-
19
- def poisson_sharpening(img, alpha):
20
- img_h, img_w = img.shape[:2]
21
- img_s = img.copy()
22
 
23
- im2var = np.arange(img_h * img_w).reshape(img_h, img_w)
 
 
 
 
 
24
 
25
- A = sp.sparse.lil_matrix((img_h*img_w*4*2, img_h*img_w))
26
- b = np.zeros(img_h*img_w*4*2)
27
-
28
- e = 0
29
- for y in range(img_h):
30
- for x in range(img_w):
31
- A[e, im2var[y][x]] = 1
32
- b[e] = img_s[y][x]
33
- e += 1
34
-
35
- for n_y, n_x in neighbours(y, x, img_h-1, img_w-1):
36
- A[e, im2var[y][x]] = 1
37
- A[e, im2var[n_y][n_x]] = -1
38
-
39
- b[e] = alpha * (img_s[y][x] - img_s[n_y][n_x])
40
- e += 1
41
-
42
- A = sp.sparse.csr_matrix(A)
43
- v = sp.sparse.linalg.lsqr(A, b)[0]
44
-
45
- return np.clip(v.reshape(img_h, img_w), 0, 1)
46
 
47
  def sharpen_image(input_img, alpha):
48
- img = get_image(input_img)
49
 
50
- sharpen_img = np.zeros(img.shape)
51
  for b in range(3):
52
- sharpen_img[:,:,b] = poisson_sharpening(img[:,:,b], alpha)
53
 
54
  return (sharpen_img * 255).astype(np.uint8)
55
 
56
- # Create examples list using the images from the original code
57
  examples = [
58
- ["img1.jpg", 9.0],
59
- ["img2.PNG", 7.0],
60
  ]
61
 
62
  # Create the Gradio interface
@@ -64,10 +37,10 @@ iface = gr.Interface(
64
  fn=sharpen_image,
65
  inputs=[
66
  gr.Image(label="Input Image", type="numpy"),
67
- gr.Slider(minimum=1.0, maximum=15.0, step=0.01, value=9.0, label="Sharpening Strength (alpha)")
68
  ],
69
  outputs=gr.Image(label="Sharpened Image"),
70
- title="Poisson Image Sharpening",
71
  description="Upload an image or choose from the examples, then adjust the sharpening strength to enhance edges and details.",
72
  theme='bethecloud/storj_theme',
73
  examples=examples,
 
1
  import cv2
2
  import numpy as np
3
+ from numba import jit, prange
 
4
  import gradio as gr
5
 
6
+ @jit(nopython=True, parallel=True)
7
+ def fast_poisson_sharpening(img, alpha):
8
+ img_h, img_w = img.shape
9
+ output = np.zeros_like(img)
 
 
 
 
 
 
 
 
 
 
 
10
 
11
+ for y in prange(1, img_h - 1):
12
+ for x in range(1, img_w - 1):
13
+ laplacian = (
14
+ img[y-1, x] + img[y+1, x] + img[y, x-1] + img[y, x+1] - 4 * img[y, x]
15
+ )
16
+ output[y, x] = img[y, x] + alpha * laplacian
17
 
18
+ return np.clip(output, 0, 1)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19
 
20
  def sharpen_image(input_img, alpha):
21
+ img = cv2.cvtColor(input_img, cv2.COLOR_BGR2RGB).astype('float32') / 255.0
22
 
23
+ sharpen_img = np.zeros_like(img)
24
  for b in range(3):
25
+ sharpen_img[:,:,b] = fast_poisson_sharpening(img[:,:,b], alpha)
26
 
27
  return (sharpen_img * 255).astype(np.uint8)
28
 
29
+ # Create examples list
30
  examples = [
31
+ ["img1.jpg", 0.5],
32
+ ["img2.PNG", 0.3],
33
  ]
34
 
35
  # Create the Gradio interface
 
37
  fn=sharpen_image,
38
  inputs=[
39
  gr.Image(label="Input Image", type="numpy"),
40
+ gr.Slider(minimum=0.1, maximum=1.0, step=0.01, value=0.5, label="Sharpening Strength (alpha)")
41
  ],
42
  outputs=gr.Image(label="Sharpened Image"),
43
+ title="Fast Poisson Image Sharpening",
44
  description="Upload an image or choose from the examples, then adjust the sharpening strength to enhance edges and details.",
45
  theme='bethecloud/storj_theme',
46
  examples=examples,