【问题标题】:Python speech recognition is not working using SpeechRecognition 3.8.1 libraryPython 语音识别无法使用 SpeechRecognition 3.8.1 库工作
【发布时间】:2021-03-15 15:43:46
【问题描述】:

在我的 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 麦克风对其进行了检查。那么这里有什么问题呢?是否需要任何硬件配置来解决此问题?

【问题讨论】:

  • 从不抑制异常开始。
  • 确保已安装 pyAudio

标签: python python-3.x speech-recognition speech-to-text


【解决方案1】:

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

要安装 PyAudio,请在终端中执行 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, 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" failed with error code 1 in C:\Users\username\AppData\Local\Temp\pip-install-e5le61j0\PyAudio\

修复此错误的步骤:

  • 转到您的终端并执行python --version,对于 Optimus Prime,他的版本是 Python 3.8.6
  • 查看您的 Python 安装是 64 位还是 32 位,您可以转到 Python 终端查看
  • 根据您的 Python 版本和 Python 安装(64 位/32 位)下载PyAudio Wheel file
  • 在您下载 Wheel 文件 (.whl) 的目录中打开终端
  • 然后执行pip install <name of your wheel file>

你就完成了!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-10-04
    • 2021-07-15
    • 1970-01-01
    • 1970-01-01
    • 2015-04-17
    • 1970-01-01
    • 1970-01-01
    • 2018-02-23
    相关资源
    最近更新 更多