Code for How to Convert Speech to Text in Python

You can also view the full code on github.

recognizer.py

import speech_recognition as sr
import sys

# read filename from arguments
filename = sys.argv[1]

# initialize the recognizer
r = sr.Recognizer()

# open the file
with sr.AudioFile(filename) as source:
    # listen for the data (load audio to memory)
    audio_data = r.record(source)
    # recognize (convert from speech to text)
    text = r.recognize_google(audio_data)
    print(text)

live_recognizer.py

import speech_recognition as sr
import sys

#read duration from the arguments
duration = int(sys.argv[1])

# initialize the recognizer
r = sr.Recognizer()
print("Please talk")
with sr.Microphone() as source:
    # read the audio data from the default microphone
    audio_data = r.record(source, duration=duration)
    print("Recognizing...")
    # convert speech to text
    text = r.recognize_google(audio_data)
    print(text)