【问题标题】:AttributeError: Could not find PyAudio; check installation...can't use speech RecognitionAttributeError:找不到 PyAudio;检查安装...无法使用语音识别
【发布时间】:2019-09-22 20:17:46
【问题描述】:

我正在尝试制作一个基本的语音识别助手。当我运行代码时,它告诉我:

Traceback (most recent call last):
  File "C:\Users\Mo.haytham\AppData\Local\Programs\Python\Python37\lib\site-packages\speech_recognition\__init__.py", line 108, in get_pyaudio
    import pyaudio
ModuleNotFoundError: No module named 'pyaudio'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:/Users/Mo.haytham/.PyCharmCE2018.3/config/scratches/ALPHA_BASIC.py", line 22, in <module>
    hear()
  File "C:/Users/Mo.haytham/.PyCharmCE2018.3/config/scratches/ALPHA_BASIC.py", line 13, in hear
    with sr.Microphone() as sourse:
  File "C:\Users\Mo.haytham\AppData\Local\Programs\Python\Python37\lib\site-packages\speech_recognition\__init__.py", line 79, in __init__
    self.pyaudio_module = self.get_pyaudio()
  File "C:\Users\Mo.haytham\AppData\Local\Programs\Python\Python37\lib\site-packages\speech_recognition\__init__.py", line 110, in get_pyaudio
    raise AttributeError("Could not find PyAudio; check installation")
AttributeError: Could not find PyAudio; check installation 

我尝试pip install pyaudio,但随后出现此错误:

Running setup.py clean for pyaudio
Failed to build pyaudio
Installing collected packages: pyaudio
  Running setup.py install for pyaudio ... error
    ERROR: Complete output from command 'c:\users\mo.haytham\appdata\local\programs\python\python37\python.exe' -u -c 'import setuptools, tokenize;__file__='"'"'C:\\Users\\MO2D8C~1.HAY\\AppData\\Local\\Temp\\pip-install-o2
10x3zl\\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\MO2
D8C~1.HAY\AppData\Local\Temp\pip-record-hr7kket1\install-record.txt' --single-version-externally-managed --compile:
    ERROR: running install
    running build
    running build_py
    creating build
    creating build\lib.win-amd64-3.7
    copying src\pyaudio.py -> build\lib.win-amd64-3.7
    running build_ext
    building '_portaudio' extension
    error: Microsoft Visual C++ 14.0 is required. Get it with "Microsoft Visual C++ Build Tools": https://visualstudio.microsoft.com/downloads/
    ----------------------------------------
ERROR: Command "'c:\users\mo.haytham\appdata\local\programs\python\python37\python.exe' -u -c 'import setuptools, tokenize;__file__='"'"'C:\\Users\\MO2D8C~1.HAY\\AppData\\Local\\Temp\\pip-install-o210x3zl\\pyaudio\\setup.p
y'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' install --record 'C:\Users\MO2D8C~1.HAY\AppData\Local\
Temp\pip-record-hr7kket1\install-record.txt' --single-version-externally-managed --compile" failed with error code 1 in C:\Users\MO2D8C~1.HAY\AppData\Local\Temp\pip-install-o210x3zl\pyaudio\
def hear():
    import speech_recognition as sr
    ear = sr.Recognizer()
    with sr.Microphone() as sourse:
        print("listening...")
        audio = ear.listen(sourse)
        try:
            text = ear.recognize_google(audio)
            print(text)
        except:
            print("i didn't get that...")

hear()

