forked from dscripka/openWakeWord
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcapture_activations.py
177 lines (157 loc) · 5.57 KB
/
capture_activations.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
# Copyright 2022 David Scripka. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
##################################
# This example scripts runs openWakeWord continuously on a microphone stream,
# and saves 5 seconds of audio immediately before the activation as WAV clips
# in the specified output location.
##################################
# Imports
import os
import platform
import collections
import time
if platform.system() == "Windows":
import pyaudiowpatch as pyaudio
else:
import pyaudio
import numpy as np
from openwakeword.model import Model
import openwakeword
import scipy.io.wavfile
import datetime
import argparse
from utils.beep import playBeep
# Parse input arguments
parser=argparse.ArgumentParser()
parser.add_argument(
"--output_dir",
help="Where to save the audio that resulted in an activation",
type=str,
default="./",
required=True
)
parser.add_argument(
"--threshold",
help="The score threshold for an activation",
type=float,
default=0.5,
required=False
)
parser.add_argument(
"--vad_threshold",
help="""The threshold to use for voice activity detection (VAD) in the openWakeWord instance.
The default (0.0), disables VAD.""",
type=float,
default=0.0,
required=False
)
parser.add_argument(
"--noise_suppression",
help="Whether to enable speex noise suppression in the openWakeWord instance.",
type=bool,
default=False,
required=False
)
parser=argparse.ArgumentParser()
parser.add_argument(
"--chunk_size",
help="How much audio (in number of 16khz samples) to predict on at once",
type=int,
default=1280,
required=False
)
parser.add_argument(
"--model_path",
help="The path of a specific model to load",
type=str,
default="",
required=False
)
parser.add_argument(
"--inference_framework",
help="The inference framework to use (either 'onnx' or 'tflite'",
type=str,
default='tflite',
required=False
)
parser.add_argument(
"--disable_activation_sound",
help="Disables the activation sound, clips are silently captured",
action='store_true',
required=False
)
args=parser.parse_args()
# Get microphone stream
FORMAT = pyaudio.paInt16
CHANNELS = 1
RATE = 16000
CHUNK = args.chunk_size
audio = pyaudio.PyAudio()
mic_stream = audio.open(format=FORMAT, channels=CHANNELS, rate=RATE, input=True, frames_per_buffer=CHUNK)
# Load pre-trained openwakeword models
if args.model_path:
model_paths = openwakeword.get_pretrained_model_paths()
for path in model_paths:
if args.model_path in path:
model_path = path
if model_path:
owwModel = Model(
wakeword_models=[model_path],
enable_speex_noise_suppression=args.noise_suppression,
vad_threshold = args.vad_threshold,
inference_framework=args.inference_framework
)
else:
print(f'Could not find model \"{args.model_path}\"')
exit()
else:
owwModel = Model(
enable_speex_noise_suppression=args.noise_suppression,
vad_threshold=args.vad_threshold
)
# Set waiting period after activation before saving clip (to get some audio context after the activation)
save_delay = 1 # seconds
# Set cooldown period before another clip can be saved
cooldown = 4 # seconds
# Create output directory if it does not already exist
if not os.path.exists(args.output_dir):
os.mkdir(args.output_dir)
# Run capture loop, checking for hotwords
if __name__ == "__main__":
# Predict continuously on audio stream
last_save = time.time()
activation_times = collections.defaultdict(list)
print("\n\nListening for wakewords...\n")
while True:
# Get audio
mic_audio = np.frombuffer(mic_stream.read(CHUNK), dtype=np.int16)
# Feed to openWakeWord model
prediction = owwModel.predict(mic_audio)
# Check for model activations (score above threshold), and save clips
for mdl in prediction.keys():
if prediction[mdl] >= args.threshold:
activation_times[mdl].append(time.time())
if activation_times.get(mdl) and (time.time() - last_save) >= cooldown \
and (time.time() - activation_times.get(mdl)[0]) >= save_delay:
last_save = time.time()
activation_times[mdl] = []
detect_time = datetime.datetime.now().strftime("%Y_%m_%d_%H_%M_%S")
print(f'Detected activation from \"{mdl}\" model at time {detect_time}!')
# Capture total of 5 seconds, with the microphone audio associated with the
# activation around the ~4 second point
audio_context = np.array(list(owwModel.preprocessor.raw_data_buffer)[-16000*5:]).astype(np.int16)
fname = detect_time + f"_{mdl}.wav"
scipy.io.wavfile.write(os.path.join(os.path.abspath(args.output_dir), fname), 16000, audio_context)
if not args.disable_activation_sound:
playBeep(os.path.join(os.path.dirname(__file__), 'audio', 'activation.wav'), audio)