-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathgeneric_filter.py
106 lines (87 loc) · 3.22 KB
/
generic_filter.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
import logging
import sys
from cli_args import GRPC_PORT_DEFAULT
from constants import MINIMUM_PIXELS_DEFAULT, HSV_RANGE_DEFAULT
from contour_finder import ContourFinder
from location_server import LocationServer
from utils import is_raspi
# I tried to include this in the constructor and make it depedent on self.__leds, but it does not work
if is_raspi():
from blinkt import set_pixel, show
logger = logging.getLogger(__name__)
class GenericFilter(object):
def __init__(self,
tracker,
bgr_color,
hsv_range=HSV_RANGE_DEFAULT,
minimum_pixels=MINIMUM_PIXELS_DEFAULT,
grpc_port=GRPC_PORT_DEFAULT,
leds=False,
display_text=False,
draw_contour=False,
draw_box=False,
vertical_lines=False,
horizontal_lines=False,
predicate=None):
self.tracker = tracker
self.leds = leds
self.display_text = display_text
self.draw_contour = draw_contour
self.draw_box = draw_box
self.vertical_lines = vertical_lines
self.horizontal_lines = horizontal_lines
self.predicate = predicate
self._prev_x, self._prev_y = -1, -1
self.height, self.width = -1, -1
self.contours = None
self.contour_finder = ContourFinder(bgr_color, hsv_range, minimum_pixels)
self.location_server = LocationServer(grpc_port)
@property
def prev_x(self):
return self._prev_x
@prev_x.setter
def prev_x(self, val):
self._prev_x = val
@property
def prev_y(self):
return self._prev_y
@prev_y.setter
def prev_y(self, val):
self._prev_y = val
@property
def middle_inc(self):
# The middle margin calculation is based on % of width for horizontal and vertical boundary
mid_x = self.width / 2
middle_pct = (float(self.tracker.middle_percent) / 100.0) / 2
return int(mid_x * middle_pct)
def start(self):
try:
self.location_server.start()
except BaseException as e:
logger.error("Unable to start location server [{0}]".format(e), exc_info=True)
sys.exit(1)
if self.leds:
self.clear_leds()
def stop(self):
if self.leds:
self.clear_leds()
self.location_server.stop()
def reset(self):
self.prev_x, self.prev_y = -1, -1
def reset_data(self):
raise Exception("Should be implemented by sub-class")
def process_image(self, image):
raise Exception("Should be implemented by sub-class")
def publish_data(self):
raise Exception("Should be implemented by sub-class")
def markup_image(self, image):
raise Exception("Should be implemented by sub-class")
def set_leds(self, left_color, right_color):
if is_raspi():
for i in range(0, 4):
set_pixel(i, left_color[2], left_color[1], left_color[0], brightness=0.05)
for i in range(4, 8):
set_pixel(i, right_color[2], right_color[1], right_color[0], brightness=0.05)
show()
def clear_leds(self):
self.set_leds([0, 0, 0], [0, 0, 0])