# Convert Speech to Text

## How To Convert Speech to Text in Python

[May 4th 2020](https://hackernoon.com/archives/2020/05/04)\


![](https://3650666907-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M16IoaANYRvc0I-U9uw%2F-MC7gctWxsTr2iNVHVC7%2F-MC7hQjA2_PLrSUxD1RR%2Fimage.png?alt=media\&token=0acfc5ec-140f-43b7-9d7d-67a13fed97d4)

{% embed url="https://youtu.be/p64--E_K--g" %}

I have provided the FULL CODE in the DESCRIPTION of this above VIDEOSpeech recognition is the ability of a computer software to identify words and phrases in spoken language and convert them to human readable text. In this tutorial, you will learn how you can convert speech to text in Python using [SpeechRecognition](https://bit.ly/2yiADVv?ref=hackernoon.com) library.As a result, we do not need to build any machine learning model from scratch, this library provides us with convenient wrappers for various well known public speech recognition APIs (such as Google Cloud Speech API, IBM Speech To Text, etc.).Alright, let's get started, installing the library using [pip](https://bit.ly/3dhuQ11?ref=hackernoon.com):

```python
pip3 install SpeechRecognition
```

Okey, open up a new [Python](https://bit.ly/2L3z6W1?ref=hackernoon.com) file and import it:

```python
import speech_recognition as sr
```

Reading from a FileMake sure you have an audio file in the current directory that contains english speech (if you want to follow along with me, get the audio file [here](https://bit.ly/2Wq29Iw?ref=hackernoon.com)):

```python
filename = "speech.wav"
```

This file was grabbed from [LibriSpeech](https://bit.ly/2W3AsWS?ref=hackernoon.com) dataset, but you can bring anything you want, just change the name of the file, let's initialize our speech recognizer:

```python
# initialize the recognizer
r = sr.Recognizer()
```

The below code is responsible for loading the audio file, and converting the speech into text using [Google Speech Recognition](https://bit.ly/2yiADVv?ref=hackernoon.com):

```python
# 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)
```

This will take few seconds to finish, as it uploads the file to [Google](https://bit.ly/2xzWoQ6?ref=hackernoon.com) and grabs the output, here is my result:

```python
I believe you're just talking nonsense
```

### Reading from the Microphone

This requires PyAudio to be installed in your machine, here is the installation process depending on your operating system:**Windows**You can just pip install it:

```
pip3 install pyaudio
```

**Linux**You need to first install the dependencies:

```python
sudo apt-get install python-pyaudio python3-pyaudio
pip3 install pyaudio
```

**MacOS**You need to first install portaudio, then you can just pip install it:

```
brew install portaudio
pip3 install pyaudio
```

Now let's use our microphone to convert our speech:

```python
with sr.Microphone() as source:
    # read the audio data from the default microphone
    audio_data = r.record(source, duration=5)
    print("Recognizing...")
    # convert speech to text
    text = r.recognize_google(audio_data)
    print(text)
```

This will hear from your microphone for 5 seconds and then tries to convert that speech into text !It is pretty similar to the previous code, but we are using Microphone() object here to read the audio from the default microphone, and then we used duration parameter in record() function to stop reading after 5 seconds and then uploads the audio data to Google to get the output text.You can also use offset parameter in record() function to start recording after offset seconds.Also, you can recognize different languages by passing language parameter to recognize\_google() function. For instance, if you want to recognize spanish speech, you would use:

```python
text = r.recognize_google(audio_data, language="es-ES")
```

Check out supported languages in [this stackoverflow answer](https://bit.ly/2zfrahu?ref=hackernoon.com).

### Conclusion

As you can see, it is pretty easy and simple to use this library for converting speech to text. This library is widely used out there in the wild, make sure you master it, check their [official documentation](https://bit.ly/2xAFLnv?ref=hackernoon.com).If you want to [convert text to speech in Python](https://www.youtube.com/watch?v=hd-t4RScpUk\&ref=hackernoon.com) as well, check [this tutorial](https://www.youtube.com/watch?v=hd-t4RScpUk\&ref=hackernoon.com).If this tutorial was helpful. Buy me a Coffee -> buymeacoff.ee/gajeshnaik\


Reference : [https://hackernoon.com/how-to-convert-speech-to-text-in-python-844e3y4l](https://hackernoon.com/how-to-convert-speech-to-text-in-python-844e3y4l)
