gpt4 book ai didi

python - 我在 python 中的 speech.listen(source) 正在停滞

转载 作者:行者123 更新时间:2023-12-05 07:07:43 26 4
gpt4 key购买 nike

我目前正在使用 SpeechRecognition 库,当我执行 audio = speech.listen(source) 时,它似乎停滞了。之后没有执行代码。我移除了环境声音,但它似乎仍然停顿并且对我的麦克风拾取的内容没有影响。当我查看计算机的设置并且未静音时,麦克风可以正常工作。

我创建了一个名为 engine 的 pyttsx3 实例,以及一个名为 speech 的 sr.Recognizer() 实例。然后我调用 getVoiceCommand() 从用户(我)那里获取一些音频,这些音频将转到 print("Listening...") 然后它停止。

audio = speech.listen(source) 之后的代码不执行,当 10 秒的超时完成时,它会给我一个 WaitTimoutError,这提示我它从未被选中响起?我尝试将 energy_threshold 的值更改为 50 到 4000 之间的值,但仍然没有发现任何噪音。

import speech_recognition as sr
import pyttsx3
import pyaudio

def speak(text):
engine.say(text)
engine.runAndWait()

def getVoiceCommand():
voice_text = ''
try:
with sr.Microphone() as source:
speech.adjust_for_ambient_noise(source)
print("Listening...")
#speech.energy_threshold = 4000
print(speech.energy_threshold)
audio = speech.listen(source, timeout=10)

print("Stopped")
voice_text = speech.recognize_google(audio, language='en-US')
except sr.UnknownValueError:
print("Unknown value error")
except sr.RequestError as e:
print('Network error.')
except sr.WaitTimeoutError:
print(audio)
print("Wait timeout error")

print(voice_text)

engine = pyttsx3.init()
engine.setProperty('voice', voice_id) # voice_id is chosen voice


rate = 220
engine.setProperty('rate', rate)

speech = sr.Recognizer()
getVoiceCommand()

最佳答案

我已将此程序与完全相同的库一起使用。这是假设您有某种麦克风。如果这不起作用,它很可能是它试图检测为输入的背景噪音,在这种情况下,您需要添加 r.adjust_for_ambient_noise(source, duration=1) 告诉我是否下面这段代码有效...

engine = pyttsx3.init('sapi5')   #Only use sapi 5 if it is Windows 10
voices = engine.getProperty('voices')
engine.setProperty('voice', voices[0].id) #This bit goes below the imports


def speak(audio):
engine.say(audio) #This bit right here goes below the above
engine.runAndWait()



def takeCommand():
r = sr.Recognizer()

with sr.Microphone() as source:

print("Listening...")
r.pause_threshold = 1
audio = r.listen(source)

try:
print("Recognizing...")
query = r.recognize_google(audio, language='en-us')
print("User said: {query}\n")

except Exception as e:
print(e)
print("Google was unable to hear")
return "None"

return query

我希望这对你有用。

关于python - 我在 python 中的 speech.listen(source) 正在停滞,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62035799/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com