How to convert Speech to text in Python
Tags: python, datascience, machinelearning, nlp
Published: 2020-09-28 16:13:37

**Speech Recognition** is the ability of a machine or program to identify words and
phrases in spoken language and convert them to textual information.

You have probably seen it on Sci-fi, and personal assistants like *Siri*, *Cortana*, and *Google Assistant*, and other virtual assistants that interact with through voice.

These AI assistants in order to understand your voice they need to do speech recognition so as to understand what you have just said.

Speech Recognition is a complex process, well I'm not going to teach you how to train a Machine Learning/Deep Learning Model to that, instead, I instruct you how to do that using google speech recognition API.

As long as you have the basics of Python you can successfully complete this tutorial and build your own fully functioning **speech recognition** programs in Python.

### Requirements
To successfully complete this tutorial, you need to have the following Python library installed on your Machine 

-PyAudio Library
-SpeechRecognition Library

**Installation**

```bash
pip install PyAudio
pip install SpeechRecognition
```

SpeechRecognition library allows you to can perform speech recognition with support for several engines and APIs, online and offline.

Below are some of the supported *Engines* 

- CMU Sphinx (works offline)
- Google Speech Recognition
- Google Cloud Speech API
- Wit.ai
- Microsoft Bing Voice Recognition
- Houndify API
- IBM Speech to Text

Snowboy Hotword Detection (works offline)

On this tutorial, we are going to use Google Speech recognition API which is free for basic uses perhaps it has a limit of requests you can send over a certain time.

Throughout this tutorial, you will learn performing Speech Recognition using sound that is directly fed from Microphone also using Audio Source from File 

### Speech Recognition from Microphone 

When Performing Speech Recognition from Microphone, we need to record the audio from the microphone and then send it to Google Speech to text recognition engine and which will perform the recognition and return out transcribed text 

**Steps involved **
- Recording Audio from Microphone ( PyAudio)
- Sending Audio to the Speech recognition engine 
- Printing the Recognized text to the screen 

Below is a sample *app.py* code just to do that you can lookout, its straight forward 

**app.py **
```python
import speech_recognition as sr

recognizer = sr.Recognizer()

''' recording the sound '''

with sr.Microphone() as source:
    print("Adjusting noise ")
    recognizer.adjust_for_ambient_noise(source, duration=1)
    print("Recording for 4 seconds")
    recorded_audio = recognizer.listen(source, timeout=4)
    print("Done recording")

''' Recorgnizing the Audio '''
try:
    print("Recognizing the text")
    text = recognizer.recognize_google(
            recorded_audio, 
            language="en-US"
        )
    print("Decoded Text : {}".format(text))

except Exception as ex:
    print(ex)
```
### Speech Recognition from Audio File 

When it comes to performing Speech Recognition from Audio line only one line of code is going to change instead of using a Microphone as a source of Audio, we will give a path to our Audio File we want to transcribe to text

On Demo, I have used the below sample audio 

[Sample Audio](https://kalebujordan.com/wp-content/uploads/2020/09/speech.wav)

The below code is a sample script to perform speech recognition of audio in a file. 

```python
import speech_recognition as sr

recognizer = sr.Recognizer()

''' recording the sound '''

with sr.AudioFile("./sample_audio/speech.wav") as source:
    recorded_audio = recognizer.listen(source)
    print("Done recording")

''' Recorgnizing the Audio '''
try:
    print("Recognizing the text")
    text = recognizer.recognize_google(
            recorded_audio, 
            language="en-US"
        )
    print("Decoded Text : {}".format(text))

except Exception as ex:
    print(ex)
```
**Output**
```bash
kalebu@kalebu-PC:~$ python3 app_audio.py 
Done recording
Recognizing the text
Decoded Text: python programming is the best of all by Jordan
```
### Speech Recognition from Long Audio Source
When you have very long audio, loading the whole audio to Memory and sending it over API it can be a very slow process, to overcome that we have to split the long audio source into small chunks and then performing speech recognition on those individual chunks

We are going to use pydub to split the Long Audio Source into those small chunks

To install pydub just use pip

```bash 
$~ pip install pydub
```
To use the below link to download sample long audio

[Long Sample Audio](https://kalebujordan.com/wp-content/uploads/2020/09/long_audio.mp3)

The Below is a sample Python code that loads the long Audio, Split into the segment, and then performing the Speech recognition on those individual chunks to to learn more about splitting the audio you can check out DataCamp Tutorial

```python
import os 
from pydub import AudioSegment
import speech_recognition as sr
from pydub.silence import split_on_silence

recognizer = sr.Recognizer()

def load_chunks(filename):
    long_audio = AudioSegment.from_mp3(filename)
    audio_chunks = split_on_silence(
        long_audio, min_silence_len=1800,
        silence_thresh=-17
    )
    return audio_chunks

for audio_chunk in load_chunks('./sample_audio/long_audio.mp3'):
    audio_chunk.export("temp", format="wav")
    with sr.AudioFile("temp") as source:
        audio = recognizer.listen(source)
        try:
            text = recognizer.recognize_google(audio)
            print("Chunk : {}".format(text))
        except Exception as ex:
            print("Error occured")
            print(ex)

print("++++++")
```

**Output**
```bash
$ python long_audio.py
    Chunk : by the time you finish reading this tutorial you have already covered several techniques and natural then
    Chunk : learn more
    Chunk : forgetting to subscribe to be updated on upcoming tutorials
    ++++++
```

Congrats you now know how to do, can't wait to see what you're going to build with the knowledge 

The [Original Article](https://kalebujordan.dev/image-stenography-with-python/) can be found on  [kalebujordan.dev](https://kalebujordan.dev/image-stenography-with-python/) 

In case of any comment, suggestion, or difficulties comment below and I will get back to you ASAP