-
Notifications
You must be signed in to change notification settings - Fork 0
/
led_determine.py
executable file
·372 lines (279 loc) · 9 KB
/
led_determine.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
#!/usr/bin/python3
# SPDX-License-Identifier: GPL-2.0-or-later
import cv2
import subprocess
import time
from enum import Enum
import sys
import glob
import socket
import os
import yaml
import pathlib
# Theory
# - Take N samples as fast as we can
# - Calculate the intensity for each of the regions over the N samples.
# - Using previous samples of known intensity states in each region, determine if a LED is illuminated or not.
# - If illuminated, simply compare RED and GREEN RGB values of sample to determine color.
# - Walk through all samples to see if the LED state is consistent or toggling on/off which indicates
# blinking. If the LED is blinking == LOCATE enabled for that region
#
# Steady Green == NORMAL
# Steady Pinkish/Red == FAILURE
# Blink Green == LOCATE
# Blink Pink == LOCATE & FAILURE
# The different LED regions in the mask, (Upper Left, Lower Right)
# Note: These are a small region of the LED locations
# TODO: move this to the config.yaml
REG_0 = ((463, 680), (464, 685))
REG_1 = ((542, 706), (546, 710))
REG_2 = ((682, 737), (688, 742))
REG_3 = ((1123, 748), (1129, 754))
REGIONS = (REG_0, REG_1, REG_2, REG_3)
# Define to dump out debug
DEBUG = bool(os.getenv("LED_DETERMINE_CV_DEBUG", ""))
__location__ = os.path.realpath(
os.path.join(os.getcwd(), os.path.dirname(__file__)))
FILE_PREFIX = "/tmp/led_determine_cv_"
class LEDState(Enum):
OFF = 1
NORM = 2
LOCATE = 3
FAULT = 4
LOCATE_FAULT = 5
UNKNOWN = 6
@staticmethod
def y():
return "{OFF: 1, NORM: 2, LOCATE: 3, FAULT: 4, LOCATE_FAULT: 5, UNKNOWN: 6}"
class LED:
def __init__(self, rgb, intensity, fn):
self._rgb = rgb
self._intensity = intensity
self._fn = fn
def is_red(self):
return self._rgb[0] > self._rgb[1]
def is_green(self):
return self._rgb[1] > self._rgb[0]
def intensity(self):
return self._intensity
def __str__(self):
return f"({self._rgb[0]}, {self._rgb[1]}, {self._rgb[2]}), {self._intensity}, {self._fn}"
def fn(self):
return self._fn
def debug(msg):
if DEBUG:
print(msg)
def find_non_black_average(img_data, r):
"""
Returns (R, G, B) average for region
:param img_data:
:param r: Region of img_data
:return: (R, G, B)
"""
r_sum = 0
g_sum = 0
b_sum = 0
pixel_samples = 0
(upper_left, lower_right) = r
for x in range(upper_left[0], lower_right[0]):
for y in range(upper_left[1], lower_right[1]):
b, g, r = img_data[y, x]
if b != 0 or g != 0 or r != 0:
pixel_samples += 1
r_sum += r
g_sum += g
b_sum += b
return (r_sum // pixel_samples, g_sum // pixel_samples,
b_sum // pixel_samples)
def find_intensity(img_data, r):
"""
Returns 0-255 representing intensity
:param img_data:
:param r: Region of img_data
:return: 0-255 intensity
"""
intensity_sum = 0
pixed_samples = 0
(upper_left, lower_right) = r
for x in range(upper_left[0], lower_right[0]):
for y in range(upper_left[1], lower_right[1]):
intensity_sum += img_data[y, x]
pixed_samples += 1
return (intensity_sum // pixed_samples)
def region_colors(img_data):
rc = []
for r in REGIONS:
rc.append(find_non_black_average(img_data, r))
return rc
def region_intensity(img_data):
rc = []
gray_scale = cv2.cvtColor(img_data, cv2.COLOR_BGR2GRAY)
for r in REGIONS:
rc.append(find_intensity(gray_scale, r))
return rc
def acquire_image(device):
image_name = FILE_PREFIX + time.strftime("%Y%m%d-%H%M%S") + ".jpg"
if "megadeth" in socket.gethostname():
# local
cmd = [
"fswebcam", "-q", "-d", device, "-r", "1920x1080", "--jpeg", "100",
"-S", "20", image_name
]
subprocess.run(cmd, check=True, capture_output=False, shell=False)
else:
# remote
cmd = f'ssh root@megadeth "fswebcam -q -d {device} -r 1920x1080 --jpeg 100 -S 20 -" > {image_name}'
subprocess.run([cmd], check=True, capture_output=False, shell=True)
return image_name
def process_image(image_file):
return cv2.imread(image_file)
def get_region_numbers(count=10):
region_numbers = []
for _ in range(0, count):
fn = acquire_image("/dev/video0")
q = process_image(fn)
colors = region_colors(q)
intensity = region_intensity(q)
leds = []
for idx in range(len(colors)):
leds.append(LED(colors[idx], intensity[idx], fn))
region_numbers.append(leds)
return region_numbers
def min_max_calc(led_samples):
"""
Returns a tuple of intensity (min, max)
:param led_samples:
:return:
"""
debug(f"samples = {led_samples}")
min_max = [255, 0]
for ls in led_samples:
if ls < min_max[0]:
min_max[0] = ls
if ls > min_max[1]:
min_max[1] = ls
debug(f"min_max = {min_max}")
return min_max
def led_state_mm(sample, on_threshold):
d = 10
debug(f"{sample} - {on_threshold}")
if sample.intensity() >= (on_threshold - d):
if sample.is_red():
return LEDState.FAULT
else:
return LEDState.NORM
else:
return LEDState.OFF
def build_numbers():
with open(os.path.join(__location__, "data.learn"), "r") as FH:
data = FH.readlines()
db = [
dict(R=[], G=[]),
dict(R=[], G=[]),
dict(R=[], G=[]),
dict(R=[], G=[])
]
for line in data:
ls = line.strip()
(t, region, it) = ls.split(",")
region = int(region)
intensity = int(it)
db[region][t].append(intensity)
rc = [
dict(R=[], G=[]),
dict(R=[], G=[]),
dict(R=[], G=[]),
dict(R=[], G=[])
]
for r in range(len(REGIONS)):
rc[r]["G"] = min_max_calc(db[r]["G"])
rc[r]["R"] = min_max_calc(db[r]["R"])
for i, v in enumerate(rc):
debug(f"Region[{i}]['R'] = {v['R']}")
debug(f"Region[{i}]['G'] = {v['G']}")
region_mins = []
for i, v in enumerate(rc):
r_min = v['R'][0]
g_min = v['G'][0]
region_mins.append(min(r_min, g_min))
debug(f"Region mins = {region_mins}")
return region_mins
def observed(led_states):
norm = 0
fault = 0
unknown = 0
for ls in led_states:
if ls == LEDState.NORM:
norm += 1
elif ls == LEDState.FAULT:
fault += 1
else:
unknown += 1
if unknown > 0:
# We likely have blinking
if norm > fault:
return LEDState.LOCATE
if fault > norm:
return LEDState.LOCATE_FAULT
else:
if norm > fault:
return LEDState.NORM
if fault > norm:
return LEDState.FAULT
return LEDState.UNKNOWN
def interpret(r):
rc = []
for i in range(len(REGIONS)):
rt = [sublist[i] for sublist in r]
rc.append(observed(rt))
return rc
def delete_captures():
for f in glob.glob(FILE_PREFIX + "*"):
pathlib.Path.unlink(f)
if __name__ == "__main__":
# File with wwn for each 'slot' we are monitoring via USB camera
with open(os.path.join(__location__, "config.yaml"), "r") as FH:
slot_ids = yaml.load(FH, Loader=yaml.Loader)
if len(sys.argv) > 1 and len(sys.argv) != 6:
print(
f"syntax: {sys.argv[0]} collect [G|R][4], eg. led_determine.py collect G G G G"
)
sys.exit(1)
if len(sys.argv) == 1:
# Open the file with the intensity values for each of the regions with known settings
# and build the needed data to interpret the LED statuses.
mmr = build_numbers()
samples = get_region_numbers(count=10)
results = []
for s in samples:
sr = []
for i, v in enumerate(s):
sr.append(led_state_mm(v, mmr[i]))
debug(f"mm result = {sr}")
results.append(sr)
# We have a list of states for each of the sample, we now need to determine what
# the LEDs are doing steady color, or flashing.
results = interpret(results)
if LEDState.UNKNOWN not in results:
delete_captures()
output = dict(statekey=LEDState.y(), results=[])
for i, v in enumerate(results):
output['results'].append(
dict(wwn=slot_ids['slots'][i], state=v.value))
print(yaml.dump(output, Dumper=yaml.Dumper))
sys.exit(0)
elif sys.argv[1] == 'collect':
# Loop collecting data and dump to stdout
agv = sys.argv[2:6]
for v in agv:
if v not in ["G", "R"]:
print(f"Invalid expected state for {v}")
numbers = get_region_numbers(count=30)
delete_captures()
for c in numbers:
for i, v in enumerate(c):
print(f"{agv[i]}, {i}, {v.intensity()}")
sys.exit(0)
else:
print(f"Invalid option {sys.argv[1]}")
sys.exit(2)