Convert Speech to Text and Text to Speech in Python

Master Python with 70+ Hands-on Projects and Get Job-ready - Learn Python

Speech recognition is useful in various appliances around us like Google Assistant, Alexa, Cortona etc. Speech recognition has made things easy like we can use our voice to talk to google assistants and find the answers to our questions.

About Converting Speech to text and text to Speech

This project will recognise the speech of the user and print it on the tkinter window and convert the text typed by the user to speech. Let’s start developing this fun and easy project.

Python Conversion of Speech to text and text to Speech project

The main objective of this project is to develop our own converter of speech to text and text to speech. Installation of speech recognition, tkinter and gtts is mandatory.

Project Prerequisites

Basic knowledge of python, speechrecognition, tkinter and gtts is sufficient to complete this project.

Download project on Conversion of Speech to text and text to Speech

Source code of project on conversion of speech to text and text to speech: Speech to Text and Text to Speech Python Project

Project File Structure

  1. Importing various modules
  2. Initializing main window
  3. Backend of speak and recordvoice function
  4. Rest Code

1. Importing various modules:

from tkinter import *
from tkinter.messagebox import showinfo
from gtts import gTTS
import speech_recognition as sr
import os

Code Explanation:

a. tkinter: Graphical user Interface can be easily be created with the help of tkinter.
b. Messagebox: Message boxes are displayed on the tkinter window.
c. gtts: This module helps in playing the sound after converting text to speech.
d. speech_recognition: The main purpose of speechrecognition is to identify the words spoken.
e. os: This module is useful to interact with the operating system

2. Initializing main window:

mainwindow= Tk()
mainwindow.title('DataFlair Text-To-Speech and Speech-To-Text Converter')
mainwindow.geometry('500x500')
mainwindow.resizable(0, 0)
mainwindow.configure(bg='yellow')

Code Explanation:

a. Tk(): Every component of tkinter applications can easily be accessed with The help of tk.
b. title(): It sets the title of the window.
c. geometry(): It helps to set the geometry of the window.
d. resizable(): It helps to change the size of the tkinter window as per the requirement of a user.

3. Backend of speak and recordvoice function:

def say(text1):
     language = 'en'
     speech = gTTS(text = text1, lang = language, slow = False)
     speech.save("text.mp3")
     os.system("start text.mp3")
 
def recordvoice():
    while True:
        r = sr.Recognizer()
        with sr.Microphone() as source:
            audio=r.listen(source)
            try:    
                text1 = r.recognize_google(audio,language="en-IN")
            except:
                pass
            return text1

Code Explanation:

a. Recognizer(): The main purpose of this instance is to recognize the voice.
b. Microphone(): It uses the default microphone as the audio source.
c. listen(): It listens to the phrase that is spoken and extracts into audio data.

4. Rest Code:

def TextToSpeech():
    texttospeechwindow = Toplevel(mainwindow)
    texttospeechwindow.title('Text-to-Speech Converter by DataFlair')
    texttospeechwindow.geometry("500x500")
    texttospeechwindow.configure(bg='Blue')
 
    Label(texttospeechwindow, text='Text-to-Speech Converter by DataFlair', font=("Times New Roman", 15), bg='Blue').place(x=50)
 
    text = Text(texttospeechwindow, height=5, width=30, font=12)
    text.place(x=7, y=60)
   
    speakbutton = Button(texttospeechwindow, text='listen', bg='coral', command=lambda: say(str(text.get(1.0, END))))
    speakbutton.place(x=140, y=200)
 
def SpeechToText():
    speechtotextwindow = Toplevel(mainwindow)
    speechtotextwindow.title('Speech-to-Text Converter by DataFlair')
    speechtotextwindow.geometry("500x500")
    speechtotextwindow.configure(bg='pink')
 
    Label(speechtotextwindow, text='Speech-to-Text Converter by DataFlair', font=("Comic Sans MS", 15), bg='IndianRed').place(x=50)
 
    text = Text(speechtotextwindow, font=12, height=3, width=30)
    text.place(x=7, y=100)
   
    recordbutton = Button(speechtotextwindow, text='Record', bg='Sienna', command=lambda: text.insert(END, recordvoice()))
    recordbutton.place(x=140, y=50)
 
Label(mainwindow, text='DataFlair Text-To-Speech and Speech-To-Text Converter',
     font=('Times New Roman', 16), bg='red', wrap=True, wraplength=450).place(x=25, y=0)
 
texttospeechbutton = Button(mainwindow, text='Text-To-Speech Conversion', font=('Times New Roman', 16), bg='Purple', command=TextToSpeech)
texttospeechbutton.place(x=100, y=150)
 
speechtotextbutton = Button(mainwindow, text='Speech-To-Text Conversion', font=('Times New Roman', 16), bg='Purple', command=SpeechToText)
speechtotextbutton.place(x=100, y=250)
 
mainwindow.update()
mainwindow.mainloop()

Code Explanation:

a. TextToSpeech(): This is the main function for converting text to speech.
b. Label(): Display boxes are displayed on the screen where you can place the text.
c. Button(): It adds a button on the tkinter window.
d. SpeechToText(): This is the main function for converting speech to text.
e. mainwindow.mainloop(): It helps in running our program.

Python Speech to Text Output

python speech to text output

Summary

We have successfully developed a project on conversion of Speech to text and text to Speech with the help of three modules speechrecognition, gtts and tkinter.

Did you like this article? If Yes, please give DataFlair 5 Stars on Google

courses

DataFlair Team

DataFlair Team is a group of passionate educators and industry experts dedicated to providing high-quality online learning resources on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. With years of experience in the field, the team aims to simplify complex topics and help learners advance their careers. At DataFlair, we believe in empowering students and professionals with the knowledge and skills needed to thrive in today’s fast-paced tech industry. Follow us for Free courses, expert insights, tutorials, and practical tips to boost your learning journey.

2 Responses

  1. subrahmanyaswamy says:

    thanks, sir it will very helpful to my project expo thanks thank you so much

Leave a Reply

Your email address will not be published. Required fields are marked *