rwheel commited on
Commit
ade6aac
1 Parent(s): 38e0113

Added more kernels and examples

Browse files
Files changed (2) hide show
  1. app.py +40 -27
  2. examples/huggingface.jpg +0 -0
app.py CHANGED
@@ -6,35 +6,36 @@ from kornia import morphology as morph
6
 
7
  import torch
8
 
9
- def morphological_operators(filepath, operator):
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
- kernel = torch.tensor([[0, 1, 0],[1, 1, 1],[0, 1, 0]]).to(device)
16
-
17
- if operator == 'Dilation':
18
- opt = morph.dilation(img, kernel)
19
- elif operator == 'Erosion':
20
- opt = morph.erosion(img, kernel)
21
- elif operator == 'Open':
22
- opt = morph.opening(img, kernel)
23
- elif operator == 'Close':
24
- opt = morph.closing(img, kernel)
25
- elif operator == 'Gradient':
26
- opt = 1. - morph.gradient(img, kernel)
27
- elif operator == 'Bottom Hat':
28
- opt = 1. - morph.bottom_hat(img, kernel)
29
- else:
30
- opt = 1. - morph.top_hat(img, kernel)
31
-
32
- output = K.tensor_to_image(opt.squeeze(0))
33
- return output
34
 
35
 
36
  examples = [
37
- ["examples/cat.png", "Dilation"]
 
38
  ]
39
 
40
  title = "Kornia Morphological Operators"
@@ -44,10 +45,22 @@ article = "<p style='text-align: center'><a href='https://kornia.readthedocs.io/
44
  iface = gr.Interface(morphological_operators,
45
  [
46
  gr.Image(type="filepath"),
47
- gr.Dropdown(choices=["Dilation", "Erosion", "Open", "Close", "Gradient", "Bottom Hat", "Top Hat"])
 
 
 
 
 
 
 
 
48
  ],
49
  "image",
50
- examples
 
 
 
 
51
 
52
  )
53
 
 
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
+ }
19
+ #torch.tensor([[0, 1, 0],[1, 1, 1],[0, 1, 0]]).to(device)
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"],
38
+ ["examples/huggingface.jpg", "Close", "Eye"]
39
  ]
40
 
41
  title = "Kornia Morphological Operators"
 
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"]),
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
 
examples/huggingface.jpg ADDED