-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWebwalk GUI v2.py
More file actions
199 lines (157 loc) · 6.68 KB
/
Webwalk GUI v2.py
File metadata and controls
199 lines (157 loc) · 6.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
import cv2
import numpy as np
from sklearn.cluster import KMeans
import cvui
import matplotlib.pyplot as plt
from matplotlib import style
WINDOW_NAME = 'CVUI GUI'
#cv2.namedWindow("test")
cannyLowThresh = [20]
cannyHighThresh = [45]
lineThresh = [20]
wWidth = [60]
aWidth = [40]
kernel = np.ones((20,20),np.uint8)
cam = cv2.VideoCapture(1)
font = cv2.FONT_HERSHEY_SIMPLEX
def calibrate(aW, wW, gap):
if gap > 0:
distance = abs(wW - aW) / 2
distPer = float(distance) / float(gap)
print(distance)
print(gap)
print(distPer)
return distPer
def main():
gui = np.zeros((500,180), np.uint8)
cvui.init(WINDOW_NAME)
disPerPix = 1.0
pixGap = 0
while True:
cvui.window(gui, 0, 0, 180, 500, 'Settings')
# Two trackbars to control the low and high threshold values
# for the Canny edge algorithm.
if cannyHighThresh[0] < cannyLowThresh[0]:
cannyHighThresh[0] = cannyLowThresh[0]
cvui.text(gui, 10, 30, 'Canny Low')
cvui.trackbar(gui, 0, 50, 165, cannyLowThresh, 1, 300, 1,"%.0Lf")
cvui.text(gui, 10, 100, 'Canny High')
cvui.trackbar(gui, 0, 120, 165, cannyHighThresh, 1, 300, 1,"%.0Lf")
cvui.text(gui, 10, 170, 'Hough Threshold')
cvui.trackbar(gui, 0, 190, 165, lineThresh, 1, 300 , 1,"%.0Lf" )
cvui.text(gui, 10, 250, 'Web Width')
cvui.trackbar(gui, 0, 260, 165, wWidth, 10, 100 , 1, "%.0Lf")
cvui.text(gui, 10, 320, 'Antenne Width')
cvui.trackbar(gui, 0, 340, 165, aWidth, 10, 100 , 1, "%.0Lf" )
if cv2.waitKey(20) == 27:
break
ret, frame = cam.read()
img = frame
rows,cols = img.shape[:2]
M = np.float32([[1,0,100],[0,1,50]])
img = cv2.resize(img,None,fx=1,fy=1,interpolation=cv2.INTER_CUBIC)
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
opening = cv2.morphologyEx(gray, cv2.MORPH_OPEN, kernel)
#opening = cv2.bilateralFilter(gray, 11, 20, 20)
check = True
try:
edges = cv2.Canny(opening,cannyLowThresh[0],cannyHighThresh[0])
lines = cv2.HoughLinesP(edges, rho=1, theta=np.pi / 180,
threshold=int(lineThresh[0]), minLineLength=100, maxLineGap=20)
lines.shape
except:
check = False
#print(check)
if check:
vlines = np.ndarray(lines.shape, int)
for line in vlines:
line[0] = 0,0,0,0
i = 0
for line in lines:
x1, y1, x2, y2, = line[0]
if abs(y1 - y2) > abs(x1 - x2):
vlines[i,0] = line[0]
i = 1 + i
vlines.resize(i,1,4)
try:
vlines.shape
except:
check = False
if check:
flines = np.ndarray((i,4), int)
maxlines = np.ndarray((i,1), int)
blines = np.ndarray((i,2), int)
elines = np.ndarray((i,2), int)
i = 0
for line in vlines:
x1, y1, x2, y2, = line[0]
maxlines[i] = np.sqrt(abs(x1 - x2)^2 + abs(y1 - y2)^2)
i = i + 1
std = maxlines.std()
avg = maxlines.mean()
j = 0
i = 0
for line in vlines:
x1, y1, x2, y2, = line[0]
if maxlines[i] >= (avg + (0 * std))*0:
#print(x1)
flines[j] = line[0]
j = j + 1
cv2.line(img, (x1, y1), (x2,y2), (0,0,255),3)
#print(maxlines[i])
i = i+1
vlines.resize(i,4)
#print(vlines)
blines.resize(j,2)
elines.resize(j,2)
#print(blines)
if blines.any() and elines.any():
try:
#print(vlines[0,2:4])
emeans = KMeans(n_clusters=2).fit(flines)
entroids = emeans.cluster_centers_.astype(int)
entroids.sort(0)
#bmeans = KMeans(n_clusters=2).fit(flines[2:4])
except:
check = False
check = False
line1 = False
line2 = False
for line in flines:
x1, y1, x2, y2, = line
if 15 >= abs(entroids[0,0]- x1) or 15 >= abs(entroids[0,2]- x1):
line1 = True
if 15 >= abs(entroids[1,0]- x1) or 15 >= abs(entroids[1,2]- x1):
line2 = True
if 15 >= abs(entroids[1,0]- entroids[0,0]) or 15 >= abs(entroids[1,2]- entroids[0,2]):
line2 = False
if line1 and line2:
check = True
#print(check)
if check:
sum1 = 0
for line in entroids:
x1, y1, x2, y2, = line
cv2.line(img, (x1, y1), (x2,y2), (0,255,0),3)
sum1 = sum1 + abs(entroids[0,0] - entroids[1,0])
sum1 = sum1 + abs(entroids[0,2] - entroids[1,2])
sum1 = sum1/2
pixGap = sum1
apa = [90]
if(wWidth[0] <= aWidth[0]):
wWidth[0] = aWidth[0] + 1
if cvui.button(gui, 10, 410, 100,30,"Calibrate"):
disPerPix = calibrate(wWidth[0],aWidth[0],pixGap)
cvui.counter(gui, 10, 460, apa)
try: val = pixGap * disPerPix
except:
val = 0
cv2.putText(img,str(pixGap),(10,30), font, 1,(255,255,255),2,cv2.LINE_AA)
cv2.putText(img,str("%.2f" %val),(100,30), font, 1,(255,255,255),2,cv2.LINE_AA)
cv2.imshow("Camera", img)
cv2.imshow("Canny", edges)
cvui.update()
cv2.imshow(WINDOW_NAME, gui)
if __name__ == '__main__':
main()
cv2.destroyAllWindows()