0
我正在嘗試使用Google語音識別,我的問題是,在對麥克風說些什麼後,結果始終是相同的。Python中的語音識別庫總是返回相同的字符串
我的功能看起來像這樣
def RecordAudio():
import speech_recognition as sr
r = sr.Recognizer()
with sr.Microphone() as source:
print("Say something!")
audio = r.listen(source)
# Speech recognition using Google Speech Recognition
try:
print("You 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 audio
當我試圖在0x111a8b358
獲取數據
data = RecordAudio()
數據總是等於這個 speech_recognition.AudioData對象但對我來說奇怪的是,當這個代碼在一個文件中,沒有函數,沒有r時剔除結果,在那個時候這工作。
比如我有test.py文件
import speech_recognition as sr
r = sr.Recognizer()
with sr.Microphone() as source:
print("Say something!")
audio = r.listen(source)
# Speech recognition using Google Speech Recognition
try:
print("You 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))
這是工作,但第一個例子不是。
任何想法發生了什麼?
您正在返回'audio'的值,這是原始錄製的聲音。語音識別的結果來自'r.recognize_google(音頻)',您可以將其打印出來,但不保存在任何地方。您需要將其保存在本地變量中,以便稍後返回。 – jasonharper
是的,你是對的,在你回答之前,我已經解決了它並且發佈了答案。無論如何謝謝你的迴應。 –