-
Notifications
You must be signed in to change notification settings - Fork 2
/
gbtest.py
837 lines (702 loc) · 33.9 KB
/
gbtest.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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
import picamera
from queue import Empty
from time import time, sleep
import numpy as np
import cv2
import imutils
import math
import os
import serial
import imageio
import logging
import signal
from qlogging import PUBLogger, SUBLogger
from threading import Thread
USE_MULTIPROCESSING = True
USE_THREADING = not USE_MULTIPROCESSING
DEBUG_FRAMES = True
if USE_MULTIPROCESSING is True and \
USE_THREADING is False:
from multiprocessing.managers import BaseManager, BaseProxy, NamespaceProxy
from multiprocessing import Manager
elif USE_MULTIPROCESSING is False and \
USE_THREADING is True:
from queue import Queue
class MyOutput():
'''
Class used by PiCamera object to record the frames coming from the camera.
The PiCamera class assumes there is a write method (and optionally a flush method).
Since we've got to pass the data to the image processor by the means of a queue.
Check https://picamera.readthedocs.io/en/release-1.13/recipes2.html#custom-outputs
'''
def __init__(self, queue, resolution, state):
'''
Initialize MyOutput object.
Init params:
queue -- The queue onto which raw frames are pushed to.
resolution -- The resolution of the pushed frames. 2-element tuple signifying (width, height).
state -- A dictionary containing important markers about the current frame: time, red/blue/green GB; TL;DR metadata.
Frames won't get pushed into the queue if state is set to None.
'''
self._queue = queue
self._resolution = resolution[::-1] + (3,)
self.state = state
def write(self, s):
'''
Write method required by PiCamera class.
Keyword params:
s -- A bytes object containing the actual image that got captured.
'''
self._buffer = np.frombuffer(s, np.uint8).reshape(*self._resolution)
self.flush()
def flush(self):
'''
Flush method that needs to be called to push the data to the queue.
Apart from being called by write method every time, it also gets called at the end of the recording by the PiCamera object.
'''
if self._buffer is not None and \
self.state is not None:
self._queue.put([
self._buffer,
self._state
], block = False)
self._buffer = None
@property
def state(self):
'''
Returns the dictionary containing important markers about the current frame(s).
'''
return self._state
@state.setter
def state(self, state):
'''
Set important markers about the current frame(s).
Contains information about the current environment that the camera "sees".
The state variable is necessary for validating the data later on.
'''
self._state = state
class CameraSource(Thread):
'''
Class used to gather BGR frames from the PiCamera on a separate thread while pushing them
in a queue that gets consumed on the fly by a process which analyses the data.
Only one instance of this class can exist at a moment.
The format of each pushed object into the queue is tailored to the GiggleBotQAValidation class. That is, each object (list)
contains a (captured) frame and metadata about it stored as a dictionary. This class can be adapted to accept any format for
these pushed objects.
On how to get a square sync signal from the (not helpful though): https://www.raspberrypi.org/forums/viewtopic.php?t=190314
'''
def __init__(self,
queue,
camera_settings,
log_queue,
state = {},
output_resize = (480, 272),
frame_callback = None,
dry_run = False,
log_level = logging.DEBUG):
'''
Initialise the CameraSource object.
Init params:
queue -- The queue into which frames are pushed in one by one. The queue must be infinite in size.
camera_settings -- Settings of the PiCamera that can be accessed as an attribute after having initialized the object.
state -- A dictionary containing important markers necessary for validating the data. Not mandatory if you've passed an object for frame_callback param.
output_resize -- The actual resolution of the frames that get pushed into the queue. Must be >= than what's specified in camera_settings param. 2-element tuple of (width, height).
frame_callback -- Optional object to use for preparing the next frame for whatever it's needed. Must have an update method which returns a state dictionary.
dry_run -- Set to True to test the PiCamera. Cannot start the thread with this setting on as the camera is closed as soon as this constructor reaches its end.
log_queue -- A queue to push logs to. Uses the implementation found in advanced_logging.
log_level -- A selection from one of the 5 levels: DEBUG, INFO, WARN, ERROR, CRITICAL.
The constructor waits 2 seconds for the camera to initialize after the PiCamera object is created. This is a suggestion found in PiCamera's documentation.
The output_resize param has to be a 2-element tuple containing the width and height of the recorded frames.
'''
super(CameraSource, self).__init__(group = None, target = None, name = 'CameraSource')
self._queue = queue
self._camera_settings = camera_settings
self._output_resize = output_resize
self._frame_callback = frame_callback
if len(state) == 0:
state = None
self._output = MyOutput(queue, output_resize, state = state)
self._logger = PUBLogger(log_queue, log_level)
self.pause = False
self._stop_thread = False
self._terminated = False
self._failed = False
self._logger.info('initializing picamera..')
try:
self.camera = picamera.PiCamera()
self.camera.start_preview()
for setting in list(camera_settings.keys()):
setattr(self.camera, setting, camera_settings[setting])
sleep(2.0)
self._logger.debug('CameraSource object created')
if dry_run is True:
self.camera.close()
except picamera.PiCameraError as error:
self._logger.critical(str(error), exc_info = 1)
self._failed = True
def run(self):
'''
This method must not be called from the user space. This gets called by the start method,
which in turn, is used to trigger this method.
This method continuously pushes frames into the queue. To stop it, call the stop method.
'''
try:
frame_period = 1.0 / self.camera.framerate
while self._stop_thread is False:
# change the color of the LEDs
if self._frame_callback is not None and self.pause is False:
try:
self._output.state = self._frame_callback.update()
except Exception as error:
self._logger.error(str(error), exc_info = 1)
self._frame_callback.initialize()
if self._frame_callback.failed() is True:
self._logger.critical('failed reinitializing the neopixels')
raise Exception('failed reinitializing the neopixels')
else:
self._logger.info('successfully reinitialized the neopixels from certain death')
continue
# make sure the next capture is indeed what we want
sleep(frame_period)
# capture the frame
try:
if self.pause is False:
if self._output.state is None:
self._logger.debug('not capturing frame from camera due to state being set to None')
elif 'id' in self._output.state:
self._logger.debug('capturing frame from picamera with id=' + str(self._output.state['id']))
self.camera.capture_sequence([self._output],
format = 'bgr',
resize = self._output_resize,
use_video_port = True)
self._logger.debug('captured frame with id=' + str(self._output.state['id']))
except (picamera.exc.PiCameraError, Exception) as error:
self._logger.critical(str(error), exc_info = 1)
break
except Exception as error:
self._logger.critical(str(error), exc_info = 1)
self._failed = True
finally:
self.camera.close()
self._terminated = True
self._logger.info('stopped collecting picamera frames')
def stop(self, blocking = True):
'''
Stops the run method that got called by start method.
Keyword params:
blocking -- Boolean to specify if it awaits for the run method to stop.
'''
self._logger.debug('got a stop command')
self._stop_thread = True
if blocking is True:
while self._terminated is False:
sleep(0.001)
self._logger.debug('terminated')
@property
def failed(self):
'''
Returns True if instantiating the PiCamera has failed (when calling the constructor) or
if something critically bad happens during the operation of the camera.
'''
return self._failed
@property
def state(self):
return self._output.state
@state.setter
def state(self, state):
self._output.state = state
@property
def pause(self):
'''
Checks if recording is paused or not.
'''
return self._pause
@pause.setter
def pause(self, val):
'''
Pauses the recording process or resumes it.
Keyword params:
val -- True to pause it and false to resume it.
'''
self._logger.info('set pause to ' + str(val))
self._pause = val
class GiggleBotQAValidation(Thread):
'''
Instantiate one or multiple objects of this class to process the images coming from the camera (by passing down
the queue object to every one of these objects).
The result of this process is a simple binary answer (yes/no) if the GiggleBot board has passed the LED test.
Keep in mind that the metadata dictionary that this object receives through the process_queue param
must have a single key called 'leds' and a string associated to it representing the color
of the LEDs that should be detected. The color string must be present in the config['color-boundaries'] list.
'''
def __init__(self,
process_queue,
config,
log_queue,
stop_when_empty = False,
stop_when_failed = False,
save_images_for_debugging = False,
path_for_images = './',
log_level = logging.DEBUG):
'''
Init params:
process_queue -- Queue from which to extract PiCamera frames and metadata.
config -- A dictionary containing settings on how to process the incoming frames.
stop_when_empty -- Flag to stop the thread when there are no more elements in the queue.
stop_when_failed -- Flag to stop the thread when the QA test has failed.
save_images_for_debugging -- To save different frames/masks/processed stuff to the disk for debugging purposes.
path_for_images -- Location where to save the frames/masks/processed stuff for debugging purposes.
log_queue -- A queue to push logs to. Uses the implementation found in advanced_logging.
log_level -- A selection from one of the 5 levels: DEBUG, INFO, WARN, ERROR, CRITICAL.
An example config dictionary should look like this:
{
'color-boundaries': [
('red', [0, 165, 128], [15, 255, 255]),
('red', [165, 165, 128], [179, 255, 255]),
('green', [35, 165, 128], [75, 255, 255]),
('blue', [90, 165, 128], [133, 255, 255])
], # HSV colors
'leds': 7,
'acceptable-leading-color-ratio': 0.95, # 95% of the detected color is the actual one present
'acceptable-ratio-between-most-popular-colors': 0.05, # 2nd most predominant / most predominant color
'gaussian-blur': (5,5),
'binary-threshold': 200,
'minimum-circle-lines': 9,
'maximum-circle-lines': 22,
'minimum-circle-size': 101,
'scale-2nd-circle': 1.7
}
'''
super(GiggleBotQAValidation, self).__init__(group = None, target = None, name = 'GiggleBotQAValidation')
self._procq = process_queue
self._stop_when_empty = stop_when_empty
self._stop_when_failed = stop_when_failed
self._save_images = save_images_for_debugging
self._path_images = path_for_images
self._stop_thread = False
self._terminated = False
self._logger = PUBLogger(log_queue, log_level)
self._boundaries = config['color-boundaries']
self._no_leds = config['leds']
self._acceptable_leading_color_ratio = config['acceptable-leading-color-ratio']
self._acceptable_ratio_between_most_popular_colors = config['acceptable-ratio-between-most-popular-colors']
self._gaussian_blur = tuple(config['gaussian-blur'])
self._binary_threshold = config['binary-threshold']
self._minimum_circle_lines = config['minimum-circle-lines']
self._maximum_circle_lines = config['maximum-circle-lines']
self._minimum_circle_size = config['minimum-circle-size']
self._scale_2nd_circle = config['scale-2nd-circle']
self.failed_qa = False # check this to see when the test fails
self.stats = {
'start_time': None,
'last_update': None
} # use this to calculate how much time this process worked for
# the 'last_update' value updates only when failed_qa is set to False
self._logger.info('GiggleBotQAValidation object created')
def run(self):
'''
This method must not be called from the user space. This gets called by the start method,
which in turn, is used to trigger this method.
This method continuously processes frames coming down the queue pipe. To stop it, call the stop method.
'''
while self._stop_thread is False:
try:
if self._stop_when_failed is True and self.failed_qa is True:
self._logger.warn('failed qa test and further incoming frames won\'t get processed')
break
frame, metadata = self._procq.get_nowait()
if self.failed_qa is False:
self._logger.debug('received valid frame')
try:
self._do_qa_on_frame(frame, metadata)
except Exception as msg:
self._logger.error(str(msg), exc_info = 1)
self.failed_qa = True
except Empty:
if self._stop_when_empty is True and self._procq.qsize() == 0:
self._logger.warn('queue empty; ending the operation')
break
finally:
sleep(0.010)
self._terminated = True
def stop(self, blocking = True):
'''
Stops the run method that got called by start method.
Keyword params:
blocking -- Boolean to specify if it awaits for the run method to stop.
'''
self._logger.debug('got a stop command')
self._stop_thread = True
if blocking is True:
while self._terminated is False:
sleep(0.001)
self._logger.debug('terminated')
def _do_qa_on_frame(self, frame, metadata):
'''
The method which gets repeatedly called by the run method.
Keyword params:
frame -- The BGR frame that needs to be analyzed.
metadata -- Data about the current frame that's used for validation.
The method determines if the test has failed so far or if it still keeps on going. This method
also calls the _do_frame_analysis method.
'''
if self.stats['start_time'] is None:
self.stats['start_time'] = time()
self.stats['last_update'] = time()
color_dist, leds = self._do_frame_analysis(frame, id = metadata['id'])
self._logger.debug('color distribution = ' + str(color_dist) + ', no LEDs = ' + str(leds) + ' w/ target = ' + metadata['leds'])
if self._no_leds != leds:
self.failed_qa = True
self._logger.warn('failed qa test ' + str(self._no_leds) + '!=' + str(leds))
return
pixels = list(color_dist.values())
primary_no_pixels = color_dist[metadata['leds']] + 1
total_pixels = sum(pixels)
if total_pixels <= 100:
self.failed_qa = True
self._logger.warn('failed because under 100 pixels have been detected')
return
if primary_no_pixels / total_pixels <= self._acceptable_leading_color_ratio:
self.failed_qa = True
self._logger.warn('failed qa on primary color test')
return
# we don't have to worry about duplicates, because if it were the case,
# this would anyway not get executed since it would stop at the above instructions
pixels.remove(max(pixels))
highest_2nd = max(pixels)
if highest_2nd / primary_no_pixels > self._acceptable_ratio_between_most_popular_colors:
self.failed_qa = True
self._logger.warn('failed qa on secondary/primary color test')
return
self._logger.debug('passed qa on frame (id=' + str(metadata['id']) + ') with ' + str(leds) + ' LEDs detected, ' \
+ str(100 * primary_no_pixels / total_pixels) + '% leading color majority, '\
+ str(100 * highest_2nd / primary_no_pixels) + '% ratio between 2nd/1st')
def _do_frame_analysis(self, frame, id):
'''
Returns the color percentage for each one listed in _boundaries dictionary and
the number of detected LEDs on the GiggleBot.
The 1st element returned is a dictionary, where each key is the color we want to detect and each
value represents the number of pixels that fit into that category.
The 2nd element returned is the number of detected LEDs.
Keyword params:
frame -- The frame to be analyzed.
id -- The id of the frame in case we want to have a way to do identification when debugging.
'''
(height, width) = frame.shape[0:2]
self._logger.debug('received ' + str(width) + 'x' + str(height) + ' frame')
if self._save_images is True:
self._logger.debug('saving frame to ' + 'images/' + str(id) + '.jpeg')
os.makedirs(self._path_images + 'images/', exist_ok=True)
cv2.imwrite(self._path_images + 'images/' + str(id) + '.jpeg', frame)
# convert to grayscale, do a binary threshold and find the contours
self._logger.debug('convert frame to grayscale')
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
if self._save_images is True:
self._logger.debug('saving grayscale frame to ' + 'grayscales/' + str(id) + '.jpeg')
os.makedirs(self._path_images + 'grayscales/', exist_ok=True)
cv2.imwrite(self._path_images + 'grayscales/' + str(id) + '.jpeg', gray)
self._logger.debug('apply gaussian blur with ' + str(self._gaussian_blur) + ' filter')
blurred = cv2.GaussianBlur(gray, self._gaussian_blur, 0)
if self._save_images is True:
self._logger.debug('saving gaussian-blurred frame to ' + 'gaussian_blur/' + str(id) + '.jpeg')
os.makedirs(self._path_images + 'gaussian_blur/', exist_ok=True)
cv2.imwrite(self._path_images + 'gaussian_blur/' + str(id) + '.jpeg', blurred)
self._logger.debug('apply binary threshold of ' + str(self._binary_threshold) + ' to frame')
thresh = cv2.threshold(blurred, self._binary_threshold, 255, cv2.THRESH_BINARY)
if self._save_images is True:
self._logger.debug('saving thresholded frame to '+ 'thresholded/' + str(id) + '.jpeg')
os.makedirs(self._path_images + 'thresholded/', exist_ok=True)
cv2.imwrite(self._path_images + 'thresholded/' + str(id) + '.jpeg', thresh[1])
contours = cv2.findContours(thresh[1].copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
contours = contours[0] if imutils.is_cv2() else contours[1]
# filter out the "bad" contours
# keep the enclosed shapes that resemble a circle
# and that have a size appropriate to that of a GB LED
contours_list = []
radiuses = []
centers = []
areas = []
for contour in contours:
perimeter = cv2.arcLength(contour, True)
approx = cv2.approxPolyDP(contour, 0.01 * perimeter, True, True)
area = cv2.contourArea(contour)
if (len(approx) >= self._minimum_circle_lines) and (len(approx) <= self._maximum_circle_lines) and area >= self._minimum_circle_size:
contours_list.append(contour)
radiuses.append(perimeter / math.pi / 2)
M = cv2.moments(contour)
cX = int(M['m10'] / M['m00'])
cY = int(M['m01'] / M['m00'])
centers.append((cX, cY))
areas.append(area)
if len(radiuses) == 0:
self._logger.warn('no circles/LEDs found')
return None, 0
else:
self._logger.debug('found ' + str(len(radiuses)) + ' LEDs')
# determine the mean radius of the detected circles
# and calculate a bigger one related to the original
scale = self._scale_2nd_circle
intern_radius = np.mean(radiuses)
extern_radius = intern_radius * scale
intern_radius = math.ceil(intern_radius)
extern_radius = math.ceil(extern_radius)
extern_diam = extern_radius * 2
self._logger.debug('following circle sizes found = ' + str(areas))
self._logger.debug('avg circle radius = ' + str(intern_radius))
# draw the bigger circles and remove the inner ones by setting them to black
mask = np.zeros((height, width, 3), np.uint8)
for center in centers:
cv2.circle(mask, center, extern_radius, (255, 255, 255), -1)
cv2.circle(mask, center, intern_radius, (0, 0, 0), -1)
if self._save_images is True:
os.makedirs(self._path_images + 'masks/', exist_ok=True)
cv2.imwrite(self._path_images + 'masks/' + str(id) + '.jpeg', mask)
# use the previous mask to get the relevant pixels from the orignal frame
out = np.zeros((height, width, 3), np.uint8)
cv2.bitwise_and(frame, mask, out)
# writearray(out[:,:,[2,1,0]], "processed/img_{}.png".format(self.counter), 'RGB')
# self.counter += 1
if self._save_images is True:
self._logger.debug('saving masked frame to '+ 'masked/' + str(id) + '.jpeg')
os.makedirs(self._path_images + 'masked/', exist_ok=True)
cv2.imwrite(self._path_images + 'masked/' + str(id) + '.jpeg', out)
# convert to HSV to be able to categorize the colors based on _boundaries dictionary
cv2.cvtColor(out, cv2.COLOR_BGR2HSV, out)
transpose_list = list(zip(*self._boundaries))
colors = {}
for elem in transpose_list[0]:
colors[elem] = 0
for (color, lower, upper) in self._boundaries:
lower = np.array(lower, dtype = np.uint8)
upper = np.array(upper, dtype = np.uint8)
mask = cv2.inRange(out, lower, upper)
filtered_channel = cv2.bitwise_and(out, out, mask = mask)
gray_channel = cv2.cvtColor(filtered_channel, cv2.COLOR_BGR2GRAY)
detected_pixels = cv2.countNonZero(gray_channel)
colors[color] += detected_pixels
return colors, len(contours_list)
def join(self):
'''
Join the main thread when the queue gets empty or if the QA test fails.
Only appliable if either stop_when_empty or stop_when_failed parameters for the constructor
are set to True.
'''
self._logger.debug('called join method')
if self._stop_when_empty is True or self._stop_when_failed is True:
while self._procq.qsize() > 0 and self.failed_qa is False and self._terminated is False:
sleep(0.001)
self._logger.info('terminated')
@property
def queue_empty(self):
'''
Returns True if the queue is empty, False otherwise.
'''
return self._procq.qsize() == 0
class LEDChanger():
'''
Changes the color of the GB's LEDs by calling update method. A new id is returned
with each call to update method. If a subsequent call is made too fast, the LEDs
might not switch colors, but the ID will still get changed.
Requires a microbit.
'''
def __init__(self, port, cooldown, color_config):
'''
Init params:
port -- The serial port to which the microbit is connected to.
cooldown -- How much time needs to pass before being able to switch colors again.
color_config -- A dictionary containing pairs of color commands (the actual cmd that's sent
to the MB) and color names (strings that are to be recognized by GiggleBotQAValidation class).
color_config must have this form:
{
'color-codes': [b'r', b'g', b'b'],
'color-names': ['red', 'green', 'blue']
}
'''
self._port = port
self._counter = 0
self._cooldown = cooldown
self._last_update = 0
self._colors = color_config['color-codes']
self._color_names = color_config['color-names']
def update(self):
now_time = time()
if now_time - self._last_update >= self._cooldown:
with serial.Serial(self._port, 115200) as ser:
ser.write(self._colors[self._counter % 3])
ser.flush()
self._state = {
'leds': self._color_names[self._counter % 3],
'id': self._counter
}
self._counter += 1
self._last_update = time()
return self._state
if USE_MULTIPROCESSING is True:
class LEDChangerProxy(NamespaceProxy):
_exposed_ = ('__getattribute__', '__setattr__', '__delattr__', 'update')
def update(self):
callmethod = object.__getattribute__(self, '_callmethod')
return callmethod('update')
def prepare_manager():
'''
Configures a multiprocessing manager by registering the classes of this module,
along with adding the required setters and getters for it in order to easily access
the attributes of a given class attributes.
Also configures a manager for shared objects between all proxy-managed instances. Used for queues.
The 1st returned argument is the manager for the classes' objects and the 2nd one is the manager
for shared objects.
To access an attribute of a given class, do this:
obj.set_attr('name_of_attribute', value_to_set)
obj.get_attr('name_of_attribute')
That's opposed to accessing them like obj.name_of_attribute.
'''
class MyManager(BaseManager):
pass
def create_setter_getters(cls):
'''
Creates set_attr and get_attr public methods for cls class.
The problem with the multiprocessing managers is that the proxy can only reach methods, but not attributes.
To fix this, we can create 2 proxy methods to interact with the said attributes.
'''
def set_attr(self, attr, val):
setattr(self, attr, val)
def get_attr(self, attr):
return getattr(self, attr)
setattr(cls, set_attr.__name__, set_attr)
setattr(cls, get_attr.__name__, get_attr)
return cls
MyManager.register(CameraSource.__name__, create_setter_getters(CameraSource))
MyManager.register(GiggleBotQAValidation.__name__, create_setter_getters(GiggleBotQAValidation))
MyManager.register(LEDChanger.__name__, LEDChanger, LEDChangerProxy)
sync_master = Manager()
return (MyManager, sync_master)
def _do_multiprocessing(settings):
(MyManager, sync_manager) = prepare_manager()
camera_settings = settings['camera']
qa_settings = settings['qa']
led_switcher_settings = settings['led-switcher']
with MyManager() as manager:
log_queue = sync_manager.Queue(0)
logger_sub = SUBLogger(log_queue, stdout=True, fileout=False, level=logging.DEBUG)
logger_sub.start()
logger = PUBLogger(log_queue)
queue = sync_manager.Queue(0)
led_changer = manager.LEDChanger('/dev/ttyACM0', 0.025, led_switcher_settings)
producer = manager.CameraSource(queue, camera_settings, log_queue, frame_callback = led_changer)
consumers = []
for i in range(2):
c = manager.GiggleBotQAValidation(queue,
qa_settings,
log_queue,
stop_when_empty = False,
stop_when_failed = False,
save_images_for_debugging = DEBUG_FRAMES,
path_for_images = './test/')
c.start()
consumers.append(c)
producer.start()
start = time()
end = start + 10
while time() <= end:
if True in list(map(lambda x: x.get_attr('failed_qa'), consumers)):
break
sleep(0.001)
producer.stop()
while True:
if True in list(map(lambda x: x.get_attr('failed_qa'), consumers)):
break
if len(list(map(lambda x: x.get_attr('failed_qa'), consumers))) == len(consumers):
break
sleep(0.001)
for c in consumers:
c.stop()
c.join()
logger_sub.stop()
def _do_threading(settings):
image_queue = Queue()
log_queue = Queue()
logger_sub = SUBLogger(log_queue, stdout=True, fileout=False, level=logging.DEBUG)
logger_sub.start()
logger = PUBLogger(log_queue)
camera_settings = settings['camera']
qa_settings = settings['qa']
led_switcher_settings = settings['led-switcher']
led_changer = LEDChanger('/dev/ttyACM0', 0.025, led_switcher_settings)
producer = CameraSource(image_queue, camera_settings, log_queue, frame_callback = led_changer)
consumer = GiggleBotQAValidation(image_queue,
qa_settings,
log_queue,
stop_when_empty = True,
save_images_for_debugging = True,
path_for_images = './test/')
producer.start()
sleep(10)
consumer.start()
producer.stop()
# wait for the thread to finish to be able to create the GIFs
while consumer.is_alive():
sleep(0.001)
# all the rest is for generating the GIFs from the collected JPEGs
if DEBUG_FRAMES is True:
list_dirs = []
os.chdir('./test/')
for entry in os.scandir('./'):
if entry.is_dir():
list_dirs.append(entry.path)
os.makedirs('./gifs', exist_ok = True)
images = {}
for directory in list_dirs:
print('gathering images from ' + directory + ' dir ..')
for file_name in os.listdir(directory):
if file_name.endswith('.jpeg'):
file_path = os.path.join(directory, file_name)
if directory[2:] not in images:
images[directory[2:]] = []
else:
images[directory[2:]].append(imageio.imread(file_path))
for key, values in images.items():
print('saving ' + key + '.gif ..')
imageio.mimsave('./gifs/' + key + '.gif', values)
os.chdir('..')
logger_sub.stop()
if __name__ == "__main__":
os.system('taskset -p 0xff %d' % os.getpid())
settings = {
'camera' : {
'iso': 100,
'shutter_speed': 3000,
'preview_alpha': 255,
'resolution': (480, 272),
'sensor_mode': 1,
'rotation': 0,
'framerate': 40,
'brightness': 50,
'awb_mode': 'off',
'awb_gains': 1.5
},
'qa' : {
'color-boundaries': [
('red', [0, 165, 128], [15, 255, 255]),
('red', [165, 165, 128], [179, 255, 255]),
('green', [35, 165, 128], [75, 255, 255]),
('blue', [90, 165, 128], [133, 255, 255])
], # HSV colors
'leds': 7,
'acceptable-leading-color-ratio': 0.95, # 95% of the detected color is the actual one present
'acceptable-ratio-between-most-popular-colors': 0.05, # 2nd most predominant / most predominant color
'gaussian-blur': (5,5),
'binary-threshold': 200,
'minimum-circle-lines': 5,
'maximum-circle-lines': 45,
'minimum-circle-size': 101,
'scale-2nd-circle': 1.7
},
'led-switcher': {
'color-codes': [b'r', b'g', b'b'],
'color-names': ['red', 'green', 'blue']
}
}
if USE_MULTIPROCESSING is True:
_do_multiprocessing(settings)
if USE_THREADING is True:
_do_threading(settings)