-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhotword.py
189 lines (152 loc) · 6.26 KB
/
hotword.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
import logging
import os
import pickle
import random
import threading
import snowboy.snowboydecoder as sd
class Hotword(object):
"""Manager to detect hotwords and control video player correspondingly.
Call destroy() before destruction.
"""
MODEL_PATH = "model/"
VIDEO_PATH = "video/"
MAP_FILE = os.path.join(MODEL_PATH, "patronum.map")
def __init__(self, strikes, cb_queue):
"""
Args:
strikes (deque): current attention status.
cb_queue (queue): callback queue for passing tasks between threads.
"""
# Logging.
logging.basicConfig()
self.logger = logging.getLogger("hotword")
self.logger.setLevel(logging.DEBUG)
self.end_detector = False
self.strikes = strikes
self.cb_queue = cb_queue
self.patronum_map = Hotword.load_map()
self.videos, self.models = self.load_videos_models()
self.update_map()
# Create detector.
sensitivities = [0.5] * len(self.models)
self.detector = sd.HotwordDetector(list(self.models),
resource="snowboy/common.res",
sensitivity=sensitivities,
audio_gain=1)
# Create detect thread.
callbacks = [self.gen_callbacks(self.patronum_map[m])
for m in list(self.models)]
hotword_kwargs = {"detected_callback": callbacks,
"interrupt_check": lambda: self.end_detector,
"sleep_time": 0.03}
self.detect_thread = threading.Thread(target=self.detector.start,
kwargs=hotword_kwargs)
self.detect_thread.start()
def destroy(self):
"""Release resources. Must be called before class destruction.
Returns:
"""
self.end_detector = True
self.detect_thread.join()
self.detector.terminate()
def gen_callbacks(self, video):
"""Generate detector callbacks corresponding to detector models.
Args:
video (str): video path to be passed to main thread for playing.
Note:
The callback functions will not be called in main thread.
Returns:
obj: callback function.
"""
def callback():
level = self.strikes.count(False) / self.strikes.maxlen
self.logger.debug("Callback: {}\t{}".format(video, level))
self.cb_queue.put((video, level))
return callback
@staticmethod
def load_map():
"""Load mapping of hotword model to patronum video.
Returns:
dict: map of models to videos.
"""
if not os.path.isfile(Hotword.MAP_FILE): # Exist no map file.
p_map = {'mapped_videos': set()}
else:
with open(Hotword.MAP_FILE, "rb") as f:
p_map = pickle.load(f)
return p_map
def load_videos_models(self):
"""Load videos from VIDEO_PATH and models from MODEL_PATH.
Videos must have extension mp4. Models must have extension pmdl.
Returns:
set: set of video paths found. Ex. "video/video.mp4".
set: set of model paths found. Ex. "model/model.pmdl".
"""
videos = set(v for v in os.listdir(Hotword.VIDEO_PATH)
if os.path.splitext(v)[-1].lower() == ".mp4")
models = set(m for m in os.listdir(Hotword.MODEL_PATH)
if os.path.splitext(m)[-1].lower() == ".pmdl")
self.logger.info("Videos: {}".format(", ".join(videos)))
self.logger.info("Models: {}".format(", ".join(models)))
videos = set(os.path.join(Hotword.VIDEO_PATH, v) for v in videos)
models = set(os.path.join(Hotword.MODEL_PATH, m) for m in models)
return videos, models
def update_map(self):
"""Update `patronum_map`.
Add new models in `models` to `patronum_map`. Remove models not listed
in `models` from `patronum_map`.
Returns:
"""
# Add models.
for model in self.models:
if not model in self.patronum_map:
unmapped_videos = list(
self.videos - self.patronum_map["mapped_videos"])
if unmapped_videos:
picked_video = random.choice(unmapped_videos)
self.patronum_map["mapped_videos"].add(picked_video)
else: # All videos are used. Renew a choosing round.
picked_video = random.choice(list(self.videos))
self.patronum_map["mapped_videos"] = set(picked_video)
self.patronum_map[model] = picked_video
# Remove models.
models_to_removed = [] # Avoid modifying `patronum_map` in iteration.
for model in self.patronum_map:
if model == "mapped_videos":
continue
if not model in self.models:
self.patronum_map["mapped_videos"].remove(self.patronum_map[model])
models_to_removed.append(model)
for model in models_to_removed:
_ = self.patronum_map.pop(model)
# Update map file.
with open(Hotword.MAP_FILE, "wb") as f:
pickle.dump(self.patronum_map, f)
self.logger.debug(self.patronum_map)
if __name__ == "__main__":
import queue
from collections import deque
from play import Player
cb_queue = queue.Queue() # Callback queue for passing tasks between threads.
strikes = deque([False]*10, 10)
player = Player()
player.logger.setLevel(logging.NOTSET)
hw = Hotword(strikes, cb_queue)
hw.logger.setLevel(logging.NOTSET)
try:
while True:
if random.random() < 0.5:
strikes.append(True)
else:
strikes.append(False)
if not cb_queue.empty():
video, level = cb_queue.get(False)
hw.logger.info(
"Ready to play {} with level {}.".format(video, level))
player.play(video, level)
# Remove additional play tasks added during previous playing.
with cb_queue.mutex:
cb_queue.queue.clear()
except KeyboardInterrupt:
player.destroy()
hw.destroy()