Using Python is fun. In a matter of minutes, I was able to build a rudimentary speech to text recognition app, for Tamil. And after that, in reverse, that is, to speak out loud a sentence of given Tamil text.

Speech to Text:

I got the base code from here. You need to install SpeechRecognition package (available through PIP) and PyAudio (available through PIP in Linux, but on Windows, you need to install appropriate package from here).


<h1>Speech To Text — Python</h1>

<h1>pip install SpeechRecognition</h1>

<h1>Download PyAudio from https://www.lfd.uci.edu/~gohlke/pythonlibs/#pyaudio</h1>

<h1>pip install PyAudio-0.2.11-cp37-cp37m-win_amd64.whl and NOT pip install PyAudio (Works only in Linux)</h1>

import speech_recognition as sr 
r = sr.Recognizer()
with sr.Microphone() as source:
    print (&quot;Say Something in Tamil and pause, no need to press any key&quot;)
    audio = r.listen (source)
    print (&quot;Got you&quot;)
try:
    WhatUSpoke = r.recognize_google (audio, language=&quot;ta-IN&quot;)
    print (&quot;What you spoke (Google):&quot;, WhatUSpoke)
except:
    pass

Text to Speech:

When I posted the above code, I got a reader request for a code that does the reverse – to speak out loud a sentence of given Tamil text. I have given this below. I got the base code from here. You need to install gTTS package (available through PIP). The code uses os.system to play the output audio file, this command works out of the box in Windows, in Linux you may need a command-line player like MPG321 (which can be installed using sudo apt-get install mpg321)

#Reference: https://www.thecrazyprogrammer.com/2018/05/python-text-to-speech.html 
#pip  install gTTS

import gtts as gt 
import os

TamilText="வணக்கம்.அறம் செய விரும்பு, ஆறுவது சினம், இயல்வது கரவேல், ஈவது விலக்கேல், உடையது விளம்பேல். நன்றி!"
tts = gt.gTTS(text=TamilText, lang='ta')
tts.save("Tamil-Audio.mp3")
os.system("Tamil-Audio.mp3")

You can listen to the output audio that’s produced by the code above, below:

The above was possible, thanks, to the numerous readymade packages that are available for free, and, the magic of cloud – in these two cases I am using Google Cloud, which required no configuration or key for trial runs. You may use Bing if you have an Azure API key.

Speech to Text in Python and its output

Speech to Text in Python and its output

Tamil Text to Speech in Python

Tamil Text to Speech in Python

Categorized in: