mischeiwiller commited on
Commit
cda78eb
·
verified ·
1 Parent(s): 6f48141

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +64 -50
app.py CHANGED
@@ -1,38 +1,31 @@
1
  import gradio as gr
2
-
3
  import kornia as K
4
  from kornia.core import Tensor
5
  from kornia import morphology as morph
6
-
7
  import torch
8
 
9
- def morphological_operators(filepath, operator, kernel, kernel_size):
10
-
11
- img: Tensor = K.io.load_image(filepath, K.io.ImageLoadType.RGB32)
12
- img = img[None]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
13
 
14
- device = 'cpu' # 'cuda:0' for GPU
15
- kernels = {
16
- "Ones": torch.ones(kernel_size,kernel_size).to(device),
17
- "Eye": torch.eye(kernel_size).to(device),
18
- "Cross": torch.tensor([[0, 1, 0],[1, 1, 1],[0, 1, 0]]).to(device),
19
- }
20
-
21
- operations = {
22
- 'Dilation': morph.dilation(img, kernels[kernel]),
23
- 'Erosion':morph.erosion(img, kernels[kernel]),
24
- 'Open': morph.opening(img, kernels[kernel]),
25
- 'Close': morph.closing(img, kernels[kernel]),
26
- 'Gradient': 1. - morph.gradient(img, kernels[kernel]),
27
- 'Bottom Hat': 1. - morph.bottom_hat(img, kernels[kernel]),
28
- 'Top Hat': 1. - morph.top_hat(img, kernels[kernel])
29
- }
30
-
31
-
32
- output = K.tensor_to_image(operations[operator].squeeze(0))
33
- return output
34
-
35
-
36
  examples = [
37
  ["examples/cat.png", "Dilation", "Ones", 3],
38
  ["examples/huggingface.jpg", "Close", "Eye", 5]
@@ -42,26 +35,47 @@ title = "Kornia Morphological Operators"
42
  description = "<p style='text-align: center'>This is a Gradio demo for Kornia's Morphological Operators.</p><p style='text-align: center'>To use it, simply upload your image, or click one of the examples to load them, and select any morphological operator to run it! Read more at the links at the bottom.</p>"
43
  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/morphology_101.html' target='_blank'>Kornia Morphological Operators Tutorial</a></p>"
44
 
45
- iface = gr.Interface(morphological_operators,
46
- [
47
- gr.Image(type="filepath"),
48
- gr.Dropdown(choices=["Dilation", "Erosion", "Open", "Close", "Gradient", "Bottom Hat", "Top Hat"]),
49
- gr.Radio(choices=["Ones", "Eye", "Cross"]),
50
- gr.Slider(
51
- minimum=1,
52
- maximum=7,
53
- step=2,
54
- value=3,
55
- label="Kernel size"
56
- )
57
- ],
58
- "image",
59
- examples,
60
- title=title,
61
- description=description,
62
- article=article,
63
- live=True
64
-
65
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
66
 
67
- iface.launch()
 
 
1
  import gradio as gr
 
2
  import kornia as K
3
  from kornia.core import Tensor
4
  from kornia import morphology as morph
 
5
  import torch
6
 
7
+ def morphological_operators(img, operator, kernel, kernel_size):
8
+ img: Tensor = K.io.image_to_tensor(img)
9
+ img = img.unsqueeze(0) # Add batch dimension
10
+ device = 'cpu' # 'cuda:0' for GPU
11
+ kernels = {
12
+ "Ones": torch.ones(kernel_size, kernel_size).to(device),
13
+ "Eye": torch.eye(kernel_size).to(device),
14
+ "Cross": torch.tensor([[0, 1, 0],[1, 1, 1],[0, 1, 0]]).to(device),
15
+ }
16
+ operations = {
17
+ 'Dilation': morph.dilation(img, kernels[kernel]),
18
+ 'Erosion': morph.erosion(img, kernels[kernel]),
19
+ 'Open': morph.opening(img, kernels[kernel]),
20
+ 'Close': morph.closing(img, kernels[kernel]),
21
+ 'Gradient': 1. - morph.gradient(img, kernels[kernel]),
22
+ 'Bottom Hat': 1. - morph.bottom_hat(img, kernels[kernel]),
23
+ 'Top Hat': 1. - morph.top_hat(img, kernels[kernel])
24
+ }
25
+
26
+ output = K.tensor_to_image(operations[operator].squeeze(0))
27
+ return output
28
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
29
  examples = [
30
  ["examples/cat.png", "Dilation", "Ones", 3],
31
  ["examples/huggingface.jpg", "Close", "Eye", 5]
 
35
  description = "<p style='text-align: center'>This is a Gradio demo for Kornia's Morphological Operators.</p><p style='text-align: center'>To use it, simply upload your image, or click one of the examples to load them, and select any morphological operator to run it! Read more at the links at the bottom.</p>"
36
  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/morphology_101.html' target='_blank'>Kornia Morphological Operators Tutorial</a></p>"
37
 
38
+ with gr.Blocks(title=title) as demo:
39
+ gr.Markdown(f"# {title}")
40
+ gr.Markdown(description)
41
+
42
+ with gr.Row():
43
+ input_image = gr.Image(label="Input Image")
44
+ output_image = gr.Image(label="Output Image")
45
+
46
+ with gr.Row():
47
+ operator = gr.Dropdown(
48
+ choices=["Dilation", "Erosion", "Open", "Close", "Gradient", "Bottom Hat", "Top Hat"],
49
+ label="Operator"
50
+ )
51
+ kernel = gr.Radio(
52
+ choices=["Ones", "Eye", "Cross"],
53
+ label="Kernel Type"
54
+ )
55
+ kernel_size = gr.Slider(
56
+ minimum=1,
57
+ maximum=7,
58
+ step=2,
59
+ value=3,
60
+ label="Kernel size"
61
+ )
62
+
63
+ apply_button = gr.Button("Apply Operator")
64
+ apply_button.click(
65
+ fn=morphological_operators,
66
+ inputs=[input_image, operator, kernel, kernel_size],
67
+ outputs=output_image
68
+ )
69
+
70
+ gr.Examples(
71
+ examples=examples,
72
+ inputs=[input_image, operator, kernel, kernel_size],
73
+ outputs=output_image,
74
+ fn=morphological_operators,
75
+ cache_examples=True
76
+ )
77
+
78
+ gr.Markdown(article)
79
 
80
+ if __name__ == "__main__":
81
+ demo.launch()