lappemic commited on
Commit
346da5d
1 Parent(s): 8efad9d

append filters

Browse files
Files changed (1) hide show
  1. app.py +72 -9
app.py CHANGED
@@ -6,6 +6,7 @@ import kornia as K
6
  from kornia.core import Tensor
7
 
8
 
 
9
  def box_blur_fn(file, box_blur):
10
  # load the image using the rust backend
11
  img: Tensor = K.io.load_image(file.name, K.io.ImageLoadType.RGB32)
@@ -15,6 +16,7 @@ def box_blur_fn(file, box_blur):
15
 
16
  return K.utils.tensor_to_image(x_out)
17
 
 
18
  def blur_pool2d_fn(file, blur_pool2d):
19
  # load the image using the rust backend
20
  img: Tensor = K.io.load_image(file.name, K.io.ImageLoadType.RGB32)
@@ -24,6 +26,7 @@ def blur_pool2d_fn(file, blur_pool2d):
24
 
25
  return K.utils.tensor_to_image(x_out)
26
 
 
27
  def gaussian_blur_fn(file, gaussian_blur2d):
28
  # load the image using the rust backend
29
  img: Tensor = K.io.load_image(file.name, K.io.ImageLoadType.RGB32)
@@ -36,17 +39,39 @@ def gaussian_blur_fn(file, gaussian_blur2d):
36
  return K.utils.tensor_to_image(x_out)
37
 
38
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
39
 
 
40
  examples = [
41
  ["examples/monkey.jpg", 1, 1, 1, 1, 1],
42
  ["examples/pikachu.jpg", 1, 1, 1, 1, 1],
43
  ]
44
 
