微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Python 语音识别无法使用 SpeechRecognition 3.8.1 库

如何解决Python 语音识别无法使用 SpeechRecognition 3.8.1 库

在我的 Python 3.8.6 项目中,我为我的 Windows 10 计算机安装了“pip install SpeechRecognition”。代码示例如下,

    import speech_recognition as sr
    
    listener = sr.Recognizer()
    try:
        with sr.Microphone() as source:
            print('Listening...')
            voice = listener.listen(source)
            command = listener.recognize_google(voice)
            print(command)
    except:
        print("Something else went wrong")
        pass

运行此代码后,输出如下,

Something else went wrong

我的笔记本电脑已经内置了麦克风,并且还使用插入外部 USB 麦克风对其进行了检查。那么这里有什么问题呢?是否需要任何硬件配置来解决此问题?

解决方法

在 PyPI 上的语音识别 Readme 中,您可以看到有一个 PyAudio 部分。这意味着您必须在您的机器上安装 PyAudio。但是如果你安装了 PyAudio 并且出现错误,你需要在没有 try except 块的情况下共享错误,以便我们分析错误并给出解决方案。

要安装 PyAudio,请在终端中执行 switch (variable) { case A -> statement_1(); statement_3(); case B -> statement_2(); statement_3(); };

有时 pip install pyaudio 会抛出类似这样的错误:

pip install pyaudio

修复此错误的步骤:

  • 转到您的终端并执行 Collecting PyAudio Using cached https://files.pythonhosted.org/packages/ab/42/b4f04721c5c5bfc196ce156b3c768998ef8c0ae3654ed29ea5020c749a6b/PyAudio-0.2.11.tar.gz Installing collected packages: PyAudio Running setup.py install for PyAudio ... error Complete output from command C:\Users\username\AppData\Local\Programs\Python\Python37-32\python.exe -u -c "import setuptools,tokenize;__file__='C:\\Users\\username\\AppData\\Local\\Temp\\pip-install-e5le61j0\\PyAudio\\setup.py';f=getattr(tokenize,'open',open)(__file__);code=f.read().replace('\r\n','\n');f.close();exec(compile(code,__file__,'exec'))" install --record C:\Users\username\AppData\Local\Temp\pip-record-adj3zivl\install-record.txt --single-version-externally-managed --compile: running install running build running build_py creating build creating build\lib.win32-3.7 copying src\pyaudio.py -> build\lib.win32-3.7 running build_ext building '_portaudio' extension error: Microsoft Visual C++ 14.0 is required. Get it with "Microsoft Visual C++ Build Tools": http://landinghub.visualstudio.com/visual-cpp-build-tools ---------------------------------------- Command "C:\Users\username\AppData\Local\Programs\Python\Python37-32\python.exe -u -c "import setuptools,'exec'))" install --record C:\Users\username\AppData\Local\Temp\pip-record-adj3zivl\install-record.txt --single-version-externally-managed --compile" failed with error code 1 in C:\Users\username\AppData\Local\Temp\pip-install-e5le61j0\PyAudio\ ,对于 Optimus Prime,他的版本是 Python 3.8.6
  • 查找您的 Python 安装是 64 位还是 32 位,您可以转到 Python 终端查看
  • 根据您的 Python 版本和 Python 安装(64 位/32 位)下载 PyAudio Wheel file
  • 在下载 Wheel 文件 (.whl) 的目录中打开一个终端
  • 然后执行python --version

大功告成!

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。