以下代码负责加载音频文件,并使用 Google Speech Recognition 将语音转换为文本:
# open the filewith sr.AudioFile(filename) as source:www.zpedu.com/
# listen for the data (load audio to memory)
audio_data = r.record(source)
# recognize (convert from speech to text)
text = r.recognize_google(audio_data)
print(text)
with sr.Microphone() as source:
# read the audio data from the default microphone
audio_data = r.record(source, duration=5)
print(“Recognizing…”)
# convert speech to text
text = r.recognize_google(audio_data)
print(text)
这会从您的麦克风听到 5 秒钟,然后尝试将该语音转换为文本!
它与先前的代码非常相似,但是我们在这里使用 Microphone () 对象从默认麦克风读取音频,然后在 record () 函数中使用 duration 参数在 5 秒后停止读取,然后上传音频数据向 Google 获取输出文本。
以下代码负责加载音频文件,并使用 Google Speech Recognition 将语音转换为文本:
# open the filewith sr.AudioFile(filename) as source:www.zpedu.com/
# listen for the data (load audio to memory)
audio_data = r.record(source)
# recognize (convert from speech to text)
text = r.recognize_google(audio_data)
print(text)
with sr.Microphone() as source:
# read the audio data from the default microphone
audio_data = r.record(source, duration=5)
print(“Recognizing…”)
# convert speech to text
text = r.recognize_google(audio_data)
print(text)
这会从您的麦克风听到 5 秒钟,然后尝试将该语音转换为文本!
它与先前的代码非常相似,但是我们在这里使用 Microphone () 对象从默认麦克风读取音频,然后在 record () 函数中使用 duration 参数在 5 秒后停止读取,然后上传音频数据向 Google 获取输出文本。