Added example script for real-time microphone inference

This commit is contained in:
dscripka 2022-09-28 22:50:34 -04:00
parent 37e37722c4
commit 478fb0f5ae
4 changed files with 102 additions and 3 deletions

View file

@ -0,0 +1,59 @@
# 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 os
import plotext as plt
import sounddevice
import numpy as np
from openwakeword.detect import Model
# Get microphone stream
mic_stream = sounddevice.InputStream(
samplerate=16000,
blocksize=1280,
device = 3,
dtype = np.int16,
)
# Load openwakeword model(s)
model_name = "hey_mycroft_v1"
model = Model(
wakeword_model_paths=[os.path.join("../", "openwakeword", "resources", "models", model_name + ".onnx")],
input_sizes=[16]
)
# Run capture loop, checking for hotwords
if __name__ == "__main__":
# Start the mic stream
mic_stream.start()
# Create a prediction buffer
prediction_buffer = [0]*30
while True:
# Get audio
audio, overflowed = mic_stream.read(1280)
audio = audio.squeeze()
# Feed to openWakeWord model
prediction = model.predict(audio)
prediction_buffer = prediction_buffer[1:] + [round(prediction[model_name], 2)]
# Plot predictions in graph
plt.cld()
plt.clt()
plt.plot(prediction_buffer)
plt.ylim(0,1)
plt.show()
plt.sleep(0.005)

View file

@ -13,8 +13,10 @@
# limitations under the License.
# imports
from math import comb
import os
import random
from tqdm import tqdm
from typing import List
import numpy as np
import torch
@ -24,6 +26,36 @@ import mutagen
# Load audio clips and structure into clips of the same length
def stack_clips(audio_data, clip_size=16000*2):
"""
Takes an input list of 1D arrays (of different lengths), concatenates them together,
and then extracts clips of a uniform size by dividing the combined array.
Also converts the resulting array to 16-bit PCM format.
Args:
audio_data (List[ndarray]): A list of 1D numpy arrays to combine and stack
clip_size (int): The desired total length of the uniform clip size (in samples)
Returns:
ndarray: A N by `clip_size` array with the audio data, converted to 16-bit PCM
"""
# Combine all clips into single clip
combined_data = np.hstack((audio_data))
# Get chunks of the specified size
new_examples = []
for i in range(0, combined_data.shape[0], clip_size):
chunk = combined_data[i:i+clip_size]
if chunk.shape[0] != clip_size:
chunk = np.hstack((chunk, np.zeros(clip_size - chunk.shape[0])))
new_examples.append(chunk)
# Convert to 16-bit PCM data
X = (np.array(new_examples).astype(np.float32)*32767).astype(np.int16)
return X
def load_audio_clips(files, clip_size=32000):
"""
Takes the specified audio files and shapes them into an array of N by `clip_size`,
@ -107,7 +139,7 @@ def filter_audio_paths(target_dirs, min_length, max_length, duration_method="siz
durations = estimate_clip_duration(file_paths, sizes)
elif duration_method == "header":
durations = [get_clip_duration(i) for i in file_paths]
durations = [get_clip_duration(i) for i in tqdm(file_paths)]
return file_paths, durations

View file

@ -0,0 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:785bdf5655863ae47553b23793aa108c7b0152d4823f7869b41f2d2d765912fc
size 503850

View file

@ -46,9 +46,9 @@ class AudioFeatures():
def _get_melspectrogram(self, x, melspec_transform = lambda x: x/10 + 2):
"""Function to compute the mel-spectrogram of the provided audio samples."""
x = np.array(x) if isinstance(x, list) else x
x = np.array(x).astype(np.int16) if isinstance(x, list) else x
if x.dtype != np.int16:
raise ValueError("Input data must be 16-bit integers (i.e., 16-bit PCM audio)")
raise ValueError(f"Input data must be 16-bit integers (i.e., 16-bit PCM audio). You provided {x.dtype} data.")
x = x[None,] if len(x.shape) < 2 else x
x = x.astype(np.float32) if x.dtype!=np.float32 else x
outputs = self.melspec_model.run(None, {'input': x})
@ -77,6 +77,11 @@ class AudioFeatures():
embedding = self.embedding_model.run(None, {'input_1': batch})[0].squeeze()
return embedding
def get_embedding_shape(self, audio_length, sr=16000):
"""Function that determines the size of the output embedding array for a given audio clip length (in seconds)"""
x = (np.random.uniform(-1, 1, audio_length*sr)*32767).astype(np.int16)
return self._get_embeddings(x).shape
def _get_melspectrogram_batch(self, x, batch_size=128, ncpu=1):
"""
Compute the melspectrogram of the input audio samples in batches.