Python Speech Recognition - Convert Speech To Text

Published December 30, 2020

If you are a programmer then you will definitely know how interesting a python programming language is. The more interesting is python projects which can make our daily life easier. Today we have made a speech recognition python bot for you that will listen to your voice and will save your information in the Diary. A very interesting python project for both beginners and professionals. Before getting started let's know more about what is included in this python project, how to install speech recognition in python

 

What's inside this Speech Recognition Python Bot Project

=> It will listen to your voice and will convert it into text.

=> To make it more interesting this python bot will ask for some information from the user and will listen to the user's answers. 

=> After collecting all the information from users the python bot will automatically generate the text file with the name of the user and will insert all the information inside this text file.

=> At last it will open the text file in the console and the user can read all the information he stored inside.

 

What do we need to start this project?

Read Top 20 Python libraries we should know

 

To start our speech recognition python bot project we have to import some modules that will help us in making this project. Use the following command in your terminal or command prompt to install these modules.

  • Python Speech Recognition module: This module will basically convert our speech into text form. Here is the code to install this module.

pip install SpeechRecognition 

  • PyAudio: The module to recognize your audio and language(English added). Install this module using:

 

 

pip install pyaudio

 

 

If your PyAudio is not installing and showing an error then please go through this article Click Here.

 


Speech Recognition Python Bot Project (Code + Output)

 

import speech_recognition as sr


def takeCommand():
    r = sr.Recognizer()
    # Using microphone for listening your voice
    with sr.Microphone() as source:
        print("Please Speak.....")
        # If the user do not speak for 2 seconds it will stop listening
        r.pause_threshold = 2
        audio = r.listen(source)
    try:
        # It will recognise your voice and conver it into the text.
        print("Recognising......")
        query = r.recognize_google(audio, language='en-in')
        print(f"You said: {query}")

    except Exception as e:
        # if some problem occurred than this message will be shown
        print("say that again please")
        return None
    return query


# It will get all the information from the user by listening them &
# will store the value in the variable
print("Please enter your name")
name = takeCommand()
print("Please enter your class")
Class = "My class is: " + takeCommand()
print("Please enter your phone number")
phNum = "My phone number is:" + takeCommand()
print("Please tell me your hobby")
hobby = "My hobby is to play:" + takeCommand()
print("What is your fathers name")
Fname = "My father name is:" + takeCommand()
print("What is your School Name")
SName = "My school name is: " + takeCommand()


def openFile():
    b = name + ".txt"
    # New text file will be created with the name of user.
    f = open(b, "w+")
    # Now this code will write the collected information inside the text file.
    f.write("My name is:" + name + "\r\n" + Class + "\r\n" + phNum + "\r\n" + hobby + "\r\n" + Fname + "\r\n" + SName)
    f.close()
    # To open the text file in the console and read the contents
    print("Here is your text file")
    f = open(b, "r")
    if f.mode == 'r':
        contents = f.read()
        print (contents)


if __name__ == "__main__":
    openFile()

 

Output:

Convert speech into text & Collect all the information from users

Python Speech To text Conversion

 

Create a new text file with the name of the user and save all the information of the user in a text file

Python Speech to text Converion

 

Also, open the text file in the console to read all the information about the user

 

Python Speech to text Conversion

 

Full Code of Speech Recognition Python Bot Project in GitHub Click Here

 

Python Notepad Editor Project

 

 

Article Contributed By :
https://www.rrtutors.com/site_assets/profile/assets/img/avataaars.svg

2863 Views