Merge pull request #14 from secretsauceai/main

For #12
This commit is contained in:
dscripka 2023-03-04 23:41:05 -05:00 committed by GitHub
commit 4eaebb16e1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 61 additions and 10 deletions

View file

@ -26,7 +26,7 @@ pip install pyaudio scipy
pip install PyAudioWPatch scipy
```
2) Run the script: `python capture_activations.py --threshold 0.5 --output_dir <my_dir>`
2) Run the script: `python capture_activations.py --threshold 0.5 --output_dir <my_dir> --model <my_model>`
Note that if you have more than one microphone connected to your system, you may need to adjust the PyAudio configuration in the script to select the appropriate input device.
@ -34,4 +34,4 @@ Note that if you have more than one microphone connected to your system, you may
This is a script that estimates how many openWakeWord models could be run on on the specified number of cores for the current system. Can be useful to determine if a given system has the resources required for a particular use-case.
To run the script: `python benchmark_efficiency.py --ncores <desired integer number of cores>`
To run the script: `python benchmark_efficiency.py --ncores <desired integer number of cores>`

Binary file not shown.

View file

@ -31,9 +31,11 @@ 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()
@ -66,6 +68,14 @@ parser.add_argument(
default=False,
required=False
)
parser.add_argument(
"--model",
help="The model to use for openWakeWord, leave blank to use all available models",
type=str,
required=False
)
args=parser.parse_args()
# Get microphone stream
@ -77,10 +87,26 @@ 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(
enable_speex_noise_suppression=args.noise_suppression,
vad_threshold = args.vad_threshold
)
if args.model:
model_paths = openwakeword.get_pretrained_model_paths()
for path in model_paths:
if args.model in path:
model_path = path
if model_path:
owwModel = Model(
wakeword_model_paths=[model_path],
enable_speex_noise_suppression=args.noise_suppression,
vad_threshold = args.vad_threshold
)
else:
print(f'Could not find model \"{args.model}\"')
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
@ -101,10 +127,10 @@ if __name__ == "__main__":
print("\n\nListening for wakewords...\n")
while True:
# Get audio
audio = np.frombuffer(mic_stream.read(CHUNK), dtype=np.int16)
mic_audio = np.frombuffer(mic_stream.read(CHUNK), dtype=np.int16)
# Feed to openWakeWord model
prediction = owwModel.predict(audio)
prediction = owwModel.predict(mic_audio)
# Check for model activations (score above threshold), and save clips
for mdl in prediction.keys():
@ -116,10 +142,13 @@ if __name__ == "__main__":
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 audio associated with the
# Capture total of 5 seconds, with the mic_ 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)
scipy.io.wavfile.write(os.path.join(os.path.abspath(args.output_dir), fname), 16000, audio_context)
playBeep('audio/activation.wav', audio)

22
examples/utils/beep.py Normal file
View file

@ -0,0 +1,22 @@
import pyaudio
import wave
def playBeep(file_path, audio):
CHUNK = 1024
wf = wave.open(file_path, 'rb')
stream = audio.open(format=audio.get_format_from_width(wf.getsampwidth()),
channels=wf.getnchannels(),
rate=wf.getframerate(),
output=True)
data = wf.readframes(CHUNK)
while data != b'':
stream.write(data)
data = wf.readframes(CHUNK)
stream.stop_stream()
stream.close()