Building a Speech Translator in Python

Original article was published on Artificial Intelligence on Medium

1. Speech Recognition

Define Recognizer

r = sr.Recognizer()

Define your Microphone

Before defining our microphone instance, we will choose our input device. There might be multiple input devices plugged into your computer and we need to choose which one we are planning to use. As you know machines are dummies, you have to tell them exactly what to do! Using the following code you will be able to see your input devices.

print(sr.Microphone.list_microphone_names())

Here you can see the results of me checking the input devices. I recommend running this script before you define your microphone, because you may get a different result. The script returns an array list with input names, for me I want to use the “Built-in Microphone”, so the first element of the array list. Defining the microphone code will look as follows:

mic = sr.Microphone(device_index=0)

Recognize Speech

As mentioned earlier, we will be using the recognize_google method, which is a speech recognition model created by our friends at Google. Thanks to them!

with mic as source: 
r.adjust_for_ambient_noise(source)
audio = r.listen(source)
result = r.recognize_google(audio)

If you want to check your result before doing the translation, you can add the following line to your code.

print(result)