gokaygokay commited on
Commit
38d65f9
·
verified ·
1 Parent(s): d5e1db7

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +77 -0
app.py ADDED
@@ -0,0 +1,77 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ ["samples/img1.jpg", 9.0],
59
+ ["samples/img2.jpg", 7.0],
60
+ ]
61
+
62
+ # Create the Gradio interface
63
+ 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.1, default=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,
74
+ cache_examples=True
75
+ )
76
+
77
+ iface.launch()