/
main.py
309 lines (257 loc) · 8.41 KB
/
main.py
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
import cv2 as cv
import http.client
from time import sleep
import numpy as np
from datetime import datetime
import math
NUMLEDS = 500
# Measures in cm
# campos = np.mat([[230,50,90]]).transpose()
#campos = np.mat([[146,210,118]]).transpose() # Auf tisch
# campos = np.mat([[-40,250,87]]).transpose() # Hinter balken
# campos = np.mat([[165, 197,118]]).transpose()
# campos = np.mat([[227,93,86]]).transpose()
a = -76
b = 220
w = -math.pi/4
print(w)
x = a*math.cos(w)-b*math.sin(w)
y = a*math.sin(w)+b*math.cos(w)
print(x,y)
campos = np.mat([[x,y,62]]).transpose() # Hinter balken
#campos = np.mat([[237,24,65]]).transpose() # Hinter balken
# Matrix to get space coordinates
v = campos[0:2,:]
v = v/np.linalg.norm(v)
S = np.mat([[-v[1,0],v[0,0],0], [0,0,1], [0,0,0]]).transpose()
baumpoly = np.mat(((0,0), (80,30), (0,215), (-80,30))).transpose()
baumpoly = np.vstack([baumpoly, np.ones(4)])
print("baumpoly")
print(baumpoly)
def setLed(nr):
conn = http.client.HTTPConnection("192.168.42.1",80)
# conn = http.client.HTTPConnection("192.168.1.75",80)
conn.request("GET", "/setled?led=%d"%nr)
r = conn.getresponse()
data = r.read()
conn.close()
# See https://www.pyimagesearch.com/2014/09/29/finding-brightest-spot-image-using-python-opencv/ for inspiration
def getCoords(diff):
# global mask
diff = cv.GaussianBlur(diff, (5,5), 0)
(minVal, maxVal, minLoc, maxLoc) = cv.minMaxLoc(diff)
#print(maxVal)
#print(maxLoc)
return maxLoc
unitPoints = []
def click(event, x, y, flags, param):
global unitPoints
if event == cv.EVENT_LBUTTONDBLCLK:
print("unitPoint: %d, %d" % (x,y))
unitPoints.append(np.mat([x,y,1]).transpose())
def getHelp():
global unitPoints
print("Sag mir wo die LED ist, wo ist sie geblieben?")
unitPoints = []
cv.setMouseCallback('diff',click)
while len(unitPoints)<1:
k = cv.waitKey(20) & 0xFF
if k == 27:
exit()
wo = S*F*unitPoints[0]
print("Human says ", wo)
return wo,1.0
def getPoints(img):
global unitPoints, baumpoly, F, Finv
cv.imshow('diff', img)
cv.setMouseCallback('diff',click)
while len(unitPoints)<2:
k = cv.waitKey(20) & 0xFF
if k == 27:
exit()
cv.setMouseCallback('diff',lambda *args : None)
unitPoints = np.hstack(unitPoints)
neunzig = np.mat([[0,1,0],[-1,0,0],[0,0,1]]).transpose()
print(neunzig)
zraum = baumpoly[:,2]-baumpoly[:,0]
print("zraum")
print(zraum)
zbild = unitPoints[:,1]-unitPoints[:,0]
print("zbild")
print(zbild)
# Koordinatensystem im Raum
O = np.hstack([np.linalg.inv(neunzig)*zraum, zraum, baumpoly[:,0]])
# Koordinatensystem auf dem Bild
B = np.hstack([neunzig*zbild, zbild, unitPoints[:,0]])
# Abbildung vom Bild in den Raum
F = O*np.linalg.inv(B)
# Abbildung vom Raum auf das Bild
Finv = np.linalg.inv(F)
print("O")
print(O)
print("B")
print(B)
print("Finv")
print(Finv)
poly = Finv*baumpoly
poly = np.int32(np.array(poly[0:2,:].transpose()))
print(poly)
h,w = img.shape[0:2]
mask = np.zeros((h,w),dtype=np.uint8)
cv.fillPoly(mask,pts=[poly], color=255)
cv.imshow('diff', mask)
sleep(2)
return mask
def getLedPos(nr, frameoff, prev=None, prevc=0.0, succ=None, succc=0.0):
setLed(nr)
# Capture the video frame
# by frame
for i in range(10):
ret, frameon = vid.read()
diff = cv.cvtColor(cv.absdiff(frameon, frameoff),cv.COLOR_BGR2GRAY)
diff = cv.bitwise_and(diff, mask)
frameon = cv.bitwise_and(cv.cvtColor(frameon,cv.COLOR_BGR2GRAY), mask)
# Display the resulting frame
wo = getCoords(diff)
cv.circle(frameon, wo, 10, (255, 0, 0), 2)
cv.imshow('diff', frameon)
wo = S*F*(np.mat(wo+(1,)).transpose())
esty = (25-abs(nr%50-25))/25*150+30
estymin = 10
estymax = (25-abs(nr%50-25))/25*140+60
confidence = 1.0
if (wo[2,0]<estymin or wo[2,0]>estymax):
confidence=0.5
dist = 0
if confidence<0.0:
confidence = 0.0
if not isinstance(succ, type(None)):
dist = np.linalg.norm(succ-wo)
if succc>0 and dist>20/succc:
print("Oops! Dist to succ is %.1f>20" % dist)
confidence *= 1-(dist-20)/20;
if not isinstance(prev, type(None)):
dist = np.linalg.norm(prev-wo)
if prevc>0 and dist>20/prevc:
print("Oops! Dist to prev is =%.1f>20" % dist)
confidence *= 1-(dist-20)/20;
if confidence<0:
confidence = 0.0
print("%.1f, %.1f, %.1f esty=%.1f, dist=%.1f c=%.3f" % (wo[0,0], wo[1,0], wo[2,0], esty, dist, confidence))
return wo,confidence
def getOffFrame():
setLed(9999)
for i in range(24):
ret, frameoff = vid.read()
return frameoff
def findCam():
cams = []
for i in range(5):
cap = cv.VideoCapture(i)
if cap.read()[0]:
cap.set(cv.CAP_PROP_FRAME_WIDTH, 10000)
cap.set(cv.CAP_PROP_FRAME_HEIGHT, 10000)
cams.append(i)
print(i)
print("Index %i -> %s", (i,cap.getBackendName()))
cap.release()
return cams
def newConf(d, c1, c2):
c = max(c1,c2)
c = (1-d/2)*1+d/2*c
if (c<0):
c = 0
return c
def weightedMedian(wo, conf):
if (len(wo)==0):
return None,0.0
if (len(wo)==1):
return wo[0],conf[0]
if (len(wo)==2):
return (conf[0]*wo[0]+conf[1]*wo[1])/(conf[0]+conf[1]), newConf(np.linalg.norm(wo[0]- wo[1]), conf[0], conf[1])
res = []
c = []
s = sum(conf)
for i in range(3):
xw = sorted(list(zip([[i,0] for c in wo], conf)))
ss = 0.0
for k in range(len(xw)):
if ss<=s/2 and ss+xw[k][1]>=s/2:
if k==0:
res.append(xw[k][0])
c.append(xw[k][1])
else:
res.append((xw[k-1][0]*xw[k-1][1] + xw[k][0]*xw[k][1])/(xw[k-1][1]+xw[k][1]))
c.append(newConf(abs(xw[k-1][0]-xw[k][0]), xw[k-1][1], xw[k][1]))
break
ss+=xw[k][1]
print("res, c")
print(res)
print(c)
return np.mat([res]).transpose(),min(c)
setLed(9999)
sleep(1)
cv.namedWindow(winname='diff')
allcams = findCam()
vid = cv.VideoCapture(min(allcams))
if not vid.isOpened():
raise IOError("Cannot open webcam")
vid.set(cv.CAP_PROP_FRAME_WIDTH, 10000)
vid.set(cv.CAP_PROP_FRAME_HEIGHT, 10000)
#vid.set(cv.CAP_PROP_FRAME_WIDTH, 1280)
#vid.set(cv.CAP_PROP_FRAME_HEIGHT, 720)
width = int(vid.get(cv.CAP_PROP_FRAME_WIDTH))
height = int(vid.get(cv.CAP_PROP_FRAME_HEIGHT))
print(width,height)
frameoff = getOffFrame()
mask = getPoints(frameoff)
sleep(1)
wo = None
ledPos = [[] for i in range(NUMLEDS)]
confidence = [[] for i in range(NUMLEDS)]
tries = [0 for i in range(NUMLEDS)]
limitconf = 0.8
ledsok = 0
nr = 0
while ledsok<NUMLEDS:
if (len(ledPos[nr])==0 or max(confidence[nr])<limitconf):
print("Locating LED %d" % nr)
prev = None
succ = None
prevc = 0.0
succc = 0.0
if (nr>0):
prev,prevc = weightedMedian(ledPos[nr-1], confidence[nr-1])
if (nr<NUMLEDS-1):
succ,succc = weightedMedian(ledPos[nr+1], confidence[nr+1])
wo,c = getLedPos(nr, frameoff, prev, prevc, succ, succc)
tries[nr]+=1
if (tries[nr]>5):
wo,c = getHelp()
if (c>0.3): # Do not even store too bad a reading
ledPos[nr].append(wo)
confidence[nr].append(c)
if (len(ledPos[nr])>1):
w,c = weightedMedian(ledPos[nr], confidence[nr])
ledPos[nr] = [w]
confidence[nr] = [c]
print("Combined confidence:%.3f" % c)
if (c>limitconf):
ledsok+=1
# the 'q' button is set as the
# quitting button you may use any
# desired button of your choice
if (cv.waitKey(1) & 0xFF) in [ord('q'), 27, ord('Q')]:
break
nr = (nr+1)%NUMLEDS
stamp = datetime.now().strftime("%Y%m%d-%H%M%S")
datafile = open("posdata-"+stamp+"-%d-%d-%d.txt" % (campos[0,0], campos[1,0],campos[2,0]), "w")
datafile.write("%f %f %f 0.0\n" % (campos[0,0], campos[1,0], campos[2,0]))
for nr in range(NUMLEDS):
wo,c = weightedMedian(ledPos[nr], confidence[nr])
datafile.write("%.1f %.1f %.1f %.3f\n" %(wo[0,0], wo[1,0], wo[2,0], c))
datafile.close()
# After the loop release the cap object
vid.release()
# Destroy all the windows
cv.destroyAllWindows()