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
是的,你是对的,在你回答之前,我已经解决了它并且发布了答案。无论如何谢谢你的回应。 –