domingo, 8 de noviembre de 2020

How to create a Speech Recognition assistant in 2 minutes using Google Speech-to-Text API and Sphinx

 

https://saiadarsh99.medium.com/how-to-create-a-speech-recognition-assistant-in-2-minutes-using-google-speech-to-test-api-and-2f34947c17d3

How to create a Speech Recognition assistant in 2 minutes using Google Speech-to-Text API and Sphinx



Prerequisites

🚀 Let’s start:

  • Now let’s move to the working directory :
cd <your desired directory path>
  • cd is shell command which means create directory
cd C:\Users\saiadarsh\Desktop\sample
  • For example, the above command is used to move to the directory specified.
git clone http://people.csail.mit.edu/hubert/git/pyaudio.git
cd pyaudio
python setup.py install
pip install SpeechRecognition
pip install PyAudio
pip install PySpeech
  • Run the above commands in the terminal one by one. Now open a Text Editor. ( I use sublime text editor). Paste the following code in it.
import speech_recognition as sr
from gtts import gTTS
#quiet the endless 'insecurerequest' warning
import urllib3
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)

from pygame import mixer
mixer.init()

while (True == True):
# obtain audio from the microphone
r = sr.Recognizer()
with sr.Microphone() as source:
#print("Please wait. Calibrating microphone...")
# listen for 1 second and create the ambient noise energy level
r.adjust_for_ambient_noise(source, duration=1)
print("Say something!")
audio = r.listen(source,phrase_time_limit=5)

# recognize speech using Sphinx/Google
try:
#response = r.recognize_sphinx(audio)
response = r.recognize_google(audio)
print("I think you said '" + response + "'")
tts = gTTS(text="I think you said "+str(response), lang='en')
tts.save("response.mp3")
mixer.music.load('response.mp3')
mixer.music.play()


except sr.UnknownValueError:
print("Sphinx could not understand audio")
except sr.RequestError as e:
print("Sphinx error; {0}".format(e)
  • Save this with a .py extension and make sure the save type is ‘All files’
Image for post
  • Now in the terminal
python yourfilename.py
  • Make sure you are in the same directory where the file is present and use cd commands to navigate to directories.
  • Now you can speak something, the Speech API recognizes it, Sphinx will respond back

💻 Output screen:

Image for post

The response gets saved in response.mp3

No hay comentarios:

Publicar un comentario