/
sampler.py
286 lines (230 loc) · 9.53 KB
/
sampler.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
import cv2
from numpy import median
import time
from math import floor
import serial
import struct
from WebcamVideoStream import WebcamVideoStream
class Sampler:
num_side_leds = 38
num_top_leds = 65
num_bottom_leds = 68
roi_size = 100
left_leds_colors = []
right_leds_colors = []
top_leds_colors = []
bottom_led_colors = []
"""Size of the ROI from where colors will be sampled."""
sample_roi_size = 100
def __init__(self):
print("Initializing sampler")
def sample_from_webcam(self):
# cap = cv2.VideoCapture(0)
vs = WebcamVideoStream(src=0).start()
# test = cap.get(cv2.CAP_PROP_POS_MSEC)
# ratio = cap.get(cv2.CAP_PROP_POS_AVI_RATIO)
# frame_rate = cap.get(cv2.CAP_PROP_FPS)
# width = cap.get(cv2.CAP_PROP_FRAME_WIDTH)
# height = cap.get(cv2.CAP_PROP_FRAME_HEIGHT)
# brightness = cap.get(cv2.CAP_PROP_BRIGHTNESS)
# contrast = cap.get(cv2.CAP_PROP_CONTRAST)
# saturation = cap.get(cv2.CAP_PROP_SATURATION)
# hue = cap.get(cv2.CAP_PROP_HUE)
# gain = cap.get(cv2.CAP_PROP_GAIN)
# exposure = cap.get(cv2.CAP_PROP_EXPOSURE)
# cap.set(cv2.CAP_PROP_FRAME_WIDTH, 320)
# cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 240)
# cap.set(cv2.CAP_PROP_EXPOSURE, -6.0)
# cap.set(cv2.CAP_PROP_FPS, 30)
# cap.set(cv2.CAP_PROP_GAIN, 0)
# cap.set(cv2.CAP_PROP_BRIGHTNESS, 64)
# cap.set(cv2.CAP_PROP_CONTRAST, 64)
# cap.set(cv2.CAP_PROP_SATURATION, 64)
# print("Test: ", test)
# print("Ratio: ", ratio)
# print("Frame Rate: ", frame_rate)
# print("Height: ", height)
# print("Width: ", width)
# print("Brightness: ", brightness)
# print("Contrast: ", contrast)
# print("Saturation: ", saturation)
# print("Hue: ", hue)
# print("Gain: ", gain)
# print("Exposure: ", exposure)
arduino = serial.Serial('COM3', 1000000, timeout=.1)
time.sleep(1)
arduino.write('r'.encode())
time.sleep(1)
# arduino.write("Hello from Python".encode())
# img = cv2.imread('red.png')
while True:
start = time.time()
#s, img = cap.read()
img = vs.read()
#cv2.resize(img, None, 0.5, 0.5, cv2.INTER_LINEAR)
self.sample_from_image(img)
colors = []
for color in self.left_leds_colors:
colors.append(int(color[2]))
colors.append(int(color[1]))
colors.append(int(color[0]))
for color in self.top_leds_colors:
colors.append(int(color[2]))
colors.append(int(color[1]))
colors.append(int(color[0]))
for color in self.right_leds_colors:
colors.append(int(color[2]))
colors.append(int(color[1]))
colors.append(int(color[0]))
for color in self.bottom_led_colors:
colors.append(int(color[2]))
colors.append(int(color[1]))
colors.append(int(color[0]))
bytes = struct.pack('c' + ('B' * len(colors)) + 'c', '@'.encode(), *colors, '#'.encode())
arduino.write(bytes)
img_with_borders = self.show_image_with_colors(img)
# cv2.imshow("Image with Borders", img_with_borders)
# cv2.waitKey(30)
# time.sleep(0.1)
end = time.time()
elapsed = end - start
print(str(1 / elapsed) + " FPS")
def sample_from_image(self, img):
# rols, cols, channels
# print('Img is: ' + str(width) + ' x ' + str(height))
self.process_side_borders(img)
self.process_top_border(img)
self.process_bottom_border(img)
def show_image_with_colors(self, img):
img_with_borders = cv2.copyMakeBorder(img, self.roi_size, self.roi_size, self.roi_size, self.roi_size,
cv2.BORDER_CONSTANT, value=[0, 0, 0])
height, width, channels = img.shape
vertical_size_roi = round(height / self.num_side_leds)
for i in range(self.num_side_leds):
y_start_pos = self.roi_size + i * vertical_size_roi
y_end_pos = y_start_pos + vertical_size_roi
if i == self.num_side_leds - 1:
y_end_pos = self.roi_size + height
img_with_borders[y_start_pos:y_end_pos, 0:self.roi_size] = self.left_leds_colors[i]
img_with_borders[y_start_pos:y_end_pos, width+self.roi_size:width+2*self.roi_size] = self.right_leds_colors[i]
horizontal_size_roi = floor(width / self.num_top_leds)
for i in range(self.num_top_leds):
x_start_pos = self.roi_size + i * horizontal_size_roi
x_end_pos = x_start_pos + horizontal_size_roi
if i == self.num_top_leds - 1:
x_end_pos = self.roi_size + width
img_with_borders[0:self.roi_size, x_start_pos:x_end_pos] = self.top_leds_colors[i]
for i in range(self.num_bottom_leds):
x_start_pos = self.roi_size + i * horizontal_size_roi
x_end_pos = x_start_pos + horizontal_size_roi
if i == self.num_top_leds - 1:
x_end_pos = self.roi_size + width
img_with_borders[height+self.roi_size:height+2*self.roi_size, x_start_pos:x_end_pos] = self.bottom_led_colors[i]
return img_with_borders
def process_top_border(self, img):
height, width, channels = img.shape
horizontal_size_roi = floor(width / self.num_top_leds)
self.top_leds_colors = []
for i in range(self.num_top_leds):
x_start_pos = i * horizontal_size_roi
x_end_pos = x_start_pos + horizontal_size_roi
if i == self.num_top_leds - 1:
x_end_pos = width
top_roi = img[0:self.roi_size, x_start_pos:x_end_pos]
b, g, r = cv2.split(top_roi)
self.top_leds_colors.append([median(b), median(g), median(r)])
def process_bottom_border(self, img):
height, width, channels = img.shape
horizontal_size_roi = floor(width / self.num_bottom_leds)
self.bottom_led_colors = []
for i in range(self.num_bottom_leds):
x_start_pos = i * horizontal_size_roi
x_end_pos = x_start_pos + horizontal_size_roi
if i == self.num_top_leds - 1:
x_end_pos = width
top_roi = img[height-self.roi_size:height, x_start_pos:x_end_pos]
b, g, r = cv2.split(top_roi)
self.bottom_led_colors.append([median(b), median(g), median(r)])
def process_side_borders(self, img):
height, width, channels = img.shape
vertical_size_roi = floor(height / self.num_side_leds)
self.left_leds_colors = []
self.right_leds_colors = []
for i in range(self.num_side_leds):
y_start_pos = i*vertical_size_roi
y_end_pos = y_start_pos + vertical_size_roi
if i == self.num_side_leds - 1:
y_end_pos = height
left_roi = img[y_start_pos:y_end_pos, 0:self.roi_size]
b,g,r = cv2.split(left_roi)
self.left_leds_colors.append([median(b), median(g), median(r)])
right_roi = img[y_start_pos:y_end_pos, width-self.roi_size:width]
b, g, r = cv2.split(right_roi)
self.right_leds_colors.append([median(b), median(g), median(r)])
if __name__ == '__main__':
s = Sampler()
s.sample_from_webcam()
# arduino = serial.Serial('COM3', 1000000, timeout=.1)
# time.sleep(1)
# #
# arduino.write('r'.encode())
# #
# time.sleep(2)
# #
# # count = 0
#
# while True:
# colors = []
# for x in range(209):
# colors.append(255)
# colors.append(0)
# colors.append(0)
# # s = bytes('@' + chr(255) + chr(0) + chr(0) + '#', "utf-8")
# # print(s)
# # arduino.write(s)
# test = struct.pack('c' + 'B' * len(colors) + 'c', '@'.encode(), *colors, '#'.encode())
# arduino.write(test)
#
# # data = arduino.readline()
# # if data:
# # print(data.decode())
# time.sleep(1)
#
# colors = []
# for x in range(209):
# colors.append(0)
# colors.append(255)
# colors.append(0)
# # s = bytes('@' + chr(255) + chr(0) + chr(0) + '#', "utf-8")
# # print(s)
# # arduino.write(s)
# test = struct.pack('c' + 'B' * len(colors) + 'c', '@'.encode(), *colors, '#'.encode())
# arduino.write(test)
#
# time.sleep(1)
#
# colors = []
# for x in range(209):
# colors.append(0)
# colors.append(0)
# colors.append(255)
# # s = bytes('@' + chr(255) + chr(0) + chr(0) + '#', "utf-8")
# # print(s)
# # arduino.write(s)
# test = struct.pack('c' + 'B' * len(colors) + 'c', '@'.encode(), *colors, '#'.encode())
# arduino.write(test)
#
# time.sleep(1)
# print(i)
# arduino.write("@".encode())
# if count == 0:
# arduino.write(struct.pack('>BBBB', i, int(0), int(0), int(255)))
# else:
# arduino.write(struct.pack('>BBBB', i, int(255), int(0), int(0)))
# arduino.write('#'.encode())
# # data = arduino.readline()
# # if data:
# # print(data.decode())
# arduino.write('$'.encode())
# time.sleep(0.1)
# count = 1