Okay not the error is away but deepspeech return always None.
My code:
recorder.py
class Recorder:
def init(self):
_pyAudio = pyaudio.PyAudio()
self._stream = _pyAudio.open(rate=RATE,
channels=CHANNELS,
format=FORMAT,
input=True,
output=True,
input_device_index=None,
output_device_index=0,
frames_per_buffer=CHUNK)
self.stt = speechtotextengines.DeepSpeech('./models/deepspeech-0.9.2-models.tflite',
'./models/deepspeech-0.9.2-models.scorer')
@staticmethod
def rms(frame):
shorts = array.array(FRAME_FORMAT, frame)
sum_squares = sum(
(sample * NORMALIZE) ** 2
for sample in shorts
)
return (sum_squares / len(shorts)) ** 0.5 * 1000
def record(self):
print('Noise detected')
rec = []
current = time.time()
end = time.time() + TIMEOUT_LENGTH
while current <= end:
data = self._stream.read(CHUNK)
if self.rms(data) >= THRESHOLD:
end = time.time() + TIMEOUT_LENGTH
current = time.time()
rec.append(data)
# self.stt.run(b''.join(rec))
"""Play Stream"""
# self._stream.write(b''.join(rec))
text = self.stt.run(b''.join(rec))
print(text)
print('Listening...')
def listen(self):
print('Listening...')
while True:
mic_input = self._stream.read(CHUNK)
rms_val = self.rms(mic_input)
if rms_val >= THRESHOLD:
self.record()
if __name__ == '__main__':
recorder = Recorder()
recorder.listen()
speechtotextengines.py
import numpy
import deepspeech
import wave
class DeepSpeech:
"""Class to perform speech-to-text transcription and related functionality"""
def __init__(self, model_path, scorer_path):
self.model = deepspeech.Model(model_path)
self.model.enableExternalScorer(scorer_path)
def run(self, audio):
print('start')
self.model.stt(numpy.frombuffer(audio, numpy.int16))
print('end')