我在 python 中的 speech.listen(source) 正在停滞
My speech.listen(source) in python is taking stalling
我目前正在使用 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
希望这对你有用。
我尝试了一些为此 post 回复的答案。当我在消声室中时,我的代码可以完美运行。为了克服前一个不正确的情况,我只是在源中使用了环境噪声。
import pyttsx3
import speech_recognition as sr
engine = pyttsx3.init()
voices = engine.getProperty('voices')
engine.setProperty('voice', voices[1].id)
rate = engine.getProperty('rate')
engine.setProperty('rate', 190)
engine.runAndWait()
r = sr.Recognizer()
with sr.Microphone() as source:
r.adjust_for_ambient_noise(source)
print("Listening......")
r.pause_threshold = 1
audio = r.listen(source)
我目前正在使用 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
希望这对你有用。
我尝试了一些为此 post 回复的答案。当我在消声室中时,我的代码可以完美运行。为了克服前一个不正确的情况,我只是在源中使用了环境噪声。
import pyttsx3
import speech_recognition as sr
engine = pyttsx3.init()
voices = engine.getProperty('voices')
engine.setProperty('voice', voices[1].id)
rate = engine.getProperty('rate')
engine.setProperty('rate', 190)
engine.runAndWait()
r = sr.Recognizer()
with sr.Microphone() as source:
r.adjust_for_ambient_noise(source)
print("Listening......")
r.pause_threshold = 1
audio = r.listen(source)