[英]Python code for Speech Recognition not working
我瀏覽了有關此主題的所有類似問題並嘗試了所有方法,但沒有任何效果。
我嘗試了以下鏈接中的解決方案: 語音識別 python 停止監聽SpeechRecognition 產生 OSError: No Default Input Device Available Python,Speech Recognition 卡在“Listening...” 語音識別 python 代碼不起作用等。
import speech_recognition as sr
def get_audio():
r = sr.Recognizer()
with sr.Microphone() as source:
r.adjust_for_ambient_noise(source, duration=5)
print("listening... ")
audio = r.listen(source)
said = ""
try:
said = r.recognize_google(audio)
except sr.UnknownValueError:
print("Google Speech Recognition could not understand audio")
except sr.RequestError as e:
print("Could not request results from Google Speech Recognition service; {0}".format(e))
return said.lower()
print(get_audio())
我得到的錯誤是:
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
以下命令卡在“說點什么……”並且什么也不做。
python -m speech_recognition
我還嘗試了以下代碼來檢查默認音頻設備:
import pyaudio
print(pyaudio.pa.get_default_input_device())
輸出:
OSError: No Default Input Device Available
我在這里做錯了什么?
正如上面的評論中所說,您似乎沒有任何可用的麥克風。
在使用語音識別時,我通常會在聽之前詢問用戶使用哪種設備。
例如,使用這個實現:
# Identify the microphone device to use
def get_microphone_device():
microphone_list = []
for index, name in enumerate(sr.Microphone.list_microphone_names()):
microphone_list.append("Microphone {1} `(device_index={0})`".format(index, name))
questions = [
inquirer.List('microphone',
message = "What Microphone will you use?",
choices = microphone_list,
),
]
answers = inquirer.prompt(questions)
microphone = answers["microphone"]
microphone = re.search("(?=`)(.*)", microphone).group(0)
device = re.search("[0-9]", microphone).group()
return device
然后,要使用設備並獲取消息,您可以按照您的方式進行操作,例如:
# Listen to the speaker through the microphone device
def get_speech(device):
rec = sr.Recognizer()
with sr.Microphone(device_index=int(device)) as source:
print("Speak, we are listening:")
audio = rec.listen(source)
try:
text = rec.recognize_google(audio)
print("You said: \"{}\"".format(text))
except:
print("Sorry, we couldn't recognize what you said")
return text
我希望它有幫助!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.