mischeiwiller commited on
Commit
2006f30
1 Parent(s): 12441f2

fix file handling

Browse files
Files changed (1) hide show
  1. app.py +45 -68
app.py CHANGED
@@ -1,109 +1,87 @@
1
- # created with great guidance from https://github.com/NimaBoscarino
2
-
3
  import gradio as gr
4
-
5
  import kornia as K
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)
13
- img = img[None] # 1xCxHxW / fp32 / [0, 1]
14
-
 
 
 
 
 
 
15
  x_out: Tensor = K.filters.box_blur(img, (int(box_blur), int(box_blur)))
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)
23
- img = img[None] # 1xCxHxW / fp32 / [0, 1]
24
 
 
 
25
  x_out: Tensor = K.filters.blur_pool2d(img, int(blur_pool2d))
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)
33
- img = img[None] # 1xCxHxW / fp32 / [0, 1]
34
 
35
- x_out: Tensor = K.filters.gaussian_blur2d(img,
 
 
36
  (int(gaussian_blur2d), int(gaussian_blur2d)),
37
- (float(gaussian_blur2d), float(gaussian_blur2d)))
38
-
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.Image(type="filepath"),
74
  gr.Slider(minimum=1, maximum=20, step=1, value=10, label="Box Blur")
75
  ],
76
  "image",
77
  examples=examples,
78
  )
79
 
80
-
81
  blur_pool2d_demo = gr.Interface(
82
  blur_pool2d_fn,
83
  [
84
- gr.Image(type="file"),
85
  gr.Slider(minimum=1, maximum=40, step=1, value=20, label="Blur Pool")
86
  ],
87
  "image",
88
  examples=examples,
89
  )
90
 
91
-
92
  gaussian_blur_demo = gr.Interface(
93
  gaussian_blur_fn,
94
  [
95
- gr.Image(type="file"),
96
  gr.Slider(minimum=1, maximum=30, step=2, value=15, label="Gaussian Blur")
97
  ],
98
  "image",
99
  examples=examples,
100
  )
101
 