【问题讨论】:

    标签: python python-3.x visual-c++ pyaudio


    【解决方案1】:

    在终端类型中

    pip install pipwin
    

    然后

    pipwin install pyaudio
    

    【讨论】:

      【解决方案2】:

      我还发现,由于安装困难,PyAudio 安装可能会很痛苦,对某些最终用户来说甚至会破坏交易。原则上,speech_recognition.Recognizer.listen() 没有理由不能从其他音频库(例如sounddevicesoundcardaudiomath)获取其输入,所有这些通常都更容易安装。幸运的是,虽然speech_recognition 代码本身只提供了 PyAudio 实现,但在内部它只需要对 Microphone 的几个属性进行鸭式输入,就可以成功地实现listen()。具体来说:

      • source 必须是 speech_recognition.AudioSource 子类的实例
      • 当源处于活动状态时,source.stream 必须是非None
      • source.CHUNK 必须是每个块的(整数)样本数
      • source.SAMPLE_RATE 必须是采样率
      • source.SAMPLE_WIDTH 必须是每个样本的字节数
      • source.stream.read(numberOfSamples) 必须返回原始单声道音频数据

      这是使用audiomath 的鸭式解决方案:

      import audiomath; audiomath.RequireAudiomathVersion( '1.12.0' )
      import speech_recognition  # NB: python -m pip install SpeechRecognition
      
      class DuckTypedMicrophone( speech_recognition.AudioSource ): # descent from AudioSource is required purely to pass an assertion in Recognizer.listen()
          def __init__( self, device=None, chunkSeconds=1024/44100.0 ):  # 1024 samples at 44100 Hz is about 23 ms
              self.recorder = None
              self.device = device
              self.chunkSeconds = chunkSeconds
          def __enter__( self ):
              self.nSamplesRead = 0
              self.recorder = audiomath.Recorder( audiomath.Sound( 5, nChannels=1 ), loop=True, device=self.device )
              # Attributes required by Recognizer.listen():
              self.CHUNK = audiomath.SecondsToSamples( self.chunkSeconds, self.recorder.fs, int )
              self.SAMPLE_RATE = int( self.recorder.fs )
              self.SAMPLE_WIDTH = self.recorder.sound.nbytes
              return self
          def __exit__( self, *blx ):
              self.recorder.Stop()
              self.recorder = None
          def read( self, nSamples ):
              sampleArray = self.recorder.ReadSamples( self.nSamplesRead, nSamples )
              self.nSamplesRead += nSamples
              return self.recorder.sound.dat2str( sampleArray )
          @property
          def stream( self ): # attribute must be present to pass an assertion in Recognizer.listen(), and its value must have a .read() method
              return self if self.recorder else None
      
      if __name__ == '__main__':
          import speech_recognition as sr
          r = sr.Recognizer()
          with DuckTypedMicrophone() as source:
              print('\nSay something to the %s...' % source.__class__.__name__)
              audio = r.listen(source)
          print('Got it.')
          print('\nUnderstood: "%s"\n' % r.recognize_google(audio))
      
          if 0: # plot and/or play back captured audio
              s = audiomath.Sound(audio.get_wav_data(), fs=audio.sample_rate, nChannels=1)
              s.Play()
              s.Plot()
      

      【讨论】:

        【解决方案3】:

        如果您是 ubuntu 18.04 用户,请按照以下步骤操作

        sudo apt-get install portaudio19-dev python-pyaudio
        

        然后

        pip install PyAudio
        

        【讨论】:

          【解决方案4】:

          您在安装 pyaudio 时遇到错误,因为您没有 c++ 构建工具来安装 pyaudio。

          要安装 Mircosoft Visual C++ 14.0,请考虑此链接 https://stackoverflow.com/a/49986365/8227403

          那么, 安装 pyaudio。

          如果你在 anaconda 提示符下使用 jupyter notebook,那么

          conda install pyaudio
          

          如果您使用 cmd 使用 jupyter notebook,那么在 jupyter cell 上,

          import sys
          !{sys.executable} -m pip install pyaudio
          

          如果你在 cmd 上运行 python 文件,那么,

          pip3 install pyaudio #for python3
          

          【讨论】:

            【解决方案5】:

            您似乎缺少一些构建pyaudio 所需的文件。

            从您的错误日志中,

            需要 Microsoft Visual C++ 14.0。使用“Microsoft Visual C++ Build >Tools”获取它:https://visualstudio.microsoft.com/downloads/

            你需要安装Microsoft Visual C++ Build Tools

            【讨论】:

              【解决方案6】:

              即使在安装 pipwin 之后我也遇到了问题,所以我发现在安装 PyAudio 之前执行下面的解决方案

              !apt install libasound2-dev portaudio19-dev libportaudio2 libportaudiocpp0 ffmpeg
              

              【讨论】:

                【解决方案7】:

                sudo apt-get install libportaudio-dev (先试试这个) sudo apt-get install portaudio19-dev (而不是使用这个) 稍后安装pyaudio(python -m pip install PyAudio)

                【讨论】:

                  【解决方案8】:

                  对于您说下载 C++ 构建工具的错误,我遇到了同样的错误。我下载了 Microsoft Visual Studio 运行时,但它不起作用。然后我下载了带有 anaconda 插件的 pycharm 社区版。我下载了anaconda,激活了它,然后用conda的python.exe配置了一个conda解释器。然后我输入以下内容: 康达安装 PyAudio 它为我安装了一切都很好。我建议这样做。

                  【讨论】:

                    【解决方案9】:

                    Python 2.7、3.4、3.5 和 3.6(32 位和 64 位)支持 PyAudio。您可能必须安装上述任何 python 才能使 PyAudio 正常工作。

                    【讨论】:

                      猜你喜欢
                      • 2021-03-03
                      • 1970-01-01
                      • 2021-04-10
                      • 1970-01-01
                      • 2021-04-17
                      • 1970-01-01
                      • 2020-01-19
                      • 1970-01-01
                      • 1970-01-01
                      相关资源
                      最近更新 更多