Ivanrs commited on
Commit
844c647
·
1 Parent(s): b2b8640

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +132 -0
app.py ADDED
@@ -0,0 +1,132 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import cv2
3
+ import matplotlib.pyplot as plt
4
+ from scipy import ndimage
5
+ from scipy.ndimage.filters import convolve
6
+ import numpy as np
7
+
8
+ def hysteresis(img, weak = 75, strong=255):
9
+ M, N = img.shape
10
+ for i in range(1, M-1):
11
+ for j in range(1, N-1):
12
+ if (img[i,j] == weak):
13
+ try:
14
+ if ((img[i+1, j-1] == strong) or (img[i+1, j] == strong) or (img[i+1, j+1] == strong)
15
+ or (img[i, j-1] == strong) or (img[i, j+1] == strong)
16
+ or (img[i-1, j-1] == strong) or (img[i-1, j] == strong) or (img[i-1, j+1] == strong)):
17
+ img[i, j] = strong
18
+ else:
19
+ img[i, j] = 0
20
+ except IndexError as e:
21
+ pass
22
+ return img
23
+
24
+ def threshold(img, lowThresholdRatio=0.05, highThresholdRatio=0.09):
25
+
26
+ highThreshold = img.max() * highThresholdRatio;
27
+ lowThreshold = highThreshold * lowThresholdRatio;
28
+
29
+ M, N = img.shape
30
+ res = np.zeros((M,N), dtype=np.int32)
31
+
32
+ weak = np.int32(25)
33
+ strong = np.int32(255)
34
+
35
+ strong_i, strong_j = np.where(img >= highThreshold)
36
+ zeros_i, zeros_j = np.where(img < lowThreshold)
37
+
38
+ weak_i, weak_j = np.where((img <= highThreshold) & (img >= lowThreshold))
39
+
40
+ res[strong_i, strong_j] = strong
41
+ res[weak_i, weak_j] = weak
42
+
43
+ return (res)
44
+
45
+ def non_max_suppression(img, D):
46
+ M, N = img.shape
47
+ Z = np.zeros((M,N), dtype=np.int32)
48
+ angle = D * 180. / np.pi
49
+ angle[angle < 0] += 180
50
+
51
+
52
+ for i in range(1,M-1):
53
+ for j in range(1,N-1):
54
+ try:
55
+ q = 255
56
+ r = 255
57
+
58
+ #angle 0
59
+ if (0 <= angle[i,j] < 22.5) or (157.5 <= angle[i,j] <= 180):
60
+ q = img[i, j+1]
61
+ r = img[i, j-1]
62
+ #angle 45
63
+ elif (22.5 <= angle[i,j] < 67.5):
64
+ q = img[i+1, j-1]
65
+ r = img[i-1, j+1]
66
+ #angle 90
67
+ elif (67.5 <= angle[i,j] < 112.5):
68
+ q = img[i+1, j]
69
+ r = img[i-1, j]
70
+ #angle 135
71
+ elif (112.5 <= angle[i,j] < 157.5):
72
+ q = img[i-1, j-1]
73
+ r = img[i+1, j+1]
74
+
75
+ if (img[i,j] >= q) and (img[i,j] >= r):
76
+ Z[i,j] = img[i,j]
77
+ else:
78
+ Z[i,j] = 0
79
+
80
+ except IndexError as e:
81
+ pass
82
+
83
+ return Z
84
+
85
+ def gaussian_kernel(size, sigma=1):
86
+ size = int(size) // 2
87
+ x, y = np.mgrid[-size:size+1, -size:size+1]
88
+ normal = 1 / (2.0 * np.pi * sigma**2)
89
+ g = np.exp(-((x**2 + y**2) / (2.0*sigma**2))) * normal
90
+ return g
91
+
92
+ def sobel_filters(img):
93
+ Kx = np.array([[-1, 0, 1], [-2, 0, 2], [-1, 0, 1]], np.float32)
94
+ Ky = np.array([[1, 2, 1], [0, 0, 0], [-1, -2, -1]], np.float32)
95
+
96
+ Ix = ndimage.filters.convolve(img, Kx)
97
+ Iy = ndimage.filters.convolve(img, Ky)
98
+
99
+ G = np.hypot(Ix, Iy)
100
+ G = G / G.max() * 255
101
+ theta = np.arctan2(Iy, Ix)
102
+
103
+ return (G, theta)
104
+
105
+ def canny(img, kernel, sigma):
106
+ img_color = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
107
+ img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
108
+
109
+ img_gaussian = convolve(img_gray, gaussian_kernel(kernel, sigma))
110
+ G, theta = sobel_filters(img_gaussian)
111
+ img_nonmax = non_max_suppression(G, theta)
112
+ img_threshold = threshold(img_nonmax)
113
+ img_final = hysteresis(img_threshold)
114
+
115
+ return img_final
116
+
117
+
118
+ interface = gr.Interface(
119
+ title = "Canny Edge Detector 🤖",
120
+ description = "<h3>The Canny edge detector is an edge detection operator that uses a multi-stage algorithm to detect a wide range of edges in images.</h3> <br> <b>Select an image 🖼</b>",
121
+ article='<a href="#"> Hello </a>',
122
+ allow_flagging = "never",
123
+ fn = canny,
124
+ inputs = [
125
+ gr.Image(),
126
+ gr.Slider(1, 9, step = 1, value=3, label = "Kernel Size"),
127
+ gr.Slider(1, 20, step = 5, value=10, label = "Sigma"),
128
+ ],
129
+ outputs = "image"
130
+ )
131
+
132
+ interface.launch(share = False)