-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaction_taker.py
28 lines (22 loc) · 1.04 KB
/
action_taker.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
import collections
import os
class ActionTaker:
def __init__(self, commands={}, history_len=1, min_confidence=0.98):
self.commands = commands
self.min_confidence = min_confidence
self.history_len = history_len
self.history = {}
for key in commands.keys():
self.history[key] = collections.deque(history_len*[0.], history_len)
def on_new_state(self, state: str, confidence: float):
if state not in self.commands.keys():
raise RuntimeError("State ", state, " not in commands.")
self.history[state].appendleft(confidence)
if all(conf > self.min_confidence for conf in self.history[state]):
self.history[state] = collections.deque(self.history_len*[0.], self.history_len)
print("Executing ", self.commands[state])
self.execute_action(state)
def execute_action(self, state: str):
if state not in self.commands.keys():
raise RuntimeError("State ", state, " not in commands.")
os.system(self.commands[state])