mirror of
https://github.com/dscripka/openWakeWord.git
synced 2026-08-27 18:17:20 -04:00
64 lines
2.1 KiB
Python
64 lines
2.1 KiB
Python
# 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
|
|
import pyaudio
|
|
import numpy as np
|
|
from openwakeword.model import Model
|
|
|
|
# Get microphone stream
|
|
FORMAT = pyaudio.paInt16
|
|
CHANNELS = 1
|
|
RATE = 16000
|
|
CHUNK = 1280
|
|
audio = pyaudio.PyAudio()
|
|
mic_stream = audio.open(format=FORMAT, channels=CHANNELS, rate=RATE, input=True, frames_per_buffer=CHUNK)
|
|
|
|
# Load pre-trained openwakeword models
|
|
owwModel = Model()
|
|
|
|
# Run capture loop, checking for hotwords
|
|
if __name__ == "__main__":
|
|
# Predict continuously on audio stream
|
|
print("\n\n")
|
|
print("#"*100)
|
|
print("Listening for wakewords...")
|
|
print("#"*100)
|
|
print("\n"*13)
|
|
|
|
while True:
|
|
# Get audio
|
|
audio = np.frombuffer(mic_stream.read(CHUNK), dtype=np.int16)
|
|
|
|
# Feed to openWakeWord model
|
|
prediction = owwModel.predict(audio)
|
|
|
|
# Generate output string header
|
|
n_spaces = 16
|
|
output_string_header = """
|
|
Model Name | Score | Wakeword Status
|
|
--------------------------------------
|
|
"""
|
|
|
|
for mdl in owwModel.prediction_buffer.keys():
|
|
# Add scores in formatted table
|
|
scores = list(owwModel.prediction_buffer[mdl])
|
|
curr_score = format(scores[-1], '.20f').replace("-", "")
|
|
|
|
output_string_header += f"""{mdl}{" "*(n_spaces - len(mdl))} | {curr_score[0:5]} | {"--"+" "*20 if scores[-1] <= 0.5 else "Wakeword Detected!"}
|
|
"""
|
|
|
|
# Print results table
|
|
print("\033[F"*14)
|
|
print(output_string_header, " ", end='\r')
|