-
Notifications
You must be signed in to change notification settings - Fork 3
/
recognize.py
59 lines (45 loc) · 1.93 KB
/
recognize.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
from classify import ShapeClassifier
from data import PassingDirects
from detect import SignalDetector
from filter import SignalFilter
class RulesRecognizer:
def __init__(self, configs):
self.configs = configs
self.detector = SignalDetector(configs)
self.filter = SignalFilter(configs)
self.classifier = ShapeClassifier(configs)
if self.strategy == 'radical':
self.is_passable = lambda signal: signal.color != 'red'
else:
self.is_passable = lambda signal: signal.color == 'green'
def __call__(self, image):
detections = self.detector(image)
if len(detections) == 0:
return [], PassingDirects.allow()
signals = self.filter(detections)
signals = self.classifier(image, signals)
return signals, self.recognize(signals)
@property
def strategy(self):
return self.configs['strategy']
def global_recognize(self, signals):
if self.strategy == 'radical':
if any(signal.shape == 'full' and not self.is_passable(signal) for signal in signals):
return PassingDirects.prohibit()
else:
return PassingDirects.allow()
else:
if any(signal.shape == 'full' and self.is_passable(signal) for signal in signals):
return PassingDirects.allow()
else:
return PassingDirects.prohibit()
def recognize(self, signals):
passing_directs = self.global_recognize(signals)
for signal in filter(lambda signal: signal.shape != 'full', signals):
if signal.shape == 'straight':
passing_directs.straight = self.is_passable(signal)
elif signal.shape == 'left':
passing_directs.left = self.is_passable(signal)
elif signal.shape == 'right':
passing_directs.right = self.is_passable(signal)
return passing_directs