Movatterモバイル変換


[0]ホーム

URL:


Open In App
Next Article:
Personal Voice Assistant in Python
Next article icon

Speech Recognition is an important feature in several applications used such as home automation, artificial intelligence, etc. This article aims to provide an introduction on how to make use of the SpeechRecognition and pyttsx3 library of Python.
Installation required: 

pip install speechrecognition
sudo apt-get install python3-pyaudio
pip install pyaudio
pip install pyttsx3

Speech Input Using a Microphone and Translation of Speech to Text 

Translation of Speech to Text:
First, we need to import the library and then initialize it using init() function. This function may take 2 arguments. 

init(driverName string, debug bool)

After initialization, we will make the program speak the text using say() function. 
This method may also take 2 arguments.  

say(text unicode, name string)

Finally, to run the speech we use runAndWait() All the say() texts won’t be said unless the interpreter encounters runAndWait().
Below is the implementation. 

Python
# Python program to translate# speech to text and text to speechimportspeech_recognitionassrimportpyttsx3# Initialize the recognizerr=sr.Recognizer()# Function to convert text to# speechdefSpeakText(command):# Initialize the engineengine=pyttsx3.init()engine.say(command)engine.runAndWait()# Loop infinitely for user to# speakwhile(1):# Exception handling to handle# exceptions at the runtimetry:# use the microphone as source for input.withsr.Microphone()assource2:# wait for a second to let the recognizer# adjust the energy threshold based on# the surrounding noise levelr.adjust_for_ambient_noise(source2,duration=0.2)#listens for the user's inputaudio2=r.listen(source2)# Using google to recognize audioMyText=r.recognize_google(audio2)MyText=MyText.lower()print("Didyousay",MyText)SpeakText(MyText)exceptsr.RequestErrorase:print("Couldnotrequestresults;{0}".format(e))exceptsr.UnknownValueError:print("unknownerroroccurred")
Input: voice speech (Hi buddy how are you)
Output: Did you say hi buddy how are you

Note: Since, Colab didn't have Microphone source so it's getting error there. You can use VS Code instead.

Get the complete notebook link here:

Colab Link :click here.


Improve
Article Tags :
Practice Tags :

Similar Reads

We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood ourCookie Policy &Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences

[8]ページ先頭

©2009-2025 Movatter.jp