2022-09-28 22:50:34 -04:00
|
|
|
# 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.
|
|
|
|
|
|
|
|
|
|
# Imports
|
2022-12-23 14:51:49 -05:00
|
|
|
import pyaudio
|
2022-09-28 22:50:34 -04:00
|
|
|
import numpy as np
|
2022-10-09 23:45:09 -04:00
|
|
|
from openwakeword.model import Model
|
2023-03-20 21:32:39 -04:00
|
|
|
import argparse
|
|
|
|
|
|
|
|
|
|
# Parse input arguments
|
|
|
|
|
parser=argparse.ArgumentParser()
|
|
|
|
|
parser.add_argument(
|
|
|
|
|
"--chunk_size",
|
2023-08-15 22:00:13 -04:00
|
|
|
help="How much audio (in number of samples) to predict on at once",
|
2023-03-20 21:32:39 -04:00
|
|
|
type=int,
|
|
|
|
|
default=1280,
|
2023-08-15 22:00:13 -04:00
|
|
|
required=False
|
2023-03-20 21:32:39 -04:00
|
|
|
)
|
2023-06-13 21:23:57 -04:00
|
|
|
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
|
|
|
|
|
)
|
2023-03-20 21:32:39 -04:00
|
|
|
|
|
|
|
|
args=parser.parse_args()
|
2022-09-28 22:50:34 -04:00
|
|
|
|
|
|
|
|
# Get microphone stream
|
2022-12-23 14:51:49 -05:00
|
|
|
FORMAT = pyaudio.paInt16
|
|
|
|
|
CHANNELS = 1
|
|
|
|
|
RATE = 16000
|
2023-03-20 21:32:39 -04:00
|
|
|
CHUNK = args.chunk_size
|
2022-12-23 14:51:49 -05:00
|
|
|
audio = pyaudio.PyAudio()
|
|
|
|
|
mic_stream = audio.open(format=FORMAT, channels=CHANNELS, rate=RATE, input=True, frames_per_buffer=CHUNK)
|
2022-09-28 22:50:34 -04:00
|
|
|
|
2022-11-21 11:56:43 -05:00
|
|
|
# Load pre-trained openwakeword models
|
2023-06-13 21:23:57 -04:00
|
|
|
if args.model_path != "":
|
|
|
|
|
owwModel = Model(wakeword_models=[args.model_path], inference_framework=args.inference_framework)
|
|
|
|
|
else:
|
|
|
|
|
owwModel = Model(inference_framework=args.inference_framework)
|
|
|
|
|
|
|
|
|
|
n_models = len(owwModel.models.keys())
|
2022-09-28 22:50:34 -04:00
|
|
|
|
2023-03-20 21:32:39 -04:00
|
|
|
# Run capture loop continuosly, checking for wakewords
|
2022-09-28 22:50:34 -04:00
|
|
|
if __name__ == "__main__":
|
2023-03-20 21:32:39 -04:00
|
|
|
# Generate output string header
|
2023-01-28 13:58:57 -05:00
|
|
|
print("\n\n")
|
|
|
|
|
print("#"*100)
|
|
|
|
|
print("Listening for wakewords...")
|
|
|
|
|
print("#"*100)
|
2023-06-13 21:23:57 -04:00
|
|
|
print("\n"*(n_models*3))
|
2023-03-20 21:32:39 -04:00
|
|
|
|
2022-09-28 22:50:34 -04:00
|
|
|
while True:
|
|
|
|
|
# Get audio
|
2022-12-23 14:51:49 -05:00
|
|
|
audio = np.frombuffer(mic_stream.read(CHUNK), dtype=np.int16)
|
2022-09-28 22:50:34 -04:00
|
|
|
|
|
|
|
|
# Feed to openWakeWord model
|
2022-11-21 11:56:43 -05:00
|
|
|
prediction = owwModel.predict(audio)
|
|
|
|
|
|
2023-03-20 21:32:39 -04:00
|
|
|
# Column titles
|
2023-01-28 13:58:57 -05:00
|
|
|
n_spaces = 16
|
|
|
|
|
output_string_header = """
|
|
|
|
|
Model Name | Score | Wakeword Status
|
|
|
|
|
--------------------------------------
|
|
|
|
|
"""
|
|
|
|
|
|
2022-11-21 11:56:43 -05:00
|
|
|
for mdl in owwModel.prediction_buffer.keys():
|
2023-01-28 13:58:57 -05:00
|
|
|
# Add scores in formatted table
|
2022-11-21 11:56:43 -05:00
|
|
|
scores = list(owwModel.prediction_buffer[mdl])
|
2023-01-28 13:58:57 -05:00
|
|
|
curr_score = format(scores[-1], '.20f').replace("-", "")
|
2022-11-21 11:56:43 -05:00
|
|
|
|
2023-01-28 13:58:57 -05:00
|
|
|
output_string_header += f"""{mdl}{" "*(n_spaces - len(mdl))} | {curr_score[0:5]} | {"--"+" "*20 if scores[-1] <= 0.5 else "Wakeword Detected!"}
|
|
|
|
|
"""
|
2022-11-21 11:56:43 -05:00
|
|
|
|
2023-01-28 13:58:57 -05:00
|
|
|
# Print results table
|
2023-06-13 21:23:57 -04:00
|
|
|
print("\033[F"*(4*n_models+1))
|
2023-01-28 13:58:57 -05:00
|
|
|
print(output_string_header, " ", end='\r')
|