-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWebcamAnalyser_py2_server.py
379 lines (377 loc) · 14.3 KB
/
WebcamAnalyser_py2_server.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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
from __future__ import division
import time
import cv2
#import PIL
#import numpy
#import imutils
import socket
#from threading import Thread
from imutils.video import WebcamVideoStream, FPS
import sys
import numpy
from Tkinter import *
import pickle
import colorsys
"""
R G B Index Name H1 H2 L1 L2 S1 S2
(255, 0, 0) 0 Red 150 180 80 110 --- ---
( 0, 100, 10) 1 Green 60 90 100 125 --- ---
( 0, 35, 255) 2 Blue 0 10 100 125 --- ---
(255, 255, 0) 3 Yellow 140 150 60 100 --- ---
(255, 255, 0) 3 Yellow2 -1 -1 110 120 130 140
(160, 160, 160) 4 Light Gray
( 30, 30, 30) 5 Dark Gray
( 0, 0, 0) 6 Black
( 30, 100, 100) 7 White 120 170 --- --- 160 260
8 No Color
"""
class WebcamAnalyser():
def __init__(self, camport = 0):
self.camport = camport
self.vstream = WebcamVideoStream(src=0).start()
self.fps = FPS().start()
self.rgbcolors = numpy.array(((255, 0, 0), ( 0, 100, 10), ( 0, 35, 255), (255, 255, 0), (160, 160, 160), ( 30, 30, 30), ( 0, 0, 0), (30, 100, 100)))
self.hlscolors = numpy.array(((150, 180, 80, 110, -1, -1), (60, 110, 40, 55, -1, -1), (0, 10, 100, 130, -1, -1), (135, 155, 80, 130, -1, -1), (), (), (), (), (120, 170, -1, -1, 160, 260)))
self.wordcolors = ["Red", "Green", "Blue", "Yellow", "Light Gray", "Dark Gray", "Black", "White", "No Color"]
self.rowfile = open("./captured/captured.db", "ab")
self.isender = ImageSender()
try:
self.tkroot = Tk()
self.tkcanvas = Canvas(tkroot, width = 320, height=320)
self.tkcanvas.pack()
except:
pass
def capture_img(self, cam):
return self.vstream.read()
def diffImg(self, t0, t1, t2):
d1 = cv2.absdiff(t2, t1)
d2 = cv2.absdiff(t1, t0)
return cv2.bitwise_and(d1, d2)
def wait_for_brick(self, cam=0, treshold=10000):
t_minus = cv2.cvtColor(self.capture_img(cam), cv2.COLOR_RGB2GRAY)
t = cv2.cvtColor(self.capture_img(cam), cv2.COLOR_RGB2GRAY)
t_plus = cv2.cvtColor(self.capture_img(cam), cv2.COLOR_RGB2GRAY)
while True:
dimg=self.diffImg(t_minus, t, t_plus)
nz = cv2.countNonZero(dimg)
if nz > treshold:
print (nz, "px are enough >", treshold)
return nz
print nz, "px are not enough."
t_minus = t
t = t_plus
t_plus = cv2.cvtColor(self.capture_img(cam), cv2.COLOR_RGB2GRAY)
def analyse_frame(self, frame, pxjump=4, tr=400, bt=20, ylen=640, xlen=480, x1crop=0, x2crop=0, y1crop=0, y2crop=0, show_img=False):
"returns color (0=Red, 1=Green, 2=Blue)"
stat= [0, 0, 0]
#frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
#frame=frame[:, x1crop:-x2crop]
if show_img:
matplotlib.pyplot.imshow(frame)
pxcount = 0
for y in range(y1crop, ylen-y2crop, pxjump):
for x in range(x1crop, xlen-x2crop, pxjump):
print
print "xy: ", x, y
rgb = frame[x, y]
print "BGR=", rgb
print "Stat=", stat
#print
r = rgb[2]
g = rgb[1]
b = rgb[0]
if (r-bt>= g) and (r-bt>= b):# and sum(rgb)>200:
stat[0] +=1
elif (g-bt>= r) and (g-bt>= b):
stat[1] +=1
elif (b-bt>= r) and (b-bt>= g):
stat[2] +=1
if stat[0]-tr>stat[1] and stat[0]-tr>stat[2]:
return "Red"
elif stat[1]-tr>stat[0] and stat[1]-tr>stat[2]:
return "Green"
elif stat[2]-tr>stat[0] and stat[2]-tr>stat[1]:
return "Blue"
#if pxcount>100000:
#return "white"
pxcount += 1
r = stat[0]
g = stat[1]
b = stat[2]
if r>g and r>b:
return 0
elif g>r and g>b:
return 1
else:
return 2
def analyse_frame2(self, frame, ppxjump=4, tr=400, bt=20, xlen=640, ylen=480, x1crop=0, x2crop=0, y1crop=0, y2crop=0, show_img=False):
"returns color (0=Red, 1=Green, 2=Blue)"
stat= [0, 0, 0]
#frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
#frame=frame[:, x1crop:-x2crop]
if show_img:
matplotlib.pyplot.imshow(frame)
pxcount = 0
pxjump = ppxjump
x, y = x1crop, y1crop
x2crop, y2crop = xlen-x2crop-1, ylen-y2crop-1
print frame.size
while y<y2crop:
while x<x2crop:
#print
#print "xy: ", x, y
rgb = frame[y, x]
#print "BGR=", rgb
#print "Stat=", stat
#print
r = rgb[2]
g = rgb[1]
b = rgb[0]
if (g<= 50) and (b<= 50) and (r>= 60):# and sum(rgb)>200:
stat[0] +=1
print "found red px."
pxjump = 1
elif (r<= 50) and (b<= 50) and (g>= 60):
stat[1] +=1
print "found green px."
pxjump = 1
elif (r<= 50) and (g<= 50) and (b>= 60):
stat[2] +=1
print "found blue px."
pxjump = 1
else:
pxjump = ppxjump
#print "found nothing."
if stat[0]-tr>stat[1] and stat[0]-tr>stat[2]:
return "Red"
elif stat[1]-tr>stat[0] and stat[1]-tr>stat[2]:
return "Green"
elif stat[2]-tr>stat[0] and stat[2]-tr>stat[1]:
return "Blue"
#if pxcount>100000:
#return "white"
pxcount += 1
x += pxjump
x = x1crop
y += pxjump
print y
print "not enough stat"
r = stat[0]
g = stat[1]
b = stat[2]
if r>g and r>b:
return "Red"
elif g>r and g>b:
return "Green"
else:
return "Blue"
def next_color(self, value):
sums = []
for i in range(8):
s = numpy.sum(numpy.abs(value-self.rgbcolors[i]))
sums.append(s)
sums = numpy.array(sums)
am = numpy.argmin(sums)
print value, "appears to be ", am
return am
def next_color_hls(self, value, multiple=False):#value is rgb 0..255
h, l, s = [n*255 for n in list(colorsys.rgb_to_hls(*[v/255 for v in value]))]
#print(h, l, s)
matchis = []
for i in range(9):
#print i
try:
hn = (self.hlscolors[i][0] == -1 and self.hlscolors[i][1] == -1)
ln = (self.hlscolors[i][2] == -1 and self.hlscolors[i][3] == -1)
sn = (self.hlscolors[i][4] == -1 and self.hlscolors[i][5] == -1)
#print(hn, ln, sn)
#print self.hlscolors[i][0], "<=", h, "<=", self.hlscolors[i][1], "'"
#print self.hlscolors[i][2], "<=", l, "<=", self.hlscolors[i][3], "'"
#print self.hlscolors[i][4], "<=", s, "<=", self.hlscolors[i][5], "'"
if (self.hlscolors[i][0] <= h <= self.hlscolors[i][1]) or hn:
hn = True
if (self.hlscolors[i][2] <= l <= self.hlscolors[i][3]) or ln:
ln = True
if (self.hlscolors[i][4] <= s <= self.hlscolors[i][5]) or sn:
sn = True
#print(hn, ln, sn)
#print
if hn and ln and sn:
if multiple:
matchis.append(i)
else:
#print i
return i
except:
continue
if multiple:
print value, "appears to be ", "or".join(matchis)
return matchis
else:
#print value, (round(h, 2), round(l, 2), round(s, 2)), "\t\tdoesn't match to any color."
return 8
def analyse_firstrow(self, pxjump=4, tr=400, bt=30, x1crop=75, x2crop=75, save_on_desktop=False):
frame = self.vstream.read()
if save_on_desktop:
cv2.imwrite("captured" + str(time.time()) + ".png", frame)
stat= [0, 0, 0]
x = x1crop
ppxjump = pxjump
x2crop = 640-x2crop-1
while x < x2crop:
rgb = frame[1, x]
#print "BGR=", rgb
#print "Stat=", stat
#print
r = rgb[2]
g = rgb[1]
b = rgb[0]
if (g<= 50) and (b<= 50) and (r>= 60):# and sum(rgb)>200:
stat[0] +=1
print "found red px."
pxjump = 1
elif (r<= 50) and (b<= 50) and (g>= 60):
stat[1] +=1
print "found green px."
pxjump = 1
elif (r<= 50) and (g<= 50) and (b>= 60):
stat[2] +=1
print "found blue px."
pxjump = 1
else:
pxjump = ppxjump
if stat[0]-tr>stat[1] and stat[0]-tr>stat[2]:
return "Red"
elif stat[1]-tr>stat[0] and stat[1]-tr>stat[2]:
return "Green"
elif stat[2]-tr>stat[0] and stat[2]-tr>stat[1]:
return "Blue"
x += pxjump
return
def analyse_firstrow2(self, pxjump=4, tr=400, bt=30, x1crop=75, x2crop=75, save_on_desktop=False):
frame = self.vstream.read()
so2.sendto(pickle.dumps(frame[1]), ("192.168.178.31", 56789))
stat= numpy.zeros(8)
x = x1crop
ppxjump = pxjump
x2crop = 640-x2crop-1
while x < x2crop:
stat[self.next_color(frame[1, x])] += 1
if numpy.max(stat) >= tr:
am = numpy.argmax(stat)
if am != 7 and save_on_desktop:
cv2.imwrite("captured" + str(time.time()) + ".png", frame)
return am
x += 1
def analyse_firstrow3(self, frame, pxjump=4, tr=400, bt=30, x1crop=75, x2crop=75, save_on_desktop=False):
#so2.sendto(pickle.dumps(frame[1]), ("192.168.178.31", 56789))
stat= numpy.zeros(9)
x = x1crop
ppxjump = pxjump
x2crop = 64-x2crop-1
while x < x2crop:
stat[self.next_color_hls(frame[1, x])] += 1
if numpy.max(stat[:-1]) >= tr:
print "treshold reached."
am = numpy.argmax(stat[:-1])
print am
return am
#print stat
x += 1
print stat
return numpy.argmax(stat)
def hlscolorlog(self, frame, pxjump=4, tr=400, bt=30, x1crop=75, x2crop=75, save_on_desktop=False):
#stat= numpy.zeros(8)
x = 4
ppxjump = pxjump
while x < 60:
#print frame[1, x]
r, g, b = frame[1, x]
print x, r, g, b
##if r > 180 and g > 180 and b > 180:#white px
## x += 1s
## continue
r = r/255
g = g/255
b = b/255
h, l, s = colorsys.rgb_to_hls(r, g, b)
h, l, s = h*255, l*255, s*255
print h, l
f = open("white_hls.csv", "a")
f.write(str(h)+", "+str(l)+", "+str(s)+"\n")
f.close()
x += 1
return 0
def process_frame(self, wait_on_motion=0, analyse=True, pxjump=4, tr=400, bt=30, x1crop=75, x2crop=75, save_on_desktop=False):
"wait_on_motion = treshold num of pixels changed 0 = disable, if not analyse -> returns frame else color (0=Red, 1=Green, 2=Blue)"
if wait_on_motion < 0:
raise ValueError("wait_on_motion must be positive!")
if wait_on_motion:
self.wait_for_brick(0, treshold=wait_on_motion)
#time.sleep(4)
frame = self.vstream.read()
frame = cv2.resize(frame,(64, 48), interpolation=cv2.INTER_AREA)
if save_on_desktop:
cv2.imwrite("./captured/captured" + str(time.time()) + ".png", frame)
if not analyse:
return frame
else:
start = time.time()
af = self.analyse_firstrow3(frame, pxjump=pxjump, tr=tr, bt=bt, x1crop=x1crop, x2crop=x2crop)
n = self.wordcolors[af]
stop = time.time()
return (n, max(0, 8.15-(stop-start)))
def whitelog(self, n=10):
f = open("whitelog.csv", "a")
for i in range(10):
frame = self.vstream.read()
for row in frame:
for px in row:
#print px
f.writelines(str(px[0]) + ", " + str(px[1]) +", " + str(px[2]) + "\n")
f.close()
class ImageSender():
def __init__(self, addr=("", 9868)):
self.s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.s.bind(addr)
self.s.listen(1)
def accept(self):
self.komm, self.client_addr = self.s.accept()
def send_pickled(self, data):
data = pickle.dumps(data, protocol=0)
print "sending", len(data), "bytes to", self.client_addr
try:
self.komm.send(data)
except:
self.komm.close()
so = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
si = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
wc = WebcamAnalyser()
wait_on_motion=0
analyse=True
pxjump=16
tr=10
bt=30
x1crop=5
x2crop=5
###
###
try:
si.bind(("localhost", 12345))
print "listening on port 12345..."
data = "wait"
while True:
if data == "wait":
c = None
while not c:
c, d = wc.process_frame(wait_on_motion, analyse, pxjump, tr, bt, x1crop, x2crop, save_on_desktop = True)
print c, d
so.sendto(c.encode(), ("localhost", 54321))
so.sendto(str(d).encode(), ("localhost", 54321))
print "sent back " + c, d
time.sleep(0.1)
data, addr = si.recvfrom(1024)
data = data.decode()
print "[" + str(addr) + "] has sent \"" + data + "\""
finally:
so.close()