diff --git a/examples/README.md b/examples/README.md index e100c6f..391baa8 100644 --- a/examples/README.md +++ b/examples/README.md @@ -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 ` +2) Run the script: `python capture_activations.py --threshold 0.5 --output_dir --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 ` \ No newline at end of file +To run the script: `python benchmark_efficiency.py --ncores ` diff --git a/examples/audio/activation.wav b/examples/audio/activation.wav new file mode 100644 index 0000000..979e8a2 Binary files /dev/null and b/examples/audio/activation.wav differ diff --git a/examples/capture_activations.py b/examples/capture_activations.py index 59f0339..5dc3b33 100644 --- a/examples/capture_activations.py +++ b/examples/capture_activations.py @@ -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) \ No newline at end of file + scipy.io.wavfile.write(os.path.join(os.path.abspath(args.output_dir), fname), 16000, audio_context) + + playBeep('audio/activation.wav', audio) diff --git a/examples/utils/beep.py b/examples/utils/beep.py new file mode 100644 index 0000000..8536c94 --- /dev/null +++ b/examples/utils/beep.py @@ -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() \ No newline at end of file