Michael Scheiwiller commited on
Commit
f1690e5
1 Parent(s): 52336ed

fix: update code to use latest versions

Browse files
Files changed (1) hide show
  1. app.py +38 -25
app.py CHANGED
@@ -1,26 +1,46 @@
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
- def filters(file, box_blur, blur_pool2d, gaussian_blur2d, max_blur_pool2d, median_blur):
10
- # load the image using the rust backend
11
- img: Tensor = K.io.load_image(file.name, K.io.ImageLoadType.RGB32)
 
 
 
 
 
 
 
 
 
 
 
 
12
  img = img[None] # 1xCxHxW / fp32 / [0, 1]
13
 
14
  # apply tensor image enhancement
15
  x_out: Tensor = K.filters.box_blur(img, (int(box_blur), int(box_blur)))
16
  x_out = K.filters.blur_pool2d(x_out, int(blur_pool2d))
17
- x_out = K.filters.gaussian_blur2d(x_out,
18
- (int(gaussian_blur2d), int(gaussian_blur2d)),
19
- (float(gaussian_blur2d), float(gaussian_blur2d)))
20
  x_out = K.filters.max_blur_pool2d(x_out, int(max_blur_pool2d))
21
  x_out = K.filters.median_blur(x_out, (int(median_blur), int(median_blur)))
22
 
23
- return K.utils.tensor_to_image(x_out)
 
 
 
 
 
 
24
 
25
 
26
  examples = [
@@ -28,26 +48,19 @@ examples = [
28
  ["examples/kitty.jpg", 1, 1, 1, 1, 1],
29
  ]
30
 
31
- title = "Kornia Image Filters"
32
- description = "<p style='text-align: center'>This is a Gradio demo for Kornia's Image Filters.</p><p style='text-align: center'>To use it, simply upload your image, or click one of the examples to load them, and use the sliders to enhance! Read more at the links at the bottom.</p>"
33
- article = "<p style='text-align: center'><a href='https://kornia.readthedocs.io/en/latest/' target='_blank'>Kornia Docs</a> | <a href='https://github.com/kornia/kornia' target='_blank'>Kornia Github Repo</a> | <a href='https://kornia-tutorials.readthedocs.io/en/latest/image_enhancement.html' target='_blank'>Kornia Enhancements Tutorial</a></p>"
34
-
35
- iface = gr.Interface(
36
- filters,
37
- [
38
- gr.inputs.Image(type="file"),
39
- gr.inputs.Slider(minimum=1, maximum=10, step=1, default=1, label="Box Blur"),
40
- gr.inputs.Slider(minimum=1, maximum=10, step=1, default=1, label="Blur Pool"),
41
- gr.inputs.Slider(minimum=1, maximum=21, step=2, default=1, label="Gaussian Blur"),
42
- gr.inputs.Slider(minimum=1, maximum=20, step=1, default=1, label="Max Pool"),
43
- gr.inputs.Slider(minimum=1, maximum=5, step=2, default=1, label="Median Blur"),
44
  ],
45
- "image",
46
  examples=examples,
47
- # title=title,
48
- # description=description,
49
- # article=article,
50
  live=True
51
  )
52
 
53
- iface.launch()
 
1
  # created with great guidance from https://github.com/NimaBoscarino
2
 
3
+ import os
4
  import gradio as gr
5
 
6
  import kornia as K
7
  from kornia.core import Tensor
8
+ import torch
9
 
10
 
11
+ def filters(file, box_blur, blur_pool2d, gaussian_blur2d, max_blur_pool2d, median_blur):
12
+ """
13
+ Apply a series of image filters to the input image.
14
+ """
15
+ # Handle filepath string and file object
16
+ if isinstance(file, str):
17
+ filepath = file
18
+ else:
19
+ filepath = file.name
20
+
21
+ # Check if file exists
22
+ if not os.path.exists(filepath):
23
+ raise ValueError(f"File not found: {filepath}")
24
+ # load the image using the rust backend
25
+ img: Tensor = K.io.load_image(filepath, K.io.ImageLoadType.RGB32)
26
  img = img[None] # 1xCxHxW / fp32 / [0, 1]
27
 
28
  # apply tensor image enhancement
29
  x_out: Tensor = K.filters.box_blur(img, (int(box_blur), int(box_blur)))
30
  x_out = K.filters.blur_pool2d(x_out, int(blur_pool2d))
31
+ x_out = K.filters.gaussian_blur2d(x_out,
32
+ (int(gaussian_blur2d), int(gaussian_blur2d)),
33
+ (float(gaussian_blur2d), float(gaussian_blur2d)))
34
  x_out = K.filters.max_blur_pool2d(x_out, int(max_blur_pool2d))
35
  x_out = K.filters.median_blur(x_out, (int(median_blur), int(median_blur)))
36
 
37
+ # Ensure output is in the range [0, 1]
38
+ x_out = torch.clamp(x_out, 0, 1)
39
+
40
+ # Convert to numpy array and scale to [0, 255]
41
+ output_image = (x_out.squeeze().permute(1, 2, 0).cpu().numpy() * 255).astype('uint8')
42
+
43
+ return output_image
44
 
45
 
46
  examples = [
 
48
  ["examples/kitty.jpg", 1, 1, 1, 1, 1],
49
  ]
50
 
51
+ demo = gr.Interface(
52
+ fn=filters,
53
+ inputs=[
54
+ gr.Image(type="filepath", label="Input Image"),
55
+ gr.Slider(minimum=1, maximum=10, step=1, value=1, label="Box Blur"),
56
+ gr.Slider(minimum=1, maximum=10, step=1, value=1, label="Blur Pool"),
57
+ gr.Slider(minimum=1, maximum=21, step=2, value=1, label="Gaussian Blur"),
58
+ gr.Slider(minimum=1, maximum=20, step=1, value=1, label="Max Pool"),
59
+ gr.Slider(minimum=1, maximum=5, step=2, value=1, label="Median Blur"),
 
 
 
 
60
  ],
61
+ outputs=gr.Image(type="numpy", label="Output Image"),
62
  examples=examples,
 
 
 
63
  live=True
64
  )
65
 
66
+ demo.launch()