I have this code that uses sounddevice as the microphone recording method.
import threading
import time
import sounddevice as sd
import librosa
import numpy as np
from tensorflow.keras.models import load_model
fs = 44100
seconds = 2
model = load_model("model.h5")
##### LISTENING THREAD #########
def listener():
while True:
myrecording = sd.rec(int(seconds * fs), samplerate=fs, channels=1)
sd.wait()
mfcc = librosa.feature.mfcc(y=myrecording.ravel(), sr=fs, n_mfcc=40)
mfcc_processed = np.mean(mfcc.T, axis=0)
prediction_thread(mfcc_processed)
time.sleep(0.001)
def voice_thread():
listen_thread = threading.Thread(target=listener, name="ListeningFunction")
listen_thread.start()
##### PREDICTION THREAD #############
def prediction(y):
prediction = model.predict(np.expand_dims(y, axis=0))
if prediction[:, 1] > 0.96:
print("ready!")
else:
print("not ready")
time.sleep(0.1)`
def prediction_thread(y):
pred_thread = threading.Thread(target=prediction, name="PredictFunction", args=(y,))
pred_thread.start()
voice_thread()
However I need to use Pyaudio instead of sounddevice because sounddevice isn't working for me for I have another scripts that I collected data with, sounddevice wasn't catching audio like it should so I used Pyaudio instead and it's working perfectly. I need to do the same with this script and I can't make it work.
this is for a classification model that predicts classes 1 or 0 and these classes/ files are .wav files, it should run until the model predicts class 1 meaning it's always listening until it's not (class 1 predicted).
here's a code I tried using Pyaudio
import threading
import time
import sounddevice as sd
import librosa
import numpy as np
from tensorflow.keras.models import load_model
import pyaudio
model = load_model("model.h5")
##### LISTENING THREAD #########
def listener():
seconds = 2
CHUNK = 1024
FORMAT = pyaudio.paInt16
CHANNELS = 1
RATE = 44100
p=pyaudio.PyAudio()
stream=p.open(format=FORMAT,channels=CHANNELS,rate=RATE,input=True,
frames_per_buffer=CHUNK)
for i in range(0, int(RATE / CHUNK * seconds)): #go for a few seconds
data = np.frombuffer(stream.read(CHUNK),dtype=np.int16)
peak=np.average(np.abs(data))*2
bars="#"*int(50*peak/2**16)
arr = np.array("%04d %05d %s"%(i,peak,bars))
stream.stop_stream()
stream.close()
p.terminate()
mfcc = librosa.feature.mfcc(y=arr, sr=RATE, n_mfcc=40)
mfcc_processed = np.mean(mfcc.T, axis=0)
prediction_thread(mfcc_processed)
time.sleep(0.001)
def voice_thread():
listen_thread = threading.Thread(target=listener, name="ListeningFunction")
listen_thread.start()
##### PREDICTION THREAD #############
def prediction(y):
prediction = model.predict(np.expand_dims(y, axis=0))
if prediction[:, 1] > 0.96:
print("ready!")
else:
print("not ready")
time.sleep(0.1)
def prediction_thread(y):
pred_thread = threading.Thread(target=prediction, name="PredictFunction", args=(y,))
pred_thread.start()
voice_thread()
However it gives this error and I can't seem to process audio correctly so the model can predict on it.
Exception in thread ListeningFunction:
Traceback (most recent call last):
File "C:\Users\Moham\anaconda3\envs\AI_development\lib\threading.py", line 932, in _bootstrap_inner
self.run()
File "C:\Users\Moham\anaconda3\envs\AI_development\lib\threading.py", line 870, in run
self._target(*self._args, **self._kwargs)
File "<ipython-input-14-5fe9df97cdaa>", line 48, in listener
File "C:\Users\Moham\anaconda3\envs\AI_development\lib\site-packages \librosa\util\decorators.py", line 88, in inner_f
return f(*args, **kwargs)
File "C:\Users\Moham\anaconda3\envs\AI_development\lib\site- packages \librosa\feature\spectral.py", line 1903, in mfcc
S = power_to_db(melspectrogram(y=y, sr=sr, **kwargs))
File "C:\Users\Moham\anaconda3\envs\AI_development\lib\site-packages\librosa\util\decorators.py", line 88, in inner_f
return f(*args, **kwargs)
File "C:\Users\Moham\anaconda3\envs\AI_development\lib\site-packages\librosa\feature\spectral.py", line 2043, in melspectrogram
S, n_fft = _spectrogram(
File "C:\Users\Moham\anaconda3\envs\AI_development\lib\site-packages\librosa\core\spectrum.py", line 2564, in _spectrogram
stft(
File "C:\Users\Moham\anaconda3\envs\AI_development\lib\site-packages\librosa\util\decorators.py", line 88, in inner_f
return f(*args, **kwargs)
File "C:\Users\Moham\anaconda3\envs\AI_development\lib\site-packages\librosa\core\spectrum.py", line 202, in stft
util.valid_audio(y, mono=False)
File "C:\Users\Moham\anaconda3\envs\AI_development\lib\site-packages\librosa\util\decorators.py", line 88, in inner_f
return f(*args, **kwargs)
File "C:\Users\Moham\anaconda3\envs\AI_development\lib\site-packages \librosa\util\utils.py", line 275, in valid_audio
raise ParameterError("Audio data must be floating-point")
librosa.util.exceptions.ParameterError: Audio data must be floating-point