45
- box_blur_fn_demo = gr.Interface(
 
 
46
  box_blur_fn,
47
  [
48
  gr.inputs.Image(type="file"),
49
- gr.inputs.Slider(minimum=1, maximum=10, step=1, default=1, label="Box Blur"),
50
  ],
51
  "image",
52
  examples=examples,
@@ -56,11 +81,12 @@ box_blur_fn_demo = gr.Interface(
56
  live=True
57
  )
58
 
59
- blur_pool2d_fn_demo = gr.Interface(
 
60
  blur_pool2d_fn,
61
  [
62
  gr.inputs.Image(type="file"),
63
- gr.inputs.Slider(minimum=1, maximum=10, step=1, default=1, label="Blur Pool"),
64
  ],
65
  "image",
66
  examples=examples,
@@ -70,7 +96,8 @@ blur_pool2d_fn_demo = gr.Interface(
70
  live=True
71
  )
72
 
73
- gaussian_blur_fn_demo = gr.Interface(
 
74
  gaussian_blur_fn,
75
  [
76
  gr.inputs.Image(type="file"),
@@ -84,15 +111,51 @@ gaussian_blur_fn_demo = gr.Interface(
84
  live=True
85
  )
86
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
87
  demo = gr.TabbedInterface(
88
  [
89
- box_blur_fn_demo,
90
- blur_pool2d_fn_demo,
91
- gaussian_blur_fn_demo
 
 
92
  ],
93
  [
94
  "Box Blur",
95
- "Blur Pool"
 
 
 
96
  ]
97
  )
98
 
 
6
  from kornia.core import Tensor
7
 
8
 
9
+ # Define Functions
10
  def box_blur_fn(file, box_blur):
11
  # load the image using the rust backend
12
  img: Tensor = K.io.load_image(file.name, K.io.ImageLoadType.RGB32)
 
16
 
17
  return K.utils.tensor_to_image(x_out)
18
 
19
+
20
  def blur_pool2d_fn(file, blur_pool2d):
21
  # load the image using the rust backend
22
  img: Tensor = K.io.load_image(file.name, K.io.ImageLoadType.RGB32)
 
26
 
27
  return K.utils.tensor_to_image(x_out)
28
 
29
+
30
  def gaussian_blur_fn(file, gaussian_blur2d):
31
  # load the image using the rust backend
32
  img: Tensor = K.io.load_image(file.name, K.io.ImageLoadType.RGB32)
 
39
  return K.utils.tensor_to_image(x_out)
40
 
41
 
42
+ def max_blur_pool2d_fn(file, max_blur_pool2d):
43
+ # load the image using the rust backend
44
+ img: Tensor = K.io.load_image(file.name, K.io.ImageLoadType.RGB32)
45
+ img = img[None] # 1xCxHxW / fp32 / [0, 1]
46
+
47
+ x_out: Tensor = K.filters.max_blur_pool2d(img, int(max_blur_pool2d))
48
+
49
+ return K.utils.tensor_to_image(x_out)
50
+
51
+
52
+ def median_blur_fn(file, median_blur):
53
+ # load the image using the rust backend
54
+ img: Tensor = K.io.load_image(file.name, K.io.ImageLoadType.RGB32)
55
+ img = img[None] # 1xCxHxW / fp32 / [0, 1]
56
+
57
+ x_out: Tensor = K.filters.median_blur(img, (int(median_blur), int(median_blur)))
58
+
59
+ return K.utils.tensor_to_image(x_out)
60
+
61
 
62
+ # Define Examples
63
  examples = [
64
  ["examples/monkey.jpg", 1, 1, 1, 1, 1],
65
  ["examples/pikachu.jpg", 1, 1, 1, 1, 1],
66
  ]
67
 
68
+
69
+ # Define Demos
70
+ box_blur_demo = gr.Interface(
71
  box_blur_fn,
72
  [
73
  gr.inputs.Image(type="file"),
74
+ gr.inputs.Slider(minimum=1, maximum=10, step=1, default=1, label="Box Blur")
75
  ],
76
  "image",
77
  examples=examples,
 
81
  live=True
82
  )
83
 
84
+
85
+ blur_pool2d_demo = gr.Interface(
86
  blur_pool2d_fn,
87
  [
88
  gr.inputs.Image(type="file"),
89
+ gr.inputs.Slider(minimum=1, maximum=20, step=1, default=1, label="Blur Pool")
90
  ],
91
  "image",
92
  examples=examples,
 
96
  live=True
97
  )
98
 
99
+
100
+ gaussian_blur_demo = gr.Interface(
101
  gaussian_blur_fn,
102
  [
103
  gr.inputs.Image(type="file"),
 
111
  live=True
112
  )
113
 
114
+
115
+ max_blur_pool2d_demo = gr.Interface(
116
+ max_blur_pool2d_fn,
117
+ [
118
+ gr.inputs.Image(type="file"),
119
+ gr.inputs.Slider(minimum=1, maximum=20, step=1, default=1, label="Max Pool")
120
+ ],
121
+ "image",
122
+ examples=examples,
123
+ # title=title,
124
+ # description=description,
125
+ # article=article,
126
+ live=True
127
+ )
128
+
129
+ median_blur_demo = gr.Interface(
130
+ median_blur_fn,
131
+ [
132
+ gr.inputs.Image(type="file"),
133
+ gr.inputs.Slider(minimum=1, maximum=5, step=2, default=1, label="Median Blur")
134
+ ],
135
+ "image",
136
+ examples=examples,
137
+ # title=title,
138
+ # description=description,
139
+ # article=article,
140
+ live=True
141
+ )
142
+
143
+
144
+ # Create Interface
145
  demo = gr.TabbedInterface(
146
  [
147
+ box_blur_demo,
148
+ blur_pool2d_demo,
149
+ gaussian_blur_demo,
150
+ max_blur_pool2d_demo,
151
+ median_blur_demo
152
  ],
153
  [
154
  "Box Blur",
155
+ "Blur Pool",
156
+ "Gaussian Blur",
157
+ "Max Pool",
158
+ "Median Blur"
159
  ]
160
  )
161