如何获得语音识别以在一个单词后停止收听?

时间:2018-10-19 00:25:49

标签: python-3.x translation speech-recognition speech-to-text

我正在翻译PygLatin。 至少一个非常基础的单词可以告诉一个翻译的单词。

如果我说的不止一个单词,则会把所有单词弄混,并把第一个单词的第一个字符放在最后一个说“ ay”的单词上。

因此,我一直遇到一些问题。有没有办法让它只说一个字就停止听?是否有办法也将每个单词的第一个字母放在每个口语单词的末尾,然后加上“ ay”?

这是到目前为止我的代码示例:

import time
import speech_recognition as sr
from gtts import gTTS
import urllib3
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
from pygame import mixer
mixer.init()
pyg = "ay" 
count = 3
while count > 0:
    while (True == True):
  # get audio input
        original = sr.Recognizer()
        with sr.Microphone() as source:
      # listen for 1 second and create the ambient noise energy level
            original.adjust_for_ambient_noise(source, duration = 1)
            print("Say ONE word. If you do not wish to use the translator, say 'stop' ")
        audio = original.listen(source,phrase_time_limit = 1)

  # speech recognition with Google
        try:
            response = original.recognize_google(audio)
            if response == "stop":
                print("Thanks for using the PygLatin Translator, Goodbye!")
                break
                pyg_word = response + pyg
                w = response + pyg
                pyg_word = w[1 : len(pyg_word)]
                print(pyg_word)
                tts = gTTS(text = str(pyg_word), lang='en')
                tts.save("response.mp3")
                mixer.music.load('response.mp3')
                mixer.music.play()


        except sr.UnknownValueError:
            print("Translator could not understand audio")
        except sr.RequestError as e:
            print("Sphinx error; {0}".format(e))

0 个答案:

没有答案