102
-
103
  max_blur_pool2d_demo = gr.Interface(
104
  max_blur_pool2d_fn,
105
  [
106
- gr.Image(type="file"),
107
  gr.Slider(minimum=1, maximum=40, step=1, value=20, label="Max Pool")
108
  ],
109
  "image",
@@ -113,29 +91,28 @@ max_blur_pool2d_demo = gr.Interface(
113
  median_blur_demo = gr.Interface(
114
  median_blur_fn,
115
  [
116
- gr.Image(type="file"),
117
  gr.Slider(minimum=1, maximum=30, step=2, value=15, label="Median Blur")
118
  ],
119
  "image",
120
  examples=examples,
121
  )
122
 
123
-
124
  # Create Interface
125
  demo = gr.TabbedInterface(
126
  [
127
- box_blur_demo,
128
- blur_pool2d_demo,
129
- gaussian_blur_demo,
130
- max_blur_pool2d_demo,
131
- median_blur_demo
132
- ],
133
  [
134
- "Box Blur",
135
- "Blur Pool",
136
- "Gaussian Blur",
137
- "Max Pool",
138
- "Median Blur"
139
  ]
140
  )
141
 
 
 
 
1
  import gradio as gr
 
2
  import kornia as K
3
  from kornia.core import Tensor
4
+ import torch
5
+ import numpy as np
6
 
7
  # Define Functions
8
+ def process_image(file):
9
+ if isinstance(file, np.ndarray):
10
+ # If the input is already a numpy array, convert it to a tensor
11
+ img = K.image_to_tensor(file).float() / 255.0
12
+ else:
13
+ # If it's a file path, load it using kornia
14
+ img = K.io.load_image(file, K.io.ImageLoadType.RGB32)
15
+ return img.unsqueeze(0) # Add batch dimension: 1xCxHxW
16
+
17
+ def box_blur_fn(file, box_blur):
18
+ img = process_image(file)
19
  x_out: Tensor = K.filters.box_blur(img, (int(box_blur), int(box_blur)))
20
+ return K.utils.tensor_to_image(x_out.squeeze())
 
 
 
 
 
 
 
21
 
22
+ def blur_pool2d_fn(file, blur_pool2d):
23
+ img = process_image(file)
24
  x_out: Tensor = K.filters.blur_pool2d(img, int(blur_pool2d))
25
+ return K.utils.tensor_to_image(x_out.squeeze())
 
 
 
 
 
 
 
26
 
27
+ def gaussian_blur_fn(file, gaussian_blur2d):
28
+ img = process_image(file)
29
+ x_out: Tensor = K.filters.gaussian_blur2d(img,
30
  (int(gaussian_blur2d), int(gaussian_blur2d)),
31
+ (float(gaussian_blur2d)/2, float(gaussian_blur2d)/2))
32
+ return K.utils.tensor_to_image(x_out.squeeze())
 
 
33
 
34
  def max_blur_pool2d_fn(file, max_blur_pool2d):
35
+ img = process_image(file)
 
 
 
36
  x_out: Tensor = K.filters.max_blur_pool2d(img, int(max_blur_pool2d))
37
+ return K.utils.tensor_to_image(x_out.squeeze())
 
 
 
 
 
 
 
38
 
39
+ def median_blur_fn(file, median_blur):
40
+ img = process_image(file)
41
  x_out: Tensor = K.filters.median_blur(img, (int(median_blur), int(median_blur)))
42
+ return K.utils.tensor_to_image(x_out.squeeze())
 
 
43
 
44
  # Define Examples
45
  examples = [
46
+ ["examples/monkey.jpg", 1],
47
+ ["examples/pikachu.jpg", 1]
48
  ]
49
 
 
50
  # Define Demos
51
  box_blur_demo = gr.Interface(
52
  box_blur_fn,
53
  [
54
+ gr.Image(type="numpy"),
55
  gr.Slider(minimum=1, maximum=20, step=1, value=10, label="Box Blur")
56
  ],
57
  "image",
58
  examples=examples,
59
  )
60
 
 
61
  blur_pool2d_demo = gr.Interface(
62
  blur_pool2d_fn,
63
  [
64
+ gr.Image(type="numpy"),
65
  gr.Slider(minimum=1, maximum=40, step=1, value=20, label="Blur Pool")
66
  ],
67
  "image",
68
  examples=examples,
69
  )
70
 
 
71
  gaussian_blur_demo = gr.Interface(
72
  gaussian_blur_fn,
73
  [
74
+ gr.Image(type="numpy"),
75
  gr.Slider(minimum=1, maximum=30, step=2, value=15, label="Gaussian Blur")
76
  ],
77
  "image",
78
  examples=examples,
79
  )
80
 
 
81
  max_blur_pool2d_demo = gr.Interface(
82
  max_blur_pool2d_fn,
83
  [
84
+ gr.Image(type="numpy"),
85
  gr.Slider(minimum=1, maximum=40, step=1, value=20, label="Max Pool")
86
  ],
87
  "image",
 
91
  median_blur_demo = gr.Interface(
92
  median_blur_fn,
93
  [
94
+ gr.Image(type="numpy"),
95
  gr.Slider(minimum=1, maximum=30, step=2, value=15, label="Median Blur")
96
  ],
97
  "image",
98
  examples=examples,
99
  )
100
 
 
101
  # Create Interface
102
  demo = gr.TabbedInterface(
103
  [
104
+ box_blur_demo,
105
+ blur_pool2d_demo,
106
+ gaussian_blur_demo,
107
+ max_blur_pool2d_demo,
108
+ median_blur_demo
109
+ ],
110
  [
111
+ "Box Blur",
112
+ "Blur Pool",
113
+ "Gaussian Blur",
114
+ "Max Pool",
115
+ "Median Blur"
116
  ]
117
  )
118