speech recognition in python code example
Example 1: python text to speech
The best library because you dont have to save the
text file or open the file to start the speech
pip install pyttsx3
import pyttsx3
engine = pyttsx3.init()
engine.say("Hello world")
engine.runAndWait()
Example 2: python speech recognition
import speech_recognition as sr
def take_command():
r = sr.Recognizer()
with sr.Microphone() as source:
print('Listening...')
r.pause_threshold = 1
r.energy_threshold = 50
audio = r.listen(source)
try:
print('Recognizing...')
qry = r.recognize_google(audio, language='en-in')
print(f"user said: {qry}\n")
except Exeption as e:
print(e)
print('Say that again please\n')
return 'None'
return qry
if __name__ == '__main__':
while True:
qry = takecommand().lower()
/\/\/\/\/\/\/\/\/\/\/\--- *HAPPYCODING* ---/\/\/\/\/\/\/\/\/\/\/\
Example 3: python speech recognition
import speech_recognition as sr
import pyttsx3
engine = pyttsx3.init('sapi5')
voices = engine.getProperty('voices')
engine.setProperty('voice', voices[0].id)
engine.setProperty('rate',180)
def speak(audio):
engine.say(audio)
engine.runAndWait()
def takecommand():
r = sr.Recognizer()
with sr.Microphone() as source:
print('Listening.....')
r.pause_threshold = 1
r.energy_threshold = 4000
audio = r.listen(source)
try:
print('Recognising...')
query = r.recognize_google(audio, language='en-in')
print('User Said : ' , query)
except Exception as e:
print('exception : ',e)
speak("Sorry, I didn't hear that, Say that again Please")
return "None"
return query
while True:
query = takecommand()
print("The Test got in program is : "+query)
Example 4: speech recognition in python
from easySpeech import speech
a=speech.speech('google')
print(a)
Example 5: python speech to text
import speech_recognition as sr
def main():
r = sr.Recognizer()
with sr.Microphone() as source:
r.adjust_for_ambient_noise(source)
audio = r.listen(source)
try:
print(r.recognize_google(audio))
except Exception as e:
print("Error : " + str(e))
with open("recorded.wav", "wb") as f:
f.write(audio.get_wav_data())
if __name__ == "__main__":
main()
Example 6: how to fix speech recognition in python
import speech_recognition as sr
r = sr.Recognizer()
with sr.Microphone() as source:
audio = r.listen(source)
try:
print("You said " + r.recognize(audio))
except LookupError:
print("Could not understand audio")