Skip to content

Commit

Permalink
Merge pull request #14 from calpoly-csai/deepspeech
Browse files Browse the repository at this point in the history
added deepspeech streaming inference
  • Loading branch information
Jason-Ku authored Oct 12, 2020
2 parents 6346f59 + 5406e9b commit b8e5502
Showing 1 changed file with 60 additions and 0 deletions.
60 changes: 60 additions & 0 deletions stream_deepspeech.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
from deepspeech import Model, version
import wave
import pyaudio
import numpy as np
from timeit import default_timer as timer

# Audio constants
CHUNK = 2048 # Buffer size
FORMAT = pyaudio.paInt16 # Sample Size
CHANNELS = 1 # Sample Depth
RATE = 16000 # Sample Rate

# Timer length
TIME_LEN = 5

def run_stt(time_len=TIME_LEN):

# Audio buffer
frames = []

# Instantiate Pyaudio
p = pyaudio.PyAudio()

# Instantiate Deepspeech
ds = Model("deepspeech-0.8.1-models.pbmm")
ds.enableExternalScorer("deepspeech-0.8.1-models.scorer")

# Get the model sample rate
desired_sample_rate = ds.sampleRate()

# Start Deepspeech inference stream
stream = ds.createStream()

# Start stream timer
stream_start = timer()

# Open the audio stream
i_stream = p.open(format=FORMAT, channels=CHANNELS, rate=desired_sample_rate,
input=True, output=True, frames_per_buffer=CHUNK)

# Record audio and run inference on audio buffers
while(timer() - stream_start < time_len):
buff = np.frombuffer(i_stream.read(CHUNK), dtype=np.int16)
stream.feedAudioContent(buff)

# Close the stream and call PyAudio destructor
i_stream.stop_stream()
i_stream.close()
p.terminate()

# Obtain the model prediction
result = stream.finishStream()

# Output the prediction
print("result: ", result)

return result

if __name__ == "__main__":
run_stt(5)

0 comments on commit b8e5502

Please sign in to comment.