DEV Community

Jordan Kalebu
Jordan Kalebu

Posted on • Updated on

How to convert Speech to text in Python

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

pip install PyAudio
pip install SpeechRecognition
Enter fullscreen mode Exit fullscreen mode

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 *

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)
Enter fullscreen mode Exit fullscreen mode

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

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

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)
Enter fullscreen mode Exit fullscreen mode

Output

kalebu@kalebu-PC:~$ python3 app_audio.py 
Done recording
Recognizing the text
Decoded Text: python programming is the best of all by Jordan
Enter fullscreen mode Exit fullscreen mode

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

$~ pip install pydub
Enter fullscreen mode Exit fullscreen mode

To use the below link to download sample long audio

Long Sample Audio

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

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("++++++")
Enter fullscreen mode Exit fullscreen mode

Output

$ 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
    ++++++
Enter fullscreen mode Exit fullscreen mode

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

The Original Article can be found on kalebujordan.com

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

Discussion (1)

Collapse
balnamlah profile image
balnamlah • Edited

hello Mr. Jordan
Thank you a lot for this article, it helped me a lot in my project,
I have a question that I still didn't find an answer for it on google, could you help me with that, please

I am using the record method to record live audio from the microphone as the following:
audio = r.record(source, duration=200)
so the system will record for 200 minutes, then will stop recording and return the result
Is there a way to make the recording stop when user do action? like when user click "Stop" button, system will stop even if the 200 min is not finished?

Thanks a lot in advanced