masdar commited on
Commit
9730689
·
1 Parent(s): 8e92654

Create new file

Browse files
Files changed (1) hide show
  1. app.py +74 -0
app.py ADDED
@@ -0,0 +1,74 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+
58
+ title = "Kornia Edge Detection"
59
+ 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>"
60
+ 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>"
61
+
62
+ iface = gr.Interface(edge_detection,
63
+ [
64
+ gr.Image(type="filepath"),
65
+ 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"])
66
+ ],
67
+ "image",
68
+ title=title,
69
+ description=description,
70
+ article=article
71
+
72
+ )
73
+
74
+ iface.launch()