我想在 python 程序中将语音输入转换为文本输出(使用 PyCharm IDE), 我已经成功安装了 PyAudio,但我的程序无法运行,然后我意识到我的电脑上需要 PortAudio,但我不知道如何安装它,
我从 portaudio 官方网站下载了
pa_stable_v190700_20210406.tgz
文件,该网站还提供了其 GitHub 的链接,
我是 GitHub 的新手,我很困惑,你能一步步指导我如何将 PortAudio 安装到我的电脑上吗?
如果您想做的只是语音转文本,这个简单的代码应该可以工作,请先运行
pip install SpeechRecognition
,然后这个代码应该可以工作
import speech_recognition as sr
r = sr.Recognizer()
with sr.Microphone() as source:
print("Listening...")
audio = r.listen(source)
try:
print("Recognizing...")
text = r.recognize_google(audio)
print("You said:", text)
except sr.UnknownValueError:
print("Sorry, I could not understand your speech.")
except sr.RequestError as e:
print("Sorry, an error occurred. {0}".format(e))
感谢您的代码。
我尝试了一下,但出现错误:
Traceback (most recent call last):
File "C:\Users\renato\Documents\mvp_1\reconocimiento_voz.py", line 7, in <module>
audio = r.listen(source)
^^^^^^^^^^^^^^^^
File "C:\Users\renato\Documents\entornos\mvp_1\Lib\site-packages\speech_recognition\__init__.py", line 465, in listen
Listening...
assert source.stream is not None, "Audio source must be entered before listening, see documentation for ``AudioSource``; are you using ``source`` outside of a ``with`` statement?"
^^^^^^^^^^^^^^^^^^^^^^^^^
AssertionError: Audio source must be entered before listening, see documentation for ``AudioSource``; are you using ``source`` outside of a ``with`` statement?
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\renato\Documents\mvp_1\reconocimiento_voz.py", line 5, in <module>
with sr.Microphone() as source:
File "C:\Users\renato\Documents\entornos\mvp_1\Lib\site-packages\speech_recognition\__init__.py", line 189, in __exit__
self.stream.close()
^^^^^^^^^^^^^^^^^
AttributeError: 'NoneType' object has no attribute 'close'