mischeiwiller commited on
Commit
6d95ef7
1 Parent(s): 0c62c68

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -34
app.py CHANGED
@@ -1,79 +1,76 @@
1
  import gradio as gr
2
-
3
  import kornia as K
4
  from kornia.core import Tensor
 
 
5
 
6
- def edge_detection(filepath, detector):
7
-
8
- img: Tensor = K.io.load_image(filepath, K.io.ImageLoadType.RGB32)
9
- img = img[None]
10
-
11
  x_gray = K.color.rgb_to_grayscale(img)
12
 
13
-
14
  if detector == '1st order derivates in x':
15
  grads: Tensor = K.filters.spatial_gradient(x_gray, order=1)
16
  grads_x = grads[:, :, 0]
17
- grads_y = grads[:, :, 1]
18
-
19
  output = K.utils.tensor_to_image(1. - grads_x.clamp(0., 1.))
20
 
21
  elif detector == '1st order derivates in y':
22
  grads: Tensor = K.filters.spatial_gradient(x_gray, order=1)
23
- grads_x = grads[:, :, 0]
24
  grads_y = grads[:, :, 1]
25
-
26
  output = K.utils.tensor_to_image(1. - grads_y.clamp(0., 1.))
27
 
28
  elif detector == '2nd order derivatives in x':
29
  grads: Tensor = K.filters.spatial_gradient(x_gray, order=2)
30
  grads_x = grads[:, :, 0]
31
- grads_y = grads[:, :, 1]
32
-
33
  output = K.utils.tensor_to_image(1. - grads_x.clamp(0., 1.))
34
 
35
  elif detector == '2nd order derivatives in y':
36
  grads: Tensor = K.filters.spatial_gradient(x_gray, order=2)
37
- grads_x = grads[:, :, 0]
38
  grads_y = grads[:, :, 1]
39
-
40
  output = K.utils.tensor_to_image(1. - grads_y.clamp(0., 1.))
41
 
42
  elif detector == 'Sobel':
43
  x_sobel: Tensor = K.filters.sobel(x_gray)
44
  output = K.utils.tensor_to_image(1. - x_sobel)
45
-
46
  elif detector == 'Laplacian':
47
  x_laplacian: Tensor = K.filters.laplacian(x_gray, kernel_size=5)
48
  output = K.utils.tensor_to_image(1. - x_laplacian.clamp(0., 1.))
49
-
50
  else:
51
  x_canny: Tensor = K.filters.canny(x_gray)[0]
52
  output = K.utils.tensor_to_image(1. - x_canny.clamp(0., 1.0))
53
-
54
  return output
55
-
56
-
57
  examples = [
58
  ["examples/huggingface.jpg", "1st order derivates in x"],
59
  ["examples/doraemon.jpg", "Canny"]
60
  ]
61
-
62
  title = "Kornia Edge Detection"
63
  description = "<p style='text-align: center'>This is a Gradio demo for Kornia's Edge Detection.</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 edge detector to run it! Read more at the links at the bottom.</p>"
64
  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/filtering_edges.html' target='_blank'>Kornia Edge Detection Tutorial</a></p>"
65
 
66
- iface = gr.Interface(edge_detection,
67
- [
68
- gr.Image(type="filepath"),
69
- gr.Dropdown(choices=["1st order derivates in x", "1st order derivates in y", "2nd order derivatives in x", "2nd order derivatives in y", "Sobel", "Laplacian", "Canny"])
70
- ],
71
- "image",
72
- examples,
73
- title=title,
74
- description=description,
75
- article=article
76
-
77
- )
 
 
 
 
 
 
 
 
 
78
 
79
- iface.launch()
 
 
1
  import gradio as gr
 
2
  import kornia as K
3
  from kornia.core import Tensor
4
+ from PIL import Image
5
+ import numpy as np
6
 
7
+ def edge_detection(image, detector):
8
+ # Convert PIL Image to Tensor
9
+ img_np = np.array(image)
10
+ img: Tensor = K.utils.image_to_tensor(img_np).float() / 255.0
11
+ img = img.unsqueeze(0) # Add batch dimension
12
  x_gray = K.color.rgb_to_grayscale(img)
13
 
 
14
  if detector == '1st order derivates in x':
15
  grads: Tensor = K.filters.spatial_gradient(x_gray, order=1)
16
  grads_x = grads[:, :, 0]
 
 
17
  output = K.utils.tensor_to_image(1. - grads_x.clamp(0., 1.))
18
 
19
  elif detector == '1st order derivates in y':
20
  grads: Tensor = K.filters.spatial_gradient(x_gray, order=1)
 
21
  grads_y = grads[:, :, 1]
 
22
  output = K.utils.tensor_to_image(1. - grads_y.clamp(0., 1.))
23
 
24
  elif detector == '2nd order derivatives in x':
25
  grads: Tensor = K.filters.spatial_gradient(x_gray, order=2)
26
  grads_x = grads[:, :, 0]
 
 
27
  output = K.utils.tensor_to_image(1. - grads_x.clamp(0., 1.))
28
 
29
  elif detector == '2nd order derivatives in y':
30
  grads: Tensor = K.filters.spatial_gradient(x_gray, order=2)
 
31
  grads_y = grads[:, :, 1]
 
32
  output = K.utils.tensor_to_image(1. - grads_y.clamp(0., 1.))
33
 
34
  elif detector == 'Sobel':
35
  x_sobel: Tensor = K.filters.sobel(x_gray)
36
  output = K.utils.tensor_to_image(1. - x_sobel)
 
37
  elif detector == 'Laplacian':
38
  x_laplacian: Tensor = K.filters.laplacian(x_gray, kernel_size=5)
39
  output = K.utils.tensor_to_image(1. - x_laplacian.clamp(0., 1.))
 
40
  else:
41
  x_canny: Tensor = K.filters.canny(x_gray)[0]
42
  output = K.utils.tensor_to_image(1. - x_canny.clamp(0., 1.0))
 
43
  return output
44
+
 
45
  examples = [
46
  ["examples/huggingface.jpg", "1st order derivates in x"],
47
  ["examples/doraemon.jpg", "Canny"]
48
  ]
 
49
  title = "Kornia Edge Detection"
50
  description = "<p style='text-align: center'>This is a Gradio demo for Kornia's Edge Detection.</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 edge detector to run it! Read more at the links at the bottom.</p>"
51
  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/filtering_edges.html' target='_blank'>Kornia Edge Detection Tutorial</a></p>"
52
 
53
+ with gr.Blocks(title=title) as demo:
54
+ gr.Markdown(f"# {title}")
55
+ gr.Markdown(description)
56
+
57
+ with gr.Row():
58
+ input_image = gr.Image(type="pil", label="Input Image")
59
+ output_image = gr.Image(type="numpy", label="Edge Detection Result")
60
+
61
+ detector = gr.Dropdown(
62
+ choices=["1st order derivates in x", "1st order derivates in y", "2nd order derivatives in x", "2nd order derivatives in y", "Sobel", "Laplacian", "Canny"],
63
+ label="Edge Detector",
64
+ value="Canny"
65
+ )
66
+
67
+ detect_button = gr.Button("Detect Edges")
68
+
69
+ detect_button.click(fn=edge_detection, inputs=[input_image, detector], outputs=output_image)
70
+
71
+ gr.Examples(examples=examples, inputs=[input_image, detector], outputs=output_image)
72
+
73
+ gr.Markdown(article)
74
 
75
+ if __name__ == "__main__":
76
+ demo.launch()