python中具有语音识别的多个短语

python中具有语音识别的多个短语,python,python-3.x,speech-to-text,pocketsphinx,Python,Python 3.x,Speech To Text,Pocketsphinx,我已经在下面发布了完整的代码,我希望能够重复执行audio=r.listen(source)。我让代码重复,但每次都返回相同的内容。我真的不喜欢放弃,来这里问答案(第一次发帖)。我需要做什么才能让代码在每次执行do\u reach(quit)函数时返回新短语。基本上,程序要求说些什么,第一次就可以正常运行。当我被提示继续或退出并且输入“c”时,我想重复整个过程。任何帮助都将不胜感激。PS我是python新手,可能做得完全错误。任何提示也将不胜感激 下面是代码(欢迎所有批评。比如:有没有更好的更干

我已经在下面发布了完整的代码,我希望能够重复执行
audio=r.listen(source)
。我让代码重复,但每次都返回相同的内容。我真的不喜欢放弃,来这里问答案(第一次发帖)。我需要做什么才能让代码在每次执行
do\u reach(quit)
函数时返回新短语。基本上,程序要求说些什么,第一次就可以正常运行。当我被提示继续或退出并且输入“c”时,我想重复整个过程。任何帮助都将不胜感激。PS我是python新手,可能做得完全错误。任何提示也将不胜感激

下面是代码(欢迎所有批评。比如:有没有更好的更干净的方法来做到这一点?)


我想我找到了答案。我正在使用
帮助(r.Recognizer)
特性阅读python解释器中的文档。我在后台找到了一个
listen\u
方法。
会在后台收听吗
做我需要的事吗?我知道这是一个愚蠢的计划,但我计划为自己做一些实际的事情。如果这行得通,你们可以关闭这个线程。我觉得很傻哈哈。我会尝试一下,如果有效,我会发布我的解决方案。再次感谢

[编辑] 成功了!我定义了这个函数:
def callback(r,audio)
,它是后台(m,callback)中的
stop\u listening=r.listen\u所需要的。我很难理解这一行的目的:
stop\u监听(wait\u for\u stop=True)
。如果有人能解释一下,我以后会用得更好。最后,我添加了一个“魔术词”退出程序(只需说“退出”并
sys.quit()
终止程序)。有更好的方法吗

以下是新代码:

import time
import speech_recognition as sr
import sys

def callback(r, audio):
    global done
    try:
        print(f"[PocketSphinx]: Thinks you said: '{r.recognize_sphinx(audio)}'. Read some more ")
    except sr.UnknownValueError:
        print(f"[PocketSphinx]: Fatal error! Unknown Value Error! Exiting program.")
    except sr.RequestError as ex:
        print(f"[PocketSphinx]: Fatal error! Could not process request! {ex}")
    if r.recognize_sphinx(audio) == 'quit':
        print(f"[PocketSphinx]: You said the magic word: 'quit'... Exiting program.")
        done = True

r = sr.Recognizer()
r.energy_threshold = 4000
m = sr.Microphone()
done = False
with m as source:
    r.adjust_for_ambient_noise(source)

print(f"[PocketSphinx]: Read to me... Say 'quit' to exit...")
stop_listening = r.listen_in_background(m, callback)

# 5 second delay 
for _ in range(50):
    time.sleep(0.1)

while not done:
    time.sleep(0.1)
    if done:
        stop_listening(wait_for_stop=True)
        sys.exit()
import time
import speech_recognition as sr
import sys

def callback(r, audio):
    global done
    try:
        print(f"[PocketSphinx]: Thinks you said: '{r.recognize_sphinx(audio)}'. Read some more ")
    except sr.UnknownValueError:
        print(f"[PocketSphinx]: Fatal error! Unknown Value Error! Exiting program.")
    except sr.RequestError as ex:
        print(f"[PocketSphinx]: Fatal error! Could not process request! {ex}")
    if r.recognize_sphinx(audio) == 'quit':
        print(f"[PocketSphinx]: You said the magic word: 'quit'... Exiting program.")
        done = True

r = sr.Recognizer()
r.energy_threshold = 4000
m = sr.Microphone()
done = False
with m as source:
    r.adjust_for_ambient_noise(source)

print(f"[PocketSphinx]: Read to me... Say 'quit' to exit...")
stop_listening = r.listen_in_background(m, callback)

# 5 second delay 
for _ in range(50):
    time.sleep(0.1)

while not done:
    time.sleep(0.1)
    if done:
        stop_listening(wait_for_stop=True)
        sys.exit()