首页 > 解决方案 > 使用 speech_recognition Python 库进行语音到文本转换会出错

问题描述

我已经编写了一个 python 代码来将语音转换为文本,最终我想将输出保存在一个文件中。

import speech_recognition as sr
r= sr.Recognizer()
with sr.AudioFile("c://Akash//male.wav") as source:    
    audio= r.listen(source)
try:
    print("system predicts"+r.recognize_google(audio))
except Exception:
    print("something wrong")

上面的代码总是把我带到异常部分并打印“有问题”。我还需要帮助将输出保存到文本文件。编辑 1:错误 在此处输入图像描述

编辑 2:我通过使用 Azure Data Lake 中的音频文件进行了尝试,并且成功了一次。但在那之后它没有用。不知道为什么当我再次执行相同的代码时,它不起作用。

在此处输入图像描述

标签: pythonspeech-recognition

解决方案


我猜这个问题可能是由路径引起的"c://Akash//male.wav",可能是斜杠 - 如果您使用的是Windows操作系统,请尝试更改它"c:/Akash/male.wav""c:\\Akash\\male.wav". 如果两者都没有帮助,请用单行替换你的 4 行try-except

print("system predicts"+r.recognize_google(audio))

并将错误信息中的内容写给我们。

编辑:它提出了UnknownValueError,在我检查了speech_recognition名为audio_transcribe.py的使用示例之后,我认为这只是意味着Google Speech Recognition could not understand audio.


推荐阅读